Skip to content

Commit

Permalink
refactor: simplify chat import formats
Browse files Browse the repository at this point in the history
- Remove multi-format support from DataTab
- Keep only standard Bolt export formats
- Simplify ImportButtons to handle standard format only
  • Loading branch information
sidbetatester committed Jan 2, 2025
1 parent bf2b119 commit 5b6ba80
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 90 deletions.
22 changes: 0 additions & 22 deletions app/components/chat/chatExportAndImport/ImportButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import { ImportFolderButton } from '~/components/chat/ImportFolderButton';
type ChatData = {
messages?: Message[]; // Standard Bolt format
description?: string; // Optional description
history?: Array<{ messages: Message[]; description?: string }>; // Backup format
boltHistory?: {
chats: Record<string, { messages: Message[]; description?: string }>; // Chrome extension format
};
};

export function ImportButtons(importChat: ((description: string, messages: Message[]) => Promise<void>) | undefined) {
Expand Down Expand Up @@ -38,24 +34,6 @@ export function ImportButtons(importChat: ((description: string, messages: Messa
return;
}

// Backup format
if (data.history?.[0]?.messages) {
const chat = data.history[0];
await importChat(chat.description || 'Imported Chat', chat.messages);
toast.success('Chat imported successfully');
return;
}

// Chrome format
if (data.boltHistory?.chats) {
const chat = Object.values(data.boltHistory.chats)[0];
if (chat?.messages) {
await importChat(chat.description || 'Imported Chat', chat.messages);
toast.success('Chat imported successfully');
return;
}
}

toast.error('Invalid chat file format');
} catch (error: unknown) {
if (error instanceof Error) {
Expand Down
96 changes: 28 additions & 68 deletions app/components/settings/data/DataTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -232,85 +232,45 @@ export default function DataTab() {
event.target.value = '';
};

const handleImportChats = () => {
const input = document.createElement('input');
input.type = 'file';
input.accept = '.json';

const processChatData = (data: any): Array<{
id: string;
messages: Message[];
description: string;
urlId?: string;
}> => {
// Add logging to debug format detection
console.log('Processing data:', {
hasMessages: !!data.messages,
isMessagesArray: Array.isArray(data.messages),
hasBoltHistory: !!data.boltHistory,
hasHistory: !!data.history,
hasChats: !!data.chats,
keys: Object.keys(data)
});

// Handle Bolt standard format (single chat)
if (data.messages && Array.isArray(data.messages)) {
console.log('Importing single chat:', {
messages: data.messages.length,
description: data.description
});

// Generate both id and urlId for single chat
const chatId = crypto.randomUUID();
return [{
id: chatId,
messages: data.messages,
description: data.description || 'Imported Chat',
urlId: chatId // Add urlId for proper navigation
}];
}

// Handle Chrome extension format
if (data.boltHistory?.chats) {
return Object.values(data.boltHistory.chats).map((chat: any) => ({
id: chat.id || crypto.randomUUID(),
messages: chat.messages,
description: chat.description || 'Imported Chat',
urlId: chat.urlId
}));
}

// Handle history array format
if (data.history && Array.isArray(data.history)) {
return data.history.map((chat: any) => ({
id: chat.id || crypto.randomUUID(),
messages: chat.messages,
description: chat.description || 'Imported Chat',
urlId: chat.urlId
}));
}
const processChatData = (data: any): Array<{
id: string;
messages: Message[];
description: string;
urlId?: string;
}> => {
// Handle Bolt standard format (single chat)
if (data.messages && Array.isArray(data.messages)) {
const chatId = crypto.randomUUID();
return [{
id: chatId,
messages: data.messages,
description: data.description || 'Imported Chat',
urlId: chatId
}];
}

// Handle Bolt export format (multiple chats)
if (data.chats && Array.isArray(data.chats)) {
return data.chats.map((chat: {
id?: string;
messages: Message[];
description?: string;
urlId?: string;
}) => ({
return data.chats.map((chat: { id?: string; messages: Message[]; description?: string; urlId?: string; }) => ({
id: chat.id || crypto.randomUUID(),
messages: chat.messages,
description: chat.description || 'Imported Chat',
urlId: chat.urlId
urlId: chat.urlId,
}));
}

console.error('No matching format found for:', data);
throw new Error('Unsupported chat format');
};
console.error('No matching format found for:', data);
throw new Error('Unsupported chat format');
};

const handleImportChats = () => {
const input = document.createElement('input');
input.type = 'file';
input.accept = '.json';

input.onchange = async (e) => {
const file = (e.target as HTMLInputElement).files?.[0];

if (!file || !db) {
toast.error('Something went wrong');
return;
Expand All @@ -330,7 +290,7 @@ export default function DataTab() {
window.location.reload();
} catch (error) {
if (error instanceof Error) {
logStore.logError('Failed to import chats', error);
logStore.logError('Failed to import chats:', error);
toast.error('Failed to import chats: ' + error.message);
} else {
toast.error('Failed to import chats');
Expand Down

0 comments on commit 5b6ba80

Please sign in to comment.