Skip to content

⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.

Made importBookmarks reusable

dereulenspiegel8973bdd94ee8
diff --git a/lib/client/importBookmarks.ts b/lib/client/importBookmarks.ts
new file mode 100644
index 00000000..cbac2cce
--- /dev/null
+++ b/lib/client/importBookmarks.ts
@@ -0,0 +1,61 @@
+import { MigrationFormat } from "@/types/global";
+import { toast } from "react-hot-toast";
+
+const importBookmarks = async (
+ e: React.ChangeEvent<HTMLInputElement>,
+ format: MigrationFormat
+) => {
+ const file: File | null = e.target.files && e.target.files[0];
+
+ if (file) {
+ const reader = new FileReader();
+
+ reader.readAsArrayBuffer(file);
+ reader.onload = async function (e) {
+ const load = toast.loading("Importing...");
+
+ try {
+ const migrationFormatName = MigrationFormat[format];
+ const response = await fetch(`/api/v1/migration/${migrationFormatName}`, {
+ method: "POST",
+ body: e.target?.result as ArrayBuffer,
+ headers: { "Content-Type": "application/octet-stream" },
+ });
+
+ if (!response.ok) {
+ const errorData = await response.json();
+ toast.dismiss(load);
+
+ toast.error(
+ errorData.response ||
+ "Failed to import bookmarks. Please try again."
+ );
+ return;
+ }
+
+ await response.json();
+ toast.dismiss(load);
+ toast.success("Imported the Bookmarks! Reloading the page...");
+
+ setTimeout(() => {
+ location.reload();
+ }, 2000);
+ } catch (error) {
+ console.error("Request failed", error);
+ toast.dismiss(load);
+ toast.error(
+ "An error occurred while importing bookmarks. Please check the logs for more info."
+ );
+ }
+ };
+
+ reader.onerror = function (e) {
+ console.log("Error reading file:", e);
+ toast.error(
+ "Failed to read the file. Please make sure the file is correct and try again."
+ );
+ };
+ }
+};
+
+export default importBookmarks;
\ No newline at end of file
diff --git a/pages/dashboard.tsx b/pages/dashboard.tsx
index 2917f014..38227d73 100644
--- a/pages/dashboard.tsx
+++ b/pages/dashboard.tsx
@@ -18,6 +18,7 @@ import Links from "@/components/LinkViews/Links";
import useLocalSettingsStore from "@/store/localSettings";
import { useUpdateUser, useUser } from "@/hooks/store/user";
import SurveyModal from "@/components/ModalContent/SurveyModal";
+import importBookmarks from "@/lib/client/importBookmarks"
export default function Dashboard() {
const { t } = useTranslation();
@@ -73,70 +74,6 @@ export default function Dashboard() {
}
}, []);
- const importBookmarks = async (
- e: React.ChangeEvent<HTMLInputElement>,
- format: MigrationFormat
- ) => {
- const file: File | null = e.target.files && e.target.files[0];
-
- if (file) {
- const reader = new FileReader();
- //reader.readAsText(file, "UTF-8");
- reader.readAsArrayBuffer(file);
- reader.onload = async function (e) {
- const load = toast.loading("Importing...");
-
- // const request: string = e.target?.result as string;
-
- // const body: MigrationRequest = {
- // format,
- // data: request,
- // };
-
- try {
- const migrationFormatName = MigrationFormat[format];
- const response = await fetch(`/api/v1/migration/${migrationFormatName}`, {
- method: "POST",
- body: e.target?.result as ArrayBuffer,
- headers: { "Content-Type": "application/octet-stream" },
- });
-
- if (!response.ok) {
- const errorData = await response.json();
- toast.dismiss(load);
-
- toast.error(
- errorData.response ||
- "Failed to import bookmarks. Please try again."
- );
- return;
- }
-
- await response.json();
- toast.dismiss(load);
- toast.success("Imported the Bookmarks! Reloading the page...");
-
- setTimeout(() => {
- location.reload();
- }, 2000);
- } catch (error) {
- console.error("Request failed", error);
- toast.dismiss(load);
- toast.error(
- "An error occurred while importing bookmarks. Please check the logs for more info."
- );
- }
- };
-
- reader.onerror = function (e) {
- console.log("Error reading file:", e);
- toast.error(
- "Failed to read the file. Please make sure the file is correct and try again."
- );
- };
- }
- };
-
const [newLinkModal, setNewLinkModal] = useState(false);
const [viewMode, setViewMode] = useState<ViewMode>(
diff --git a/pages/settings/account.tsx b/pages/settings/account.tsx
index db5bf345..06042161 100644
--- a/pages/settings/account.tsx
+++ b/pages/settings/account.tsx
@@ -18,6 +18,7 @@ import { useTranslation } from "next-i18next";
import getServerSideProps from "@/lib/client/getServerSideProps";
import { useUpdateUser, useUser } from "@/hooks/store/user";
import { z } from "zod";
+import importBookmarks from "@/lib/client/importBookmarks"
const emailEnabled = process.env.NEXT_PUBLIC_EMAIL_PROVIDER;
@@ -31,19 +32,19 @@ export default function Account() {
!objectIsEmpty(account)
? account
: ({
- // @ts-ignore
- id: null,
- name: "",
- username: "",
- email: "",
- emailVerified: null,
- password: undefined,
- image: "",
- isPrivate: true,
- // @ts-ignore
- createdAt: null,
- whitelistedUsers: [],
- } as unknown as AccountSettings)
+ // @ts-ignore
+ id: null,
+ name: "",
+ username: "",
+ email: "",
+ emailVerified: null,
+ password: undefined,
+ image: "",
+ isPrivate: true,
+ // @ts-ignore
+ createdAt: null,
+ whitelistedUsers: [],
+ } as unknown as AccountSettings)
);
const { t } = useTranslation();
@@ -126,67 +127,6 @@ export default function Account() {
}
};
- const importBookmarks = async (
- e: React.ChangeEvent<HTMLInputElement>,
- format: MigrationFormat
- ) => {
- const file: File | null = e.target.files && e.target.files[0];
-
- if (file) {
- const reader = new FileReader();
- reader.readAsText(file, "UTF-8");
- reader.onload = async function (e) {
- const load = toast.loading("Importing...");
-
- const request: string = e.target?.result as string;
-
- const body: MigrationRequest = {
- format,
- data: request,
- };
-
- try {
- const response = await fetch("/api/v1/migration", {
- method: "POST",
- body: JSON.stringify(body),
- });
-
- if (!response.ok) {
- const errorData = await response.json();
- toast.dismiss(load);
-
- toast.error(
- errorData.response ||
- "Failed to import bookmarks. Please try again."
- );
- return;
- }
-
- await response.json();
- toast.dismiss(load);
- toast.success("Imported the Bookmarks! Reloading the page...");
-
- setTimeout(() => {
- location.reload();
- }, 2000);
- } catch (error) {
- console.error("Request failed", error);
- toast.dismiss(load);
- toast.error(
- "An error occurred while importing bookmarks. Please check the logs for more info."
- );
- }
- };
-
- reader.onerror = function (e) {
- console.log("Error reading file:", e);
- toast.error(
- "Failed to read the file. Please make sure the file is correct and try again."
- );
- };
- }
- };
-
const [whitelistedUsersTextbox, setWhiteListedUsersTextbox] = useState("");
useEffect(() => {