From 1f31cc55074c0f2f99ceca46e579ec875c9021a8 Mon Sep 17 00:00:00 2001 From: Mckay Wrigley Date: Thu, 23 Mar 2023 19:05:47 -0600 Subject: [PATCH] hotfix import --- components/Sidebar/Import.tsx | 13 +++++++++---- components/Sidebar/Sidebar.tsx | 2 +- components/Sidebar/SidebarSettings.tsx | 4 ++-- pages/index.tsx | 18 ++++++++++-------- utils/app/importExport.ts | 25 +++++++++++++++++++------ 5 files changed, 41 insertions(+), 21 deletions(-) diff --git a/components/Sidebar/Import.tsx b/components/Sidebar/Import.tsx index 4bafde6..a3988dc 100644 --- a/components/Sidebar/Import.tsx +++ b/components/Sidebar/Import.tsx @@ -1,9 +1,9 @@ -import { Conversation } from "@/types"; +import { ChatFolder, Conversation } from "@/types"; import { IconFileImport } from "@tabler/icons-react"; import { FC } from "react"; interface Props { - onImport: (conversations: Conversation[]) => void; + onImport: (data: { conversations: Conversation[]; folders: ChatFolder[] }) => void; } export const Import: FC = ({ onImport }) => { @@ -19,8 +19,13 @@ export const Import: FC = ({ onImport }) => { const file = e.target.files[0]; const reader = new FileReader(); reader.onload = (e) => { - const conversations: Conversation[] = JSON.parse(e.target?.result as string); - onImport(conversations); + let json = JSON.parse(e.target?.result as string); + + if (!json.folders) { + json = { history: json, folders: [] }; + } + + onImport({ conversations: json.history, folders: json.folders }); }; reader.readAsText(file); }} diff --git a/components/Sidebar/Sidebar.tsx b/components/Sidebar/Sidebar.tsx index fd0abbe..2448d93 100644 --- a/components/Sidebar/Sidebar.tsx +++ b/components/Sidebar/Sidebar.tsx @@ -25,7 +25,7 @@ interface Props { onApiKeyChange: (apiKey: string) => void; onClearConversations: () => void; onExportConversations: () => void; - onImportConversations: (conversations: Conversation[]) => void; + onImportConversations: (data: { conversations: Conversation[]; folders: ChatFolder[] }) => void; } export const Sidebar: FC = ({ loading, conversations, lightMode, selectedConversation, apiKey, folders, onCreateFolder, onDeleteFolder, onUpdateFolder, onNewConversation, onToggleLightMode, onSelectConversation, onDeleteConversation, onToggleSidebar, onUpdateConversation, onApiKeyChange, onClearConversations, onExportConversations, onImportConversations }) => { diff --git a/components/Sidebar/SidebarSettings.tsx b/components/Sidebar/SidebarSettings.tsx index 0264dae..1f5fc22 100644 --- a/components/Sidebar/SidebarSettings.tsx +++ b/components/Sidebar/SidebarSettings.tsx @@ -1,4 +1,4 @@ -import { Conversation } from "@/types"; +import { ChatFolder, Conversation } from "@/types"; import { IconFileExport, IconMoon, IconSun } from "@tabler/icons-react"; import { FC } from "react"; import { ClearConversations } from "./ClearConversations"; @@ -13,7 +13,7 @@ interface Props { onApiKeyChange: (apiKey: string) => void; onClearConversations: () => void; onExportConversations: () => void; - onImportConversations: (conversations: Conversation[]) => void; + onImportConversations: (data: { conversations: Conversation[]; folders: ChatFolder[] }) => void; } export const SidebarSettings: FC = ({ lightMode, apiKey, onToggleLightMode, onApiKeyChange, onClearConversations, onExportConversations, onImportConversations }) => { diff --git a/pages/index.tsx b/pages/index.tsx index a287bba..a97ee34 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -6,7 +6,7 @@ import { cleanConversationHistory, cleanSelectedConversation } from "@/utils/app import { DEFAULT_SYSTEM_PROMPT } from "@/utils/app/const"; import { saveConversation, saveConversations, updateConversation } from "@/utils/app/conversation"; import { saveFolders } from "@/utils/app/folders"; -import { exportConversations, importConversations } from "@/utils/app/importExport"; +import { exportData, importData } from "@/utils/app/importExport"; import { IconArrowBarLeft, IconArrowBarRight } from "@tabler/icons-react"; import Head from "next/head"; import { useEffect, useRef, useState } from "react"; @@ -210,14 +210,16 @@ export default function Home() { localStorage.setItem("isUsingEnv", isUsingEnv.toString()); }; - const handleExportConversations = () => { - exportConversations(); + const handleExportData = () => { + exportData(); }; - const handleImportConversations = (conversations: Conversation[]) => { - importConversations(conversations); - setConversations(conversations); - setSelectedConversation(conversations[conversations.length - 1]); + const handleImportConversations = (data: { conversations: Conversation[]; folders: ChatFolder[] }) => { + console.log(data); + importData(data.conversations, data.folders); + setConversations(data.conversations); + setSelectedConversation(data.conversations[data.conversations.length - 1]); + setFolders(data.folders); }; const handleSelectConversation = (conversation: Conversation) => { @@ -461,7 +463,7 @@ export default function Home() { onUpdateConversation={handleUpdateConversation} onApiKeyChange={handleApiKeyChange} onClearConversations={handleClearConversations} - onExportConversations={handleExportConversations} + onExportConversations={handleExportData} onImportConversations={handleImportConversations} /> diff --git a/utils/app/importExport.ts b/utils/app/importExport.ts index b85b53f..8b3bdb9 100644 --- a/utils/app/importExport.ts +++ b/utils/app/importExport.ts @@ -1,11 +1,23 @@ -import { Conversation } from "@/types"; +import { ChatFolder, Conversation } from "@/types"; -export const exportConversations = () => { - const history = localStorage.getItem("conversationHistory"); +export const exportData = () => { + let history = localStorage.getItem("conversationHistory"); + let folders = localStorage.getItem("folders"); - if (!history) return; + if (history) { + history = JSON.parse(history); + } - const blob = new Blob([history], { type: "application/json" }); + if (folders) { + folders = JSON.parse(folders); + } + + const data = { + history, + folders + }; + + const blob = new Blob([JSON.stringify(data)], { type: "application/json" }); const url = URL.createObjectURL(blob); const link = document.createElement("a"); link.download = "chatbot_ui_history.json"; @@ -17,7 +29,8 @@ export const exportConversations = () => { URL.revokeObjectURL(url); }; -export const importConversations = (conversations: Conversation[]) => { +export const importData = (conversations: Conversation[], folders: ChatFolder[]) => { localStorage.setItem("conversationHistory", JSON.stringify(conversations)); localStorage.setItem("selectedConversation", JSON.stringify(conversations[conversations.length - 1])); + localStorage.setItem("folders", JSON.stringify(folders)); };