import { useEffect, useRef, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { Settings, Pencil, Trash2, Plus, Download, Braces } from 'lucide-react';
import { parse as yamlParse } from 'yaml';
import { snippets } from '../../api';
import type { Snippet } from '../../api';
import { Breadcrumbs, Button, Card, ContextMenu, Timestamp, UuidBadge } from '../../ui';
import styles from '../Page.module.css';
function SnippetCard({ snippet, onDelete }: { snippet: Snippet; onDelete: (id: string) => void }) {
const { t } = useTranslation();
const navigate = useNavigate();
const header = (
{snippet.name}
e.stopPropagation()}>
}
items={[
{
label: t('snippets.action_edit'),
icon: ,
onClick: () => navigate(`/snippets/${snippet.id}/edit`),
},
{
label: t('snippets.action_delete'),
icon: ,
variant: 'danger',
onClick: () => onDelete(snippet.id),
},
]}
/>
);
const footer = (
);
return (
navigate(`/snippets/${snippet.id}`)}
>
{snippet.description && {snippet.description}
}
);
}
function AddSnippetCard() {
const { t } = useTranslation();
const navigate = useNavigate();
return (
);
}
export function SnippetsPage() {
const { t } = useTranslation();
const navigate = useNavigate();
const [items, setItems] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const fileInputRef = useRef(null);
const load = () => {
snippets
.list()
.then((res) => setItems(res.data))
.catch((err: Error) => setError(err.message))
.finally(() => setLoading(false));
};
useEffect(() => {
load();
}, []);
const handleDelete = async (id: string) => {
await snippets.remove(id);
load();
};
const handleImport = async (e: React.ChangeEvent) => {
const file = e.target.files?.[0];
if (!file) return;
e.target.value = '';
try {
const text = await file.text();
const payload = yamlParse(text) as unknown;
const imported = await snippets.importSnippet(payload);
navigate(`/snippets/${imported.id}`);
} catch (err) {
setError(err instanceof Error ? err.message : String(err));
}
};
return (
{loading &&
{t('snippets.loading')}
}
{!loading && (
{items.map((s) => (
))}
{items.length === 0 &&
{t('snippets.empty')}
}
)}
);
}