import { Action, ActionPanel, Clipboard, Detail, Form, Icon, showToast, Toast, useNavigation, } from "@raycast/api"; import { getSelectedTextSafely } from "./selection"; import { useEffect, useMemo, useState } from "react"; import { createSession, getActiveSessionId, getSession, listSessions, setActiveSession, updateSessionMapping, } from "./sessions"; import type { EntityType, PseudonymizeResponse, VelumSession } from "./types"; import { markdownCodeBlock, mappingSummary, NEW_SESSION_ID, sessionSubtitle, } from "./ui"; import { getEntityTypes, pseudonymize } from "./velum"; type FormValues = { text: string; sessionId: string; entityTypes: string[]; }; export default function Command() { const [text, setText] = useState(""); 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; } 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; } setText(clipboardText); } async function handleSubmit(values: FormValues) { const input = values.text.trim(); if (!input) { await showToast({ style: Toast.Style.Failure, title: "Text zum Pseudonymisieren eingeben", }); return; } 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); 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); } } return (
} > {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)} /> } /> ); }