Skip to content

Commit

Permalink
import/export add instead of replace (#601)
Browse files Browse the repository at this point in the history
* change search language

* add to import/export

* fix log
  • Loading branch information
mckaywrigley committed Apr 18, 2023
1 parent b964188 commit 5725024
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
8 changes: 1 addition & 7 deletions components/Chatbar/components/ChatbarSettings.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
IconFileExport,
IconMoon,
IconSettings,
IconSun,
} from '@tabler/icons-react';
import { IconFileExport, IconSettings } from '@tabler/icons-react';
import { useContext, useState } from 'react';

import { useTranslation } from 'next-i18next';
Expand Down Expand Up @@ -38,7 +33,6 @@ export const ChatbarSettings = () => {
handleClearConversations,
handleImportConversations,
handleExportData,

handleApiKeyChange,
} = useContext(ChatbarContext);

Expand Down
2 changes: 1 addition & 1 deletion components/Sidebar/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const Sidebar = <T,>({
</button>
</div>
<Search
placeholder={t('Search prompts...') || ''}
placeholder={t('Search...') || ''}
searchTerm={searchTerm}
onSearch={handleSearchTerm}
/>
Expand Down
36 changes: 27 additions & 9 deletions utils/app/importExport.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Conversation } from '@/types/chat';
import {
ExportFormatV1,
ExportFormatV2,
Expand All @@ -6,6 +7,8 @@ import {
LatestExportFormat,
SupportedExportFormats,
} from '@/types/export';
import { FolderInterface } from '@/types/folder';
import { Prompt } from '@/types/prompt';

import { cleanConversationHistory } from './clean';

Expand Down Expand Up @@ -109,18 +112,33 @@ export const exportData = () => {
export const importData = (
data: SupportedExportFormats,
): LatestExportFormat => {
const cleanedData = cleanData(data);
const { history, folders, prompts } = cleanedData;

const conversations = history;
localStorage.setItem('conversationHistory', JSON.stringify(conversations));
const { history, folders, prompts } = cleanData(data);

const oldConversations = localStorage.getItem('conversationHistory');
const oldConversationsParsed = oldConversations
? JSON.parse(oldConversations)
: [];
const newHistory: Conversation[] = [...oldConversationsParsed, ...history];
localStorage.setItem('conversationHistory', JSON.stringify(newHistory));
localStorage.setItem(
'selectedConversation',
JSON.stringify(conversations[conversations.length - 1]),
JSON.stringify(newHistory[newHistory.length - 1]),
);

localStorage.setItem('folders', JSON.stringify(folders));
localStorage.setItem('prompts', JSON.stringify(prompts));
const oldFolders = localStorage.getItem('folders');
const oldFoldersParsed = oldFolders ? JSON.parse(oldFolders) : [];
const newFolders: FolderInterface[] = [...oldFoldersParsed, ...folders];
localStorage.setItem('folders', JSON.stringify(newFolders));

const oldPrompts = localStorage.getItem('prompts');
const oldPromptsParsed = oldPrompts ? JSON.parse(oldPrompts) : [];
const newPrompts: Prompt[] = [...oldPromptsParsed, ...prompts];
localStorage.setItem('prompts', JSON.stringify(newPrompts));

return cleanedData;
return {
version: 4,
history: newHistory.map((e, idx) => ({ ...e, id: `${idx}` })),
folders: newFolders.map((e, idx) => ({ ...e, id: `${idx}` })),
prompts: newPrompts.map((e, idx) => ({ ...e, id: `${idx}` })),
};
};

1 comment on commit 5725024

@vercel
Copy link

@vercel vercel bot commented on 5725024 Apr 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.