import { Action, ActionPanel, Alert, confirmAlert, Detail, Form, Icon, List, showToast, Toast, useNavigation, } from "@raycast/api"; import { useEffect, useState } from "react"; import { clearSessionMapping, countMappingEntries, createSession, deleteAllSessions, deleteSession, getActiveSessionId, listSessions, releaseSessionMappingGroup, renameSession, setActiveSession, } from "./sessions"; import type { VelumSession } from "./types"; import { mappingSummary, sessionSubtitle } from "./ui"; import { TranslationTable } from "./show-translation-tables"; export default function Command() { const [sessions, setSessions] = useState([]); const [activeSessionId, setActiveSessionId] = useState(); const [isLoading, setIsLoading] = useState(true); async function reload() { const [loadedSessions, activeId] = await Promise.all([ listSessions(), getActiveSessionId(), ]); setSessions(loadedSessions); setActiveSessionId(activeId); setIsLoading(false); } useEffect(() => { reload(); }, []); async function activate(session: VelumSession) { await setActiveSession(session.id); await reload(); await showToast({ style: Toast.Style.Success, title: "Aktive Sitzung geändert", message: session.name, }); } async function clearMapping(session: VelumSession) { const confirmed = await confirmAlert({ title: "Zuordnung leeren?", message: `${session.name} behält den Namen, verliert aber alle Platzhalter-Zuordnungen.`, primaryAction: { title: "Zuordnung leeren", style: Alert.ActionStyle.Destructive, }, }); if (!confirmed) { return; } await clearSessionMapping(session.id); await reload(); await showToast({ style: Toast.Style.Success, title: "Zuordnung geleert", message: session.name, }); } async function remove(session: VelumSession) { const confirmed = await confirmAlert({ title: "Sitzung löschen?", message: `${session.name} und ihre Zuordnung werden gelöscht.`, primaryAction: { title: "Sitzung löschen", style: Alert.ActionStyle.Destructive, }, }); if (!confirmed) { return; } await deleteSession(session.id); await reload(); await showToast({ style: Toast.Style.Success, title: "Sitzung gelöscht", message: session.name, }); } async function removeAll() { const confirmed = await confirmAlert({ title: "Alle Sitzungen löschen?", message: "Alle gespeicherten Velum-Zuordnungen dieser Raycast-Extension werden gelöscht.", primaryAction: { title: "Alle löschen", style: Alert.ActionStyle.Destructive, }, }); if (!confirmed) { return; } await deleteAllSessions(); await reload(); await showToast({ style: Toast.Style.Success, title: "Alle Sitzungen gelöscht", }); } return ( } /> } /> {sessions.map((session) => ( { const updated = await releaseSessionMappingGroup(session.id, root); await reload(); return updated.mapping; }} />} /> activate(session)} /> } /> } /> } /> clearMapping(session)} /> remove(session)} /> } /> ))} ); } function SessionNameForm({ session, onSaved, }: { session?: VelumSession; onSaved: () => Promise; }) { const { pop } = useNavigation(); async function handleSubmit(values: { name: string }) { if (session) { await renameSession(session.id, values.name); await showToast({ style: Toast.Style.Success, title: "Sitzung umbenannt", }); } else { await createSession(values.name.trim() || undefined); await showToast({ style: Toast.Style.Success, title: "Sitzung erstellt", }); } await onSaved(); pop(); } return (
} > ); } function SessionDetail({ session }: { session: VelumSession }) { const markdown = [ `# ${session.name}`, sessionSubtitle(session), "", `Erstellt: ${new Date(session.createdAt).toLocaleString()}`, "", "## Zuordnung", mappingSummary(session.mapping), "", "## Platzhalter", ...Object.entries(session.mapping) .sort(([a], [b]) => a.localeCompare(b)) .map(([placeholder, entry]) => `- \`${placeholder}\` · ${entry.type}`), ].join("\n"); return ; }