Debian Codegraph @

frontend/src/reports/errors/Errors.svelte

raw
1 <script lang="ts">
2 import type { BeancountError } from "../../api/validators";
3 import { urlForAccount, urlForSource } from "../../helpers";
4 import { _, format } from "../../i18n";
5 import { NumberColumn, Sorter, StringColumn } from "../../sort";
6 import SortHeader from "../../sort/SortHeader.svelte";
7 import { accounts, errors } from "../../stores";
8
9 let account_re = $derived(new RegExp(`(${$accounts.join("|")})`));
10
11 /** Split and extract account names to replace them with links to the account page. */
12 function extract_accounts(msg: string): ["text" | "account", string][] {
13 return msg
14 .split(account_re)
15 .map((text, index) =>
16 index % 2 === 0 ? ["text", text] : ["account", text],
17 );
18 }
19
20 type T = BeancountError;
21 const columns = [
22 new StringColumn<T>(_("File"), (d) => d.source?.filename ?? ""),
23 new NumberColumn<T>(_("Line"), (d) => d.source?.lineno ?? 0),
24 new StringColumn<T>(_("Error"), (d) => d.message),
25 ] as const;
26 let sorter = $state(new Sorter(columns[0], "desc"));
27
28 let sorted_errors = $derived(sorter.sort($errors));
29 </script>
30
31 {#if $errors.length}
32 <table>
33 <thead>
34 <tr>
35 {#each columns as column}
36 <SortHeader bind:sorter {column} />
37 {/each}
38 </tr>
39 </thead>
40 <tbody>
41 {#each sorted_errors as { message, source }}
42 <tr>
43 {#if source}
44 {@const url = $urlForSource(
45 source.filename,
46 source.lineno.toString(),
47 )}
48 {@const title = format(_("Show source %(file)s:%(lineno)s"), {
49 file: source.filename,
50 lineno: source.lineno.toString(),
51 })}
52 <td>{source.filename}</td>
53 <td class="num">
54 <a class="source" href={url} {title}>{source.lineno}</a>
55 </td>
56 {:else}
57 <td></td>
58 <td class="num"></td>
59 {/if}
60 <td class="pre">
61 {#each extract_accounts(message) as [type, text]}
62 {#if type === "text"}
63 {text}
64 {:else}
65 <a href={$urlForAccount(text)}>{text}</a>
66 {/if}
67 {/each}
68 </td>
69 </tr>
70 {/each}
71 </tbody>
72 </table>
73 {:else}
74 <p>
75 {_("No errors.")}
76 </p>
77 {/if}