Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248import {useEffect, useRef} from 'react'import {AtUri} from '@atproto/syntax'import { type InfiniteData, type QueryClient, type QueryKey,} from '@tanstack/react-query'
import {isSpaceRecordUri} from '#/lib/api/space-uri'import {app} from '#/lexicons'import * as bsky from '#/types/bsky'
/** * The appview does its own `fillPage`, and defaults to 10 pages. Previously * the frontend tried up to 50 pages, thus the MAX_ATTEMPTS of 5 is a * reasonable compromise to match pre-existing behavior and without blowing up * our backend. */const MAX_ATTEMPTS = 5
type AutoPaginationQuery = { data?: {pageParams: unknown[]} isLoading: boolean isRefetching: boolean isFetchingNextPage: boolean hasNextPage: boolean fetchNextPage: () => Promise<unknown>}
export function useAutoPagination( query: AutoPaginationQuery, itemCount: number, pageSize: number,) { const lastItemCount = useRef(0) const lastPageParams = useRef(query.data?.pageParams) const wantedItemCount = useRef(pageSize) const attemptCount = useRef(0)
useEffect(() => { const cursorOf = (param: unknown) => param && typeof param === 'object' && 'cursor' in param ? param.cursor : param const pageParams = query.data?.pageParams const previousPageParams = lastPageParams.current const continuedPagination = pageParams && previousPageParams && pageParams.length > previousPageParams.length && previousPageParams.every((param, index) => Object.is(cursorOf(param), cursorOf(pageParams[index])), ) if ( pageParams !== previousPageParams && previousPageParams && !continuedPagination ) { wantedItemCount.current = pageSize attemptCount.current = 0 } lastPageParams.current = pageParams
if (itemCount !== lastItemCount.current) { attemptCount.current = 0 if (itemCount < lastItemCount.current) { wantedItemCount.current = Math.max(itemCount, pageSize) } lastItemCount.current = itemCount }
if (query.isLoading || query.isRefetching) { wantedItemCount.current = pageSize attemptCount.current = 0 } else if (query.isFetchingNextPage) { if (itemCount > wantedItemCount.current) { wantedItemCount.current = itemCount + pageSize } } else if (query.hasNextPage) { if (itemCount < wantedItemCount.current) { const currentCursor = cursorOf(pageParams?.at(-1)) const repeatedCursor = pageParams ?.slice(0, -1) .some(param => Object.is(cursorOf(param), currentCursor)) if (repeatedCursor) return attemptCount.current++ if (attemptCount.current < MAX_ATTEMPTS) { void query.fetchNextPage() } } else { attemptCount.current = 0 } } }, [itemCount, pageSize, query])}
export type StructuredQueryKey<T extends Record<string, unknown>> = readonly [ string, T, { persistedVersion?: number },]
/** * Helper method to ensure consistent query keys and key ordering */export function createQueryKey<T extends Record<string, unknown>>( /** * The query key root. All queries must have a root. */ root: string, /** * Any arguments the query depends on, and if changed, should result in the query being refetched. */ args: T, options: { /** * If provided, this indicates that the query is persisted and the version * of the persisted query format. * * This is used to ensure that when we make breaking changes to the * persisted query format, we can increment the version and avoid trying to * read old persisted queries with the new format. * * If you're persisting your queries, you probably want to set `gcTime: * GCTIME.INFINITY` for this query, otherwise it'll get busted immediately * after being persisted. */ persistedVersion?: number } = {},): StructuredQueryKey<T> { return [root, args, options] as const}
export function isQueryPersisted( queryKey: QueryKey,): queryKey is StructuredQueryKey<Record<string, unknown>> { return ( Array.isArray(queryKey) && queryKey.length === 3 && typeof queryKey[0] === 'string' && typeof queryKey[1] === 'object' && queryKey[1] !== null && typeof queryKey[2] === 'object' && queryKey[2] !== null && 'persistedVersion' in queryKey[2] && typeof queryKey[2].persistedVersion === 'number' )}
export async function truncateAndInvalidate<T = any>( queryClient: QueryClient, queryKey: QueryKey,) { queryClient.setQueriesData<InfiniteData<T>>({queryKey}, data => { if (data) { return { pageParams: data.pageParams.slice(0, 1), pages: data.pages.slice(0, 1), } } return data }) return queryClient.invalidateQueries({queryKey})}
// Given an AtUri, this function will check if the AtUri matches a// hit regardless of whether the AtUri uses a DID or handle as a host.//// AtUri should be the URI that is being searched for, while currentUri// is the URI that is being checked. currentAuthor is the author// of the currentUri that is being checked.export function didOrHandleUriMatches( atUri: AtUri, record: {uri: string; author: app.bsky.actor.defs.ProfileViewBasic},) { if (atUri.host.startsWith('did:')) { return atUri.href === record.uri }
return atUri.host === record.author.handle && record.uri.endsWith(atUri.rkey)}
/** * A predicate matching cached posts against a target URI, for shadow-cache * updates (like, delete, ...). * * A permissioned-space record URI is not an at-uri: `AtUri` misparses it, so * its `host` becomes the space authority and its `rkey` the space type. The * handle branch of `didOrHandleUriMatches` would then compare the wrong * segments and could match a different post entirely, which is why the space * form is matched by exact equality — it is always fully qualified — and never * constructed as an `AtUri` at all. */export function makeUriMatcher(uri: string) { if (isSpaceRecordUri(uri)) { return (record: { uri: string author: app.bsky.actor.defs.ProfileViewBasic }) => record.uri === uri } const atUri = new AtUri(uri) return (record: { uri: string author: app.bsky.actor.defs.ProfileViewBasic }) => didOrHandleUriMatches(atUri, record)}
export function getEmbeddedPost( v: unknown,): app.bsky.embed.record.ViewRecord | undefined { if (bsky.isType(app.bsky.embed.record.view, v)) { if ( bsky.isType(app.bsky.embed.record.viewRecord, v.record) && bsky.isType(app.bsky.feed.post, v.record.value) ) { return v.record } } if (bsky.isType(app.bsky.embed.recordWithMedia.view, v)) { if ( bsky.isType(app.bsky.embed.record.viewRecord, v.record.record) && bsky.isType(app.bsky.feed.post, v.record.record.value) ) { return v.record.record } }}
export function embedViewRecordToPostView( v: app.bsky.embed.record.ViewRecord,): app.bsky.feed.defs.PostView { return { uri: v.uri, cid: v.cid, author: v.author, record: v.value, indexedAt: v.indexedAt, labels: v.labels, embed: v.embeds?.[0], likeCount: v.likeCount, quoteCount: v.quoteCount, replyCount: v.replyCount, repostCount: v.repostCount, }}