Something went wrong. Try again.
Retro Bulletin Board Systems on atproto. Web app and TUI.
lazy mirror of alyraffauf/atbbs atbbs.xyz
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159/** Query-key factories. Every useQuery/useMutation in the app goes through * one of these so query keys live in one place. */
import { infiniteQueryOptions, queryOptions } from "@tanstack/react-query";import { fetchIdentityDoc, fetchAvatarUrl, fetchBacklinkCount, getBacklinkCountsBatch,} from "./atproto";import { PIN } from "./lexicon";import { STALE_SLOW } from "./queryClient";import { resolveBBS } from "./bbs";import { fetchNews } from "./news";import { fetchProfile } from "./profile";import { fetchMyThreads } from "./mythreads";import { fetchActivity } from "./activity";import { fetchPins } from "./pins";import { fetchDiscovery } from "./discovery";import { fetchHomeSysopInfo } from "./home";import { fetchSysopModeration } from "./sysopModeration";import { fetchBBSModeration } from "./bbsModeration";import { hydrateThreadPage } from "./boardThreads";import { fetchThreadRefs, fetchThreadRoot, hydrateReplyPage } from "./thread";import type { BacklinkRef } from "./atproto";
// Shared by slow-changing queries: 5-minute staleTime, and skip the// "refetch on every mount" default that live queries use.const slowQueryOpts = { staleTime: STALE_SLOW, refetchOnMount: true } as const;
export const bbsQuery = (handle: string) => queryOptions({ ...slowQueryOpts, queryKey: ["bbs", handle] as const, queryFn: () => resolveBBS(handle), });
export const newsQuery = (bbsDid: string) => queryOptions({ queryKey: ["news", bbsDid] as const, queryFn: () => fetchNews(bbsDid), });
export const identityQuery = (identifier: string) => queryOptions({ ...slowQueryOpts, queryKey: ["identity", identifier] as const, queryFn: () => fetchIdentityDoc(identifier), });
export const avatarQuery = (did: string) => queryOptions({ ...slowQueryOpts, queryKey: ["avatar", did] as const, queryFn: () => fetchAvatarUrl(did), });
export const backlinkCountQuery = (subject: string, source: string) => queryOptions({ queryKey: ["backlink-count", source, subject] as const, queryFn: () => fetchBacklinkCount(subject, source), });
export const profileQuery = (handle: string) => queryOptions({ ...slowQueryOpts, queryKey: ["profile", handle] as const, queryFn: () => fetchProfile(handle), });
export const myThreadsQuery = (pdsUrl: string, did: string) => queryOptions({ queryKey: ["my-threads", did] as const, queryFn: () => fetchMyThreads(pdsUrl, did), });
export const activityQuery = (pdsUrl: string, did: string) => queryOptions({ queryKey: ["activity", did] as const, queryFn: () => fetchActivity(did, pdsUrl), });
export const pinsQuery = (pdsUrl: string, did: string) => queryOptions({ queryKey: ["pins", did] as const, queryFn: () => fetchPins(pdsUrl, did), });
export const discoveryQuery = () => queryOptions({ queryKey: ["discovery"] as const, queryFn: fetchDiscovery, });
export const pinCountsQuery = (dids: string[]) => queryOptions({ queryKey: ["pin-counts", [...dids].sort()] as const, queryFn: () => getBacklinkCountsBatch(dids, `${PIN}:did`), enabled: dids.length > 0, });
export const homeSysopQuery = (did: string) => queryOptions({ queryKey: ["home-sysop", did] as const, queryFn: () => fetchHomeSysopInfo(did), });
export const sysopModerationQuery = (pdsUrl: string, did: string) => queryOptions({ queryKey: ["sysop-moderation", did] as const, queryFn: () => fetchSysopModeration(pdsUrl, did), });
export const bbsModerationQuery = (pdsUrl: string, did: string) => queryOptions({ queryKey: ["bbs-moderation", did] as const, queryFn: () => fetchBBSModeration(pdsUrl, did), });
export const boardThreadsInfiniteQuery = (bbsDid: string, slug: string) => infiniteQueryOptions({ queryKey: ["board-threads", bbsDid, slug] as const, queryFn: ({ pageParam }: { pageParam: string | undefined }) => hydrateThreadPage(bbsDid, slug, pageParam), initialPageParam: undefined as string | undefined, getNextPageParam: (last) => last.cursor ?? undefined, refetchOnMount: "always", });
export const threadRefsQuery = (threadUri: string) => queryOptions({ queryKey: ["thread-refs", threadUri] as const, queryFn: () => fetchThreadRefs(threadUri), });
export const threadRootQuery = (did: string, tid: string) => queryOptions({ queryKey: ["thread-root", did, tid] as const, queryFn: () => fetchThreadRoot(did, tid), });
export const threadPageQuery = ( threadUri: string, page: number, pageRefs: BacklinkRef[],) => queryOptions({ // Fingerprint is part of the key so that when the thread-refs cache // gets updated (new replies, deletes), this page's cache entry gets // a new key and refetches — rather than serving a stale hydration. queryKey: [ "thread-page", threadUri, page, pageRefs.map((ref) => ref.rkey).join("/"), ] as const, queryFn: () => hydrateReplyPage(pageRefs), });