diff --git a/app/components/chat/BaseChat.tsx b/app/components/chat/BaseChat.tsx index 1a33375d..98f13c7a 100644 --- a/app/components/chat/BaseChat.tsx +++ b/app/components/chat/BaseChat.tsx @@ -14,20 +14,13 @@ import { Messages } from './Messages.client'; import { SendButton } from './SendButton.client'; import { APIKeyManager } from './APIKeyManager'; import Cookies from 'js-cookie'; -import { toast } from 'react-toastify'; import * as Tooltip from '@radix-ui/react-tooltip'; import styles from './BaseChat.module.scss'; import type { ProviderInfo } from '~/utils/types'; -import { ExportChatButton } from '~/components/chat/ExportChatButton'; - -const EXAMPLE_PROMPTS = [ - { text: 'Build a todo app in React using Tailwind' }, - { text: 'Build a simple blog using Astro' }, - { text: 'Create a cookie consent form using Material UI' }, - { text: 'Make a space invaders game' }, - { text: 'How do I center a div?' }, -]; +import { ExportChatButton } from '~/components/chat/chatExportAndImport/ExportChatButton'; +import { ImportButton } from '~/components/chat/chatExportAndImport/ImportButton'; +import { ExamplePrompts } from '~/components/chat/ExamplePrompts'; // eslint-disable-next-line @typescript-eslint/no-unused-vars const providerList = PROVIDER_LIST; @@ -168,67 +161,6 @@ export const BaseChat = React.forwardRef( } }; - const chatImportButton = !chatStarted && ( -
- { - 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'); - } - }} - /> -
-
- -
-
-
- ); - const baseChat = (
(
- {chatImportButton} - {!chatStarted && ( -
-
- {EXAMPLE_PROMPTS.map((examplePrompt, index) => { - return ( - - ); - })} -
-
- )} + {!chatStarted && ImportButton(importChat)} + {!chatStarted && ExamplePrompts(sendMessage)} {() => } diff --git a/app/components/chat/ExamplePrompts.tsx b/app/components/chat/ExamplePrompts.tsx new file mode 100644 index 00000000..60b7643a --- /dev/null +++ b/app/components/chat/ExamplePrompts.tsx @@ -0,0 +1,32 @@ +import React from 'react'; + +const EXAMPLE_PROMPTS = [ + { text: 'Build a todo app in React using Tailwind' }, + { text: 'Build a simple blog using Astro' }, + { text: 'Create a cookie consent form using Material UI' }, + { text: 'Make a space invaders game' }, + { text: 'How do I center a div?' }, +]; + +export function ExamplePrompts(sendMessage?: { (event: React.UIEvent, messageInput?: string): void | undefined }) { + return ( +
+
+ {EXAMPLE_PROMPTS.map((examplePrompt, index: number) => { + return ( + + ); + })} +
+
+ ); +} diff --git a/app/components/chat/ExportChatButton.tsx b/app/components/chat/chatExportAndImport/ExportChatButton.tsx similarity index 100% rename from app/components/chat/ExportChatButton.tsx rename to app/components/chat/chatExportAndImport/ExportChatButton.tsx diff --git a/app/components/chat/chatExportAndImport/ImportButton.tsx b/app/components/chat/chatExportAndImport/ImportButton.tsx new file mode 100644 index 00000000..91c699e5 --- /dev/null +++ b/app/components/chat/chatExportAndImport/ImportButton.tsx @@ -0,0 +1,66 @@ +import type { Message } from 'ai'; +import { toast } from 'react-toastify'; +import React from 'react'; + +export function ImportButton(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'); + } + }} + /> +
+
+ +
+
+
+ ); +}