1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
"use client";
import { useCallback, useEffect, useState } from "react";
import { Button, buttonVariants } from "@/components/ui/button";
import FilePickerButton from "@/components/ui/file-picker-button";
import { Progress } from "@/components/ui/progress";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { useBookmarkImport } from "@/lib/hooks/useBookmarkImport";
import { useTranslation } from "@/lib/i18n/client";
import { cn } from "@/lib/utils";
import { useQuery, useQueryClient } from "@tanstack/react-query";
import { Download, Loader2, Upload } from "lucide-react";
import { Card, CardContent } from "../ui/card";
import { toast } from "../ui/use-toast";
import { ImportSessionsSection } from "./ImportSessionsSection";
function ImportCard({
text,
description,
children,
}: {
text: string;
description: string;
children: React.ReactNode;
}) {
return (
<Card className="transition-all hover:shadow-md">
<CardContent className="flex items-center gap-3 p-4">
<div className="rounded-full bg-primary/10 p-2">
<Download className="h-5 w-5 text-primary" />
</div>
<div className="flex-1">
<h3 className="font-medium">{text}</h3>
<p>{description}</p>
</div>
{children}
</CardContent>
</Card>
);
}
function ExportButton() {
const { t } = useTranslation();
const [format, setFormat] = useState<"json" | "netscape">("json");
const queryClient = useQueryClient();
const { isFetching, refetch, error } = useQuery({
queryKey: ["exportBookmarks"],
queryFn: async () => {
const res = await fetch(`/api/bookmarks/export?format=${format}`);
if (!res.ok) {
const error = await res.json();
throw new Error(error?.error || "Failed to export bookmarks");
}
const match = res.headers
.get("Content-Disposition")
?.match(/filename\*?=(?:UTF-8''|")?([^"]+)/i);
const filename = match
? match[1]
: `karakeep-export-${new Date().toISOString()}.${format}`;
return { blob: res.blob(), filename };
},
enabled: false,
});
useEffect(() => {
if (error) {
toast({
description: error.message,
variant: "destructive",
});
}
}, [error]);
const onExport = useCallback(async () => {
const { data } = await refetch();
if (!data) return;
const { blob, filename } = data;
const url = window.URL.createObjectURL(await blob);
const a = document.createElement("a");
a.href = url;
a.download = filename;
a.click();
window.URL.revokeObjectURL(url);
queryClient.setQueryData(["exportBookmarks"], () => null);
}, [refetch]);
return (
<Card className="transition-all hover:shadow-md">
<CardContent className="flex items-center gap-3 p-4">
<div className="rounded-full bg-primary/10 p-2">
<Upload className="h-5 w-5 text-primary" />
</div>
<div className="flex-1">
<h3 className="font-medium">Export File</h3>
<p>{t("settings.import.export_links_and_notes")}</p>
<Select
value={format}
onValueChange={(value) => setFormat(value as "json" | "netscape")}
>
<SelectTrigger className="mt-2 w-[180px]">
<SelectValue placeholder="Format" />
</SelectTrigger>
<SelectContent>
<SelectItem value="json">JSON (Karakeep format)</SelectItem>
<SelectItem value="netscape">HTML (Netscape format)</SelectItem>
</SelectContent>
</Select>
</div>
<Button
className={cn(
buttonVariants({ variant: "default", size: "sm" }),
"flex items-center gap-2",
)}
onClick={onExport}
disabled={isFetching}
>
{isFetching && <Loader2 className="mr-2 animate-spin" />}
<p>Export</p>
</Button>
</CardContent>
</Card>
);
}
export function ImportExportRow() {
const { t } = useTranslation();
const { importProgress, runUploadBookmarkFile } = useBookmarkImport();
return (
<div className="flex flex-col gap-3">
<div className="grid gap-4 md:grid-cols-2">
<ImportCard
text="HTML File"
description={t("settings.import.import_bookmarks_from_html_file")}
>
<FilePickerButton
size={"sm"}
loading={false}
accept=".html"
multiple={false}
className="flex items-center gap-2"
onFileSelect={(file) =>
runUploadBookmarkFile({ file, source: "html" })
}
>
<p>Import</p>
</FilePickerButton>
</ImportCard>
<ImportCard
text="Pocket"
description={t("settings.import.import_bookmarks_from_pocket_export")}
>
<FilePickerButton
size={"sm"}
loading={false}
accept=".csv"
multiple={false}
className="flex items-center gap-2"
onFileSelect={(file) =>
runUploadBookmarkFile({ file, source: "pocket" })
}
>
<p>Import</p>
</FilePickerButton>
</ImportCard>
<ImportCard
text="Omnivore"
description={t(
"settings.import.import_bookmarks_from_omnivore_export",
)}
>
<FilePickerButton
size={"sm"}
loading={false}
accept=".json"
multiple={false}
className="flex items-center gap-2"
onFileSelect={(file) =>
runUploadBookmarkFile({ file, source: "omnivore" })
}
>
<p>Import</p>
</FilePickerButton>
</ImportCard>
<ImportCard
text="Linkwarden"
description={t(
"settings.import.import_bookmarks_from_linkwarden_export",
)}
>
<FilePickerButton
size={"sm"}
loading={false}
accept=".json"
multiple={false}
className="flex items-center gap-2"
onFileSelect={(file) =>
runUploadBookmarkFile({ file, source: "linkwarden" })
}
>
<p>Import</p>
</FilePickerButton>
</ImportCard>
<ImportCard
text="Tab Session Manager"
description={t(
"settings.import.import_bookmarks_from_tab_session_manager_export",
)}
>
<FilePickerButton
size={"sm"}
loading={false}
accept=".json"
multiple={false}
className="flex items-center gap-2"
onFileSelect={(file) =>
runUploadBookmarkFile({ file, source: "tab-session-manager" })
}
>
<p>Import</p>
</FilePickerButton>
</ImportCard>
<ImportCard
text="mymind"
description={t("settings.import.import_bookmarks_from_mymind_export")}
>
<FilePickerButton
size={"sm"}
loading={false}
accept=".csv"
multiple={false}
className="flex items-center gap-2"
onFileSelect={(file) =>
runUploadBookmarkFile({ file, source: "mymind" })
}
>
<p>Import</p>
</FilePickerButton>
</ImportCard>
<ImportCard
text="Karakeep"
description={t(
"settings.import.import_bookmarks_from_karakeep_export",
)}
>
<FilePickerButton
size={"sm"}
loading={false}
accept=".json"
multiple={false}
className="flex items-center gap-2"
onFileSelect={(file) =>
runUploadBookmarkFile({ file, source: "karakeep" })
}
>
<p>Import</p>
</FilePickerButton>
</ImportCard>
<ExportButton />
</div>
{importProgress && (
<div className="flex flex-col gap-2">
<p className="shrink-0 text-sm">
Processed {importProgress.done} of {importProgress.total} bookmarks
</p>
<div className="w-full">
<Progress
value={(importProgress.done * 100) / importProgress.total}
/>
</div>
</div>
)}
</div>
);
}
export default function ImportExport() {
const { t } = useTranslation();
return (
<div className="space-y-3">
<div className="rounded-md border bg-background p-4">
<div className="flex w-full flex-col gap-6">
<div>
<p className="mb-4 text-lg font-medium">
{t("settings.import.import_export_bookmarks")}
</p>
<ImportExportRow />
</div>
</div>
</div>
<div className="rounded-md border bg-background p-4">
<ImportSessionsSection />
</div>
</div>
);
}
|