aboutsummaryrefslogtreecommitdiffstats
path: root/packages/shared/utils
diff options
context:
space:
mode:
Diffstat (limited to 'packages/shared/utils')
-rw-r--r--packages/shared/utils/assetUtils.ts3
-rw-r--r--packages/shared/utils/bookmarkUtils.ts76
2 files changed, 79 insertions, 0 deletions
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;
+}