anki/ts/import-page/lib.ts
RumovZ 850043b49b
Tooltips for CSV import and import page refactoring (#2655)
* Make enum selector generic

* Refactor ImportCsvPage to support tooltips

* Improve csv import defaults

* Unify import pages

* Improve import page styling

* Fix life cycle issue with import properties

* Remove size constraints to fix scrollbar styling

* Add help strings and urls to csv import page

* Show ErrorPage on ImportPage error

* Fix escaping of import path

* Unify ImportPage and ImportLogPage

* Apply suggestions from code review (dae)

* Fix import progress

* Fix preview overflowing container

* Don't include <br> in FileIoErrors (dae)

e.g. 500: Failed to read '/home/dae/foo2.csv':<br>stream did not contain valid UTF-8

I thought about using {@html ...} here, but that's a potential security issue,
as the filename is not something we control.
2023-09-14 09:06:15 +10:00

130 lines
4.1 KiB
TypeScript

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import type { ImportResponse_Log, ImportResponse_Note } from "@tslib/anki/import_export_pb";
import { CsvMetadata_DupeResolution } from "@tslib/anki/import_export_pb";
import { searchInBrowser } from "@tslib/backend";
import * as tr from "@tslib/ftl";
import { checkCircle, closeBox, newBox, updateIcon } from "./icons";
import type { LogQueue, NoteRow, SummarizedLogQueues } from "./types";
function getFirstFieldQueue(log: ImportResponse_Log): {
action: string;
queue: LogQueue;
} {
let reason: string;
let action: string;
if (log.dupeResolution === CsvMetadata_DupeResolution.DUPLICATE) {
reason = tr.importingDuplicateNoteAdded();
action = tr.addingAdded();
} else if (log.dupeResolution === CsvMetadata_DupeResolution.PRESERVE) {
reason = tr.importingExistingNoteSkipped();
action = tr.importingSkipped();
} else {
reason = tr.importingNoteUpdatedAsFileHadNewer();
action = tr.importingUpdated();
}
const queue: LogQueue = {
reason,
notes: log.firstFieldMatch,
};
return { action, queue };
}
export function getSummaries(log: ImportResponse_Log): SummarizedLogQueues[] {
const summarizedQueues = [
{
queues: [
{
notes: log.new,
reason: tr.importingAddedNewNote(),
},
],
action: tr.addingAdded(),
summaryTemplate: tr.importingNotesAdded,
canBrowse: true,
icon: newBox,
},
{
queues: [
{
notes: log.duplicate,
reason: tr.importingExistingNoteSkipped(),
},
],
action: tr.importingSkipped(),
summaryTemplate: tr.importingExistingNotesSkipped,
canBrowse: true,
icon: checkCircle,
},
{
queues: [
{
notes: log.updated,
reason: tr.importingNoteUpdatedAsFileHadNewer(),
},
],
action: tr.importingUpdated(),
summaryTemplate: tr.importingNotesUpdated,
canBrowse: true,
icon: updateIcon,
},
{
queues: [
{
notes: log.conflicting,
reason: tr.importingNoteSkippedUpdateDueToNotetype(),
},
{
notes: log.missingNotetype,
reason: tr.importingNoteSkippedDueToMissingNotetype(),
},
{
notes: log.missingDeck,
reason: tr.importingNoteSkippedDueToMissingDeck(),
},
{
notes: log.emptyFirstField,
reason: tr.importingNoteSkippedDueToEmptyFirstField(),
},
],
action: tr.importingSkipped(),
summaryTemplate: tr.importingConflictingNotesSkipped2,
canBrowse: false,
icon: closeBox,
},
];
const firstFieldQueue = getFirstFieldQueue(log);
for (const summary of summarizedQueues) {
if (summary.action === firstFieldQueue.action) {
summary.queues.push(firstFieldQueue.queue);
break;
}
}
return summarizedQueues;
}
export function getRows(summaries: SummarizedLogQueues[]): NoteRow[] {
const rows: NoteRow[] = [];
for (const summary of summaries) {
for (const queue of summary.queues) {
if (queue.notes) {
for (const note of queue.notes) {
rows.push({ summary, queue, note });
}
}
}
}
return rows;
}
export function showInBrowser(notes: ImportResponse_Note[]): void {
searchInBrowser({
filter: {
case: "nids",
value: { ids: notes.map((note) => note.id!.nid) },
},
});
}