From ec31a971f6c2e3e058debf05fe8235c1d599b055 Mon Sep 17 00:00:00 2001 From: Simon Kenny Date: Sat, 7 Jun 2025 21:57:48 +0100 Subject: feat(mobile): add reader/screenshot/archive view to bookmark preview (#1509) * feat(mobile): add reader view by default to bookmark detail view, retaining WebView fallback * feat(mobile): add dark mode support for mobile reader view * Add selectors for different views for bookmark link --------- Co-authored-by: MohamedBassem --- .../app/dashboard/bookmarks/[slug]/index.tsx | 117 +++++++++++-- .../components/bookmarks/BookmarkLinkPreview.tsx | 183 +++++++++++++++++++++ 2 files changed, 288 insertions(+), 12 deletions(-) create mode 100644 apps/mobile/components/bookmarks/BookmarkLinkPreview.tsx diff --git a/apps/mobile/app/dashboard/bookmarks/[slug]/index.tsx b/apps/mobile/app/dashboard/bookmarks/[slug]/index.tsx index 7edbd0b8..01ee61de 100644 --- a/apps/mobile/app/dashboard/bookmarks/[slug]/index.tsx +++ b/apps/mobile/app/dashboard/bookmarks/[slug]/index.tsx @@ -8,9 +8,15 @@ import { View, } from "react-native"; import ImageView from "react-native-image-viewing"; -import WebView from "react-native-webview"; +import * as Haptics from "expo-haptics"; import { Stack, useLocalSearchParams, useRouter } from "expo-router"; import BookmarkAssetImage from "@/components/bookmarks/BookmarkAssetImage"; +import { + BookmarkLinkArchivePreview, + BookmarkLinkBrowserPreview, + BookmarkLinkReaderPreview, + BookmarkLinkScreenshotPreview, +} from "@/components/bookmarks/BookmarkLinkPreview"; import BookmarkTextMarkdown from "@/components/bookmarks/BookmarkTextMarkdown"; import FullPageError from "@/components/FullPageError"; import { TailwindResolver } from "@/components/TailwindResolver"; @@ -21,7 +27,16 @@ import { Input } from "@/components/ui/Input"; import { useToast } from "@/components/ui/Toast"; import { useAssetUrl } from "@/lib/hooks"; import { api } from "@/lib/trpc"; -import { ClipboardList, Globe, Info, Tag, Trash2 } from "lucide-react-native"; +import { MenuView } from "@react-native-menu/menu"; +import { + ChevronDown, + ClipboardList, + Globe, + Info, + Tag, + Trash2, +} from "lucide-react-native"; +import { useColorScheme } from "nativewind"; import { useDeleteBookmark, @@ -29,6 +44,50 @@ import { } from "@karakeep/shared-react/hooks/bookmarks"; import { BookmarkTypes, ZBookmark } from "@karakeep/shared/types/bookmarks"; +type BookmarkLinkType = "browser" | "reader" | "screenshot" | "archive"; + +function BookmarkLinkTypeSelector({ + type, + onChange, +}: { + type: BookmarkLinkType; + onChange: (type: BookmarkLinkType) => void; +}) { + 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, + }, + ]} + shouldOpenOnLongPress={false} + > + Haptics.selectionAsync()} color="gray" /> + + ); +} + function BottomActions({ bookmark }: { bookmark: ZBookmark }) { const { toast } = useToast(); const router = useRouter(); @@ -151,17 +210,27 @@ function BottomActions({ bookmark }: { bookmark: ZBookmark }) { ); } -function BookmarkLinkView({ bookmark }: { bookmark: ZBookmark }) { +function BookmarkLinkView({ + bookmark, + bookmarkPreviewType, +}: { + bookmark: ZBookmark; + bookmarkPreviewType: BookmarkLinkType; +}) { if (bookmark.content.type !== BookmarkTypes.LINK) { throw new Error("Wrong content type rendered"); } - return ( - - ); + + switch (bookmarkPreviewType) { + case "browser": + return ; + case "reader": + return ; + case "screenshot": + return ; + case "archive": + return ; + } } function BookmarkTextView({ bookmark }: { bookmark: ZBookmark }) { @@ -257,6 +326,11 @@ function BookmarkAssetView({ bookmark }: { bookmark: ZBookmark }) { export default function ListView() { const { slug } = useLocalSearchParams(); + const { colorScheme } = useColorScheme(); + + const [bookmarkLinkType, setBookmarkLinkType] = + useState("reader"); + if (typeof slug !== "string") { throw new Error("Unexpected param type"); } @@ -265,7 +339,10 @@ export default function ListView() { data: bookmark, error, refetch, - } = api.bookmarks.getBookmark.useQuery({ bookmarkId: slug }); + } = api.bookmarks.getBookmark.useQuery({ + bookmarkId: slug, + includeContent: false, + }); if (error) { return ; @@ -280,7 +357,12 @@ export default function ListView() { switch (bookmark.content.type) { case BookmarkTypes.LINK: title = bookmark.title ?? bookmark.content.title; - comp = ; + comp = ( + + ); break; case BookmarkTypes.TEXT: title = bookmark.title; @@ -298,6 +380,17 @@ export default function ListView() { headerTitle: title ?? "", headerBackTitle: "Back", headerTransparent: false, + headerTintColor: colorScheme === "dark" ? "#ffffff" : undefined, + headerStyle: { + backgroundColor: colorScheme === "dark" ? "#000000" : undefined, + }, + headerRight: () => + bookmark.content.type === BookmarkTypes.LINK ? ( + setBookmarkLinkType(type)} + /> + ) : undefined, }} /> diff --git a/apps/mobile/components/bookmarks/BookmarkLinkPreview.tsx b/apps/mobile/components/bookmarks/BookmarkLinkPreview.tsx new file mode 100644 index 00000000..5e1e7aa7 --- /dev/null +++ b/apps/mobile/components/bookmarks/BookmarkLinkPreview.tsx @@ -0,0 +1,183 @@ +import { useState } from "react"; +import { Pressable, 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"; +import { useAssetUrl } from "@/lib/hooks"; +import { api } from "@/lib/trpc"; +import { useColorScheme } from "nativewind"; + +import { BookmarkTypes, ZBookmark } from "@karakeep/shared/types/bookmarks"; + +import FullPageError from "../FullPageError"; +import FullPageSpinner from "../ui/FullPageSpinner"; +import BookmarkAssetImage from "./BookmarkAssetImage"; + +export function BookmarkLinkBrowserPreview({ + bookmark, +}: { + bookmark: ZBookmark; +}) { + if (bookmark.content.type !== BookmarkTypes.LINK) { + throw new Error("Wrong content type rendered"); + } + + return ( + + ); +} + +export function BookmarkLinkReaderPreview({ + bookmark, +}: { + bookmark: ZBookmark; +}) { + const { colorScheme } = useColorScheme(); + + const { + data: bookmarkWithContent, + error, + isLoading, + refetch, + } = api.bookmarks.getBookmark.useQuery({ + bookmarkId: bookmark.id, + includeContent: true, + }); + + if (isLoading) { + return ; + } + + if (error) { + return ; + } + + if (bookmarkWithContent?.content.type !== BookmarkTypes.LINK) { + throw new Error("Wrong content type rendered"); + } + + const isDark = colorScheme === "dark"; + + return ( + + + + + + + + + ${bookmarkWithContent.content.htmlContent} + + + `, + }} + style={{ + flex: 1, + backgroundColor: isDark ? "#000000" : "#ffffff", + }} + showsVerticalScrollIndicator={false} + showsHorizontalScrollIndicator={false} + /> + + ); +} + +export function BookmarkLinkArchivePreview({ + bookmark, +}: { + bookmark: ZBookmark; +}) { + const asset = + bookmark.assets.find((r) => r.assetType == "precrawledArchive") ?? + bookmark.assets.find((r) => r.assetType == "fullPageArchive"); + + const assetSource = useAssetUrl(asset?.id ?? ""); + + if (!asset) { + return ( + Asset has no offline archive + ); + } + + const webViewUri: WebViewSourceUri = { + uri: assetSource.uri!, + headers: assetSource.headers, + }; + return ( + + ); +} + +export function BookmarkLinkScreenshotPreview({ + bookmark, +}: { + bookmark: ZBookmark; +}) { + const asset = bookmark.assets.find((r) => r.assetType == "screenshot"); + + const assetSource = useAssetUrl(asset?.id ?? ""); + const [imageZoom, setImageZoom] = useState(false); + + if (!asset) { + return ( + Asset has no screenshot + ); + } + + return ( + + setImageZoom(false)} + doubleTapToZoomEnabled={true} + images={[assetSource]} + /> + setImageZoom(true)}> + + + + ); +} -- cgit v1.2.3-70-g09d2