diff --git a/apps/mobile/src/icons/star_cute_re.tsx b/apps/mobile/src/icons/star_cute_re.tsx index d9ab4287b..49bfa5ca3 100644 --- a/apps/mobile/src/icons/star_cute_re.tsx +++ b/apps/mobile/src/icons/star_cute_re.tsx @@ -14,7 +14,7 @@ export const StarCuteReIcon = ({ }: StarCuteReIconProps) => { return ( - + ( <> {pressed && ( - { const opacityAnimatedValue = useSharedValue(0) const headerHeight = useHeaderHeight() + + const [isHeaderTitleVisible, setIsHeaderTitleVisible] = useState(true) + useEffect(() => { const id = scrollY.addListener((value) => { if (value.value > titleHeight + headerHeight) { opacityAnimatedValue.value = withTiming(1, { duration: 100 }) + setIsHeaderTitleVisible(true) } else { opacityAnimatedValue.value = withTiming(0, { duration: 100 }) + setIsHeaderTitleVisible(false) } }) @@ -174,18 +195,22 @@ const EntryTitle = ({ title, entryId }: { title: string; entryId: string }) => { headerShown headerRight={useCallback( () => ( - + ), - [entryId], + [entryId, opacityAnimatedValue, isHeaderTitleVisible], )} headerTitle={() => ( - {title} - + )} /> = ({ mediaList, id ) } -const HeaderRightActions = ({ entryId }: { entryId: string }) => { - return +const HeaderRightActions = (props: HeaderRightActionsProps) => { + return } interface HeaderRightActionsProps { entryId: string + titleOpacityShareValue: SharedValue + isHeaderTitleVisible: boolean } -const HeaderRightActionsImpl = ({ entryId }: HeaderRightActionsProps) => { +const HeaderRightActionsImpl = ({ + entryId, + titleOpacityShareValue, + isHeaderTitleVisible, +}: HeaderRightActionsProps) => { const labelColor = useColor("label") + const isStarred = useIsEntryStarred(entryId) const entry = useEntry(entryId, (entry) => { if (!entry) return return { url: entry.url, + feedId: entry.feedId, + title: entry.title, } }) + const feed = useFeed(entry?.feedId as string, (feed) => { + return { + feedId: feed.id, + } + }) + const subscription = useSubscription(feed?.feedId as string) + + const handleToggleStar = () => { + if (!entry) return + if (!feed) return + if (!subscription) return + if (isStarred) collectionSyncService.unstarEntry(entryId) + else + collectionSyncService.starEntry({ + entryId, + feedId: feed.feedId, + view: subscription.view, + }) + } + + const handleShare = () => { + if (!entry) return + Share.share({ + title: entry.title!, + url: entry.url!, + }) + } return ( - + + { + return { + opacity: interpolate(titleOpacityShareValue.value, [0, 1], [1, 0]), + } + })} + className="absolute right-[32] flex-row gap-4" + > + {!!subscription && ( + + {isStarred ? : } + + )} + + + + + + + + {isHeaderTitleVisible && ( + + + {isStarred ? "Unstar" : "Star"} + + + Share + + + )} { if (!entry?.url) return Clipboard.setString(entry.url) + toast.info("Link copied to clipboard") }} > Copy Link diff --git a/apps/mobile/src/store/collection/store.ts b/apps/mobile/src/store/collection/store.ts index 217db356f..6fd44a0a0 100644 --- a/apps/mobile/src/store/collection/store.ts +++ b/apps/mobile/src/store/collection/store.ts @@ -22,26 +22,62 @@ export const useCollectionStore = createZustandStore("collectio const set = useCollectionStore.setState class CollectionSyncService { - async starEntry(collection: CollectionSchema, view: FeedViewType) { - await apiClient.collections.$post({ - json: { - entryId: collection.entryId, - view, - }, + async starEntry({ + entryId, + feedId, + view, + }: { + entryId: string + feedId: string + view: FeedViewType + }) { + const tx = createTransaction() + tx.store(async () => { + await collectionActions.upsertMany([ + { + createdAt: new Date().toISOString(), + entryId, + feedId, + view, + }, + ]) + }) + tx.request(async () => { + await apiClient.collections.$post({ + json: { + entryId, + view, + }, + }) + }) + tx.rollback(() => { + collectionActions.delete(entryId) }) - await collectionActions.upsertMany([collection]) - return + await tx.run() } - async unstarEntry(collection: CollectionSchema) { - await apiClient.collections.$delete({ - json: { - entryId: collection.entryId, - }, + async unstarEntry(entryId: string) { + const tx = createTransaction() + + const snapshot = useCollectionStore.getState().collections[entryId] + tx.store(() => { + collectionActions.delete(entryId) + }) + tx.request(async () => { + await apiClient.collections.$delete({ + json: { + entryId, + }, + }) }) - await collectionActions.delete(collection.entryId) + tx.rollback(() => { + if (!snapshot) return + collectionActions.upsertMany([snapshot]) + }) + + await tx.run() } } diff --git a/apps/mobile/src/store/feed/hooks.ts b/apps/mobile/src/store/feed/hooks.ts index e985d7e4b..7a9a65a3f 100644 --- a/apps/mobile/src/store/feed/hooks.ts +++ b/apps/mobile/src/store/feed/hooks.ts @@ -1,11 +1,27 @@ import { useQuery } from "@tanstack/react-query" +import { useCallback } from "react" import { feedSyncServices, useFeedStore } from "./store" +import type { FeedModel } from "./types" -export const useFeed = (id: string) => { - return useFeedStore((state) => { - return state.feeds[id] - }) +const defaultSelector = (feed: FeedModel) => feed +export function useFeed(id: string): FeedModel | undefined +export function useFeed(id: string, selector: (feed: FeedModel) => T): T | undefined +export function useFeed( + id: string, + // @ts-expect-error + selector: (feed: FeedModel) => T = defaultSelector, +): T | undefined { + return useFeedStore( + useCallback( + (state) => { + const feed = state.feeds[id] + if (!feed) return + return selector(feed) + }, + [id], + ), + ) } export const usePrefetchFeed = (id: string, options?: { enabled?: boolean }) => {