import type { Message } from 'ai'; import { toast } from 'react-toastify'; import React from 'react'; import { ImportFolderButton } from '~/components/chat/ImportFolderButton'; export function ImportButtons(importChat: ((description: string, messages: Message[]) => Promise) | undefined) { return (
{ const file = e.target.files?.[0]; if (file && importChat) { try { const reader = new FileReader(); reader.onload = async (e) => { try { const content = e.target?.result as string; const data = JSON.parse(content); if (!Array.isArray(data.messages)) { toast.error('Invalid chat file format'); } await importChat(data.description, data.messages); toast.success('Chat imported successfully'); } catch (error: unknown) { if (error instanceof Error) { toast.error('Failed to parse chat file: ' + error.message); } else { toast.error('Failed to parse chat file'); } } }; reader.onerror = () => toast.error('Failed to read chat file'); reader.readAsText(file); } catch (error) { toast.error(error instanceof Error ? error.message : 'Failed to import chat'); } e.target.value = ''; // Reset file input } else { toast.error('Something went wrong'); } }} />
); }