forked from r.muellner/velum-raycast
226 lines
6.5 KiB
TypeScript
226 lines
6.5 KiB
TypeScript
import {
|
|
Action,
|
|
ActionPanel,
|
|
Clipboard,
|
|
Detail,
|
|
Form,
|
|
Icon,
|
|
showHUD,
|
|
showToast,
|
|
Toast,
|
|
useNavigation,
|
|
} from "@raycast/api";
|
|
import { getSelectedTextSafely } from "./selection";
|
|
import { useEffect, useState } from "react";
|
|
import { getActiveSessionId, listSessions } from "./sessions";
|
|
import type { EntityType, PseudonymizeResponse, VelumSession } from "./types";
|
|
import {
|
|
markdownCodeBlock,
|
|
mappingSummary,
|
|
sessionSubtitle,
|
|
} from "./ui";
|
|
import { getEntityTypes } from "./velum";
|
|
import { ContextPicker, type ContextFormValues } from "./context-picker";
|
|
import { prepareTextInContext } from "./text-context";
|
|
import { TextHitReview } from "./text-hit-review";
|
|
import { getPreferences } from "./preferences";
|
|
|
|
type FormValues = ContextFormValues & {
|
|
text: string;
|
|
entityTypes: string[];
|
|
};
|
|
|
|
export default function Command(props: { initialSource?: "selection" | "clipboard"; autoOutput?: boolean } = {}) {
|
|
const [text, setText] = useState("");
|
|
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);
|
|
if (props.initialSource === "selection") {
|
|
const selected = await getSelectedTextSafely();
|
|
if (selected) setText(selected);
|
|
else await showToast({ style: Toast.Style.Failure, title: "Kein markierter Text gefunden" });
|
|
} else if (props.initialSource === "clipboard") {
|
|
const clipboard = await Clipboard.readText();
|
|
if (clipboard) setText(clipboard);
|
|
else await showToast({ style: Toast.Style.Failure, title: "Zwischenablage enthält keinen Text" });
|
|
}
|
|
|
|
try {
|
|
const loadedEntityTypes = await getEntityTypes();
|
|
setEntityTypes(loadedEntityTypes);
|
|
setSelectedEntityTypes(loadedEntityTypes);
|
|
} catch {
|
|
setEntityTypes([]);
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
}
|
|
load();
|
|
}, []);
|
|
|
|
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 prepared = await prepareTextInContext(values, input, values.entityTypes);
|
|
toast.style = Toast.Style.Success;
|
|
toast.title = "Treffer zur Prüfung bereit";
|
|
toast.message = `${prepared.raw.spans.length} Treffer prüfen`;
|
|
push(<TextHitReview prepared={prepared} onConfirmed={async (result, session) => {
|
|
if (props.autoOutput) {
|
|
if (getPreferences().quickOutput === "paste") {
|
|
await Clipboard.paste(result.pseudonymized_text);
|
|
await showHUD(`Pseudonymisierten Text eingefügt (${session.name})`);
|
|
} else {
|
|
await Clipboard.copy(result.pseudonymized_text);
|
|
await showHUD(`Pseudonymisierten Text kopiert (${session.name})`);
|
|
}
|
|
} else {
|
|
push(<PseudonymizeResult result={result} session={session} />);
|
|
}
|
|
}} />);
|
|
} catch (error) {
|
|
toast.style = Toast.Style.Failure;
|
|
toast.title = "Pseudonymisierung fehlgeschlagen";
|
|
toast.message = error instanceof Error ? error.message : String(error);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Form
|
|
isLoading={isLoading}
|
|
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.TextArea
|
|
id="text"
|
|
title="Text"
|
|
value={text}
|
|
onChange={setText}
|
|
placeholder="Zu pseudonymisierender Text"
|
|
/>
|
|
<ContextPicker sessions={sessions} activeSessionId={activeSessionId}
|
|
availableEntityTypes={entityTypes} onEntityTypesChange={setSelectedEntityTypes} />
|
|
{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>
|
|
}
|
|
/>
|
|
);
|
|
}
|