refactor: renamed and refactored topic properties and added date-time tracking

- Renamed localforage topic item property from topic object to id.
- Added date-time tracking for assistant topics.
- Incremented the store version to 24.
- Refactored migrate function to add support for local storage and update topics timestamps.
- Added createdAt and updatedAt properties to Topic type.
This commit is contained in:
kangfenmao 2024-09-05 16:15:48 +08:00
parent 0dacc20e74
commit a48d24de26
5 changed files with 34 additions and 7 deletions

View File

@ -34,7 +34,7 @@ const Messages: FC<Props> = ({ assistant, topic, setActiveTopic }) => {
(message: Message) => { (message: Message) => {
const _messages = [...messages, message] const _messages = [...messages, message]
setMessages(_messages) setMessages(_messages)
localforage.setItem(`topic:${topic.id}`, { ...topic, messages: _messages }) localforage.setItem(`topic:${topic.id}`, { id: topic.id, messages: _messages })
}, },
[messages, topic] [messages, topic]
) )

View File

@ -44,11 +44,14 @@ const assistantsSlice = createSlice({
) )
}, },
addTopic: (state, action: PayloadAction<{ assistantId: string; topic: Topic }>) => { addTopic: (state, action: PayloadAction<{ assistantId: string; topic: Topic }>) => {
const topic = action.payload.topic
topic.createdAt = new Date().toISOString()
topic.updatedAt = new Date().toISOString()
state.assistants = state.assistants.map((assistant) => state.assistants = state.assistants.map((assistant) =>
assistant.id === action.payload.assistantId assistant.id === action.payload.assistantId
? { ? {
...assistant, ...assistant,
topics: uniqBy([action.payload.topic, ...assistant.topics], 'id') topics: uniqBy([topic, ...assistant.topics], 'id')
} }
: assistant : assistant
) )
@ -64,13 +67,13 @@ const assistantsSlice = createSlice({
) )
}, },
updateTopic: (state, action: PayloadAction<{ assistantId: string; topic: Topic }>) => { updateTopic: (state, action: PayloadAction<{ assistantId: string; topic: Topic }>) => {
const newTopic = action.payload.topic
newTopic.updatedAt = new Date().toISOString()
state.assistants = state.assistants.map((assistant) => state.assistants = state.assistants.map((assistant) =>
assistant.id === action.payload.assistantId assistant.id === action.payload.assistantId
? { ? {
...assistant, ...assistant,
topics: assistant.topics.map((topic) => topics: assistant.topics.map((topic) => (topic.id === newTopic.id ? newTopic : topic))
topic.id === action.payload.topic.id ? action.payload.topic : topic
)
} }
: assistant : assistant
) )

View File

@ -22,7 +22,7 @@ const persistedReducer = persistReducer(
{ {
key: 'cherry-studio', key: 'cherry-studio',
storage, storage,
version: 23, version: 24,
blacklist: ['runtime'], blacklist: ['runtime'],
migrate migrate
}, },

View File

@ -1,7 +1,8 @@
import { SYSTEM_MODELS } from '@renderer/config/models' import { SYSTEM_MODELS } from '@renderer/config/models'
import i18n from '@renderer/i18n' import i18n from '@renderer/i18n'
import { Assistant } from '@renderer/types' import { Assistant } from '@renderer/types'
import { isEmpty } from 'lodash' import localforage from 'localforage'
import { isEmpty, pick } from 'lodash'
import { createMigrate } from 'redux-persist' import { createMigrate } from 'redux-persist'
import { RootState } from '.' import { RootState } from '.'
@ -373,6 +374,27 @@ const migrateConfig = {
windowStyle: 'transparent' windowStyle: 'transparent'
} }
} }
},
'24': async (state: RootState) => {
for (const key of await localforage.keys()) {
if (key.startsWith('topic:')) {
localforage.getItem(key).then((topic) => localforage.setItem(key, pick(topic, ['id', 'messages'])))
}
}
return {
...state,
assistants: {
...state.assistants,
assistants: state.assistants.assistants.map((assistant) => ({
...assistant,
topics: assistant.topics.map((topic) => ({
...topic,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString()
}))
}))
}
}
} }
} }

View File

@ -36,6 +36,8 @@ export type Message = {
export type Topic = { export type Topic = {
id: string id: string
name: string name: string
createdAt: string
updatedAt: string
messages: Message[] messages: Message[]
} }