368 lines
10 KiB
TypeScript
368 lines
10 KiB
TypeScript
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<ImagePolicy>("placeholder");
|
|
const [files, setFiles] = useState<string[]>([]);
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
const [sessions, setSessions] = useState<VelumSession[]>([]);
|
|
const [activeSessionId, setActiveSessionId] = useState<string>();
|
|
const [entityTypes, setEntityTypes] = useState<EntityType[]>([]);
|
|
const [selectedEntityTypes, setSelectedEntityTypes] = useState<string[]>([]);
|
|
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(
|
|
<DocumentResult
|
|
result={result}
|
|
session={updatedSession}
|
|
path={path}
|
|
/>,
|
|
);
|
|
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(<PseudonymizeResult result={result} session={updatedSession} />);
|
|
} catch (error) {
|
|
toast.style = Toast.Style.Failure;
|
|
toast.title = "Pseudonymisierung fehlgeschlagen";
|
|
toast.message = error instanceof Error ? error.message : String(error);
|
|
} finally {
|
|
setIsSubmitting(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Form
|
|
isLoading={isLoading || isSubmitting}
|
|
actions={
|
|
<ActionPanel>
|
|
<Action.SubmitForm
|
|
title="Pseudonymisieren"
|
|
icon={Icon.EyeDisabled}
|
|
onSubmit={handleSubmit}
|
|
/>
|
|
<Action
|
|
title="Markierten Text übernehmen"
|
|
icon={Icon.TextSelection}
|
|
onAction={loadSelectedText}
|
|
/>
|
|
<Action
|
|
title="Zwischenablage übernehmen"
|
|
icon={Icon.Clipboard}
|
|
onAction={loadClipboardText}
|
|
/>
|
|
</ActionPanel>
|
|
}
|
|
>
|
|
<Form.Dropdown
|
|
id="inputMode"
|
|
title="Eingabe"
|
|
value={inputMode}
|
|
onChange={setInputMode}
|
|
>
|
|
<Form.Dropdown.Item value="text" title="Text" icon={Icon.Text} />
|
|
<Form.Dropdown.Item
|
|
value="document"
|
|
title="Office-Dokument oder PDF"
|
|
icon={Icon.Document}
|
|
/>
|
|
</Form.Dropdown>
|
|
{inputMode === "text" ? (
|
|
<Form.TextArea
|
|
id="text"
|
|
title="Text"
|
|
value={text}
|
|
onChange={setText}
|
|
placeholder="Zu pseudonymisierender Text"
|
|
/>
|
|
) : (
|
|
<>
|
|
<Form.FilePicker
|
|
id="files"
|
|
title="Dokument"
|
|
allowMultipleSelection={false}
|
|
canChooseDirectories={false}
|
|
value={files}
|
|
onChange={setFiles}
|
|
/>
|
|
<Form.Description text="DOCX, XLSX, PPTX oder PDF · maximal 20 MiB. PDF wird als Word-Dokument ausgegeben." />
|
|
<Form.Dropdown
|
|
id="imagePolicy"
|
|
title="Bilder im Dokument"
|
|
value={imagePolicy}
|
|
onChange={(value) => setImagePolicy(value as ImagePolicy)}
|
|
>
|
|
<Form.Dropdown.Item
|
|
value="placeholder"
|
|
title="Datenschutz-Platzhalter (Standard)"
|
|
/>
|
|
<Form.Dropdown.Item
|
|
value="remove"
|
|
title="Bilder vollständig entfernen"
|
|
/>
|
|
<Form.Dropdown.Item
|
|
value="keep"
|
|
title="Bilder behalten — Datenschutzrisiko"
|
|
icon={Icon.Warning}
|
|
/>
|
|
</Form.Dropdown>
|
|
<Form.Description
|
|
title={imagePolicy === "keep" ? "Achtung" : "Bildbehandlung"}
|
|
text={
|
|
imagePolicy === "keep"
|
|
? "Bilder können sensible Informationen enthalten. Sie werden unverändert übernommen und nicht auf personenbezogene Daten geprüft."
|
|
: "Bildinhalte werden nicht analysiert. Platzhalter behalten Position und Größe soweit möglich bei. Nicht sicher bereinigbare Inhalte werden abgewiesen."
|
|
}
|
|
/>
|
|
<Form.FilePicker
|
|
id="outputDirectory"
|
|
title="Ausgabeordner"
|
|
canChooseFiles={false}
|
|
canChooseDirectories
|
|
allowMultipleSelection={false}
|
|
/>
|
|
<Form.Description text="Ohne Ordnerauswahl wird die Kopie neben dem Original gespeichert. Bestehende Dateien werden nicht überschrieben." />
|
|
</>
|
|
)}
|
|
<Form.Dropdown
|
|
id="sessionId"
|
|
title="Sitzung"
|
|
defaultValue={defaultSessionId}
|
|
>
|
|
<Form.Dropdown.Item
|
|
value={NEW_SESSION_ID}
|
|
title="Neue Sitzung"
|
|
icon={Icon.Plus}
|
|
/>
|
|
{sessions.map((session) => (
|
|
<Form.Dropdown.Item
|
|
key={session.id}
|
|
value={session.id}
|
|
title={session.name}
|
|
icon={
|
|
session.id === activeSessionId ? Icon.CheckCircle : Icon.Circle
|
|
}
|
|
/>
|
|
))}
|
|
</Form.Dropdown>
|
|
{entityTypes.length > 0 ? (
|
|
<Form.TagPicker
|
|
id="entityTypes"
|
|
title="Entitätstypen"
|
|
value={selectedEntityTypes}
|
|
onChange={setSelectedEntityTypes}
|
|
>
|
|
{entityTypes.map((entityType) => (
|
|
<Form.TagPicker.Item
|
|
key={entityType}
|
|
value={entityType}
|
|
title={entityType}
|
|
/>
|
|
))}
|
|
</Form.TagPicker>
|
|
) : null}
|
|
</Form>
|
|
);
|
|
}
|
|
|
|
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 (
|
|
<Detail
|
|
markdown={markdown}
|
|
actions={
|
|
<ActionPanel>
|
|
<Action.CopyToClipboard
|
|
title="Pseudonymisierten Text kopieren"
|
|
content={result.pseudonymized_text}
|
|
/>
|
|
<Action
|
|
title="Pseudonymisierten Text einfügen"
|
|
icon={Icon.TextCursor}
|
|
onAction={() => Clipboard.paste(result.pseudonymized_text)}
|
|
/>
|
|
</ActionPanel>
|
|
}
|
|
/>
|
|
);
|
|
}
|