Files
chatbot-ui/utils/app/importExport.ts
T
Mckay Wrigley 6500db9c1c MAJOR REFACTOR (#494)
* move index to home folder, create state and context files and barrell folder

* Sanity Check Commit:  reducer added to home.tsx manual QA all working

* WIP: promptBar

* fix missing json parse on folders and prompts

* split context and add promptbar context

* add context to nested prompt componets and componetize Folder componet

* remove log

* Create buttons folder and componetize sidebar action button

* tidy up prompt handlers

* componetized sidebar

* added back chatbar componet to left side sidebar

* monster commit: Componetized the common code between chatbar and promptbar into new componet Sidebar and added context to both bars

* add useFetch service

* added prettier import sort to keep imports ordered and easier to indentify

* added react query and useFetch to work with RQ

* added apiService, errorService and reactQuery

* add callback and tidy up error service

* refactor chat and child componets to useContext

* fix extra calls and bad calls to mel endpoint

* minor import cleanup

---------

Co-authored-by: jc.durbin <jc.durbin@ardanis.com>
2023-04-10 21:10:18 -06:00

127 lines
3.0 KiB
TypeScript

import {
ExportFormatV1,
ExportFormatV2,
ExportFormatV3,
ExportFormatV4,
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 function isExportFormatV4(obj: any): obj is ExportFormatV4 {
return obj.version === 4;
}
export const isLatestExportFormat = isExportFormatV4;
export function cleanData(data: SupportedExportFormats): LatestExportFormat {
if (isExportFormatV1(data)) {
return {
version: 4,
history: cleanConversationHistory(data),
folders: [],
prompts: [],
};
}
if (isExportFormatV2(data)) {
return {
version: 4,
history: cleanConversationHistory(data.history || []),
folders: (data.folders || []).map((chatFolder) => ({
id: chatFolder.id.toString(),
name: chatFolder.name,
type: 'chat',
})),
prompts: [],
};
}
if (isExportFormatV3(data)) {
return { ...data, version: 4, prompts: [] };
}
if (isExportFormatV4(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');
let prompts = localStorage.getItem('prompts');
if (history) {
history = JSON.parse(history);
}
if (folders) {
folders = JSON.parse(folders);
}
if (prompts) {
prompts = JSON.parse(prompts);
}
const data = {
version: 4,
history: history || [],
folders: folders || [],
prompts: prompts || [],
} 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 { history, folders, prompts } = cleanedData;
const conversations = history;
localStorage.setItem('conversationHistory', JSON.stringify(conversations));
localStorage.setItem(
'selectedConversation',
JSON.stringify(conversations[conversations.length - 1]),
);
localStorage.setItem('folders', JSON.stringify(folders));
localStorage.setItem('prompts', JSON.stringify(prompts));
return cleanedData;
};