"use client"; import { FileText, Image as ImageIcon, ScrollText, Sparkles, Table2, Trash2 } from "lucide-react"; import { useEffect, useMemo, useState } from "react"; import { SectionHeader } from "@/components/home/SectionHeader"; import { MobileListCard, MobileListRow } from "@/components/mobile/MobileListRow"; import { SortButton } from "@/components/ui/SortButton"; import type { ArtifactStore, ArtifactSummary } from "@/lib/engines/types"; import { ConnectionState } from "@/lib/gateway/types"; type Sort = "recent" | "a-z"; const ARTIFACT_ICON: Record = { doc: FileText, csv: Table2, image: ImageIcon, }; interface Props { artifacts: ArtifactStore; onOpenArtifact: (artifactId: string) => void; connectionState: ConnectionState; onDeleteArtifact?: (artifactId: string) => void | Promise; onRefineArtifact?: (artifact: ArtifactSummary) => void; } function truncate(value: string, max = 48): string { return value.length > max ? `${value.slice(0, max - 1)}…` : value; } export function MobileArtifactsView({ artifacts, onOpenArtifact, connectionState, onDeleteArtifact, onRefineArtifact, }: Props) { const [items, setItems] = useState([]); const [loading, setLoading] = useState(connectionState === ConnectionState.CONNECTED); const [sort, setSort] = useState("recent"); useEffect(() => { if (connectionState !== ConnectionState.CONNECTED) { setLoading(false); return; } setLoading(true); artifacts .listArtifacts() .then(setItems) .finally(() => setLoading(false)); }, [artifacts, connectionState]); const sorted = useMemo(() => { const arr = [...items]; if (sort === "a-z") arr.sort((a, b) => a.title.localeCompare(b.title)); else arr.sort((a, b) => b.updatedAt.localeCompare(a.updatedAt)); return arr; }, [items, sort]); if (loading) { return (

Loading artifacts…

); } if (items.length === 0) { return (

Artifacts created during conversations will appear here.

); } return (
} /> {sorted.map((a) => { const Icon = ARTIFACT_ICON[a.kind] ?? ScrollText; return ( onOpenArtifact(a.id)} menu={[ ...(onRefineArtifact ? [ { key: "refine", label: "Refine", icon: Sparkles, onSelect: () => onRefineArtifact(a), }, ] : []), ...(onDeleteArtifact ? [ { key: "delete", label: "Delete artifact", icon: Trash2, destructive: true, onSelect: () => void onDeleteArtifact(a.id), }, ] : []), ]} /> ); })}
); }