import { useEffect, useState } from 'react'; import { useNavigate, useParams } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; import { Download, Pencil, Trash2 } from 'lucide-react'; import { stringify as yamlStringify } from 'yaml'; import { snippets } from '../../api'; import type { Snippet } from '../../api'; import { Breadcrumbs, Button, Card, DescriptionList, Notification, 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); const [error, setError] = useState(null); useEffect(() => { if (!id) return; snippets .get(id) .then(setSnippet) .catch((err: Error) => setError(err.message)) .finally(() => setLoading(false)); }, [id]); const handleDelete = async () => { if (!snippet) return; await snippets.remove(snippet.id); navigate('/snippets'); }; 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.name}.yaml`; a.click(); URL.revokeObjectURL(url); }; return (
navigate('/snippets') }, { label: snippet?.name ?? `#${id}` }, ]} /> {snippet && (
)}
{error && ( {error.startsWith('404') ? t('errors.not_found_snippet', { id }) : error} )} {loading &&

{t('snippets.loading')}

} {snippet && ( <>
}, { term: t('snippets.field_name'), detail: snippet.name }, { term: t('snippets.field_description'), detail: snippet.description ?? '—', }, { term: t('snippets.field_created'), detail: , }, { term: t('snippets.field_updated'), detail: , }, ]} />

{t('snippets.section_code')}

{snippet.code}
)}
); }