From b218118b84291de4a9c1cd400dc58afab7054b78 Mon Sep 17 00:00:00 2001 From: Mohamed Bassem Date: Sat, 31 May 2025 15:23:46 +0000 Subject: refactor: Move bookmark utils from shared-react to shared --- packages/shared-react/hooks/bookmarks.ts | 3 +- packages/shared-react/tsconfig.json | 2 +- packages/shared-react/utils/assetUtils.ts | 3 -- packages/shared-react/utils/bookmarkUtils.ts | 81 ---------------------------- packages/shared/utils/assetUtils.ts | 3 ++ packages/shared/utils/bookmarkUtils.ts | 76 ++++++++++++++++++++++++++ 6 files changed, 82 insertions(+), 86 deletions(-) delete mode 100644 packages/shared-react/utils/assetUtils.ts create mode 100644 packages/shared/utils/assetUtils.ts create mode 100644 packages/shared/utils/bookmarkUtils.ts (limited to 'packages') diff --git a/packages/shared-react/hooks/bookmarks.ts b/packages/shared-react/hooks/bookmarks.ts index 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.json index 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/assetUtils.ts b/packages/shared-react/utils/assetUtils.ts deleted file mode 100644 index 119451d9..00000000 --- a/packages/shared-react/utils/assetUtils.ts +++ /dev/null @@ -1,3 +0,0 @@ -export function getAssetUrl(assetId: string) { - return `/api/assets/${assetId}`; -} diff --git a/packages/shared-react/utils/bookmarkUtils.ts b/packages/shared-react/utils/bookmarkUtils.ts index 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/utils/assetUtils.ts b/packages/shared/utils/assetUtils.ts new file mode 100644 index 00000000..119451d9 --- /dev/null +++ b/packages/shared/utils/assetUtils.ts @@ -0,0 +1,3 @@ +export function getAssetUrl(assetId: string) { + return `/api/assets/${assetId}`; +} diff --git a/packages/shared/utils/bookmarkUtils.ts b/packages/shared/utils/bookmarkUtils.ts new file mode 100644 index 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; +} -- cgit v1.2.3-70-g09d2