From ef2a11795ff546dc8f977adca8f48128c1ed2d62 Mon Sep 17 00:00:00 2001 From: Paul Frazee Date: Mon, 1 Jul 2024 19:00:04 -0700 Subject: [PATCH] Wire up progress-guide persistence --- package.json | 2 +- src/components/ProgressGuide/List.tsx | 5 ++- src/state/queries/preferences/const.ts | 4 +++ src/state/queries/preferences/index.ts | 47 ++++++++++++++++++++++++++ src/state/shell/progress-guide.tsx | 28 ++++++++------- yarn.lock | 9 ++--- 6 files changed, 75 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index 8525655136..5f9f03457a 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ "open-analyzer": "EXPO_PUBLIC_OPEN_ANALYZER=1 yarn build-web" }, "dependencies": { - "@atproto/api": "^0.12.22", + "@atproto/api": "^0.12.23", "@bam.tech/react-native-image-resizer": "^3.0.4", "@braintree/sanitize-url": "^6.0.2", "@discord/bottom-sheet": "bluesky-social/react-native-bottom-sheet", diff --git a/src/components/ProgressGuide/List.tsx b/src/components/ProgressGuide/List.tsx index a8219debcf..ad6b14e691 100644 --- a/src/components/ProgressGuide/List.tsx +++ b/src/components/ProgressGuide/List.tsx @@ -4,7 +4,6 @@ import {msg, Trans} from '@lingui/macro' import {useLingui} from '@lingui/react' import { - ProgressGuideName, useProgressGuide, useProgressGuideControls, } from '#/state/shell/progress-guide' @@ -17,10 +16,10 @@ import {ProgressGuideTask} from './Task' export function ProgressGuideList({style}: {style: StyleProp}) { const t = useTheme() const {_} = useLingui() - const guide = useProgressGuide(ProgressGuideName.Like10AndFollow7) + const guide = useProgressGuide('like-10-and-follow-7') const {endProgressGuide} = useProgressGuideControls() - if (guide?.guide === ProgressGuideName.Like10AndFollow7) { + if (guide) { return ( diff --git a/src/state/queries/preferences/const.ts b/src/state/queries/preferences/const.ts index d94edb47e8..2a8c51165e 100644 --- a/src/state/queries/preferences/const.ts +++ b/src/state/queries/preferences/const.ts @@ -34,4 +34,8 @@ export const DEFAULT_LOGGED_OUT_PREFERENCES: UsePreferencesQueryResponse = { userAge: 13, // TODO(pwi) interests: {tags: []}, savedFeeds: [], + bskyAppState: { + queuedNudges: [], + activeProgressGuide: undefined, + }, } diff --git a/src/state/queries/preferences/index.ts b/src/state/queries/preferences/index.ts index 672abfcac5..9bb57fcaf6 100644 --- a/src/state/queries/preferences/index.ts +++ b/src/state/queries/preferences/index.ts @@ -342,3 +342,50 @@ export function useRemoveMutedWordMutation() { }, }) } + +export function useQueueNudgesMutation() { + const queryClient = useQueryClient() + const agent = useAgent() + + return useMutation({ + mutationFn: async (nudges: string | string[]) => { + await agent.bskyAppQueueNudges(nudges) + // triggers a refetch + await queryClient.invalidateQueries({ + queryKey: preferencesQueryKey, + }) + }, + }) +} + +export function useDismissNudgesMutation() { + const queryClient = useQueryClient() + const agent = useAgent() + + return useMutation({ + mutationFn: async (nudges: string | string[]) => { + await agent.bskyAppDismissNudges(nudges) + // triggers a refetch + await queryClient.invalidateQueries({ + queryKey: preferencesQueryKey, + }) + }, + }) +} + +export function useSetActiveProgressGuideMutation() { + const queryClient = useQueryClient() + const agent = useAgent() + + return useMutation({ + mutationFn: async ( + guide: AppBskyActorDefs.BskyAppProgressGuide | undefined, + ) => { + await agent.bskyAppSetActiveProgressGuide(guide) + // triggers a refetch + await queryClient.invalidateQueries({ + queryKey: preferencesQueryKey, + }) + }, + }) +} diff --git a/src/state/shell/progress-guide.tsx b/src/state/shell/progress-guide.tsx index 034010f01c..79bedb0d61 100644 --- a/src/state/shell/progress-guide.tsx +++ b/src/state/shell/progress-guide.tsx @@ -6,23 +6,22 @@ import { ProgressGuideToast, ProgressGuideToastRef, } from '#/components/ProgressGuide/Toast' +import {useSetActiveProgressGuideMutation} from '../queries/preferences' export enum ProgressGuideAction { Like = 'like', Follow = 'follow', } -export enum ProgressGuideName { - Like10AndFollow7 = 'like-10-and-follow-7', -} +type ProgressGuideName = 'like-10-and-follow-7' interface BaseProgressGuide { - guide: ProgressGuideName + guide: string isComplete: boolean + [key: string]: any } interface Like10AndFollow7ProgressGuide extends BaseProgressGuide { - guide: ProgressGuideName.Like10AndFollow7 numLikes: number numFollows: number } @@ -55,10 +54,11 @@ export function useProgressGuideControls() { export function Provider({children}: React.PropsWithChildren<{}>) { const {_} = useLingui() + const {mutate} = useSetActiveProgressGuideMutation() const [activeProgressGuide, setActiveProgressGuide] = React.useState({ - guide: ProgressGuideName.Like10AndFollow7, + guide: 'like-10-and-follow-7', numLikes: 0, numFollows: 0, isComplete: false, @@ -71,17 +71,20 @@ export function Provider({children}: React.PropsWithChildren<{}>) { const controls = React.useMemo(() => { return { startProgressGuide(guide: ProgressGuideName) { - if (guide === ProgressGuideName.Like10AndFollow7) { - setActiveProgressGuide({ - guide: ProgressGuideName.Like10AndFollow7, + if (guide === 'like-10-and-follow-7') { + const guideObj = { + guide: 'like-10-and-follow-7', numLikes: 0, numFollows: 0, isComplete: false, - }) + } + mutate(guideObj) + setActiveProgressGuide(guideObj) } }, endProgressGuide() { + mutate(undefined) setActiveProgressGuide(undefined) }, @@ -90,7 +93,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) { if (guide?.isComplete) { return } - if (guide?.guide === ProgressGuideName.Like10AndFollow7) { + if (guide?.guide === 'like-10-and-follow-7') { if (action === ProgressGuideAction.Like) { guide = { ...guide, @@ -120,10 +123,11 @@ export function Provider({children}: React.PropsWithChildren<{}>) { } } } + mutate(guide) setActiveProgressGuide(guide) }, } - }, [activeProgressGuide, setActiveProgressGuide]) + }, [activeProgressGuide, setActiveProgressGuide, mutate]) return ( diff --git a/yarn.lock b/yarn.lock index d31fa2f635..84318d1a4f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -34,15 +34,16 @@ jsonpointer "^5.0.0" leven "^3.1.0" -"@atproto/api@^0.12.22": - version "0.12.22" - resolved "https://registry.yarnpkg.com/@atproto/api/-/api-0.12.22.tgz#1880a93a0caa4485cd8463bd1e10bf2424b9826c" - integrity sha512-TIXSnf3qqyX40Ei/FkK4H24w+7s5rOc63TPwrGakRBOqIgSNBKOggei8I600fJ/AXB7HO6Vp9tBmDVOt2+021A== +"@atproto/api@^0.12.23": + version "0.12.23" + resolved "https://registry.yarnpkg.com/@atproto/api/-/api-0.12.23.tgz#b3409817d0b981a64f30d16e8257f0fe261338af" + integrity sha512-fgQ30u+q9smX5g41eep7fISSkSAhRkX0inc81PZ82QwcHbFkC8ePaha/KP0CoTaPWKi7EsC89Z/8BEBCJo0oBA== dependencies: "@atproto/common-web" "^0.3.0" "@atproto/lexicon" "^0.4.0" "@atproto/syntax" "^0.3.0" "@atproto/xrpc" "^0.5.0" + await-lock "^2.2.2" multiformats "^9.9.0" tlds "^1.234.0"