import { useEffect, useState } from 'react'; import { useNavigate, useParams } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; import { usePageTitle } from '../../hooks/usePageTitle'; import { Upload, Pencil, Trash2, Braces } from 'lucide-react'; import { CodeBlock } from '../../ui'; import { stringify as yamlStringify } from 'yaml'; import { snippets } from '../../api'; import type { Snippet } from '../../api'; import { Breadcrumbs, Button, Card, DescriptionList, MarkdownContent, Modal, Timestamp, UuidBadge, } from '../../ui'; import styles from '../Page.module.css'; export function SnippetDetailPage() { const { t } = useTranslation(); const { id } = useParams<{ id: string }>(); const navigate = useNavigate(); const [snippet, setSnippet] = useState(null); const [loading, setLoading] = useState(true); usePageTitle(snippet?.title ?? null); const [confirmDeleteOpen, setConfirmDeleteOpen] = useState(false); const [deleting, setDeleting] = useState(false); useEffect(() => { if (!id) return; snippets .get(id) .then(setSnippet) .finally(() => setLoading(false)); }, [id]); const handleDelete = async () => { if (!snippet) return; setDeleting(true); try { await snippets.remove(snippet.id); navigate('/snippets'); } finally { setDeleting(false); setConfirmDeleteOpen(false); } }; const handleExport = async () => { if (!snippet) return; const data = await snippets.exportSnippet(snippet.id); const blob = new Blob([yamlStringify(data)], { type: 'application/yaml' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `snippet-${snippet.alias}.yaml`; a.click(); URL.revokeObjectURL(url); }; return (
, onClick: () => navigate('/snippets'), }, { label: snippet?.title ?? `#${id}` }, ]} /> {snippet && (
)}
{loading &&

{t('snippets.loading')}

} {snippet && ( <> }, { term: t('snippets.field_alias'), detail: snippet.alias }, { term: t('snippets.field_title'), detail: snippet.title }, { term: t('snippets.field_version'), detail: `v${snippet.version}` }, { term: t('snippets.field_created'), detail: , }, { term: t('snippets.field_updated'), detail: , }, ]} /> {snippet.description && (

{t('snippets.field_description')}

)}

{t('snippets.section_code')}

)} !deleting && setConfirmDeleteOpen(false)} footer={ <> } > {t('common.confirm_delete_snippet')}
); }