107 lines
2.5 KiB
TypeScript
107 lines
2.5 KiB
TypeScript
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();
|
|
const month = date.getMonth() + 1;
|
|
const day = date.getDate();
|
|
return `${month}-${day}`;
|
|
}
|
|
|
|
export const exportData = () => {
|
|
let history = localStorage.getItem('conversationHistory');
|
|
let folders = localStorage.getItem('folders');
|
|
|
|
if (history) {
|
|
history = JSON.parse(history);
|
|
}
|
|
|
|
if (folders) {
|
|
folders = JSON.parse(folders);
|
|
}
|
|
|
|
const data = {
|
|
version: 3,
|
|
history: history || [],
|
|
folders: folders || [],
|
|
} as LatestExportFormat;
|
|
|
|
const blob = new Blob([JSON.stringify(data, null, 2)], {
|
|
type: 'application/json',
|
|
});
|
|
const url = URL.createObjectURL(blob);
|
|
const link = document.createElement('a');
|
|
link.download = `chatbot_ui_history_${currentDate()}.json`;
|
|
link.href = url;
|
|
link.style.display = 'none';
|
|
document.body.appendChild(link);
|
|
link.click();
|
|
document.body.removeChild(link);
|
|
URL.revokeObjectURL(url);
|
|
};
|
|
|
|
export const importData = (
|
|
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(cleanedData.folders));
|
|
|
|
return cleanedData;
|
|
};
|