'use client'; 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', { month: 'short', day: 'numeric', year: 'numeric', hour: 'numeric', minute: 'numeric', }).format(date); }; export const ExampleLibraryApps = () => { const [numApps, setNumApps] = useState(6); const [apps, setApps] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { async function fetchRecentApps() { try { setLoading(true); const recentApps = await getRecentApps(numApps); setApps(recentApps); setError(null); } catch (err) { console.error('Failed to fetch recent apps:', err); setError('Failed to load recent apps'); } finally { setLoading(false); } } if (apps.length < numApps) { fetchRecentApps(); } }, [numApps]); if (error) { return
{error}
; } if (apps.length === 0) { if (loading) { return
Loading recent apps...
; } return
No recent apps found
; } const displayApps = apps.slice(0, numApps); return (
{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 ) : (
{app.title || 'No preview'}
)}
{app.title || 'Untitled App'}
Created at {formatDate(new Date(app.createdAt))} in {Math.round(app.elapsedMinutes)} minutes
{app.totalPeanuts} peanuts
{app.outcome !== 'success' &&
⚠️ Not all tests are passing
}
))}
{loading &&
Loading recent apps...
} {!loading && (
)}
); };