/// import { useState } from 'react' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' import { Button } from '@/components/ui/button' import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs' import { ScrollArea } from '@/components/ui/scroll-area' import { Badge } from '@/components/ui/badge' import { useSeedTemplates } from '@/hooks/data/use-seed-templates' import { Copy, Download } from '@phosphor-icons/react' import { toast } from 'sonner' import templateUi from '@/config/template-ui.json' const ui = templateUi.explorer type TemplateData = Record type TemplateExplorerHeaderProps = { onExport: () => void } type TemplateListProps = { templates: Array<{ id: string; name: string; icon: string }> selectedTemplate: string onSelect: (templateId: string) => void } type TemplateDetailProps = { template: { name: string description: string icon: string data: TemplateData features: string[] } onDownload: () => void onCopyJson: () => void } const TemplateExplorerHeader = ({ onExport }: TemplateExplorerHeaderProps) => ( {ui.title} {ui.description} {ui.buttons.exportCurrentData} ) const TemplateList = ({ templates, selectedTemplate, onSelect }: TemplateListProps) => ( {templates.map((template) => ( onSelect(template.id)} > {template.icon} {template.name} ))} ) const TemplateOverviewTab = ({ data, features }: { data: TemplateData; features: string[] }) => ( {ui.sections.features} {features.map((feature, idx) => ( {feature} ))} {Object.entries(data).map(([key, value]) => ( {key.replace('project-', '')} {Array.isArray(value) ? `${value.length} ${ui.labels.itemsSuffix}` : ui.labels.configuration} ))} ) const TemplateStructureTab = ({ data }: { data: TemplateData }) => ( {Object.entries(data).map(([key, value]) => ( {key} {Array.isArray(value) ? `${value.length} ${ui.labels.itemsSuffix}` : ui.labels.object} {Array.isArray(value) && value.length > 0 && ( {value.slice(0, 3).map((item: any, idx: number) => ( • {item.name || item.title || item.id} ))} {value.length > 3 && ( {`${ui.labels.morePrefix} ${value.length - 3} ${ui.labels.moreSuffix}`} )} )} ))} ) const TemplateJsonTab = ({ data, onCopy }: { data: TemplateData; onCopy: () => void }) => ( {JSON.stringify(data, null, 2)} ) const TemplateDetails = ({ template, onDownload, onCopyJson }: TemplateDetailProps) => ( {template.icon} {template.name} {template.description} {ui.buttons.download} {ui.tabs.overview} {ui.tabs.structure} {ui.tabs.json} ) export function TemplateExplorer() { const { templates } = useSeedTemplates() const [selectedTemplate, setSelectedTemplate] = useState(templates[0]?.id || 'default') const currentTemplate = templates.find(t => t.id === selectedTemplate) const copyToClipboard = (text: string) => { navigator.clipboard.writeText(text) toast.success(ui.toasts.copySuccess) } const downloadJSON = () => { if (!currentTemplate) return const dataStr = JSON.stringify(currentTemplate.data, null, 2) const blob = new Blob([dataStr], { type: 'application/json' }) const url = URL.createObjectURL(blob) const link = document.createElement('a') link.href = url link.download = `${currentTemplate.id}-template.json` document.body.appendChild(link) link.click() document.body.removeChild(link) URL.revokeObjectURL(url) toast.success(ui.toasts.downloadSuccess) } const exportCurrentData = async () => { const keys = await window.spark.kv.keys() const data: Record = {} for (const key of keys) { data[key] = await window.spark.kv.get(key) } const dataStr = JSON.stringify(data, null, 2) const blob = new Blob([dataStr], { type: 'application/json' }) const url = URL.createObjectURL(blob) const link = document.createElement('a') link.href = url link.download = 'current-project-data.json' document.body.appendChild(link) link.click() document.body.removeChild(link) URL.revokeObjectURL(url) toast.success(ui.toasts.exportSuccess) } if (!currentTemplate) return null const handleCopyJson = () => copyToClipboard(JSON.stringify(currentTemplate.data, null, 2)) return ( ) }
{ui.description}
{JSON.stringify(data, null, 2)}