Debian Codegraph @

frontend/src/reports/query/QueryTable.svelte

raw
1 <!--
2 @component
3 A query result table.
4 -->
5 <script lang="ts">
6 import { Amount, Position } from "../../entries";
7 import { day } from "../../format";
8 import { urlForAccount } from "../../helpers";
9 import { is_empty } from "../../lib/objects";
10 import AccountIndicator from "../../sidebar/AccountIndicator.svelte";
11 import { Sorter, UnsortedColumn } from "../../sort";
12 import SortHeader from "../../sort/SortHeader.svelte";
13 import { accounts_set } from "../../stores";
14 import { ctx, num } from "../../stores/format";
15 import type { QueryCell, QueryResultTable } from "./query_table";
16 import { Inventory } from "./query_table";
17
18 /** The table to render. */
19 export let table: QueryResultTable;
20 /** A column name to filter by if empty (expected to be an Inventory column). */
21 export let filter_empty: string | undefined = undefined;
22
23 $: filter_empty_column_number = table.columns.findIndex(
24 (column) => column.name === filter_empty,
25 );
26
27 $: filtered_rows =
28 filter_empty_column_number > -1
29 ? table.rows.filter((row) => {
30 const cell = row[filter_empty_column_number];
31 return !(cell instanceof Inventory && is_empty(cell.value));
32 })
33 : table.rows;
34
35 let sorter = new Sorter<QueryCell[]>(new UnsortedColumn("<Dummy>"), "asc");
36 $: sorted_rows = sorter.sort(filtered_rows);
37 </script>
38
39 <table>
40 <thead>
41 <tr>
42 {#each table.columns as column}
43 <SortHeader bind:sorter {column} />
44 {/each}
45 </tr>
46 </thead>
47 <tbody>
48 {#each sorted_rows as row}
49 <tr>
50 {#each row as value, index}
51 {#if value === null}
52 <td>&nbsp;</td>
53 {:else if typeof value === "boolean"}
54 <td>
55 {value.toString().toUpperCase()}
56 </td>
57 {:else if typeof value === "number"}
58 <td class="num">
59 {table.columns[index]?.dtype === "int"
60 ? value.toString()
61 : $num(value)}
62 </td>
63 {:else if typeof value === "string"}
64 <td>
65 {#if $accounts_set.has(value)}
66 <a href={$urlForAccount(value)}>{value}</a>
67 <AccountIndicator account={value} small />
68 {:else if value.length === 32 && /[a-z0-9]/.test(value)}
69 <a href={`#context-${value}`}>{value}</a>
70 {:else}
71 {value}
72 {/if}
73 </td>
74 {:else if Array.isArray(value)}
75 <td>
76 {value.join(",")}
77 </td>
78 {:else if value instanceof Date}
79 <td>
80 {day(value)}
81 </td>
82 {:else if value instanceof Amount}
83 <td class="num">
84 {value.str($ctx)}
85 </td>
86 {:else if value instanceof Position}
87 <td class="num">
88 {value.units.str($ctx)}
89 {#if value.cost}
90 &lbrace;{value.cost.str($ctx)}&rbrace;{/if}
91 </td>
92 {:else if value instanceof Inventory}
93 <td class="num">
94 {#each Object.entries(value.value) as [currency, number]}
95 {$ctx.amount(number, currency)}
96 <br />
97 {/each}
98 </td>
99 {/if}
100 {/each}
101 </tr>
102 {/each}
103 </tbody>
104 </table>