feat: select all files or one folder for project restoration

This commit is contained in:
muena
2026-10-20 11:27:11 +02:00
parent 731c449c33
commit 41b935a145
3 changed files with 101 additions and 6 deletions

View File

@@ -0,0 +1,26 @@
import { dirname } from "node:path";
import type { ProjectFile } from "./velum-project";
export type RestorableFolder = { path: string; fileCount: number; textCount: number; documentCount: number };
export function listRestorableFolders(files: ProjectFile[]): RestorableFolder[] {
const folders = new Map<string, RestorableFolder>();
for (const file of files) {
let path = dirname(file.relPath);
while (path !== ".") {
const folder = folders.get(path) ?? { path, fileCount: 0, textCount: 0, documentCount: 0 };
folder.fileCount++;
if (file.kind === "text") folder.textCount++;
else folder.documentCount++;
folders.set(path, folder);
path = dirname(path);
}
}
return [...folders.values()].sort((a, b) => a.path.localeCompare(b.path, "de"));
}
export function selectRestorableFolder(files: ProjectFile[], folder: string, includeDocuments = false): Set<string> {
const prefix = `${folder}/`;
return new Set(files.filter((file) => file.relPath.startsWith(prefix)
&& (includeDocuments || file.kind === "text")).map((file) => file.relPath));
}

View File

