feat: choose project or session for text commands

This commit is contained in:
muena
2026-10-19 06:34:07 +02:00
parent 65134735f1
commit ebf78128c4
28 changed files with 386 additions and 641 deletions

View File

@@ -5,36 +5,31 @@ import {
Detail,
Form,
Icon,
showHUD,
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 { useEffect, useState } from "react";
import { getActiveSessionId, listSessions } from "./sessions";
import type { EntityType, PseudonymizeResponse, VelumSession } from "./types";
import {
markdownCodeBlock,
mappingSummary,
NEW_SESSION_ID,
sessionSubtitle,
} from "./ui";
import { getEntityTypes, pseudonymize } from "./velum";
import { getEntityTypes } from "./velum";
import { ContextPicker, type ContextFormValues } from "./context-picker";
import { pseudonymizeInContext } from "./text-context";
import { getPreferences } from "./preferences";
type FormValues = {
type FormValues = ContextFormValues & {
text: string;
sessionId: string;
entityTypes: string[];
};
export default function Command() {
export default function Command(props: { initialSource?: "selection" | "clipboard"; autoOutput?: boolean } = {}) {
const [text, setText] = useState("");
const [sessions, setSessions] = useState<VelumSession[]>([]);
const [activeSessionId, setActiveSessionId] = useState<string>();
@@ -51,6 +46,15 @@ export default function Command() {
]);
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();
@@ -65,11 +69,6 @@ export default function Command() {
load();
}, []);
const defaultSessionId = useMemo(
() => activeSessionId || sessions[0]?.id || NEW_SESSION_ID,
[activeSessionId, sessions],
);
async function loadSelectedText() {
const selected = await getSelectedTextSafely();
if (!selected) {
@@ -110,27 +109,21 @@ export default function Command() {
});
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,
);
const { result, session } = await pseudonymizeInContext(values, input, values.entityTypes);
toast.style = Toast.Style.Success;
toast.title = "Pseudonymisiert";
toast.message = `${result.entity_count} Zuordnungen in ${updatedSession.name}`;
toast.message = `${result.entity_count} Zuordnungen in ${session.name}`;
push(<PseudonymizeResult result={result} session={updatedSession} />);
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";
@@ -168,27 +161,8 @@ export default function Command() {
onChange={setText}
placeholder="Zu pseudonymisierender Text"
/>
<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>
<ContextPicker sessions={sessions} activeSessionId={activeSessionId}
availableEntityTypes={entityTypes} onEntityTypesChange={setSelectedEntityTypes} />
{entityTypes.length > 0 ? (
<Form.TagPicker
id="entityTypes"