diff --git a/app/components/app-library/ExampleLibraryApps.tsx b/app/components/app-library/ExampleLibraryApps.tsx index 8b6b40a3..4427f89d 100644 --- a/app/components/app-library/ExampleLibraryApps.tsx +++ b/app/components/app-library/ExampleLibraryApps.tsx @@ -3,7 +3,6 @@ import { useEffect, useState } from 'react'; import { type BuildAppResult, getRecentApps } from '~/lib/persistence/apps'; import styles from './ExampleLibraryApps.module.scss'; -import { importChat } from '~/lib/persistence/useChatHistory'; const formatDate = (date: Date) => { return new Intl.DateTimeFormat('en-US', { @@ -58,22 +57,10 @@ export const ExampleLibraryApps = () => {
{displayApps.map((app) => ( -
{ - importChat( - app.title ?? 'Untitled App', - app.messages.filter((msg) => { - // Workaround an issue where the messages in the database include images - // (used to generate the screenshots). - if (msg.role == 'assistant' && msg.type == 'image') { - return false; - } - return true; - }), - ); - }} > {app.imageDataURL ? ( {app.title @@ -94,7 +81,7 @@ export const ExampleLibraryApps = () => {
- + ))} {loading &&
Loading recent apps...
} diff --git a/app/lib/persistence/apps.ts b/app/lib/persistence/apps.ts index 48c5c3f9..9ad01ca9 100644 --- a/app/lib/persistence/apps.ts +++ b/app/lib/persistence/apps.ts @@ -106,3 +106,14 @@ export async function getRecentApps(numApps: number): Promise } return apps; } + +export async function getAppById(id: string): Promise { + const { data, error } = await getSupabase().from('apps').select('*').eq('id', id).single(); + + if (error) { + console.error('Error fetching app by id:', error); + throw error; + } + + return databaseRowToBuildAppResult(data); +} diff --git a/app/lib/persistence/useChatHistory.ts b/app/lib/persistence/useChatHistory.ts index eb5ae788..34c00e7d 100644 --- a/app/lib/persistence/useChatHistory.ts +++ b/app/lib/persistence/useChatHistory.ts @@ -6,13 +6,14 @@ import { chatStore } from '~/lib/stores/chat'; import { database } from './chats'; import { createMessagesForRepository, type Message } from './message'; import { debounce } from '~/utils/debounce'; +import { getAppById } from './apps'; export interface ResumeChatInfo { protocolChatId: string; protocolChatResponseId: string; } -export async function importChat(title: string, messages: Message[]) { +async function importChat(title: string, messages: Message[]) { try { // Remove any peanuts when importing another chat, these are just for the current user. const newMessages = messages.map((msg) => ({ ...msg, peanuts: undefined })); @@ -29,19 +30,30 @@ export async function importChat(title: string, messages: Message[]) { } } +async function loadRepository(repositoryId: string) { + const messages = createMessagesForRepository(`Repository: ${repositoryId}`, repositoryId); + await importChat(`Repository: ${repositoryId}`, messages); + toast.success('Repository loaded successfully'); +} + +async function loadApp(appId: string) { + const app = await getAppById(appId); + + await importChat(app.title ?? 'Untitled App', app.messages); + toast.success('App loaded successfully'); +} + export function useChatHistory() { - const { id: mixedId, repositoryId } = useLoaderData<{ id?: string; repositoryId?: string }>() ?? {}; + const { + id: mixedId, + repositoryId, + appId, + } = useLoaderData<{ id?: string; repositoryId?: string; appId?: string }>() ?? {}; const [initialMessages, setInitialMessages] = useState([]); const [resumeChat, setResumeChat] = useState(undefined); const [ready, setReady] = useState(!mixedId && !repositoryId); - const loadRepository = async (repositoryId: string) => { - const messages = createMessagesForRepository(`Repository: ${repositoryId}`, repositoryId); - await importChat(`Repository: ${repositoryId}`, messages); - toast.success('Repository loaded successfully'); - }; - const debouncedSetChatContents = debounce(async (messages: Message[]) => { const chat = chatStore.currentChat.get(); if (!chat) { @@ -74,6 +86,9 @@ export function useChatHistory() { } else if (repositoryId) { await loadRepository(repositoryId); setReady(true); + } else if (appId) { + await loadApp(appId); + setReady(true); } } catch (error) { logStore.logError('Failed to load chat messages', error); diff --git a/app/routes/app.$id.tsx b/app/routes/app.$id.tsx new file mode 100644 index 00000000..1a4cf8b5 --- /dev/null +++ b/app/routes/app.$id.tsx @@ -0,0 +1,8 @@ +import { json, type LoaderFunctionArgs } from '~/lib/remix-types'; +import { default as IndexRoute } from './_index'; + +export async function loader(args: LoaderFunctionArgs) { + return json({ appId: args.params.id }); +} + +export default IndexRoute;