fix import (#242)
* 🐛 fix import (#224) * 🐛 fix import of corrupted history see https://github.com/mckaywrigley/chatbot-ui/issues/224#issuecomment-1486080888 * add the run-test-suite github action
This commit is contained in:
@@ -1,5 +1,53 @@
|
||||
import { Conversation } from '@/types/chat';
|
||||
import { Folder } from '@/types/folder';
|
||||
import {
|
||||
ExportFormatV1,
|
||||
ExportFormatV2,
|
||||
ExportFormatV3,
|
||||
LatestExportFormat,
|
||||
SupportedExportFormats,
|
||||
} from '@/types/export';
|
||||
import { cleanConversationHistory } from './clean';
|
||||
|
||||
export function isExportFormatV1(obj: any): obj is ExportFormatV1 {
|
||||
return Array.isArray(obj);
|
||||
}
|
||||
|
||||
export function isExportFormatV2(obj: any): obj is ExportFormatV2 {
|
||||
return !('version' in obj) && 'folders' in obj && 'history' in obj;
|
||||
}
|
||||
|
||||
export function isExportFormatV3(obj: any): obj is ExportFormatV3 {
|
||||
return obj.version === 3;
|
||||
}
|
||||
|
||||
export const isLatestExportFormat = isExportFormatV3;
|
||||
|
||||
export function cleanData(data: SupportedExportFormats): LatestExportFormat {
|
||||
if (isExportFormatV1(data)) {
|
||||
return {
|
||||
version: 3,
|
||||
history: cleanConversationHistory(data),
|
||||
folders: [],
|
||||
};
|
||||
}
|
||||
|
||||
if (isExportFormatV2(data)) {
|
||||
return {
|
||||
version: 3,
|
||||
history: cleanConversationHistory(data.history || []),
|
||||
folders: (data.folders || []).map((chatFolder) => ({
|
||||
id: chatFolder.id.toString(),
|
||||
name: chatFolder.name,
|
||||
type: 'chat',
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
if (isExportFormatV3(data)) {
|
||||
return data;
|
||||
}
|
||||
|
||||
throw new Error('Unsupported data format');
|
||||
}
|
||||
|
||||
function currentDate() {
|
||||
const date = new Date();
|
||||
@@ -21,9 +69,10 @@ export const exportData = () => {
|
||||
}
|
||||
|
||||
const data = {
|
||||
history,
|
||||
folders,
|
||||
};
|
||||
version: 3,
|
||||
history: history || [],
|
||||
folders: folders || [],
|
||||
} as LatestExportFormat;
|
||||
|
||||
const blob = new Blob([JSON.stringify(data, null, 2)], {
|
||||
type: 'application/json',
|
||||
@@ -40,13 +89,18 @@ export const exportData = () => {
|
||||
};
|
||||
|
||||
export const importData = (
|
||||
conversations: Conversation[],
|
||||
folders: Folder[],
|
||||
) => {
|
||||
data: SupportedExportFormats,
|
||||
): LatestExportFormat => {
|
||||
const cleanedData = cleanData(data);
|
||||
|
||||
const conversations = cleanedData.history;
|
||||
localStorage.setItem('conversationHistory', JSON.stringify(conversations));
|
||||
localStorage.setItem(
|
||||
'selectedConversation',
|
||||
JSON.stringify(conversations[conversations.length - 1]),
|
||||
);
|
||||
localStorage.setItem('folders', JSON.stringify(folders));
|
||||
|
||||
localStorage.setItem('folders', JSON.stringify(cleanedData.folders));
|
||||
|
||||
return cleanedData;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user