Debian Codegraph @

frontend/src/reports/documents/Documents.svelte

raw
1 <script lang="ts">
2 import { group } from "d3-array";
3
4 import { moveDocument } from "../../api";
5 import type { Document } from "../../entries";
6 import AccountInput from "../../entry-forms/AccountInput.svelte";
7 import { _ } from "../../i18n";
8 import { basename } from "../../lib/paths";
9 import { stratify } from "../../lib/tree";
10 import ModalBase from "../../modals/ModalBase.svelte";
11 import router from "../../router";
12 import type { DocumentsReportProps } from ".";
13 import Accounts from "./Accounts.svelte";
14 import DocumentPreview from "./DocumentPreview.svelte";
15 import Table from "./Table.svelte";
16
17 let { documents }: DocumentsReportProps = $props();
18
19 interface MoveDetails {
20 account: string;
21 filename: string;
22 newName: string;
23 }
24
25 let grouped = $derived(group(documents, (d) => d.account));
26 let node = $derived(
27 stratify(
28 grouped.entries(),
29 ([s]) => s,
30 (name, d) => ({ name, count: d?.[1].length ?? 0 }),
31 ),
32 );
33
34 let selected: Document | null = $state(null);
35 let moving: MoveDetails | null = $state(null);
36
37 /**
38 * Rename the selected document with <F2>.
39 */
40 function keyup(ev: KeyboardEvent) {
41 if (ev.key === "F2" && selected && !moving) {
42 moving = {
43 account: selected.account,
44 filename: selected.filename,
45 newName: basename(selected.filename),
46 };
47 }
48 }
49
50 async function move(event: SubmitEvent) {
51 event.preventDefault();
52 if (moving) {
53 const moved = await moveDocument(
54 moving.filename,
55 moving.account,
56 moving.newName,
57 );
58 if (moved) {
59 moving = null;
60 router.reload();
61 }
62 }
63 }
64 </script>
65
66 <svelte:window onkeyup={keyup} />
67 {#if moving}
68 <ModalBase
69 shown={true}
70 closeHandler={() => {
71 moving = null;
72 }}
73 >
74 <form onsubmit={move}>
75 <h3>{_("Move or rename document")}</h3>
76 <p><code>{moving.filename}</code></p>
77 <p>
78 <AccountInput bind:value={moving.account} />
79 <input size={40} bind:value={moving.newName} />
80 <button type="submit">{_("Move")}</button>
81 </p>
82 </form>
83 </ModalBase>
84 {/if}
85 <div class="fixed-fullsize-container">
86 <Accounts
87 {node}
88 move={(arg: { account: string; filename: string }) => {
89 moving = { ...arg, newName: basename(arg.filename) };
90 }}
91 />
92 <div>
93 <Table bind:selected data={documents} />
94 </div>
95 {#if selected}
96 <DocumentPreview filename={selected.filename} />
97 {/if}
98 </div>
99
100 <style>
101 .fixed-fullsize-container {
102 display: grid;
103 grid-template-columns: 1fr 2fr 3fr;
104 }
105
106 .fixed-fullsize-container > :global(*) {
107 height: 100%;
108 overflow: auto;
109 resize: horizontal;
110 }
111
112 .fixed-fullsize-container > :global(* + *) {
113 border-left: thin solid var(--sidebar-border);
114 }
115 </style>