Debian Codegraph @

frontend/src/reports/import/Extract.svelte

raw
1 <script lang="ts">
2 import type { Entry as EntryType } from "../../entries";
3 import Entry from "../../entry-forms/Entry.svelte";
4 import { _ } from "../../i18n";
5 import ModalBase from "../../modals/ModalBase.svelte";
6
7 interface Props {
8 entries: EntryType[];
9 save: () => void;
10 close: () => void;
11 }
12
13 let { entries = $bindable(), save, close }: Props = $props();
14
15 let currentIndex = $state.raw(0);
16 let count = $derived(entries.length);
17 let shown = $derived(count > 0);
18
19 let entry = $derived(entries[currentIndex]);
20 let duplicate = $derived(entry?.is_duplicate());
21 let count_duplicates = $derived(
22 entries.filter((e) => e.is_duplicate()).length,
23 );
24 $effect(() => {
25 if (count > 0 && currentIndex >= count) {
26 currentIndex = 0;
27 }
28 });
29
30 function submitOrNext(event: SubmitEvent) {
31 event.preventDefault();
32 if (currentIndex < count - 1) {
33 currentIndex += 1;
34 } else {
35 save();
36 }
37 }
38
39 function previousEntry() {
40 currentIndex = Math.max(currentIndex - 1, 0);
41 }
42
43 function toggleDuplicate() {
44 if (entry) {
45 entries[currentIndex] = entry.set_meta(
46 "__duplicate__",
47 !entry.is_duplicate(),
48 );
49 }
50 }
51
52 let current_index_from_one = $derived(currentIndex + 1);
53 let count_without_duplicates = $derived(count - count_duplicates);
54 </script>
55
56 <ModalBase {shown} closeHandler={close}>
57 <form novalidate={duplicate} onsubmit={submitOrNext}>
58 <h3>{_("Import")}</h3>
59 {#if entry}
60 <div class="flex-row">
61 <h3>
62 Entry
63 {current_index_from_one}
64 of
65 {count}
66 ({count_without_duplicates}
67 to import):
68 </h3>
69 <span class="spacer"></span>
70 <label class="button muted">
71 <input
72 type="checkbox"
73 checked={duplicate}
74 onclick={toggleDuplicate}
75 />
76 {_("ignore duplicate")}
77 </label>
78 </div>
79 <div class:duplicate>
80 <Entry
81 bind:entry={
82 () => entry,
83 (entry: EntryType) => {
84 entries[currentIndex] = entry;
85 }
86 }
87 />
88 </div>
89 <div class="flex-row">
90 {#if currentIndex > 0}
91 <button
92 type="button"
93 class="muted"
94 onclick={() => {
95 currentIndex = 0;
96 }}
97 >
98 ⏮
99 </button>
100 <button type="button" class="muted" onclick={previousEntry}>
101 {_("Previous")}
102 </button>
103 {/if}
104 <span class="spacer"></span>
105 {#if currentIndex < entries.length - 1}
106 <button type="submit">{_("Next")}</button>
107 <button
108 type="button"
109 class="muted"
110 onclick={() => {
111 currentIndex = entries.length - 1;
112 }}
113 >
114 ⏭
115 </button>
116 {:else}<button type="submit">{_("Save")}</button>{/if}
117 </div>
118 <hr />
119 {#if entry.meta.get("__source__")}
120 <h3>
121 {_("Source")}
122 {#if entry.meta.lineno}({_("Line")}: {entry.meta.lineno}){/if}
123 </h3>
124 <pre>{entry.meta.get("__source__")}</pre>
125 {/if}
126 {/if}
127 </form>
128 </ModalBase>
129
130 <style>
131 pre {
132 font-size: 0.9em;
133 white-space: pre-wrap;
134 }
135
136 .duplicate {
137 opacity: 0.5;
138 }
139 </style>