From 362be3008aa8b036c4c448a86e459044af8784c2 Mon Sep 17 00:00:00 2001 From: Mohamed Bassem Date: Sun, 6 Jul 2025 22:02:07 +0000 Subject: fix(mobile): Fix crash when bookmark doesn't have archive or screenshot. Fixes #1584 --- .../app/dashboard/bookmarks/[slug]/index.tsx | 81 ++++++++++++++++------ .../components/bookmarks/BookmarkLinkPreview.tsx | 10 ++- 2 files changed, 65 insertions(+), 26 deletions(-) diff --git a/apps/mobile/app/dashboard/bookmarks/[slug]/index.tsx b/apps/mobile/app/dashboard/bookmarks/[slug]/index.tsx index 1cf2ad3d..00954dd8 100644 --- a/apps/mobile/app/dashboard/bookmarks/[slug]/index.tsx +++ b/apps/mobile/app/dashboard/bookmarks/[slug]/index.tsx @@ -47,41 +47,75 @@ import { BookmarkTypes, ZBookmark } from "@karakeep/shared/types/bookmarks"; type BookmarkLinkType = "browser" | "reader" | "screenshot" | "archive"; +function getAvailableViewTypes(bookmark: ZBookmark): BookmarkLinkType[] { + if (bookmark.content.type !== BookmarkTypes.LINK) { + return []; + } + + const availableTypes: BookmarkLinkType[] = ["browser", "reader"]; + + if (bookmark.assets.some((asset) => asset.assetType === "screenshot")) { + availableTypes.push("screenshot"); + } + + if ( + bookmark.assets.some( + (asset) => + asset.assetType === "precrawledArchive" || + asset.assetType === "fullPageArchive", + ) + ) { + availableTypes.push("archive"); + } + + return availableTypes; +} + function BookmarkLinkTypeSelector({ type, onChange, + bookmark, }: { type: BookmarkLinkType; onChange: (type: BookmarkLinkType) => void; + bookmark: ZBookmark; }) { + const availableTypes = getAvailableViewTypes(bookmark); + + const allActions = [ + { + id: "reader" as const, + title: "Reader View", + state: type === "reader" ? ("on" as const) : undefined, + }, + { + id: "browser" as const, + title: "Browser", + state: type === "browser" ? ("on" as const) : undefined, + }, + { + id: "screenshot" as const, + title: "Screenshot", + state: type === "screenshot" ? ("on" as const) : undefined, + }, + { + id: "archive" as const, + title: "Archived Page", + state: type === "archive" ? ("on" as const) : undefined, + }, + ]; + + const availableActions = allActions.filter((action) => + availableTypes.includes(action.id), + ); + return ( { Haptics.selectionAsync(); onChange(nativeEvent.event as BookmarkLinkType); }} - actions={[ - { - id: "reader", - title: "Reader", - state: type === "reader" ? "on" : undefined, - }, - { - id: "browser", - title: "Browser", - state: type === "browser" ? "on" : undefined, - }, - { - id: "screenshot", - title: "Screenshot", - state: type === "screenshot" ? "on" : undefined, - }, - { - id: "archive", - title: "Archive", - state: type === "archive" ? "on" : undefined, - }, - ]} + actions={availableActions} shouldOpenOnLongPress={false} > Haptics.selectionAsync()} color="gray" /> @@ -346,7 +380,7 @@ export default function ListView() { const isDark = colorScheme === "dark"; const [bookmarkLinkType, setBookmarkLinkType] = - useState("reader"); + useState("browser"); if (typeof slug !== "string") { throw new Error("Unexpected param type"); @@ -407,6 +441,7 @@ export default function ListView() { setBookmarkLinkType(type)} + bookmark={bookmark} /> ) : undefined, }} diff --git a/apps/mobile/components/bookmarks/BookmarkLinkPreview.tsx b/apps/mobile/components/bookmarks/BookmarkLinkPreview.tsx index 5e1e7aa7..03999c3e 100644 --- a/apps/mobile/components/bookmarks/BookmarkLinkPreview.tsx +++ b/apps/mobile/components/bookmarks/BookmarkLinkPreview.tsx @@ -1,5 +1,5 @@ import { useState } from "react"; -import { Pressable, View } from "react-native"; +import { Pressable, Text, View } from "react-native"; import ImageView from "react-native-image-viewing"; import WebView from "react-native-webview"; import { WebViewSourceUri } from "react-native-webview/lib/WebViewTypes"; @@ -130,7 +130,9 @@ export function BookmarkLinkArchivePreview({ if (!asset) { return ( - Asset has no offline archive + + Asset has no offline archive + ); } @@ -159,7 +161,9 @@ export function BookmarkLinkScreenshotPreview({ if (!asset) { return ( - Asset has no screenshot + + Asset has no screenshot + ); } -- cgit v1.2.3-70-g09d2