forked from lobehub/lobe-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ refactor: refactor the config import for server import (lobehub#2718)
- Loading branch information
Showing
10 changed files
with
156 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { useCallback } from 'react'; | ||
|
||
import { ImportResults, configService } from '@/services/config'; | ||
import { useChatStore } from '@/store/chat'; | ||
import { useSessionStore } from '@/store/session'; | ||
import { importConfigFile } from '@/utils/config'; | ||
|
||
export const useImportConfig = () => { | ||
const refreshSessions = useSessionStore((s) => s.refreshSessions); | ||
const [refreshMessages, refreshTopics] = useChatStore((s) => [s.refreshMessages, s.refreshTopic]); | ||
|
||
return useCallback( | ||
async (file: File) => | ||
new Promise<ImportResults | undefined>((resolve) => { | ||
importConfigFile(file, async (config) => { | ||
const data = await configService.importConfigState(config); | ||
|
||
await refreshSessions(); | ||
await refreshMessages(); | ||
await refreshTopics(); | ||
|
||
resolve(data); | ||
}); | ||
}), | ||
[], | ||
); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { MessageModel } from '@/database/client/models/message'; | ||
import { SessionModel } from '@/database/client/models/session'; | ||
import { SessionGroupModel } from '@/database/client/models/sessionGroup'; | ||
import { TopicModel } from '@/database/client/models/topic'; | ||
import { ImportResult, ImportResults } from '@/services/config'; | ||
import { useUserStore } from '@/store/user'; | ||
import { ConfigStateSessions } from '@/types/exportConfig'; | ||
import { UserSettings } from '@/types/user/settings'; | ||
|
||
export class ClientService { | ||
importSettings = async (settings: UserSettings) => { | ||
await useUserStore.getState().importAppSettings(settings); | ||
}; | ||
|
||
importData = async (config: ConfigStateSessions): Promise<ImportResults> => { | ||
const { messages, sessionGroups, sessions, topics } = config; | ||
|
||
let messageResult: ImportResult | undefined; | ||
let sessionResult: ImportResult | undefined; | ||
let sessionGroupResult: ImportResult | undefined; | ||
let topicResult: ImportResult | undefined; | ||
|
||
if (messages.length > 0) { | ||
const res = await MessageModel.batchCreate(messages); | ||
messageResult = this.mapImportResult(res); | ||
} | ||
|
||
if (sessionGroups.length > 0) { | ||
const res = await SessionGroupModel.batchCreate(sessionGroups); | ||
sessionGroupResult = this.mapImportResult(res); | ||
} | ||
|
||
if (topics.length > 0) { | ||
const res = await TopicModel.batchCreate(topics as any); | ||
topicResult = this.mapImportResult(res); | ||
} | ||
|
||
if (sessions.length > 0) { | ||
const data = await SessionModel.batchCreate(sessions); | ||
sessionResult = this.mapImportResult(data); | ||
} | ||
|
||
return { | ||
messages: messageResult, | ||
sessionGroups: sessionGroupResult, | ||
sessions: sessionResult, | ||
topics: topicResult, | ||
}; | ||
}; | ||
|
||
private mapImportResult = (input: { | ||
added: number; | ||
errors?: Error[]; | ||
skips: string[]; | ||
}): ImportResult => { | ||
return { | ||
added: input.added, | ||
errors: input.errors?.length || 0, | ||
skips: input.skips.length, | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { ClientService } from './client'; | ||
|
||
export const importService = new ClientService(); |
Oops, something went wrong.