From 2950b3b8c062f9b80dcc445c410ed35823bded16 Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Mon, 25 Aug 2025 14:32:32 -0500 Subject: [PATCH] Hook up shadow and mutation --- .../PostControls/BookmarkButton.tsx | 52 ++++++++++++++++--- .../PostControls/PostControlButton.tsx | 12 ++++- src/components/PostControls/index.tsx | 2 +- src/state/cache/post-shadow.ts | 3 ++ .../queries/bookmarks/useBookmarkMutation.ts | 46 ++++++++++++++++ 5 files changed, 106 insertions(+), 9 deletions(-) create mode 100644 src/state/queries/bookmarks/useBookmarkMutation.ts diff --git a/src/components/PostControls/BookmarkButton.tsx b/src/components/PostControls/BookmarkButton.tsx index f018bb11db..558f4fcc6c 100644 --- a/src/components/PostControls/BookmarkButton.tsx +++ b/src/components/PostControls/BookmarkButton.tsx @@ -1,28 +1,68 @@ -import {memo} from 'react' -import {type AppBskyFeedDefs, type AppBskyFeedPost} from '@atproto/api' +import {memo, useCallback} from 'react' +import {type AppBskyFeedDefs} from '@atproto/api' import {msg} from '@lingui/macro' import {useLingui} from '@lingui/react' import type React from 'react' +import {useCleanError} from '#/lib/hooks/useCleanError' import {type Shadow} from '#/state/cache/post-shadow' -import {Bookmark} from '#/components/icons/Bookmark' +import {useBookmarkMutation} from '#/state/queries/bookmarks/useBookmarkMutation' +import {useTheme} from '#/alf' +import {Bookmark, BookmarkFilled} from '#/components/icons/Bookmark' import {PostControlButton, PostControlButtonIcon} from './PostControlButton' export const BookmarkButton = memo(function BookmarkButton({ + post, big, }: { post: Shadow big?: boolean - record: AppBskyFeedPost.Record }): React.ReactNode { + const t = useTheme() const {_} = useLingui() + const {mutateAsync: bookmark} = useBookmarkMutation() + const cleanError = useCleanError() + + const {uri, cid, viewer} = post + const isBookmarked = !!viewer?.bookmarked + + const onHandlePress = useCallback(async () => { + try { + if (isBookmarked) { + await bookmark({ + action: 'delete', + uri, + }) + // TODO toast + } else { + await bookmark({ + action: 'create', + uri, + cid, + }) + // TODO toast + } + } catch (e) { + const {raw, clean} = cleanError(e) + console.log(clean || raw || e) + // TODO toast + } + }, [uri, cid, isBookmarked, bookmark, cleanError]) return ( - + label={ + isBookmarked + ? _(msg`Remove this post from your bookmarks`) + : _(msg`Save this post to your bookmarks`) + } + onPress={onHandlePress}> + ) }) diff --git a/src/components/PostControls/PostControlButton.tsx b/src/components/PostControls/PostControlButton.tsx index ae69b1322d..807f24d2d3 100644 --- a/src/components/PostControls/PostControlButton.tsx +++ b/src/components/PostControls/PostControlButton.tsx @@ -102,12 +102,20 @@ export function PostControlButton({ export function PostControlButtonIcon({ icon: Comp, -}: { + style, + ...rest +}: SVGIconProps & { icon: React.ComponentType }) { const {big, color} = useContext(PostControlContext) - return + return ( + + ) } export function PostControlButtonText({style, ...props}: TextProps) { diff --git a/src/components/PostControls/index.tsx b/src/components/PostControls/index.tsx index 278f54e422..27f12f372c 100644 --- a/src/components/PostControls/index.tsx +++ b/src/components/PostControls/index.tsx @@ -284,7 +284,7 @@ let PostControls = ({ a.flex_1, big ? a.gap_sm : a.gap_xs, ]}> - +