A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search
feat(mobile): switch to using context menu in ios
Mohamed Bassem28e79094ef33
diff --git a/apps/mobile/components/bookmarks/BookmarkCard.tsx b/apps/mobile/components/bookmarks/BookmarkCard.tsxindex f3397ebb..70ddefe6 100644--- a/apps/mobile/components/bookmarks/BookmarkCard.tsx+++ b/apps/mobile/components/bookmarks/BookmarkCard.tsx@@ -5,7 +5,7 @@ import { useWhoAmI } from "@karakeep/shared-react/hooks/users";import { useQuery } from "@tanstack/react-query";import { Image } from "expo-image";import { useRouter } from "expo-router";-import { Linking, View } from "react-native";+import { Linking, Platform, View } from "react-native";import { useTRPC } from "@karakeep/shared-react/trpc";import type { ZBookmark } from "@karakeep/shared/types/bookmarks";@@ -25,12 +25,13 @@ import {import TagList from "./card/TagList";import { Divider } from "../ui/Divider";import ActionBar from "./card/ActionBar";+import { useBookmarkActions } from "./card/use-bookmark-actions";function useLinkCardContext({bookmark,}: {bookmark: ZBookmark;-}): Omit<BookmarkCardContext, "isOwner" | "bookmark"> | undefined {+}): Omit<BookmarkCardContext, "isOwner" | "bookmark" | "actions"> | undefined {const { settings } = useAppSettings();if (bookmark.content.type !== BookmarkTypes.LINK) {@@ -119,7 +120,7 @@ function useTextCardContext({bookmark,}: {bookmark: ZBookmark;-}): Omit<BookmarkCardContext, "isOwner" | "bookmark"> | undefined {+}): Omit<BookmarkCardContext, "isOwner" | "bookmark" | "actions"> | undefined {if (bookmark.content.type !== BookmarkTypes.TEXT) {return undefined;}@@ -144,7 +145,7 @@ function useAssetCardContext({bookmark,}: {bookmark: ZBookmark;-}): Omit<BookmarkCardContext, "isOwner" | "bookmark"> | undefined {+}): Omit<BookmarkCardContext, "isOwner" | "bookmark" | "actions"> | undefined {if (bookmark.content.type !== BookmarkTypes.ASSET) {return undefined;}@@ -185,7 +186,7 @@ function CardLayout({ ctx }: { ctx: BookmarkCardContext }) {<Divider orientation="vertical" className="mt-2 h-0.5 w-full" /><View className="mt-2 flex flex-row justify-between px-2 pb-2"><BookmarkCardContainer.FooterExtras />- <ActionBar bookmark={ctx.bookmark} />+ {Platform.OS !== "ios" && <ActionBar actions={ctx.actions} />}</View></View></View>@@ -232,9 +233,11 @@ function ListLayout({ ctx }: { ctx: BookmarkCardContext }) {<View className="h-7 justify-center overflow-hidden"><TagList bookmark={ctx.bookmark} /></View>- <View className="flex-row justify-end pt-0.5">- <ActionBar bookmark={ctx.bookmark} compact />- </View>+ {Platform.OS !== "ios" && (+ <View className="flex-row justify-end pt-0.5">+ <ActionBar actions={ctx.actions} compact />+ </View>+ )}</View></View></BookmarkCardContainer.Root>@@ -296,12 +299,15 @@ export default function BookmarkCard({const assetContext = useAssetCardContext({ bookmark });const ctx = linkContext ?? textContext ?? assetContext;const Layout = settings.bookmarkLayout === "list" ? ListLayout : CardLayout;+ const isOwner = currentUser?.id === bookmark.userId;+ const actions = useBookmarkActions(bookmark, isOwner);return (<Layoutctx={{...ctx,- isOwner: currentUser?.id === bookmark.userId,+ isOwner,+ actions,bookmark,mediaOnPress: () => onOpenBookmark(bookmark),bodyOnPress: () => onOpenBookmark(bookmark),diff --git a/apps/mobile/components/bookmarks/card/ActionBar.tsx b/apps/mobile/components/bookmarks/card/ActionBar.tsxindex ea91a38c..7c8a07cf 100644--- a/apps/mobile/components/bookmarks/card/ActionBar.tsx+++ b/apps/mobile/components/bookmarks/card/ActionBar.tsx@@ -1,240 +1,27 @@-import { useOfflineAvailability } from "@/components/bookmarks/useOfflineAvailability";-import useAppSettings from "@/lib/settings";-import { shareBookmark } from "@/lib/shareBookmark";-import { useMenuIconColors } from "@/lib/useMenuIconColors";-import { MenuAction, MenuView } from "@react-native-menu/menu";+import type { BookmarkActionController } from "@/components/bookmarks/card/use-bookmark-actions";+import { MenuView } from "@react-native-menu/menu";import * as Haptics from "expo-haptics";-import { router } from "expo-router";import { Ellipsis, ShareIcon, Star } from "lucide-react-native";-import {- ActivityIndicator,- Alert,- Platform,- Pressable,- View,-} from "react-native";--import {- useDeleteBookmark,- useUpdateBookmark,-} from "@karakeep/shared-react/hooks/bookmarks";-import { useWhoAmI } from "@karakeep/shared-react/hooks/users";-import type { ZBookmark } from "@karakeep/shared/types/bookmarks";-import { BookmarkTypes } from "@karakeep/shared/types/bookmarks";--import { useToast } from "../../ui/Toast";+import { ActivityIndicator, Pressable, View } from "react-native";export default function ActionBar({- bookmark,+ actions,compact = false,}: {- bookmark: ZBookmark;+ actions: BookmarkActionController;compact?: boolean;}) {- const { toast } = useToast();- const { settings } = useAppSettings();- const { data: currentUser } = useWhoAmI();- const { menuIconColor, destructiveMenuIconColor } = useMenuIconColors();- const offlineAvailability = useOfflineAvailability(bookmark.id);-- // Check if the current user owns this bookmark- const isOwner = currentUser?.id === bookmark.userId;- const supportsOfflineReading =- bookmark.content.type === BookmarkTypes.LINK ||- bookmark.content.type === BookmarkTypes.TEXT;-- const onError = () => {- toast({- message: "Something went wrong",- variant: "destructive",- showProgress: false,- });- };-- const { mutate: deleteBookmark, isPending: isDeletionPending } =- useDeleteBookmark({- onSuccess: () => {- toast({- message: "The bookmark has been deleted!",- showProgress: false,- });- },- onError,- });-- const { mutate: favouriteBookmark, variables } = useUpdateBookmark({- onError,- });-- const { mutate: archiveBookmark, isPending: isArchivePending } =- useUpdateBookmark({- onSuccess: (resp) => {- toast({- message: `The bookmark has been ${resp.archived ? "archived" : "un-archived"}!`,- showProgress: false,- });- },- onError,- });-- const deleteBookmarkAlert = () =>- Alert.alert(- "Delete bookmark?",- "Are you sure you want to delete this bookmark?",- [- { text: "Cancel", style: "cancel" },- {- text: "Delete",- onPress: () => deleteBookmark({ bookmarkId: bookmark.id }),- style: "destructive",- },- ],- );-- const handleShare = () => shareBookmark(bookmark, settings, toast);-- // Build actions array based on ownership- const menuActions: MenuAction[] = [];- if (isOwner) {- menuActions.push(- {- id: "edit",- title: "Edit",- image: Platform.select({- ios: "pencil",- }),- imageColor: Platform.select({- ios: menuIconColor,- }),- },- {- id: "manage_list",- title: "Manage Lists",- image: Platform.select({- ios: "list.bullet",- }),- imageColor: Platform.select({- ios: menuIconColor,- }),- },- {- id: "manage_tags",- title: "Manage Tags",- image: Platform.select({- ios: "tag",- }),- imageColor: Platform.select({- ios: menuIconColor,- }),- },- {- id: "archive",- title: bookmark.archived ? "Un-archive" : "Archive",- image: Platform.select({- ios: "folder",- }),- imageColor: Platform.select({- ios: menuIconColor,- }),- },- );- }-- if (supportsOfflineReading) {- if (offlineAvailability.isAvailableOffline) {- menuActions.push({- id: "offline-group",- title: "Available offline",- image: Platform.select({- ios: "checkmark.circle",- }),- imageColor: Platform.select({- ios: menuIconColor,- }),- subactions: [- {- id: "update-offline-copy",- title: offlineAvailability.isSaving- ? "Saving offline copy..."- : "Update offline copy",- image: Platform.select({- ios: "arrow.clockwise",- }),- imageColor: Platform.select({- ios: menuIconColor,- }),- attributes: {- ...(offlineAvailability.isSaving && {- disabled: true,- }),- },- },- {- id: "remove-offline-copy",- title: "Remove offline copy",- attributes: {- destructive: true,- },- image: Platform.select({- ios: "trash",- }),- imageColor: Platform.select({- ios: destructiveMenuIconColor,- }),- },- ],- });- } else {- menuActions.push({- id: "make-available-offline",- title: offlineAvailability.isSaving- ? "Saving offline copy..."- : "Make available offline",- image: Platform.select({- ios: "arrow.down.circle",- }),- imageColor: Platform.select({- ios: menuIconColor,- }),- attributes: {- ...(offlineAvailability.isSaving && {- disabled: true,- }),- },- });- }- }-- if (isOwner) {- menuActions.push({- id: "delete",- title: "Delete",- attributes: {- destructive: true,- },- image: Platform.select({- ios: "trash",- }),- imageColor: Platform.select({- ios: destructiveMenuIconColor,- }),- });- }-return (<View className={compact ? "flex flex-row gap-3" : "flex flex-row gap-4"}>- {(isArchivePending || isDeletionPending) && <ActivityIndicator />}- {isOwner && (+ {actions.isBusy && <ActivityIndicator />}+ {actions.isOwner && (<PressableonPress={() => {- Haptics.selectionAsync();- favouriteBookmark({- bookmarkId: bookmark.id,- favourited: !bookmark.favourited,- });+ void Haptics.selectionAsync();+ actions.toggleFavourite();}}>- {(variables ? variables.favourited : bookmark.favourited) ? (+ {actions.isFavourited ? (<Star fill="#ebb434" color="#ebb434" size={compact ? 20 : 24} />) : (<Star color="gray" size={compact ? 20 : 24} />@@ -244,44 +31,24 @@ export default function ActionBar({<PressableonPress={() => {- Haptics.selectionAsync();- handleShare();+ void Haptics.selectionAsync();+ actions.share();}}><ShareIcon color="gray" size={compact ? 20 : 24} /></Pressable>- {menuActions.length > 0 && (+ {actions.menuActions.length > 0 && (<MenuViewonPressAction={({ nativeEvent }) => {- Haptics.selectionAsync();- if (- nativeEvent.event === "make-available-offline" ||- nativeEvent.event === "update-offline-copy"- ) {- void offlineAvailability.save();- } else if (nativeEvent.event === "remove-offline-copy") {- offlineAvailability.confirmRemove();- } else if (nativeEvent.event === "delete") {- deleteBookmarkAlert();- } else if (nativeEvent.event === "archive") {- archiveBookmark({- bookmarkId: bookmark.id,- archived: !bookmark.archived,- });- } else if (nativeEvent.event === "manage_list") {- router.push(`/dashboard/bookmarks/${bookmark.id}/manage_lists`);- } else if (nativeEvent.event === "manage_tags") {- router.push(`/dashboard/bookmarks/${bookmark.id}/manage_tags`);- } else if (nativeEvent.event === "edit") {- router.push(`/dashboard/bookmarks/${bookmark.id}/info`);- }+ void Haptics.selectionAsync();+ actions.handleAction(nativeEvent.event);}}- actions={menuActions}+ actions={actions.menuActions}shouldOpenOnLongPress={false}><Ellipsis- onPress={() => Haptics.selectionAsync()}+ onPress={() => void Haptics.selectionAsync()}color="gray"size={compact ? 20 : 24}/>diff --git a/apps/mobile/components/bookmarks/card/BookmarkCard.tsx b/apps/mobile/components/bookmarks/card/BookmarkCard.tsxindex efd30d0d..fd84954f 100644--- a/apps/mobile/components/bookmarks/card/BookmarkCard.tsx+++ b/apps/mobile/components/bookmarks/card/BookmarkCard.tsx@@ -5,6 +5,8 @@ import { createContext, useContext } from "react";import { Pressable, View } from "react-native";import { NotePreview } from "../NotePreview";+import BookmarkCardContextMenu from "./bookmark-card-context-menu";+import type { BookmarkActionController } from "./use-bookmark-actions";export interface BookmarkCardContext {bookmark: ZBookmark;@@ -15,6 +17,7 @@ export interface BookmarkCardContext {compactBody?: React.ReactNode;footerExtras?: React.ReactNode;isOwner?: boolean;+ actions: BookmarkActionController;mediaOnPress?: () => void;bodyOnPress?: () => void;titleOnPress?: () => void;@@ -137,7 +140,8 @@ function Title() {}function Root({ children }: { children: React.ReactNode }) {- return (+ const ctx = useContext(BookmarkCardContext);+ const card = (<ViewclassName="overflow-hidden rounded-xl bg-card"style={{ borderCurve: "continuous" }}@@ -145,6 +149,16 @@ function Root({ children }: { children: React.ReactNode }) {{children}</View>);++ if (!ctx) {+ return card;+ }++ return (+ <BookmarkCardContextMenu actions={ctx.actions}>+ {card}+ </BookmarkCardContextMenu>+ );}const BookmarkCardContainer = {diff --git a/apps/mobile/components/bookmarks/card/bookmark-card-context-menu.ios.tsx b/apps/mobile/components/bookmarks/card/bookmark-card-context-menu.ios.tsxnew file mode 100644index 00000000..35f0b2ac--- /dev/null+++ b/apps/mobile/components/bookmarks/card/bookmark-card-context-menu.ios.tsx@@ -0,0 +1,27 @@+import { MenuView } from "@expo/ui/community/menu";+import type { MenuAction as ExpoMenuAction } from "@expo/ui/community/menu";++import type { BookmarkActionController } from "./use-bookmark-actions";++interface BookmarkCardContextMenuProps {+ actions: BookmarkActionController;+ children: React.ReactElement;+}++export default function BookmarkCardContextMenu({+ actions,+ children,+}: BookmarkCardContextMenuProps) {+ return (+ <MenuView+ actions={actions.contextMenuActions as unknown as ExpoMenuAction[]}+ onPressAction={({ nativeEvent }) => {+ actions.handleAction(nativeEvent.event);+ }}+ shouldOpenOnLongPress+ style={{ alignSelf: "stretch" }}+ >+ {children}+ </MenuView>+ );+}diff --git a/apps/mobile/components/bookmarks/card/bookmark-card-context-menu.tsx b/apps/mobile/components/bookmarks/card/bookmark-card-context-menu.tsxnew file mode 100644index 00000000..9e10d108--- /dev/null+++ b/apps/mobile/components/bookmarks/card/bookmark-card-context-menu.tsx@@ -0,0 +1,12 @@+import type { BookmarkActionController } from "./use-bookmark-actions";++interface BookmarkCardContextMenuProps {+ actions: BookmarkActionController;+ children: React.ReactElement;+}++export default function BookmarkCardContextMenu({+ children,+}: BookmarkCardContextMenuProps) {+ return children;+}diff --git a/apps/mobile/components/bookmarks/card/use-bookmark-actions.ts b/apps/mobile/components/bookmarks/card/use-bookmark-actions.tsnew file mode 100644index 00000000..1dba1403--- /dev/null+++ b/apps/mobile/components/bookmarks/card/use-bookmark-actions.ts@@ -0,0 +1,242 @@+import { useOfflineAvailability } from "@/components/bookmarks/useOfflineAvailability";+import useAppSettings from "@/lib/settings";+import { shareBookmark } from "@/lib/shareBookmark";+import { useMenuIconColors } from "@/lib/useMenuIconColors";+import type { MenuAction } from "@react-native-menu/menu";+import { router } from "expo-router";+import { Alert, Platform } from "react-native";++import {+ useDeleteBookmark,+ useUpdateBookmark,+} from "@karakeep/shared-react/hooks/bookmarks";+import type { ZBookmark } from "@karakeep/shared/types/bookmarks";+import { BookmarkTypes } from "@karakeep/shared/types/bookmarks";++import { useToast } from "../../ui/Toast";++export interface BookmarkActionController {+ isOwner: boolean;+ isBusy: boolean;+ isFavourited: boolean;+ menuActions: MenuAction[];+ contextMenuActions: MenuAction[];+ handleAction: (actionId: string) => void;+ share: () => void;+ toggleFavourite: () => void;+}++export function useBookmarkActions(+ bookmark: ZBookmark,+ isOwner: boolean,+): BookmarkActionController {+ const { toast } = useToast();+ const { settings } = useAppSettings();+ const { menuIconColor, destructiveMenuIconColor } = useMenuIconColors();+ const offlineAvailability = useOfflineAvailability(bookmark.id);+ const supportsOfflineReading =+ bookmark.content.type === BookmarkTypes.LINK ||+ bookmark.content.type === BookmarkTypes.TEXT;++ const onError = () => {+ toast({+ message: "Something went wrong",+ variant: "destructive",+ showProgress: false,+ });+ };++ const { mutate: deleteBookmark, isPending: isDeletionPending } =+ useDeleteBookmark({+ onSuccess: () => {+ toast({+ message: "The bookmark has been deleted!",+ showProgress: false,+ });+ },+ onError,+ });++ const { mutate: favouriteBookmark, variables } = useUpdateBookmark({+ onError,+ });++ const { mutate: archiveBookmark, isPending: isArchivePending } =+ useUpdateBookmark({+ onSuccess: (response) => {+ toast({+ message: `The bookmark has been ${response.archived ? "archived" : "un-archived"}!`,+ showProgress: false,+ });+ },+ onError,+ });++ const isFavourited = Boolean(+ variables ? variables.favourited : bookmark.favourited,+ );++ const toggleFavourite = () => {+ favouriteBookmark({+ bookmarkId: bookmark.id,+ favourited: !bookmark.favourited,+ });+ };++ const share = () => shareBookmark(bookmark, settings, toast);++ const deleteBookmarkAlert = () =>+ Alert.alert(+ "Delete bookmark?",+ "Are you sure you want to delete this bookmark?",+ [+ { text: "Cancel", style: "cancel" },+ {+ text: "Delete",+ onPress: () => deleteBookmark({ bookmarkId: bookmark.id }),+ style: "destructive",+ },+ ],+ );++ const menuActions: MenuAction[] = [];+ if (isOwner) {+ menuActions.push(+ {+ id: "edit",+ title: "Edit",+ image: Platform.select({ ios: "pencil" }),+ imageColor: Platform.select({ ios: menuIconColor }),+ },+ {+ id: "manage_list",+ title: "Manage Lists",+ image: Platform.select({ ios: "list.bullet" }),+ imageColor: Platform.select({ ios: menuIconColor }),+ },+ {+ id: "manage_tags",+ title: "Manage Tags",+ image: Platform.select({ ios: "tag" }),+ imageColor: Platform.select({ ios: menuIconColor }),+ },+ {+ id: "archive",+ title: bookmark.archived ? "Un-archive" : "Archive",+ image: Platform.select({ ios: "archivebox" }),+ imageColor: Platform.select({ ios: menuIconColor }),+ },+ );+ }++ if (supportsOfflineReading) {+ if (offlineAvailability.isAvailableOffline) {+ menuActions.push({+ id: "offline-group",+ title: "Available offline",+ image: Platform.select({ ios: "checkmark.circle" }),+ imageColor: Platform.select({ ios: menuIconColor }),+ subactions: [+ {+ id: "update-offline-copy",+ title: offlineAvailability.isSaving+ ? "Saving offline copy..."+ : "Update offline copy",+ image: Platform.select({ ios: "arrow.clockwise" }),+ imageColor: Platform.select({ ios: menuIconColor }),+ attributes: {+ ...(offlineAvailability.isSaving && { disabled: true }),+ },+ },+ {+ id: "remove-offline-copy",+ title: "Remove offline copy",+ attributes: { destructive: true },+ image: Platform.select({ ios: "trash" }),+ imageColor: Platform.select({ ios: destructiveMenuIconColor }),+ },+ ],+ });+ } else {+ menuActions.push({+ id: "make-available-offline",+ title: offlineAvailability.isSaving+ ? "Saving offline copy..."+ : "Make available offline",+ image: Platform.select({ ios: "arrow.down.circle" }),+ imageColor: Platform.select({ ios: menuIconColor }),+ attributes: {+ ...(offlineAvailability.isSaving && { disabled: true }),+ },+ });+ }+ }++ if (isOwner) {+ menuActions.push({+ id: "delete",+ title: "Delete",+ attributes: { destructive: true },+ image: Platform.select({ ios: "trash" }),+ imageColor: Platform.select({ ios: destructiveMenuIconColor }),+ });+ }++ const contextMenuActions: MenuAction[] = [+ ...(isOwner+ ? [+ {+ id: "toggle-favourite",+ title: isFavourited ? "Remove from Favorites" : "Add to Favorites",+ image: isFavourited ? "star.slash" : "star",+ state: isFavourited ? ("on" as const) : ("off" as const),+ },+ ]+ : []),+ {+ id: "share",+ title: "Share",+ image: "square.and.arrow.up",+ },+ ...menuActions,+ ];++ const handleAction = (actionId: string) => {+ if (actionId === "toggle-favourite") {+ toggleFavourite();+ } else if (actionId === "share") {+ share();+ } else if (+ actionId === "make-available-offline" ||+ actionId === "update-offline-copy"+ ) {+ void offlineAvailability.save();+ } else if (actionId === "remove-offline-copy") {+ offlineAvailability.confirmRemove();+ } else if (actionId === "delete") {+ deleteBookmarkAlert();+ } else if (actionId === "archive") {+ archiveBookmark({+ bookmarkId: bookmark.id,+ archived: !bookmark.archived,+ });+ } else if (actionId === "manage_list") {+ router.push(`/dashboard/bookmarks/${bookmark.id}/manage_lists`);+ } else if (actionId === "manage_tags") {+ router.push(`/dashboard/bookmarks/${bookmark.id}/manage_tags`);+ } else if (actionId === "edit") {+ router.push(`/dashboard/bookmarks/${bookmark.id}/info`);+ }+ };++ return {+ isOwner,+ isBusy: isArchivePending || isDeletionPending,+ isFavourited,+ menuActions,+ contextMenuActions,+ handleAction,+ share,+ toggleFavourite,+ };+}diff --git a/apps/mobile/package.json b/apps/mobile/package.jsonindex fb1c33a3..0e025bf8 100644--- a/apps/mobile/package.json+++ b/apps/mobile/package.json@@ -22,6 +22,7 @@"dependencies": {"@expo/dom-webview": "~56.0.6","@expo/metro-runtime": "~56.0.16",+ "@expo/ui": "56.0.21","@expo/vector-icons": "^15.0.3","@karakeep/shared": "workspace:^0.1.0","@karakeep/shared-react": "workspace:^0.1.0",diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 83d39405..56117654 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -332,6 +332,9 @@ importers:'@expo/metro-runtime':specifier: ~56.0.16version: 56.0.16(@expo/[email protected])([email protected])([email protected]([email protected]))([email protected](patch_hash=7d74c7fbd33e9f3f3f8a8ba47a013acfc098948e39c7faab1232e6edca358ac1)(@babel/[email protected])(@react-native/[email protected](@babel/[email protected]))(@types/[email protected])([email protected]))([email protected])+ '@expo/ui':+ specifier: 56.0.21+ version: 56.0.21(@babel/[email protected])(@types/[email protected](@types/[email protected]))(@types/[email protected])([email protected])([email protected]([email protected]))([email protected](@babel/[email protected])(@react-native/[email protected](@babel/[email protected]))([email protected](patch_hash=7d74c7fbd33e9f3f3f8a8ba47a013acfc098948e39c7faab1232e6edca358ac1)(@babel/[email protected])(@react-native/[email protected](@babel/[email protected]))(@types/[email protected])([email protected]))([email protected]))([email protected](patch_hash=7d74c7fbd33e9f3f3f8a8ba47a013acfc098948e39c7faab1232e6edca358ac1)(@babel/[email protected])(@react-native/[email protected](@babel/[email protected]))(@types/[email protected])([email protected]))([email protected])'@expo/vector-icons':specifier: ^15.0.3version: 15.0.3([email protected])([email protected](patch_hash=7d74c7fbd33e9f3f3f8a8ba47a013acfc098948e39c7faab1232e6edca358ac1)(@babel/[email protected])(@react-native/[email protected](@babel/[email protected]))(@types/[email protected])([email protected]))([email protected])