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) => {
const _messages = [...messages, message]
setMessages(_messages)
localforage.setItem(`topic:${topic.id}`, { ...topic, messages: _messages })
localforage.setItem(`topic:${topic.id}`, { id: topic.id, messages: _messages })
},
[messages, topic]
)

View File

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

View File

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

View File

@ -1,7 +1,8 @@
import { SYSTEM_MODELS } from '@renderer/config/models'
import i18n from '@renderer/i18n'
import { Assistant } from '@renderer/types'
import { isEmpty } from 'lodash'
import localforage from 'localforage'
import { isEmpty, pick } from 'lodash'
import { createMigrate } from 'redux-persist'
import { RootState } from '.'
@ -373,6 +374,27 @@ const migrateConfig = {
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 = {
id: string
name: string
createdAt: string
updatedAt: string
messages: Message[]
}