A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search
refactor: Move bookmark utils from shared-react to shared
Mohamed Bassemb218118b8429
diff --git a/apps/browser-extension/src/components/TagList.tsx b/apps/browser-extension/src/components/TagList.tsxindex 2d77f17f..4c5f6dfa 100644--- a/apps/browser-extension/src/components/TagList.tsx+++ b/apps/browser-extension/src/components/TagList.tsx@@ -1,5 +1,5 @@import { useAutoRefreshingBookmarkQuery } from "@karakeep/shared-react/hooks/bookmarks";-import { isBookmarkStillTagging } from "@karakeep/shared-react/utils/bookmarkUtils";+import { isBookmarkStillTagging } from "@karakeep/shared/utils/bookmarkUtils";import { Badge } from "./ui/badge";diff --git a/apps/mobile/app/dashboard/bookmarks/[slug]/info.tsx b/apps/mobile/app/dashboard/bookmarks/[slug]/info.tsxindex 6d02af53..b79a50b2 100644--- a/apps/mobile/app/dashboard/bookmarks/[slug]/info.tsx+++ b/apps/mobile/app/dashboard/bookmarks/[slug]/info.tsx@@ -22,8 +22,8 @@ import {useAutoRefreshingBookmarkQuery,useUpdateBookmark,} from "@karakeep/shared-react/hooks/bookmarks";-import { isBookmarkStillTagging } from "@karakeep/shared-react/utils/bookmarkUtils";import { BookmarkTypes, ZBookmark } from "@karakeep/shared/types/bookmarks";+import { isBookmarkStillTagging } from "@karakeep/shared/utils/bookmarkUtils";function TagList({ bookmark }: { bookmark: ZBookmark }) {return (diff --git a/apps/mobile/components/bookmarks/BookmarkCard.tsx b/apps/mobile/components/bookmarks/BookmarkCard.tsxindex 21ca1635..6b3a0f33 100644--- a/apps/mobile/components/bookmarks/BookmarkCard.tsx+++ b/apps/mobile/components/bookmarks/BookmarkCard.tsx@@ -21,12 +21,12 @@ import {useDeleteBookmark,useUpdateBookmark,} from "@karakeep/shared-react/hooks/bookmarks";+import { BookmarkTypes } from "@karakeep/shared/types/bookmarks";import {getBookmarkLinkImageUrl,isBookmarkStillLoading,isBookmarkStillTagging,-} from "@karakeep/shared-react/utils/bookmarkUtils";-import { BookmarkTypes } from "@karakeep/shared/types/bookmarks";+} from "@karakeep/shared/utils/bookmarkUtils";import { Divider } from "../ui/Divider";import { Skeleton } from "../ui/Skeleton";diff --git a/apps/web/app/settings/assets/page.tsx b/apps/web/app/settings/assets/page.tsxindex 0b3c2b5b..0e7a3daa 100644--- a/apps/web/app/settings/assets/page.tsx+++ b/apps/web/app/settings/assets/page.tsx@@ -21,7 +21,7 @@ import { formatBytes } from "@/lib/utils";import { ExternalLink, Trash2 } from "lucide-react";import { useDetachBookmarkAsset } from "@karakeep/shared-react/hooks/assets";-import { getAssetUrl } from "@karakeep/shared-react/utils/assetUtils";+import { getAssetUrl } from "@karakeep/shared/utils/assetUtils";import {humanFriendlyNameForAssertType,isAllowedToDetachAsset,diff --git a/apps/web/components/dashboard/bookmarks/AssetCard.tsx b/apps/web/components/dashboard/bookmarks/AssetCard.tsxindex 6fc8a723..c906f2a7 100644--- a/apps/web/components/dashboard/bookmarks/AssetCard.tsx+++ b/apps/web/components/dashboard/bookmarks/AssetCard.tsx@@ -6,8 +6,8 @@ import { cn } from "@/lib/utils";import { FileText } from "lucide-react";import type { ZBookmarkTypeAsset } from "@karakeep/shared/types/bookmarks";-import { getAssetUrl } from "@karakeep/shared-react/utils/assetUtils";-import { getSourceUrl } from "@karakeep/shared-react/utils/bookmarkUtils";+import { getAssetUrl } from "@karakeep/shared/utils/assetUtils";+import { getSourceUrl } from "@karakeep/shared/utils/bookmarkUtils";import { BookmarkLayoutAdaptingCard } from "./BookmarkLayoutAdaptingCard";import FooterLinkURL from "./FooterLinkURL";diff --git a/apps/web/components/dashboard/bookmarks/BookmarkCard.tsx b/apps/web/components/dashboard/bookmarks/BookmarkCard.tsxindex 3c92e03e..4fc7d94a 100644--- a/apps/web/components/dashboard/bookmarks/BookmarkCard.tsx+++ b/apps/web/components/dashboard/bookmarks/BookmarkCard.tsx@@ -1,7 +1,7 @@import { api } from "@/lib/trpc";-import { isBookmarkStillLoading } from "@karakeep/shared-react/utils/bookmarkUtils";import { BookmarkTypes, ZBookmark } from "@karakeep/shared/types/bookmarks";+import { isBookmarkStillLoading } from "@karakeep/shared/utils/bookmarkUtils";import AssetCard from "./AssetCard";import LinkCard from "./LinkCard";diff --git a/apps/web/components/dashboard/bookmarks/BookmarkLayoutAdaptingCard.tsx b/apps/web/components/dashboard/bookmarks/BookmarkLayoutAdaptingCard.tsxindex a0437c71..6f55ca00 100644--- a/apps/web/components/dashboard/bookmarks/BookmarkLayoutAdaptingCard.tsx+++ b/apps/web/components/dashboard/bookmarks/BookmarkLayoutAdaptingCard.tsx@@ -13,8 +13,8 @@ import { Check, Image as ImageIcon, NotebookPen } from "lucide-react";import { useTheme } from "next-themes";import type { ZBookmark } from "@karakeep/shared/types/bookmarks";-import { isBookmarkStillTagging } from "@karakeep/shared-react/utils/bookmarkUtils";import { BookmarkTypes } from "@karakeep/shared/types/bookmarks";+import { isBookmarkStillTagging } from "@karakeep/shared/utils/bookmarkUtils";import BookmarkActionBar from "./BookmarkActionBar";import TagList from "./TagList";diff --git a/apps/web/components/dashboard/bookmarks/EditBookmarkDialog.tsx b/apps/web/components/dashboard/bookmarks/EditBookmarkDialog.tsxindex ab5d0364..f0ede24e 100644--- a/apps/web/components/dashboard/bookmarks/EditBookmarkDialog.tsx+++ b/apps/web/components/dashboard/bookmarks/EditBookmarkDialog.tsx@@ -35,13 +35,13 @@ import { CalendarIcon } from "lucide-react";import { useForm } from "react-hook-form";import { useUpdateBookmark } from "@karakeep/shared-react/hooks/bookmarks";-import { getBookmarkTitle } from "@karakeep/shared-react/utils/bookmarkUtils";import {BookmarkTypes,ZBookmark,ZUpdateBookmarksRequest,zUpdateBookmarksRequestSchema,} from "@karakeep/shared/types/bookmarks";+import { getBookmarkTitle } from "@karakeep/shared/utils/bookmarkUtils";import { BookmarkTagsEditor } from "./BookmarkTagsEditor";diff --git a/apps/web/components/dashboard/bookmarks/LinkCard.tsx b/apps/web/components/dashboard/bookmarks/LinkCard.tsxindex 2c91bd08..778166b5 100644--- a/apps/web/components/dashboard/bookmarks/LinkCard.tsx+++ b/apps/web/components/dashboard/bookmarks/LinkCard.tsx@@ -10,7 +10,7 @@ import {getBookmarkTitle,getSourceUrl,isBookmarkStillCrawling,-} from "@karakeep/shared-react/utils/bookmarkUtils";+} from "@karakeep/shared/utils/bookmarkUtils";import { BookmarkLayoutAdaptingCard } from "./BookmarkLayoutAdaptingCard";import FooterLinkURL from "./FooterLinkURL";diff --git a/apps/web/components/dashboard/bookmarks/TextCard.tsx b/apps/web/components/dashboard/bookmarks/TextCard.tsxindex 0233357c..3be3a093 100644--- a/apps/web/components/dashboard/bookmarks/TextCard.tsx+++ b/apps/web/components/dashboard/bookmarks/TextCard.tsx@@ -7,8 +7,8 @@ import { bookmarkLayoutSwitch } from "@/lib/userLocalSettings/bookmarksLayout";import { cn } from "@/lib/utils";import type { ZBookmarkTypeText } from "@karakeep/shared/types/bookmarks";-import { getAssetUrl } from "@karakeep/shared-react/utils/assetUtils";-import { getSourceUrl } from "@karakeep/shared-react/utils/bookmarkUtils";+import { getAssetUrl } from "@karakeep/shared/utils/assetUtils";+import { getSourceUrl } from "@karakeep/shared/utils/bookmarkUtils";import { BookmarkLayoutAdaptingCard } from "./BookmarkLayoutAdaptingCard";import FooterLinkURL from "./FooterLinkURL";diff --git a/apps/web/components/dashboard/preview/AssetContentSection.tsx b/apps/web/components/dashboard/preview/AssetContentSection.tsxindex fd299320..5cab86bd 100644--- a/apps/web/components/dashboard/preview/AssetContentSection.tsx+++ b/apps/web/components/dashboard/preview/AssetContentSection.tsx@@ -11,8 +11,8 @@ import {} from "@/components/ui/select";import { useTranslation } from "@/lib/i18n/client";-import { getAssetUrl } from "@karakeep/shared-react/utils/assetUtils";import { BookmarkTypes, ZBookmark } from "@karakeep/shared/types/bookmarks";+import { getAssetUrl } from "@karakeep/shared/utils/assetUtils";// 20 MBconst BIG_FILE_SIZE = 20 * 1024 * 1024;diff --git a/apps/web/components/dashboard/preview/AttachmentBox.tsx b/apps/web/components/dashboard/preview/AttachmentBox.tsxindex 15acd799..674f151c 100644--- a/apps/web/components/dashboard/preview/AttachmentBox.tsx+++ b/apps/web/components/dashboard/preview/AttachmentBox.tsx@@ -19,8 +19,8 @@ import {useDetachBookmarkAsset,useReplaceBookmarkAsset,} from "@karakeep/shared-react/hooks/assets";-import { getAssetUrl } from "@karakeep/shared-react/utils/assetUtils";import { BookmarkTypes, ZBookmark } from "@karakeep/shared/types/bookmarks";+import { getAssetUrl } from "@karakeep/shared/utils/assetUtils";import {humanFriendlyNameForAssertType,isAllowedToAttachAsset,diff --git a/apps/web/components/dashboard/preview/BookmarkPreview.tsx b/apps/web/components/dashboard/preview/BookmarkPreview.tsxindex c09bc949..e213b9cb 100644--- a/apps/web/components/dashboard/preview/BookmarkPreview.tsx+++ b/apps/web/components/dashboard/preview/BookmarkPreview.tsx@@ -18,13 +18,13 @@ import { useTranslation } from "@/lib/i18n/client";import { api } from "@/lib/trpc";import { CalendarDays, ExternalLink } from "lucide-react";+import { BookmarkTypes, ZBookmark } from "@karakeep/shared/types/bookmarks";import {getBookmarkTitle,getSourceUrl,isBookmarkStillCrawling,isBookmarkStillLoading,-} from "@karakeep/shared-react/utils/bookmarkUtils";-import { BookmarkTypes, ZBookmark } from "@karakeep/shared/types/bookmarks";+} from "@karakeep/shared/utils/bookmarkUtils";import SummarizeBookmarkArea from "../bookmarks/SummarizeBookmarkArea";import ActionBar from "./ActionBar";diff --git a/apps/web/components/dashboard/preview/TextContentSection.tsx b/apps/web/components/dashboard/preview/TextContentSection.tsxindex 0c1aae67..4e33bb92 100644--- a/apps/web/components/dashboard/preview/TextContentSection.tsx+++ b/apps/web/components/dashboard/preview/TextContentSection.tsx@@ -3,8 +3,8 @@ import { BookmarkMarkdownComponent } from "@/components/dashboard/bookmarks/Bookimport { ScrollArea } from "@radix-ui/react-scroll-area";import type { ZBookmarkTypeText } from "@karakeep/shared/types/bookmarks";-import { getAssetUrl } from "@karakeep/shared-react/utils/assetUtils";import { BookmarkTypes, ZBookmark } from "@karakeep/shared/types/bookmarks";+import { getAssetUrl } from "@karakeep/shared/utils/assetUtils";export function TextContentSection({ bookmark }: { bookmark: ZBookmark }) {if (bookmark.content.type != BookmarkTypes.TEXT) {diff --git a/packages/shared-react/hooks/bookmarks.ts b/packages/shared-react/hooks/bookmarks.tsindex e06c7841..15b678cc 100644--- a/packages/shared-react/hooks/bookmarks.ts+++ b/packages/shared-react/hooks/bookmarks.ts@@ -1,5 +1,6 @@+import { isBookmarkStillLoading } from "@karakeep/shared/utils/bookmarkUtils";+import { api } from "../trpc";-import { isBookmarkStillLoading } from "../utils/bookmarkUtils";import { useBookmarkGridContext } from "./bookmark-grid-context";import { useAddBookmarkToList } from "./lists";diff --git a/packages/shared-react/tsconfig.json b/packages/shared-react/tsconfig.jsonindex 3d6919d0..513582c4 100644--- a/packages/shared-react/tsconfig.json+++ b/packages/shared-react/tsconfig.json@@ -9,6 +9,6 @@},"tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json"},- "include": ["**/*.ts", "**/*.tsx"],+ "include": ["**/*.ts", "**/*.tsx", "../shared/utils/bookmarkUtils.ts", "../shared/utils/assetUtils.ts"],"exclude": ["node_modules"]}diff --git a/packages/shared-react/utils/bookmarkUtils.ts b/packages/shared-react/utils/bookmarkUtils.tsindex 08a6a5e9..e69de29b 100644--- a/packages/shared-react/utils/bookmarkUtils.ts+++ b/packages/shared-react/utils/bookmarkUtils.ts@@ -1,81 +0,0 @@-import {- BookmarkTypes,- ZBookmark,- ZBookmarkedLink,-} from "@karakeep/shared/types/bookmarks";--import { getAssetUrl } from "./assetUtils";--const MAX_LOADING_MSEC = 30 * 1000;--export function getBookmarkLinkImageUrl(bookmark: ZBookmarkedLink) {- if (bookmark.imageAssetId) {- return { url: getAssetUrl(bookmark.imageAssetId), localAsset: true };- }- if (bookmark.screenshotAssetId) {- return { url: getAssetUrl(bookmark.screenshotAssetId), localAsset: true };- }- return bookmark.imageUrl- ? { url: bookmark.imageUrl, localAsset: false }- : null;-}--export function isBookmarkStillCrawling(bookmark: ZBookmark) {- return (- bookmark.content.type == BookmarkTypes.LINK &&- !bookmark.content.crawledAt &&- Date.now().valueOf() - bookmark.createdAt.valueOf() < MAX_LOADING_MSEC- );-}--export function isBookmarkStillTagging(bookmark: ZBookmark) {- return (- bookmark.taggingStatus == "pending" &&- Date.now().valueOf() - bookmark.createdAt.valueOf() < MAX_LOADING_MSEC- );-}--export function isBookmarkStillSummarizing(bookmark: ZBookmark) {- return (- bookmark.summarizationStatus == "pending" &&- Date.now().valueOf() - bookmark.createdAt.valueOf() < MAX_LOADING_MSEC- );-}--export function isBookmarkStillLoading(bookmark: ZBookmark) {- return (- isBookmarkStillTagging(bookmark) ||- isBookmarkStillCrawling(bookmark) ||- isBookmarkStillSummarizing(bookmark)- );-}--export function getSourceUrl(bookmark: ZBookmark) {- if (bookmark.content.type === BookmarkTypes.LINK) {- return bookmark.content.url;- }- if (bookmark.content.type === BookmarkTypes.ASSET) {- return bookmark.content.sourceUrl ?? null;- }- if (bookmark.content.type === BookmarkTypes.TEXT) {- return bookmark.content.sourceUrl ?? null;- }- return null;-}--export function getBookmarkTitle(bookmark: ZBookmark) {- let title: string | null = null;- switch (bookmark.content.type) {- case BookmarkTypes.LINK:- title = bookmark.content.title ?? bookmark.content.url;- break;- case BookmarkTypes.TEXT:- title = null;- break;- case BookmarkTypes.ASSET:- title = bookmark.content.fileName ?? null;- break;- }-- return bookmark.title ? bookmark.title : title;-}diff --git a/packages/shared-react/utils/assetUtils.ts b/packages/shared/utils/assetUtils.tssimilarity index 100%rename from packages/shared-react/utils/assetUtils.tsrename to packages/shared/utils/assetUtils.tsdiff --git a/packages/shared/utils/bookmarkUtils.ts b/packages/shared/utils/bookmarkUtils.tsnew file mode 100644index 00000000..31d7b698--- /dev/null+++ b/packages/shared/utils/bookmarkUtils.ts@@ -0,0 +1,76 @@+import { BookmarkTypes, ZBookmark, ZBookmarkedLink } from "../types/bookmarks";+import { getAssetUrl } from "./assetUtils";++const MAX_LOADING_MSEC = 30 * 1000;++export function getBookmarkLinkImageUrl(bookmark: ZBookmarkedLink) {+ if (bookmark.imageAssetId) {+ return { url: getAssetUrl(bookmark.imageAssetId), localAsset: true };+ }+ if (bookmark.screenshotAssetId) {+ return { url: getAssetUrl(bookmark.screenshotAssetId), localAsset: true };+ }+ return bookmark.imageUrl+ ? { url: bookmark.imageUrl, localAsset: false }+ : null;+}++export function isBookmarkStillCrawling(bookmark: ZBookmark) {+ return (+ bookmark.content.type == BookmarkTypes.LINK &&+ !bookmark.content.crawledAt &&+ Date.now().valueOf() - bookmark.createdAt.valueOf() < MAX_LOADING_MSEC+ );+}++export function isBookmarkStillTagging(bookmark: ZBookmark) {+ return (+ bookmark.taggingStatus == "pending" &&+ Date.now().valueOf() - bookmark.createdAt.valueOf() < MAX_LOADING_MSEC+ );+}++export function isBookmarkStillSummarizing(bookmark: ZBookmark) {+ return (+ bookmark.summarizationStatus == "pending" &&+ Date.now().valueOf() - bookmark.createdAt.valueOf() < MAX_LOADING_MSEC+ );+}++export function isBookmarkStillLoading(bookmark: ZBookmark) {+ return (+ isBookmarkStillTagging(bookmark) ||+ isBookmarkStillCrawling(bookmark) ||+ isBookmarkStillSummarizing(bookmark)+ );+}++export function getSourceUrl(bookmark: ZBookmark) {+ if (bookmark.content.type === BookmarkTypes.LINK) {+ return bookmark.content.url;+ }+ if (bookmark.content.type === BookmarkTypes.ASSET) {+ return bookmark.content.sourceUrl ?? null;+ }+ if (bookmark.content.type === BookmarkTypes.TEXT) {+ return bookmark.content.sourceUrl ?? null;+ }+ return null;+}++export function getBookmarkTitle(bookmark: ZBookmark) {+ let title: string | null = null;+ switch (bookmark.content.type) {+ case BookmarkTypes.LINK:+ title = bookmark.content.title ?? bookmark.content.url;+ break;+ case BookmarkTypes.TEXT:+ title = null;+ break;+ case BookmarkTypes.ASSET:+ title = bookmark.content.fileName ?? null;+ break;+ }++ return bookmark.title ? bookmark.title : title;+}