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 }) { const [selected, setSelected] = useState>( () => 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(); 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 } actions={} /> {[...byType].map(([type, typeGroups]) => {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 1 ? `${group.hits.length} Vorkommen` : undefined} detail={} actions={ toggle(ids)} /> } />; })} )} ; }