From e6486465decd612f7e437abe904960a47ff359ce Mon Sep 17 00:00:00 2001 From: kamtschatka Date: Mon, 1 Jul 2024 13:03:53 +0200 Subject: refactor: added the bookmark type to the database (#256) * refactoring asset types Extracted out functions to silently delete assets and to update them after crawling Generalized the mapping of assets to bookmark fields to make extending them easier * Added the bookmark type to the database Introduced an enum to have better type safety cleaned up the code and based some code on the type directly * add BookmarkType.UNKNWON * lint and remove unused function --------- Co-authored-by: MohamedBassem --- apps/browser-extension/src/SavePage.tsx | 3 +- apps/cli/src/commands/bookmarks.ts | 13 +- apps/mobile/app/dashboard/add-link.tsx | 4 +- apps/mobile/app/dashboard/add-note.tsx | 7 +- apps/mobile/app/sharing.tsx | 6 +- apps/mobile/components/bookmarks/BookmarkCard.tsx | 13 +- .../components/bookmarks/UpdatingBookmarkList.tsx | 3 +- apps/mobile/lib/upload.ts | 4 +- apps/web/components/dashboard/UploadDropzone.tsx | 5 +- .../dashboard/bookmarks/BookmarkCard.tsx | 8 +- .../dashboard/bookmarks/BookmarkOptions.tsx | 7 +- .../dashboard/bookmarks/BookmarkedTextEditor.tsx | 6 +- .../components/dashboard/bookmarks/EditorCard.tsx | 12 +- .../dashboard/preview/AssetContentSection.tsx | 4 +- .../dashboard/preview/BookmarkPreview.tsx | 12 +- .../components/dashboard/preview/EditableTitle.tsx | 8 +- .../dashboard/preview/LinkContentSection.tsx | 8 +- .../dashboard/preview/TextContentSection.tsx | 4 +- apps/workers/crawlerWorker.ts | 6 + packages/db/drizzle/0025_aspiring_skaar.sql | 11 + packages/db/drizzle/meta/0025_snapshot.json | 1067 ++++++++++++++++++++ packages/db/drizzle/meta/_journal.json | 7 + packages/db/schema.ts | 5 + packages/shared-react/utils/bookmarkUtils.ts | 5 +- packages/shared/types/bookmarks.ts | 15 +- packages/trpc/routers/bookmarks.test.ts | 38 +- packages/trpc/routers/bookmarks.ts | 105 +- 27 files changed, 1266 insertions(+), 120 deletions(-) create mode 100644 packages/db/drizzle/0025_aspiring_skaar.sql create mode 100644 packages/db/drizzle/meta/0025_snapshot.json diff --git a/apps/browser-extension/src/SavePage.tsx b/apps/browser-extension/src/SavePage.tsx index 9cc1521a..c6f85b3b 100644 --- a/apps/browser-extension/src/SavePage.tsx +++ b/apps/browser-extension/src/SavePage.tsx @@ -1,6 +1,7 @@ import { useEffect, useState } from "react"; import { Navigate } from "react-router-dom"; +import { BookmarkTypes } from "../../../packages/shared/types/bookmarks"; import Spinner from "./Spinner"; import { api } from "./utils/trpc"; @@ -32,7 +33,7 @@ export default function SavePage() { } createBookmark({ - type: "link", + type: BookmarkTypes.LINK, url: currentUrl, }); } diff --git a/apps/cli/src/commands/bookmarks.ts b/apps/cli/src/commands/bookmarks.ts index 40442ec1..6efb41d9 100644 --- a/apps/cli/src/commands/bookmarks.ts +++ b/apps/cli/src/commands/bookmarks.ts @@ -9,7 +9,10 @@ import { getAPIClient } from "@/lib/trpc"; import { Command } from "@commander-js/extra-typings"; import type { ZBookmark } from "@hoarder/shared/types/bookmarks"; -import { MAX_NUM_BOOKMARKS_PER_PAGE } from "@hoarder/shared/types/bookmarks"; +import { + BookmarkTypes, + MAX_NUM_BOOKMARKS_PER_PAGE, +} from "@hoarder/shared/types/bookmarks"; export const bookmarkCmd = new Command() .name("bookmarks") @@ -26,7 +29,7 @@ function normalizeBookmark(bookmark: ZBookmark) { tags: bookmark.tags.map((t) => t.name), }; - if (ret.content.type == "link" && ret.content.htmlContent) { + if (ret.content.type == BookmarkTypes.LINK && ret.content.htmlContent) { if (ret.content.htmlContent.length > 10) { ret.content.htmlContent = ret.content.htmlContent.substring(0, 10) + "... "; @@ -63,7 +66,7 @@ bookmarkCmd const promises = [ ...opts.link.map((url) => api.bookmarks.createBookmark - .mutate({ type: "link", url }) + .mutate({ type: BookmarkTypes.LINK, url }) .then((bookmark: ZBookmark) => { results.push(normalizeBookmark(bookmark)); }) @@ -71,7 +74,7 @@ bookmarkCmd ), ...opts.note.map((text) => api.bookmarks.createBookmark - .mutate({ type: "text", text }) + .mutate({ type: BookmarkTypes.TEXT, text }) .then((bookmark: ZBookmark) => { results.push(normalizeBookmark(bookmark)); }) @@ -87,7 +90,7 @@ bookmarkCmd const text = fs.readFileSync(0, "utf-8"); promises.push( api.bookmarks.createBookmark - .mutate({ type: "text", text }) + .mutate({ type: BookmarkTypes.TEXT, text }) .then((bookmark: ZBookmark) => { results.push(normalizeBookmark(bookmark)); }) diff --git a/apps/mobile/app/dashboard/add-link.tsx b/apps/mobile/app/dashboard/add-link.tsx index 5096a9e7..d9773fb4 100644 --- a/apps/mobile/app/dashboard/add-link.tsx +++ b/apps/mobile/app/dashboard/add-link.tsx @@ -6,6 +6,8 @@ import { Input } from "@/components/ui/Input"; import { useToast } from "@/components/ui/Toast"; import { api } from "@/lib/trpc"; +import { BookmarkTypes } from "@hoarder/shared/types/bookmarks"; + export default function AddNote() { const [text, setText] = useState(""); const [error, setError] = useState(); @@ -54,7 +56,7 @@ export default function AddNote() { autoFocus />