| 1 |
<script lang="ts"> |
| 2 |
import { onMount } from "svelte"; |
| 3 |
import { SvelteMap } from "svelte/reactivity"; |
| 4 |
|
| 5 |
import { deleteDocument, get, moveDocument, saveEntries } from "../../api"; |
| 6 |
import type { Entry } from "../../entries"; |
| 7 |
import { urlFor } from "../../helpers"; |
| 8 |
import { _ } from "../../i18n"; |
| 9 |
import { notify, notify_err } from "../../notifications"; |
| 10 |
import router from "../../router"; |
| 11 |
import { fava_options } from "../../stores"; |
| 12 |
import DocumentPreview from "../documents/DocumentPreview.svelte"; |
| 13 |
import type { ImportReportProps } from "."; |
| 14 |
import Extract from "./Extract.svelte"; |
| 15 |
import FileList from "./FileList.svelte"; |
| 16 |
import ImportFileUpload from "./ImportFileUpload.svelte"; |
| 17 |
|
| 18 |
let { files }: ImportReportProps = $props(); |
| 19 |
|
| 20 |
/** Whether the `<details>` for the "other files" is open. */ |
| 21 |
let show_other_files = $state.raw( |
| 22 |
// initially show the other files if no importable files are present |
| 23 |
files.every((file) => !file.identified_by_importers), |
| 24 |
); |
| 25 |
|
| 26 |
/** The array of entries to show the modal for. */ |
| 27 |
let entries: Entry[] = $state([]); |
| 28 |
|
| 29 |
/** Name of the currently selected file. */ |
| 30 |
let selected: string | null = $state.raw(null); |
| 31 |
|
| 32 |
/** The lists of entries for file and importer combos where extract was started and not completed. */ |
| 33 |
let extract_cache = new SvelteMap<string, Entry[]>(); |
| 34 |
/** The account names chosen for a file and importer. */ |
| 35 |
let file_accounts = new SvelteMap<string, string>(); |
| 36 |
/** The names chosen for a file and importer. */ |
| 37 |
let file_names = new SvelteMap<string, string>(); |
| 38 |
|
| 39 |
/** Importable files. */ |
| 40 |
let importable_files = $derived( |
| 41 |
files.filter((file) => file.identified_by_importers), |
| 42 |
); |
| 43 |
/** Files not identified by any importer. */ |
| 44 |
let other_files = $derived( |
| 45 |
files.filter((file) => !file.identified_by_importers), |
| 46 |
); |
| 47 |
|
| 48 |
const preventNavigation = () => |
| 49 |
extract_cache.size > 0 |
| 50 |
? "There are unfinished imports, are you sure you want to continue?" |
| 51 |
: null; |
| 52 |
|
| 53 |
onMount(() => router.addInteruptHandler(preventNavigation)); |
| 54 |
|
| 55 |
/** |
| 56 |
* Move the given file to the new file name (and remove from the list). |
| 57 |
*/ |
| 58 |
async function move(filename: string, account: string, newName: string) { |
| 59 |
const moved = await moveDocument(filename, account, newName); |
| 60 |
if (moved) { |
| 61 |
router.reload(); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Delete the given file and remove it from the displayed list. |
| 67 |
*/ |
| 68 |
async function remove(filename: string) { |
| 69 |
if (!window.confirm(_("Delete this file?"))) { |
| 70 |
return; |
| 71 |
} |
| 72 |
const removed = await deleteDocument(filename); |
| 73 |
if (removed) { |
| 74 |
if (selected === filename) { |
| 75 |
selected = null; |
| 76 |
} |
| 77 |
router.reload(); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Open the extract dialog for the given file/importer combination. |
| 83 |
*/ |
| 84 |
async function extract(filename: string, importer: string) { |
| 85 |
const file_importer_key = `${filename}:${importer}`; |
| 86 |
const cached = extract_cache.get(file_importer_key); |
| 87 |
if (cached) { |
| 88 |
entries = cached; |
| 89 |
return; |
| 90 |
} |
| 91 |
try { |
| 92 |
entries = await get("extract", { filename, importer }); |
| 93 |
if (entries.length) { |
| 94 |
extract_cache.set(file_importer_key, entries); |
| 95 |
} else { |
| 96 |
notify("No entries to import from this file.", "warning"); |
| 97 |
} |
| 98 |
} catch (error) { |
| 99 |
notify_err(error); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Save the current entries. |
| 105 |
*/ |
| 106 |
async function save() { |
| 107 |
const without_duplicates = entries.filter((e) => !e.is_duplicate()); |
| 108 |
const key = [...extract_cache].find(([, e]) => e === entries)?.[0]; |
| 109 |
if (key != null) { |
| 110 |
extract_cache.delete(key); |
| 111 |
} |
| 112 |
entries = []; |
| 113 |
await saveEntries(without_duplicates); |
| 114 |
} |
| 115 |
</script> |
| 116 |
|
| 117 |
{#if $fava_options.import_config == null} |
| 118 |
<p> |
| 119 |
No importers configured. See <a href={$urlFor("help/import")} |
| 120 |
>Help (Import)</a |
| 121 |
> for more information. |
| 122 |
</p> |
| 123 |
{:else} |
| 124 |
<Extract |
| 125 |
bind:entries |
| 126 |
close={() => { |
| 127 |
entries = []; |
| 128 |
}} |
| 129 |
{save} |
| 130 |
/> |
| 131 |
<div class="fixed-fullsize-container"> |
| 132 |
<div class="filelist"> |
| 133 |
{#if files.length === 0} |
| 134 |
<p>{_("No files were found for import.")}</p> |
| 135 |
{/if} |
| 136 |
{#if importable_files.length > 0} |
| 137 |
<div> |
| 138 |
<h2>{_("Importable Files")}</h2> |
| 139 |
<FileList |
| 140 |
files={importable_files} |
| 141 |
{extract_cache} |
| 142 |
{file_accounts} |
| 143 |
{file_names} |
| 144 |
bind:selected |
| 145 |
{move} |
| 146 |
{remove} |
| 147 |
{extract} |
| 148 |
/> |
| 149 |
</div> |
| 150 |
<hr /> |
| 151 |
{/if} |
| 152 |
{#if other_files.length > 0} |
| 153 |
<details bind:open={show_other_files}> |
| 154 |
<summary>{_("Non-importable Files")}</summary> |
| 155 |
<FileList |
| 156 |
files={other_files} |
| 157 |
{extract_cache} |
| 158 |
{file_accounts} |
| 159 |
{file_names} |
| 160 |
bind:selected |
| 161 |
{move} |
| 162 |
{remove} |
| 163 |
{extract} |
| 164 |
/> |
| 165 |
</details> |
| 166 |
{/if} |
| 167 |
<ImportFileUpload /> |
| 168 |
</div> |
| 169 |
{#if selected} |
| 170 |
<div> |
| 171 |
<DocumentPreview filename={selected} /> |
| 172 |
</div> |
| 173 |
{/if} |
| 174 |
</div> |
| 175 |
{/if} |
| 176 |
|
| 177 |
<style> |
| 178 |
.fixed-fullsize-container { |
| 179 |
display: flex; |
| 180 |
align-items: stretch; |
| 181 |
} |
| 182 |
|
| 183 |
.fixed-fullsize-container > * { |
| 184 |
flex: 1 1 40%; |
| 185 |
overflow: auto; |
| 186 |
} |
| 187 |
|
| 188 |
.filelist { |
| 189 |
padding: 1rem; |
| 190 |
} |
| 191 |
|
| 192 |
hr { |
| 193 |
margin: 1rem 0; |
| 194 |
} |
| 195 |
</style> |