Files
velum-raycast/src/manage-sessions.tsx

274 lines
7.4 KiB
TypeScript

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<VelumSession[]>([]);
const [activeSessionId, setActiveSessionId] = useState<string>();
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 (
<List isLoading={isLoading} searchBarPlaceholder="Velum-Sitzungen suchen">
<List.EmptyView
title="Keine Sitzungen"
description="Lege eine Sitzung an oder pseudonymisiere Text, um Zuordnungen zu speichern."
actions={
<ActionPanel>
<Action.Push
title="Neue Sitzung"
icon={Icon.Plus}
target={<SessionNameForm onSaved={reload} />}
/>
</ActionPanel>
}
/>
{sessions.map((session) => (
<List.Item
key={session.id}
title={session.name}
subtitle={sessionSubtitle(session)}
icon={session.id === activeSessionId ? Icon.CheckCircle : Icon.Circle}
accessories={[{ text: `${countMappingEntries(session.mapping)}` }]}
actions={
<ActionPanel>
<Action.Push
title="Übersetzungstabelle anzeigen"
icon={Icon.List}
target={<TranslationTable title={session.name} mapping={session.mapping}
removeDescription="Die gesamte Gruppe wird aus der Sitzung entfernt und künftig dort nicht mehr vorgeschlagen. Bereits kopierte oder eingefügte Texte werden nicht rückwirkend geändert."
onRemove={async (root) => {
const updated = await releaseSessionMappingGroup(session.id, root);
await reload();
return updated.mapping;
}} />}
/>
<Action
title="Als Aktive Sitzung Setzen"
icon={Icon.CheckCircle}
onAction={() => activate(session)}
/>
<Action.Push
title="Details anzeigen"
icon={Icon.List}
target={<SessionDetail session={session} />}
/>
<Action.Push
title="Sitzung umbenennen"
icon={Icon.Pencil}
target={<SessionNameForm session={session} onSaved={reload} />}
/>
<Action.Push
title="Neue Sitzung"
icon={Icon.Plus}
target={<SessionNameForm onSaved={reload} />}
/>
<Action
title="Zuordnung leeren"
icon={Icon.XMarkCircle}
style={Action.Style.Destructive}
onAction={() => clearMapping(session)}
/>
<Action
title="Sitzung löschen"
icon={Icon.Trash}
style={Action.Style.Destructive}
onAction={() => remove(session)}
/>
<Action
title="Alle Sitzungen löschen"
icon={Icon.Trash}
style={Action.Style.Destructive}
onAction={removeAll}
/>
</ActionPanel>
}
/>
))}
</List>
);
}
function SessionNameForm({
session,
onSaved,
}: {
session?: VelumSession;
onSaved: () => Promise<void>;
}) {
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 (
<Form
actions={
<ActionPanel>
<Action.SubmitForm
title={session ? "Sitzung Umbenennen" : "Sitzung Erstellen"}
onSubmit={handleSubmit}
/>
</ActionPanel>
}
>
<Form.TextField
id="name"
title="Name"
defaultValue={session?.name}
placeholder="Sitzungsname"
/>
</Form>
);
}
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 <Detail markdown={markdown} />;
}