Debian Codegraph @

frontend/src/reports/documents/Table.svelte

raw
1 <script lang="ts">
2 import type { Document } from "../../entries";
3 import { _ } from "../../i18n";
4 import { isDescendant } from "../../lib/account";
5 import { basename } from "../../lib/paths";
6 import { DateColumn, Sorter, StringColumn } from "../../sort";
7 import SortHeader from "../../sort/SortHeader.svelte";
8 import { selectedAccount } from "./stores";
9
10 interface Props {
11 data: Document[];
12 selected?: Document | null;
13 }
14
15 let { data, selected = $bindable(null) }: Props = $props();
16
17 /**
18 * Extract just the latter part of the filename if it starts with a date.
19 */
20 function name(doc: Document) {
21 const base = basename(doc.filename);
22 return base.startsWith(doc.date) ? base.substring(11) : base;
23 }
24
25 const columns = [
26 new DateColumn<Document>(_("Date")),
27 new StringColumn<Document>(_("Name"), (d) => name(d)),
28 ] as const;
29 let sorter = $state(new Sorter(columns[0], "desc"));
30
31 let filtered_documents = $derived(
32 data.filter((doc) => isDescendant(doc.account, $selectedAccount)),
33 );
34 let sorted_documents = $derived(sorter.sort(filtered_documents));
35 </script>
36
37 <table>
38 <thead>
39 <tr>
40 {#each columns as column}
41 <SortHeader bind:sorter {column} />
42 {/each}
43 </tr>
44 </thead>
45 <tbody>
46 {#each sorted_documents as doc}
47 <tr
48 class:selected={selected === doc}
49 draggable={true}
50 title={doc.filename}
51 ondragstart={(ev) => {
52 ev.dataTransfer?.setData("fava/filename", doc.filename);
53 }}
54 onclick={() => {
55 selected = doc;
56 }}
57 >
58 <td>{doc.date}</td>
59 <td>{name(doc)}</td>
60 </tr>
61 {/each}
62 </tbody>
63 </table>
64
65 <style>
66 table {
67 width: 100%;
68 }
69
70 tr {
71 cursor: pointer;
72 }
73
74 .selected,
75 tr:hover {
76 background-color: var(--table-header-background);
77 }
78 </style>