aboutsummaryrefslogtreecommitdiffstats
path: root/packages/shared/import-export/importer.ts
diff options
context:
space:
mode:
authorMohamed Bassem <me@mbassem.com>2025-10-04 13:40:24 +0100
committerGitHub <noreply@github.com>2025-10-04 13:40:24 +0100
commit4a580d713621f99abb8baabc9b847ce039d44842 (patch)
treea2aa6f3ae8045ad50a9316624e2a7028dd098c6b /packages/shared/import-export/importer.ts
parent5e331a7d5b8d9666812170547574804d8b6da741 (diff)
downloadkarakeep-4a580d713621f99abb8baabc9b847ce039d44842.tar.zst
feat: Revamp import experience (#2001)
* WIP: import v2 * remove new session button * don't redirect after import * store and lint to root list * models + tests * redesign the progress * simplify the import session for ow * drop status from session schema * split the import session page * i18n * fix test * remove pagination * fix some colors in darkmode * one last fix * add privacy filter * privacy check * fix interactivity of import progress * fix test
Diffstat (limited to 'packages/shared/import-export/importer.ts')
-rw-r--r--packages/shared/import-export/importer.ts50
1 files changed, 32 insertions, 18 deletions
diff --git a/packages/shared/import-export/importer.ts b/packages/shared/import-export/importer.ts
index 88c0c3bc..b32c49c1 100644
--- a/packages/shared/import-export/importer.ts
+++ b/packages/shared/import-export/importer.ts
@@ -17,6 +17,7 @@ export interface ImportDeps {
}) => Promise<{ id: string }>;
createBookmark: (
bookmark: ParsedBookmark,
+ sessionId: string,
) => Promise<{ id: string; alreadyExists?: boolean }>;
addBookmarkToLists: (input: {
bookmarkId: string;
@@ -26,6 +27,10 @@ export interface ImportDeps {
bookmarkId: string;
tags: string[];
}) => Promise<void>;
+ createImportSession: (input: {
+ name: string;
+ rootListId: string;
+ }) => Promise<{ id: string }>;
}
export interface ImportOptions {
@@ -38,6 +43,7 @@ export interface ImportOptions {
export interface ImportResult {
counts: ImportCounts;
rootListId: string | null;
+ importSessionId: string | null;
}
export async function importBookmarksFromFile(
@@ -66,10 +72,15 @@ export async function importBookmarksFromFile(
return {
counts: { successes: 0, failures: 0, alreadyExisted: 0, total: 0 },
rootListId: null,
+ importSessionId: null,
};
}
const rootList = await deps.createList({ name: rootListName, icon: "⬆️" });
+ const session = await deps.createImportSession({
+ name: `${source.charAt(0).toUpperCase() + source.slice(1)} Import - ${new Date().toLocaleDateString()}`,
+ rootListId: rootList.id,
+ });
onProgress?.(0, parsedBookmarks.length);
@@ -109,22 +120,28 @@ export async function importBookmarksFromFile(
pathMap[pathKey] = folderList.id;
}
+ let done = 0;
const importPromises = parsedBookmarks.map((bookmark) => async () => {
- const listIds = bookmark.paths.map(
- (path) => pathMap[path.join(PATH_DELIMITER)] || rootList.id,
- );
- if (listIds.length === 0) listIds.push(rootList.id);
-
- const created = await deps.createBookmark(bookmark);
- await deps.addBookmarkToLists({ bookmarkId: created.id, listIds });
- if (bookmark.tags && bookmark.tags.length > 0) {
- await deps.updateBookmarkTags({
- bookmarkId: created.id,
- tags: bookmark.tags,
- });
- }
+ try {
+ const listIds = bookmark.paths.map(
+ (path) => pathMap[path.join(PATH_DELIMITER)] || rootList.id,
+ );
+ if (listIds.length === 0) listIds.push(rootList.id);
+
+ const created = await deps.createBookmark(bookmark, session.id);
+ await deps.addBookmarkToLists({ bookmarkId: created.id, listIds });
+ if (bookmark.tags && bookmark.tags.length > 0) {
+ await deps.updateBookmarkTags({
+ bookmarkId: created.id,
+ tags: bookmark.tags,
+ });
+ }
- return created;
+ return created;
+ } finally {
+ done += 1;
+ onProgress?.(done, parsedBookmarks.length);
+ }
});
const resultsPromises = limitConcurrency(importPromises, concurrencyLimit);
@@ -134,7 +151,6 @@ export async function importBookmarksFromFile(
let failures = 0;
let alreadyExisted = 0;
- let done = 0;
for (const r of results) {
if (r.status === "fulfilled") {
if (r.value.alreadyExists) alreadyExisted++;
@@ -142,10 +158,7 @@ export async function importBookmarksFromFile(
} else {
failures++;
}
- done += 1;
- onProgress?.(done, parsedBookmarks.length);
}
-
return {
counts: {
successes,
@@ -154,5 +167,6 @@ export async function importBookmarksFromFile(
total: parsedBookmarks.length,
},
rootListId: rootList.id,
+ importSessionId: session.id,
};
}