fix: guide project mapping recovery and allow manual table entries
This commit is contained in:
@@ -2,14 +2,16 @@ import {
|
||||
Action,
|
||||
ActionPanel,
|
||||
Detail,
|
||||
Form,
|
||||
Icon,
|
||||
List,
|
||||
showToast,
|
||||
Toast,
|
||||
useNavigation,
|
||||
} from "@raycast/api";
|
||||
import { useEffect, useState } from "react";
|
||||
import { canonicalProjectPath, listAllRuns } from "./lib/velum-project";
|
||||
import { listSessions } from "./sessions";
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { addProjectMappingEntry, canonicalProjectPath, listAllRuns, prepareManualMappingEntry } from "./lib/velum-project";
|
||||
import { getSession, listSessions, updateSessionMapping } from "./sessions";
|
||||
import type { PlaceholderMapping } from "./types";
|
||||
import { markdownCodeBlock } from "./ui";
|
||||
|
||||
@@ -20,15 +22,48 @@ type TableSource = {
|
||||
mapping: PlaceholderMapping;
|
||||
};
|
||||
|
||||
type ManualEntry = { original: string; type: string; placeholder?: string };
|
||||
|
||||
function AddMappingForm(props: { onAdd: (input: ManualEntry) => Promise<PlaceholderMapping>;
|
||||
onSaved: (mapping: PlaceholderMapping) => void }) {
|
||||
const { pop } = useNavigation();
|
||||
const [busy, setBusy] = useState(false);
|
||||
async function save(values: { original: string; type: string; placeholder: string }) {
|
||||
setBusy(true);
|
||||
try {
|
||||
const type = values.type.trim() || /^([A-ZÄÖÜ][A-ZÄÖÜ0-9_]*)_\d+$/iu.exec(values.placeholder.trim())?.[1] || "BEGRIFF";
|
||||
const mapping = await props.onAdd({ ...values, type });
|
||||
props.onSaved(mapping);
|
||||
await showToast({ style: Toast.Style.Success, title: "Zuordnung gespeichert" });
|
||||
pop();
|
||||
} catch (error) {
|
||||
await showToast({ style: Toast.Style.Failure, title: "Zuordnung nicht gespeichert",
|
||||
message: (error as Error).message });
|
||||
} finally { setBusy(false); }
|
||||
}
|
||||
return <Form isLoading={busy} navigationTitle="Zuordnung ergänzen"
|
||||
actions={<ActionPanel><Action.SubmitForm title="Zuordnung speichern" onSubmit={save} /></ActionPanel>}>
|
||||
<Form.Description text="Der Eintrag wird zur Wiederherstellung verwendet. Für eine garantierte Maskierung bei künftigen Projektläufen zusätzlich eine Projektregel anlegen." />
|
||||
<Form.TextField id="original" title="Klartext-Begriff" placeholder="Originalwert" />
|
||||
<Form.TextField id="type" title="Kategorie (optional)" placeholder="Aus Platzhalter ableiten; sonst BEGRIFF" />
|
||||
<Form.TextField id="placeholder" title="Platzhalter (optional)" placeholder="z. B. PERSON_61; leer = nächste freie Nummer" />
|
||||
</Form>;
|
||||
}
|
||||
|
||||
export function TranslationTable({
|
||||
title,
|
||||
mapping,
|
||||
onAdd,
|
||||
}: {
|
||||
title: string;
|
||||
mapping: PlaceholderMapping;
|
||||
onAdd?: (input: ManualEntry) => Promise<PlaceholderMapping>;
|
||||
}) {
|
||||
const { push } = useNavigation();
|
||||
const [visibleMapping, setVisibleMapping] = useState(mapping);
|
||||
useEffect(() => setVisibleMapping(mapping), [mapping]);
|
||||
const groups = new Map<string, Array<[string, PlaceholderMapping[string]]>>();
|
||||
for (const entry of Object.entries(mapping).sort(([a], [b]) =>
|
||||
for (const entry of Object.entries(visibleMapping).sort(([a], [b]) =>
|
||||
a.localeCompare(b),
|
||||
)) {
|
||||
const type = entry[1].type || "Sonstige";
|
||||
@@ -46,6 +81,12 @@ export function TranslationTable({
|
||||
title="Keine Zuordnungen"
|
||||
description="Für diesen Stand sind noch keine Übersetzungen gespeichert."
|
||||
/>
|
||||
{onAdd && <List.Section title="Pflege">
|
||||
<List.Item icon={Icon.Plus} title="Begriff oder fehlenden Platzhalter hinzufügen"
|
||||
subtitle="Neuer Eintrag für diese Übersetzungstabelle"
|
||||
actions={<ActionPanel><Action title="Zuordnung hinzufügen" icon={Icon.Plus}
|
||||
onAction={() => push(<AddMappingForm onAdd={onAdd} onSaved={setVisibleMapping} />)} /></ActionPanel>} />
|
||||
</List.Section>}
|
||||
{[...groups]
|
||||
.sort(([a], [b]) => a.localeCompare(b))
|
||||
.map(([type, entries]) => (
|
||||
@@ -122,8 +163,8 @@ export default function Command() {
|
||||
const [sessions, setSessions] = useState<TableSource[]>([]);
|
||||
const [busy, setBusy] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
Promise.all([listAllRuns(), listSessions()])
|
||||
const refresh = useCallback(async () => {
|
||||
await Promise.all([listAllRuns(), listSessions()])
|
||||
.then(([runs, savedSessions]) => {
|
||||
const latest = new Map<string, TableSource>();
|
||||
for (const { run } of runs) {
|
||||
@@ -147,16 +188,13 @@ export default function Command() {
|
||||
mapping: session.mapping,
|
||||
})),
|
||||
);
|
||||
})
|
||||
.catch((error: Error) =>
|
||||
showToast({
|
||||
style: Toast.Style.Failure,
|
||||
title: "Übersetzungstabellen nicht lesbar",
|
||||
message: error.message,
|
||||
}),
|
||||
)
|
||||
.finally(() => setBusy(false));
|
||||
});
|
||||
}, []);
|
||||
useEffect(() => {
|
||||
refresh().catch((error: Error) => showToast({ style: Toast.Style.Failure,
|
||||
title: "Übersetzungstabellen nicht lesbar", message: error.message }))
|
||||
.finally(() => setBusy(false));
|
||||
}, [refresh]);
|
||||
|
||||
return (
|
||||
<List
|
||||
@@ -187,6 +225,11 @@ export default function Command() {
|
||||
<TranslationTable
|
||||
title={source.title}
|
||||
mapping={source.mapping}
|
||||
onAdd={async (input) => {
|
||||
const run = await addProjectMappingEntry(source.id, input);
|
||||
await refresh();
|
||||
return run.mapping;
|
||||
}}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
@@ -211,6 +254,19 @@ export default function Command() {
|
||||
<TranslationTable
|
||||
title={source.title}
|
||||
mapping={source.mapping}
|
||||
onAdd={async (input) => {
|
||||
const session = await getSession(source.id);
|
||||
if (!session) throw new Error("Sitzung nicht gefunden.");
|
||||
const normalized = Object.fromEntries(Object.entries(session.mapping)
|
||||
.map(([key, entry]) => [key.replace(/^<|>$/g, ""), entry]));
|
||||
const { placeholder, entry } = prepareManualMappingEntry(normalized, [], input);
|
||||
const bracketed = Object.keys(session.mapping).some((key) => key.startsWith("<"));
|
||||
const key = bracketed ? `<${placeholder}>` : placeholder;
|
||||
const updated = await updateSessionMapping(session.id,
|
||||
{ ...session.mapping, [key]: entry }, session.entityTypes);
|
||||
await refresh();
|
||||
return updated.mapping;
|
||||
}}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user