Files
velum-raycast/src/text-hit-review.tsx

73 lines
4.0 KiB
TypeScript

import { Action, ActionPanel, Icon, List, showToast, Toast } from "@raycast/api";
import { useMemo, useState } from "react";
import { groupReviewHits } from "./lib/project-review";
import { commitPreparedText, type PreparedText } from "./text-context";
import type { PseudonymizeResponse, VelumSession } from "./types";
export function TextHitReview(props: { prepared: PreparedText; forAi?: boolean;
onConfirmed: (result: PseudonymizeResponse, session: VelumSession) => Promise<void> | void }) {
const [selected, setSelected] = useState<Set<number>>(
() => new Set(props.prepared.raw.spans.map((_, index) => index)));
const [busy, setBusy] = useState(false);
const groups = useMemo(() => groupReviewHits(props.prepared.raw.spans.map((span, index) => ({
...span, id: String(index),
}))), [props.prepared]);
const byType = new Map<string, typeof groups>();
for (const group of groups) {
if (!byType.has(group.type)) byType.set(group.type, []);
byType.get(group.type)!.push(group);
}
async function confirm() {
if (busy) return;
setBusy(true);
const toast = await showToast({ style: Toast.Style.Animated, title: "Auswahl wird übernommen" });
try {
const { result, session } = await commitPreparedText(props.prepared, selected, props.forAi);
toast.style = Toast.Style.Success;
toast.title = "Auswahl übernommen";
toast.message = `${selected.size} Treffer bestätigt`;
await props.onConfirmed(result, session);
} catch (error) {
toast.style = Toast.Style.Failure;
toast.title = "Auswahl nicht übernommen";
toast.message = error instanceof Error ? error.message : String(error);
} finally { setBusy(false); }
}
function toggle(ids: number[]) {
setSelected((current) => {
const next = new Set(current);
const all = ids.every((id) => next.has(id));
for (const id of ids) { if (all) next.delete(id); else next.add(id); }
return next;
});
}
return <List isLoading={busy} isShowingDetail navigationTitle="Treffer vor Pseudonymisierung prüfen"
searchBarPlaceholder="Begriff, Kategorie oder Platzhalter suchen">
<List.Section title="Abschluss">
<List.Item icon={Icon.CheckCircle} title="Auswahl bestätigen"
subtitle={`${selected.size} von ${props.prepared.raw.spans.length} Treffern werden maskiert · Abgewählte werden als Ausnahme gespeichert`}
detail={<List.Item.Detail markdown="Prüfe die erkannten Begriffe unten. Klicke auf einen Treffer, um ihn ab- oder wieder anzuwählen. Erst mit **Auswahl bestätigen** werden die Zuordnungen gespeichert und der nächste Schritt gestartet." />}
actions={<ActionPanel><Action title="Auswahl bestätigen und fortfahren" icon={Icon.CheckCircle} onAction={confirm} /></ActionPanel>} />
</List.Section>
{[...byType].map(([type, typeGroups]) => <List.Section key={type} title={`${type} · ${typeGroups.length} Begriffe`}>
{typeGroups.map((group) => {
const ids = group.hits.map((hit) => Number(hit.id));
const all = ids.every((id) => selected.has(id));
const first = group.hits[0];
const start = Math.max(0, first.start - 90);
const end = Math.min([...props.prepared.text].length, first.end + 90);
const context = [...props.prepared.text].slice(start, end).join("");
return <List.Item key={group.key} icon={all ? Icon.CheckCircle : Icon.Circle}
title={`${group.original} → ${group.placeholder}`}
subtitle={group.hits.length > 1 ? `${group.hits.length} Vorkommen` : undefined}
detail={<List.Item.Detail markdown={`**Kontext**\n\n${context.replace(/`/g, "'")}`} />}
actions={<ActionPanel>
<Action title={all ? "Nicht maskieren" : "Maskieren"} icon={all ? Icon.XMarkCircle : Icon.CheckCircle}
onAction={() => toggle(ids)} />
<Action title="Auswahl bestätigen und fortfahren" icon={Icon.CheckCircle} onAction={confirm} />
</ActionPanel>} />;
})}
</List.Section>)}
</List>;
}