@@ -13,6 +13,8 @@ import { TranslationTable } from "./show-translation-tables";
import { detachMappingGroup, mappingForRestoration, mergeMappingGroups } from "./lib/mapping-groups"; import { detachMappingGroup, mappingForRestoration, mergeMappingGroups } from "./lib/mapping-groups";
import { releaseProjectMapping } from "./project-mapping-actions"; import { releaseProjectMapping } from "./project-mapping-actions";
import { projectMappingCreatedAt } from "./lib/mapping-created-at"; import { projectMappingCreatedAt } from "./lib/mapping-created-at";
import { listRestorableFolders, selectRestorableFolder } from "./lib/restore-file-selection";
import type { RestorableFolder } from "./lib/restore-file-selection";
type SavedRun = { run: ProjectRun; directory: string }; type SavedRun = { run: ProjectRun; directory: string };
type RestoreDecision = MappingEntry | null; type RestoreDecision = MappingEntry | null;
@@ -176,6 +178,26 @@ function RevisionReview(props: { saved: SavedRun; onSaved: () => Promise<void> }
</List>; </List>;
} }
function FolderPicker(props: { folders: RestorableFolder[]; onSelect: (folder: string, includeDocuments: boolean) => void }) {
const { pop } = useNavigation();
function choose(folder: string, includeDocuments: boolean) {
props.onSelect(folder, includeDocuments);
pop();
}
return <List navigationTitle="Nur einen Ordner wiederherstellen" searchBarPlaceholder="Ordner suchen">
<List.Section title="Ordner samt Unterordnern · Textdateien zuerst">
{props.folders.map((folder) => <List.Item key={folder.path} icon={Icon.Folder}
title={folder.path} subtitle={`${folder.fileCount} Dateien · ${folder.textCount} Text · ${folder.documentCount} Office`}
actions={<ActionPanel>
{folder.textCount > 0 && <Action title="Nur Textdateien dieses Ordners auswählen" icon={Icon.CheckCircle}
onAction={() => choose(folder.path, false)} />}
{folder.documentCount > 0 && <Action title="Diesen Ordner inklusive Office auswählen" icon={Icon.Folder}
onAction={() => choose(folder.path, true)} />}
</ActionPanel>} />)}
</List.Section>
</List>;
}
function FilePicker(props: { run: ProjectRun; directory: string; historical?: boolean; earlierRuns: SavedRun[] }) { function FilePicker(props: { run: ProjectRun; directory: string; historical?: boolean; earlierRuns: SavedRun[] }) {
const { push } = useNavigation(); const { push } = useNavigation();
const [files, setFiles] = useState<ProjectFile[]>([]); const [files, setFiles] = useState<ProjectFile[]>([]);
@@ -313,9 +335,25 @@ function FilePicker(props: { run: ProjectRun; directory: string; historical?: bo
} }
const textFiles = files.filter((file) => file.kind === "text"); const textFiles = files.filter((file) => file.kind === "text");
const complexFiles = files.filter((file) => file.kind === "document"); const complexFiles = files.filter((file) => file.kind === "document");
const folders = listRestorableFolders(files);
const allSelected = files.length > 0 && selected.size === files.length;
const selectOnlyFolder = (folder: string, includeDocuments = false) =>
setSelected(selectRestorableFolder(files, folder, includeDocuments));
const openFolderPicker = () => push(<FolderPicker folders={folders} onSelect={selectOnlyFolder} />);
const selectionActions = () => <ActionPanel.Section title="Dateiauswahl">
<Action title={allSelected ? "Alle Dateien abwählen" : "Alle Dateien auswählen (inklusive Office)"}
shortcut={{ modifiers: ["cmd", "shift"], key: "a" }}
onAction={() => setSelected((current) => current.size === files.length
? new Set() : new Set(files.map((file) => file.relPath)))} />
<Action title="Nur Textdateien auswählen" onAction={() => setSelected(new Set(textFiles.map((item) => item.relPath)))} />
<Action title="Keine auswählen" onAction={() => setSelected(new Set())} />
{folders.length > 0 && <Action title="Nur einen Ordner auswählen" icon={Icon.Folder}
shortcut={{ modifiers: ["cmd", "shift"], key: "o" }} onAction={openFolderPicker} />}
</ActionPanel.Section>;
const sourceLabel = props.historical ? "Gespeicherter Projektstand" const sourceLabel = props.historical ? "Gespeicherter Projektstand"
: props.run.inPlace || props.run.sourceInPlace ? "Aktueller Projektordner" : "Pseudonymisierte Kopien"; : props.run.inPlace || props.run.sourceInPlace ? "Aktueller Projektordner" : "Pseudonymisierte Kopien";
const renderFile = (file: ProjectFile) => { const renderFile = (file: ProjectFile) => {
const fileFolder = folders.find((folder) => folder.path === dirname(file.relPath));
const displayMapping: PlaceholderMapping = { ...baseMapping }; const displayMapping: PlaceholderMapping = { ...baseMapping };
for (const [id, entry] of Object.entries(decisions[file.relPath] ?? {})) if (entry) displayMapping[id] = entry; for (const [id, entry] of Object.entries(decisions[file.relPath] ?? {})) if (entry) displayMapping[id] = entry;
return <List.Item key={file.relPath} return <List.Item key={file.relPath}
@@ -325,9 +363,10 @@ function FilePicker(props: { run: ProjectRun; directory: string; historical?: bo
actions={<ActionPanel> actions={<ActionPanel>
<Action title={selected.has(file.relPath) ? "Datei abwählen" : "Datei auswählen"} onAction={() => toggle(file.relPath)} /> <Action title={selected.has(file.relPath) ? "Datei abwählen" : "Datei auswählen"} onAction={() => toggle(file.relPath)} />
<Action title="Auswahl nach Downloads wiederherstellen" icon={Icon.Download} onAction={exportFiles} /> <Action title="Auswahl nach Downloads wiederherstellen" icon={Icon.Download} onAction={exportFiles} />
<Action title="Nur Textdateien auswählen" onAction={() => setSelected(new Set(textFiles.map((item) => item.relPath)))} /> {fileFolder && <Action title={fileFolder.textCount > 0
<Action title="Alle Dateien auswählen (inklusive Office)" onAction={() => setSelected(new Set(files.map((item) => item.relPath)))} /> ? "Nur Textdateien dieses Ordners auswählen" : "Diesen Ordner inklusive Office auswählen"}
<Action title="Keine auswählen" onAction={() => setSelected(new Set())} /> icon={Icon.Folder} onAction={() => selectOnlyFolder(fileFolder.path, fileFolder.textCount === 0)} />}
{selectionActions()}
</ActionPanel>} />; </ActionPanel>} />;
}; };
return <List isLoading={busy || !sourceRoot} navigationTitle={`${props.run.projectName}: Dateien für Klartext wählen`} return <List isLoading={busy || !sourceRoot} navigationTitle={`${props.run.projectName}: Dateien für Klartext wählen`}
@@ -336,9 +375,10 @@ function FilePicker(props: { run: ProjectRun; directory: string; historical?: bo
<List.Item key="export" icon={Icon.Download} title="Ausgewählte Dateien wiederherstellen" <List.Item key="export" icon={Icon.Download} title="Ausgewählte Dateien wiederherstellen"
subtitle="Klartext-Kopien in Downloads/Velum erstellen · Office-Dateien nur nach Auswahl" subtitle="Klartext-Kopien in Downloads/Velum erstellen · Office-Dateien nur nach Auswahl"
actions={<ActionPanel><Action title="Auswahl nach Downloads wiederherstellen" actions={<ActionPanel><Action title="Auswahl nach Downloads wiederherstellen"
icon={Icon.Download} onAction={exportFiles} /></ActionPanel>} /> icon={Icon.Download} onAction={exportFiles} />{selectionActions()}</ActionPanel>} />
<List.Item key="source" icon={Icon.Folder} title={sourceLabel} subtitle={sourceRoot} <List.Item key="source" icon={Icon.Folder} title={sourceLabel} subtitle={sourceRoot}
actions={<ActionPanel><Action title="Quellordner im Finder anzeigen" onAction={() => showInFinder(sourceRoot)} /></ActionPanel>} /> actions={<ActionPanel><Action title="Quellordner im Finder anzeigen" onAction={() => showInFinder(sourceRoot)} />
{selectionActions()}</ActionPanel>} />
</List.Section> </List.Section>
{unresolved.some((item) => selected.has(item.file)) && <List.Section title="Platzhalter für diesen Export klären"> {unresolved.some((item) => selected.has(item.file)) && <List.Section title="Platzhalter für diesen Export klären">
{unresolved.filter((item) => selected.has(item.file)).map(({ file, id }) => { {unresolved.filter((item) => selected.has(item.file)).map(({ file, id }) => {
@@ -349,7 +389,7 @@ function FilePicker(props: { run: ProjectRun; directory: string; historical?: bo
: candidates.length ? "Gespeicherter Eintrag – bitte prüfen" : "Kein gespeicherter Eintrag" }]} : candidates.length ? "Gespeicherter Eintrag – bitte prüfen" : "Kein gespeicherter Eintrag" }]}
actions={<ActionPanel><Action title="Zuordnung festlegen" icon={Icon.Pencil} actions={<ActionPanel><Action title="Zuordnung festlegen" icon={Icon.Pencil}
onAction={() => push(<RestoreDecisionForm file={file} id={id} candidates={candidates} onAction={() => push(<RestoreDecisionForm file={file} id={id} candidates={candidates}
onSave={(decision) => saveDecision(file, id, decision)} />)} /></ActionPanel>} />; onSave={(decision) => saveDecision(file, id, decision)} />)} />{selectionActions()}</ActionPanel>} />;
})} })}
</List.Section>} </List.Section>}
<List.Section title={`Textdateien · ${textFiles.length} · standardmäßig ausgewählt`}> <List.Section title={`Textdateien · ${textFiles.length} · standardmäßig ausgewählt`}>

View File

@@ -0,0 +1,29 @@
import { describe, expect, it } from "bun:test";
import { listRestorableFolders, selectRestorableFolder } from "../src/lib/restore-file-selection";
import type { ProjectFile } from "../src/lib/velum-project";
const files: ProjectFile[] = [
{ relPath: "A/Notiz.txt", absPath: "", kind: "text", size: 0 },
{ relPath: "A/Unterordner/Tabelle.xlsx", absPath: "", kind: "document", size: 0 },
{ relPath: "AB/Nachbar.txt", absPath: "", kind: "text", size: 0 },
{ relPath: "Wurzel.txt", absPath: "", kind: "text", size: 0 },
];
describe("Ordnerauswahl beim Klartext-Export", () => {
it("zeigt übergeordnete Ordner mit der Anzahl ihrer Unterordner-Dateien", () => {
expect(listRestorableFolders(files)).toEqual([
{ path: "A", fileCount: 2, textCount: 1, documentCount: 1 },
{ path: "A/Unterordner", fileCount: 1, textCount: 0, documentCount: 1 },
{ path: "AB", fileCount: 1, textCount: 1, documentCount: 0 },
]);
});
it("wählt nur den gewählten Ordner rekursiv und keine ähnlich benannten Nachbarn", () => {
expect([...selectRestorableFolder(files, "A")]).toEqual([
"A/Notiz.txt",
]);
expect([...selectRestorableFolder(files, "A", true)]).toEqual([
"A/Notiz.txt", "A/Unterordner/Tabelle.xlsx",
]);
});
});