Change size (#4957)
This commit is contained in:
parent
6616a6467e
commit
61f0be705d
11 changed files with 170 additions and 90 deletions
|
@ -1,9 +1,15 @@
|
|||
import {AppBskyActorDefs, AppBskyGraphGetList} from '@atproto/api'
|
||||
import {
|
||||
AppBskyActorDefs,
|
||||
AppBskyGraphDefs,
|
||||
AppBskyGraphGetList,
|
||||
BskyAgent,
|
||||
} from '@atproto/api'
|
||||
import {
|
||||
InfiniteData,
|
||||
QueryClient,
|
||||
QueryKey,
|
||||
useInfiniteQuery,
|
||||
useQuery,
|
||||
} from '@tanstack/react-query'
|
||||
|
||||
import {STALE} from '#/state/queries'
|
||||
|
@ -14,6 +20,7 @@ type RQPageParam = string | undefined
|
|||
|
||||
const RQKEY_ROOT = 'list-members'
|
||||
export const RQKEY = (uri: string) => [RQKEY_ROOT, uri]
|
||||
export const RQKEY_ALL = (uri: string) => [RQKEY_ROOT, uri, 'all']
|
||||
|
||||
export function useListMembersQuery(uri?: string, limit: number = PAGE_SIZE) {
|
||||
const agent = useAgent()
|
||||
|
@ -40,6 +47,38 @@ export function useListMembersQuery(uri?: string, limit: number = PAGE_SIZE) {
|
|||
})
|
||||
}
|
||||
|
||||
export function useAllListMembersQuery(uri?: string) {
|
||||
const agent = useAgent()
|
||||
return useQuery({
|
||||
staleTime: STALE.MINUTES.ONE,
|
||||
queryKey: RQKEY_ALL(uri ?? ''),
|
||||
queryFn: async () => {
|
||||
return getAllListMembers(agent, uri!)
|
||||
},
|
||||
enabled: Boolean(uri),
|
||||
})
|
||||
}
|
||||
|
||||
export async function getAllListMembers(agent: BskyAgent, uri: string) {
|
||||
let hasMore = true
|
||||
let cursor: string | undefined
|
||||
const listItems: AppBskyGraphDefs.ListItemView[] = []
|
||||
// We want to cap this at 6 pages, just for anything weird happening with the api
|
||||
let i = 0
|
||||
while (hasMore && i < 6) {
|
||||
const res = await agent.app.bsky.graph.getList({
|
||||
list: uri,
|
||||
limit: 50,
|
||||
cursor,
|
||||
})
|
||||
listItems.push(...res.data.items)
|
||||
hasMore = Boolean(res.data.cursor)
|
||||
cursor = res.data.cursor
|
||||
}
|
||||
i++
|
||||
return listItems
|
||||
}
|
||||
|
||||
export async function invalidateListMembersQuery({
|
||||
queryClient,
|
||||
uri,
|
||||
|
|
|
@ -16,6 +16,7 @@ import {
|
|||
useQuery,
|
||||
useQueryClient,
|
||||
} from '@tanstack/react-query'
|
||||
import chunk from 'lodash.chunk'
|
||||
|
||||
import {until} from 'lib/async/until'
|
||||
import {createStarterPackList} from 'lib/generate-starterpack'
|
||||
|
@ -200,36 +201,40 @@ export function useEditStarterPackMutation({
|
|||
i.subject.did !== agent.session?.did &&
|
||||
!profiles.find(p => p.did === i.subject.did && p.did),
|
||||
)
|
||||
|
||||
if (removedItems.length !== 0) {
|
||||
await agent.com.atproto.repo.applyWrites({
|
||||
repo: agent.session!.did,
|
||||
writes: removedItems.map(i => ({
|
||||
$type: 'com.atproto.repo.applyWrites#delete',
|
||||
collection: 'app.bsky.graph.listitem',
|
||||
rkey: new AtUri(i.uri).rkey,
|
||||
})),
|
||||
})
|
||||
const chunks = chunk(removedItems, 50)
|
||||
for (const chunk of chunks) {
|
||||
await agent.com.atproto.repo.applyWrites({
|
||||
repo: agent.session!.did,
|
||||
writes: chunk.map(i => ({
|
||||
$type: 'com.atproto.repo.applyWrites#delete',
|
||||
collection: 'app.bsky.graph.listitem',
|
||||
rkey: new AtUri(i.uri).rkey,
|
||||
})),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const addedProfiles = profiles.filter(
|
||||
p => !currentListItems.find(i => i.subject.did === p.did),
|
||||
)
|
||||
|
||||
if (addedProfiles.length > 0) {
|
||||
await agent.com.atproto.repo.applyWrites({
|
||||
repo: agent.session!.did,
|
||||
writes: addedProfiles.map(p => ({
|
||||
$type: 'com.atproto.repo.applyWrites#create',
|
||||
collection: 'app.bsky.graph.listitem',
|
||||
value: {
|
||||
$type: 'app.bsky.graph.listitem',
|
||||
subject: p.did,
|
||||
list: currentStarterPack.list?.uri,
|
||||
createdAt: new Date().toISOString(),
|
||||
},
|
||||
})),
|
||||
})
|
||||
const chunks = chunk(addedProfiles, 50)
|
||||
for (const chunk of chunks) {
|
||||
await agent.com.atproto.repo.applyWrites({
|
||||
repo: agent.session!.did,
|
||||
writes: chunk.map(p => ({
|
||||
$type: 'com.atproto.repo.applyWrites#create',
|
||||
collection: 'app.bsky.graph.listitem',
|
||||
value: {
|
||||
$type: 'app.bsky.graph.listitem',
|
||||
subject: p.did,
|
||||
list: currentStarterPack.list?.uri,
|
||||
createdAt: new Date().toISOString(),
|
||||
},
|
||||
})),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const rkey = parseStarterPackUri(currentStarterPack.uri)!.rkey
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue