import { Action, ActionPanel, Clipboard, Detail, Form, Icon, showToast, Toast, useNavigation, } from "@raycast/api"; import { getSelectedTextSafely } from "./selection"; import { useEffect, useMemo, useState } from "react"; import { dirname } from "node:path"; import { DocumentResult } from "./document-result"; import { saveDocumentCopy } from "./documents"; import { createSession, getActiveSessionId, getSession, listSessions, setActiveSession, updateSessionMapping, } from "./sessions"; import type { EntityType, ImagePolicy, PseudonymizeResponse, VelumSession, } from "./types"; import { markdownCodeBlock, mappingSummary, NEW_SESSION_ID, sessionSubtitle, } from "./ui"; import { getEntityTypes, pseudonymize, pseudonymizeDocument } from "./velum"; type FormValues = { text?: string; inputMode: "text" | "document"; files?: string[]; outputDirectory?: string[]; imagePolicy?: ImagePolicy; sessionId: string; entityTypes: string[]; }; export default function Command() { const [text, setText] = useState(""); const [inputMode, setInputMode] = useState("text"); const [imagePolicy, setImagePolicy] = useState("placeholder"); const [files, setFiles] = useState([]); const [isSubmitting, setIsSubmitting] = useState(false); const [sessions, setSessions] = useState([]); const [activeSessionId, setActiveSessionId] = useState(); const [entityTypes, setEntityTypes] = useState([]); const [selectedEntityTypes, setSelectedEntityTypes] = useState([]); const [isLoading, setIsLoading] = useState(true); const { push } = useNavigation(); useEffect(() => { async function load() { const [loadedSessions, activeId] = await Promise.all([ listSessions(), getActiveSessionId(), ]); setSessions(loadedSessions); setActiveSessionId(activeId); try { const loadedEntityTypes = await getEntityTypes(); setEntityTypes(loadedEntityTypes); setSelectedEntityTypes(loadedEntityTypes); } catch { setEntityTypes([]); } finally { setIsLoading(false); } } load(); }, []); const defaultSessionId = useMemo( () => activeSessionId || sessions[0]?.id || NEW_SESSION_ID, [activeSessionId, sessions], ); async function loadSelectedText() { const selected = await getSelectedTextSafely(); if (!selected) { await showToast({ style: Toast.Style.Failure, title: "Kein markierter Text gefunden", }); return; } setInputMode("text"); setText(selected); } async function loadClipboardText() { const clipboardText = await Clipboard.readText(); if (!clipboardText) { await showToast({ style: Toast.Style.Failure, title: "Zwischenablage enthält keinen Text", }); return; } setInputMode("text"); setText(clipboardText); } async function handleSubmit(values: FormValues) { if (isSubmitting || isLoading) return; const isDocument = values.inputMode === "document"; const input = (values.text ?? "").trim(); const filePath = values.files?.[0]; if (isDocument ? !filePath : !input) { await showToast({ style: Toast.Style.Failure, title: isDocument ? "Dokument zum Pseudonymisieren auswählen" : "Text zum Pseudonymisieren eingeben", }); return; } setIsSubmitting(true); const toast = await showToast({ style: Toast.Style.Animated, title: "Pseudonymisiere…", }); try { const session = values.sessionId === NEW_SESSION_ID ? await createSession() : ((await getSession(values.sessionId)) ?? (await createSession())); await setActiveSession(session.id); if (isDocument && filePath) { const result = await pseudonymizeDocument( filePath, session.mapping, values.entityTypes, values.imagePolicy ?? "placeholder", ); const updatedSession = await updateSessionMapping( session.id, result.mapping, values.entityTypes, ); const path = await saveDocumentCopy( result, values.outputDirectory?.[0] ?? dirname(filePath), ); toast.style = Toast.Style.Success; toast.title = result.warnings.length ? "Dokument mit Warnhinweis bereit" : "Dokument pseudonymisiert"; toast.message = `${result.images_replaced} Bilder ersetzt · ${result.images_removed} entfernt`; push( , ); return; } const result = await pseudonymize( input, session.mapping, values.entityTypes, ); const updatedSession = await updateSessionMapping( session.id, result.mapping, result.selected_entity_types, ); toast.style = Toast.Style.Success; toast.title = "Pseudonymisiert"; toast.message = `${result.entity_count} Zuordnungen in ${updatedSession.name}`; push(); } catch (error) { toast.style = Toast.Style.Failure; toast.title = "Pseudonymisierung fehlgeschlagen"; toast.message = error instanceof Error ? error.message : String(error); } finally { setIsSubmitting(false); } } return (
} > {inputMode === "text" ? ( ) : ( <> setImagePolicy(value as ImagePolicy)} > )} {sessions.map((session) => ( ))} {entityTypes.length > 0 ? ( {entityTypes.map((entityType) => ( ))} ) : null} ); } function PseudonymizeResult({ result, session, }: { result: PseudonymizeResponse; session: VelumSession; }) { const markdown = [ `# ${session.name}`, sessionSubtitle(session), "", "## Pseudonymisierter Text", markdownCodeBlock(result.pseudonymized_text), "", "## Zuordnung", mappingSummary(result.mapping), ].join("\n"); return ( Clipboard.paste(result.pseudonymized_text)} /> } /> ); }