anki/ts/import-csv/ImportCsvPage.svelte
Abdo 98715e593a
Improve presentation of importing results (#2568)
* Implement import log screen in Svelte

* Show filename in import log screen title

* Remove unused NoteRow property

* Show number of imported notes

* Use a single nid expression

* Use 'count' as variable name for consistency

* Import from @tslib/backend instead

* Fix summary_template typing

* Fix clippy warning

* Apply suggestions from code review

* Fix imports

* Contents -> Fields

* Increase max length of browser search bar

https://github.com/ankitects/anki/pull/2568/files#r1255227035

* Fix race condition in Bootstrap tooltip destruction

https://github.com/twbs/bootstrap/issues/37474

* summary_template -> summaryTemplate

* Make show link a button

* Run import ops on Svelte side

* Fix geometry not being restored in CSV Import page

* Make VirtualTable fill available height

* Keep CSV dialog modal

* Reword importing-existing-notes-skipped

* Avoid mentioning matching based on first field

* Change tick and cross icons

* List skipped notes last

* Pure CSS spinner

* Move set_wants_abort() call to relevant dialogs

* Show number of imported cards

* Remove bold from first sentence and indent summaries

* Update UI after import operations

* Add close button to import log page

Also make virtual table react to resize event.

* Fix typing

* Make CSV dialog non-modal again

Otherwise user can't interact with browser window.

* Update window modality after import

* Commit DB and update undo actions after import op

* Split frontend proto into separate file, so backend can ignore it

Currently the automatically-generated frontend RPC methods get placed in
'backend.js' with all the backend methods; we could optionally split them
into a separate 'frontend.js' file in the future.

* Migrate import_done from a bridgecmd to a HTTP request

* Update plural form of importing-notes-added

* Move import response handling to mediasrv

* Move task callback to script section

* Avoid unnecessary :global()

* .log cannot be missing if result exists

* Move import log search handling to mediasrv

* Type common params of ImportLogDialog

* Use else if

* Remove console.log()

* Add way to test apkg imports in new log screen

* Remove unused import

* Get actual card count for CSV imports

* Use import type

* Fix typing error

* Ignore import log when checking for changes in Python layer

* Apply suggestions from code review

* Remove imported card count for now

* Avoid non-null assertion in assignment

* Change showInBrowser to take an array of notes

* Use dataclasses for import log args

* Simplify ResultWithChanges in TS

* Only abort import when window is modal

* Fix ResultWithChanges typing

* Fix Rust warnings

* Only log one duplicate per incoming note

* Update wording about note updates

* Remove caveat about found_notes

* Reduce font size

* Remove redundant map

* Give credit to loading.io

* Remove unused line

---------

Co-authored-by: RumovZ <gp5glkw78@relay.firefox.com>
2023-08-02 20:29:44 +10:00

195 lines
6.9 KiB
Svelte

<!--
Copyright: Ankitects Pty Ltd and contributors
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
-->
<script lang="ts">
import type { DeckNameId } from "@tslib/anki/decks_pb";
import type { StringList } from "@tslib/anki/generic_pb";
import type {
CsvMetadata_Delimiter,
CsvMetadata_DupeResolution,
CsvMetadata_MappedNotetype,
CsvMetadata_MatchScope,
ImportResponse,
} from "@tslib/anki/import_export_pb";
import type { NotetypeNameId } from "@tslib/anki/notetypes_pb";
import { getCsvMetadata, importCsv, importDone } from "@tslib/backend";
import * as tr from "@tslib/ftl";
import BackendProgressIndicator from "../components/BackendProgressIndicator.svelte";
import Col from "../components/Col.svelte";
import Container from "../components/Container.svelte";
import Row from "../components/Row.svelte";
import Spacer from "../components/Spacer.svelte";
import ImportLogPage from "../import-log/ImportLogPage.svelte";
import DeckDupeCheckSwitch from "./DeckDupeCheckSwitch.svelte";
import DeckSelector from "./DeckSelector.svelte";
import DelimiterSelector from "./DelimiterSelector.svelte";
import DupeResolutionSelector from "./DupeResolutionSelector.svelte";
import FieldMapper from "./FieldMapper.svelte";
import Header from "./Header.svelte";
import HtmlSwitch from "./HtmlSwitch.svelte";
import {
buildDeckOneof,
buildNotetypeOneof,
getColumnOptions,
tryGetDeckId,
tryGetGlobalNotetype,
} from "./lib";
import NotetypeSelector from "./NotetypeSelector.svelte";
import Preview from "./Preview.svelte";
import StickyHeader from "./StickyHeader.svelte";
import Tags from "./Tags.svelte";
export let path: string;
export let notetypeNameIds: NotetypeNameId[];
export let deckNameIds: DeckNameId[];
export let dupeResolution: CsvMetadata_DupeResolution;
export let matchScope: CsvMetadata_MatchScope;
export let delimiter: CsvMetadata_Delimiter;
export let forceDelimiter: boolean;
export let forceIsHtml: boolean;
export let isHtml: boolean;
export let globalTags: string[];
export let updatedTags: string[];
export let columnLabels: string[];
export let tagsColumn: number;
export let guidColumn: number;
export let preview: StringList[];
// Protobuf oneofs. Exactly one of these pairs is expected to be set.
export let notetypeColumn: number | null;
export let globalNotetype: CsvMetadata_MappedNotetype | null;
export let deckId: bigint | null;
export let deckColumn: number | null;
let importResponse: ImportResponse | undefined = undefined;
let lastNotetypeId = globalNotetype?.id;
let lastDelimeter = delimiter;
let importing = false;
$: columnOptions = getColumnOptions(
columnLabels,
preview[0].vals,
notetypeColumn,
deckColumn,
guidColumn,
);
$: getCsvMetadata({
path,
delimiter,
notetypeId: undefined,
deckId: undefined,
isHtml,
}).then((meta) => {
columnLabels = meta.columnLabels;
preview = meta.preview;
});
$: if (globalNotetype?.id !== lastNotetypeId || delimiter !== lastDelimeter) {
lastNotetypeId = globalNotetype?.id;
lastDelimeter = delimiter;
getCsvMetadata({
path,
delimiter,
notetypeId: globalNotetype?.id,
deckId: deckId ?? undefined,
}).then((meta) => {
globalNotetype = tryGetGlobalNotetype(meta);
deckId = tryGetDeckId(meta);
tagsColumn = meta.tagsColumn;
});
}
async function onImport(): Promise<ImportResponse> {
const result = await importCsv({
path,
metadata: {
dupeResolution,
matchScope,
delimiter,
forceDelimiter,
isHtml,
forceIsHtml,
globalTags,
updatedTags,
columnLabels,
tagsColumn,
guidColumn,
deck: buildDeckOneof(deckColumn, deckId),
notetype: buildNotetypeOneof(globalNotetype, notetypeColumn),
preview: [],
},
});
await importDone({});
importing = false;
return result;
}
</script>
<div class="outer">
{#if importing}
<BackendProgressIndicator task={onImport} bind:result={importResponse} />
{:else if importResponse}
<ImportLogPage response={importResponse} params={{ path }} />
{:else}
<StickyHeader {path} onImport={() => (importing = true)} />
<Container class="csv-page">
<Row --cols={2}>
<Col --col-size={1} breakpoint="md">
<Container>
<Header heading={tr.importingFile()} />
<Spacer --height="1.5rem" />
<DelimiterSelector bind:delimiter disabled={forceDelimiter} />
<HtmlSwitch bind:isHtml disabled={forceIsHtml} />
<Preview {columnOptions} {preview} />
</Container>
</Col>
<Col --col-size={1} breakpoint="md">
<Container>
<Header heading={tr.importingImportOptions()} />
<Spacer --height="1.5rem" />
{#if globalNotetype}
<NotetypeSelector
{notetypeNameIds}
bind:notetypeId={globalNotetype.id}
/>
{/if}
{#if deckId}
<DeckSelector {deckNameIds} bind:deckId />
{/if}
<DupeResolutionSelector bind:dupeResolution />
<DeckDupeCheckSwitch bind:matchScope />
<Tags bind:globalTags bind:updatedTags />
</Container>
</Col>
<Col --col-size={1} breakpoint="md">
<Container>
<Header heading={tr.importingFieldMapping()} />
<Spacer --height="1.5rem" />
<FieldMapper
{columnOptions}
bind:globalNotetype
bind:tagsColumn
/>
</Container>
</Col>
</Row>
</Container>
{/if}
</div>
<style lang="scss">
.outer {
margin: 0 auto;
}
:global(.csv-page) {
--gutter-inline: 0.25rem;
:global(.row) {
// rows have negative margins by default
--bs-gutter-x: 0;
margin-bottom: 0.5rem;
}
}
</style>