From a1880ac1ba306e320e1ac1d4e7092db362c69b68 Mon Sep 17 00:00:00 2001 From: Hailey Date: Wed, 19 Jun 2024 15:53:00 -0700 Subject: [PATCH] add mutation for creating pack --- src/state/queries/actor-starter-packs.ts | 2 +- src/state/queries/starter-pack.ts | 93 +++++++++++++++++++++++- 2 files changed, 93 insertions(+), 2 deletions(-) diff --git a/src/state/queries/actor-starter-packs.ts b/src/state/queries/actor-starter-packs.ts index 3aff90761c..9de80b07de 100644 --- a/src/state/queries/actor-starter-packs.ts +++ b/src/state/queries/actor-starter-packs.ts @@ -9,7 +9,7 @@ import { import {useAgent} from 'state/session' const RQKEY_ROOT = 'actor-starter-packs' -const RQKEY = (did?: string) => [RQKEY_ROOT, did] +export const RQKEY = (did?: string) => [RQKEY_ROOT, did] export function useActorStarterPacksQuery({did}: {did?: string}) { const agent = useAgent() diff --git a/src/state/queries/starter-pack.ts b/src/state/queries/starter-pack.ts index 0c3416d640..4f700295d9 100644 --- a/src/state/queries/starter-pack.ts +++ b/src/state/queries/starter-pack.ts @@ -1,10 +1,28 @@ +import { + AppBskyActorDefs, + AppBskyFeedDefs, + AppBskyGraphDefs, + AppBskyGraphGetList, + BskyAgent, +} from '@atproto/api' import {StarterPackView} from '@atproto/api/dist/client/types/app/bsky/graph/defs' -import {QueryClient, useQuery} from '@tanstack/react-query' +import { + QueryClient, + useMutation, + useQuery, + useQueryClient, +} from '@tanstack/react-query' +import {until} from 'lib/async/until' +import {createStarterPackList} from 'lib/generate-starterpack' import { httpStarterPackUriToAtUri, parseStarterPackUri, } from 'lib/strings/starter-pack' +import { + invalidateActorStarterPacksQuery, + RQKEY, +} from 'state/queries/actor-starter-packs' import {useAgent} from 'state/session' const RQKEY_ROOT = 'starter-pack' @@ -57,3 +75,76 @@ export async function invalidateStarterPack({ }) { await queryClient.invalidateQueries({queryKey: RQKEY(did, rkey)}) } + +interface UseCreateStarterPackMutationParams { + name: string + description?: string + descriptionFacets: [] + profiles: AppBskyActorDefs.ProfileViewBasic[] + feeds?: AppBskyFeedDefs.GeneratorView[] +} + +export function useCreateStarterPackMutation({ + onSuccess, +}: { + onSuccess: () => void + onError: (e: Error) => void +}) { + const queryClient = useQueryClient() + const agent = useAgent() + + return useMutation< + {uri: string; cid: string}, + Error, + UseCreateStarterPackMutationParams + >({ + mutationFn: async params => { + let listRes + const listRes = await createStarterPackList({...params, agent}) + return await agent.app.bsky.graph.starterpack.create( + { + repo: agent.session?.did, + validate: false, // TODO remove + }, + { + ...params, + list: listRes.uri, + createdAt: new Date().toISOString(), + }, + ) + }, + onSuccess: async data => { + await whenAppViewReady( + agent, + data.uri, + (v?: AppBskyGraphDefs.StarterPackView) => { + return typeof v.uri === 'string' + }, + ) + await invalidateActorStarterPacksQuery({ + queryClient, + did: agent.session?.did, + }) + onSuccess() + }, + onError: async error => { + onError(error) + }, + }) +} + +async function whenAppViewReady( + agent: BskyAgent, + uri: string, + fn: (res?: AppBskyGraphGetList.Response) => boolean, +) { + await until( + 5, // 5 tries + 1e3, // 1s delay between tries + fn, + () => + agent.app.bsky.graph.getStarterPack({ + starterPack: uri, + }), + ) +}