Skip to content

Commit

Permalink
Extract query key roots to constants (#3404)
Browse files Browse the repository at this point in the history
* Extract query key roots to constants

* Dedupe labelers-detailed-info

* Align naming
  • Loading branch information
gaearon authored Apr 4, 2024
1 parent b26abf2 commit b783745
Show file tree
Hide file tree
Showing 31 changed files with 198 additions and 139 deletions.
5 changes: 3 additions & 2 deletions src/lib/react-query.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
import {isNative} from '#/platform/detection'

// any query keys in this array will be persisted to AsyncStorage
const STORED_CACHE_QUERY_KEYS = ['labelers-detailed-info']
export const labelersDetailedInfoQueryKeyRoot = 'labelers-detailed-info'
const STORED_CACHE_QUERY_KEY_ROOTS = [labelersDetailedInfoQueryKeyRoot]

focusManager.setEventListener(onFocus => {
if (isNative) {
Expand Down Expand Up @@ -65,7 +66,7 @@ const dehydrateOptions: PersistQueryClientProviderProps['persistOptions']['dehyd
{
shouldDehydrateMutation: (_: any) => false,
shouldDehydrateQuery: query => {
return STORED_CACHE_QUERY_KEYS.includes(String(query.queryKey[0]))
return STORED_CACHE_QUERY_KEY_ROOTS.includes(String(query.queryKey[0]))
},
}

Expand Down
13 changes: 7 additions & 6 deletions src/state/queries/actor-autocomplete.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import React from 'react'
import {AppBskyActorDefs, ModerationOpts, moderateProfile} from '@atproto/api'
import {AppBskyActorDefs, moderateProfile, ModerationOpts} from '@atproto/api'
import {useQuery, useQueryClient} from '@tanstack/react-query'

import {isJustAMute} from '#/lib/moderation'
import {isInvalidHandle} from '#/lib/strings/handles'
import {logger} from '#/logger'
import {getAgent} from '#/state/session'
import {useMyFollowsQuery} from '#/state/queries/my-follows'
import {STALE} from '#/state/queries'
import {useMyFollowsQuery} from '#/state/queries/my-follows'
import {getAgent} from '#/state/session'
import {DEFAULT_LOGGED_OUT_PREFERENCES, useModerationOpts} from './preferences'
import {isInvalidHandle} from '#/lib/strings/handles'
import {isJustAMute} from '#/lib/moderation'

const DEFAULT_MOD_OPTS = {
userDid: undefined,
prefs: DEFAULT_LOGGED_OUT_PREFERENCES.moderationPrefs,
}

export const RQKEY = (prefix: string) => ['actor-autocomplete', prefix]
const RQKEY_ROOT = 'actor-autocomplete'
export const RQKEY = (prefix: string) => [RQKEY_ROOT, prefix]

export function useActorAutocompleteQuery(prefix: string) {
const {data: follows, isFetching} = useMyFollowsQuery()
Expand Down
7 changes: 4 additions & 3 deletions src/state/queries/actor-search.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {AppBskyActorDefs} from '@atproto/api'
import {QueryClient, useQuery} from '@tanstack/react-query'

import {getAgent} from '#/state/session'
import {STALE} from '#/state/queries'
import {getAgent} from '#/state/session'

export const RQKEY = (prefix: string) => ['actor-search', prefix]
const RQKEY_ROOT = 'actor-search'
export const RQKEY = (prefix: string) => [RQKEY_ROOT, prefix]

export function useActorSearch(prefix: string) {
return useQuery<AppBskyActorDefs.ProfileView[]>({
Expand All @@ -26,7 +27,7 @@ export function* findAllProfilesInQueryData(
) {
const queryDatas = queryClient.getQueriesData<AppBskyActorDefs.ProfileView[]>(
{
queryKey: ['actor-search'],
queryKey: [RQKEY_ROOT],
},
)
for (const [_queryKey, queryData] of queryDatas) {
Expand Down
5 changes: 3 additions & 2 deletions src/state/queries/app-passwords.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {ComAtprotoServerCreateAppPassword} from '@atproto/api'
import {useQuery, useQueryClient, useMutation} from '@tanstack/react-query'
import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query'

import {STALE} from '#/state/queries'
import {getAgent} from '../session'

export const RQKEY = () => ['app-passwords']
const RQKEY_ROOT = 'app-passwords'
export const RQKEY = () => [RQKEY_ROOT]

export function useAppPasswordsQuery() {
return useQuery({
Expand Down
31 changes: 17 additions & 14 deletions src/state/queries/feed.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import {
useQuery,
useInfiniteQuery,
InfiniteData,
QueryKey,
useMutation,
} from '@tanstack/react-query'
import {
AtUri,
RichText,
AppBskyFeedDefs,
AppBskyGraphDefs,
AppBskyUnspeccedGetPopularFeedGenerators,
AtUri,
RichText,
} from '@atproto/api'
import {
InfiniteData,
QueryKey,
useInfiniteQuery,
useMutation,
useQuery,
} from '@tanstack/react-query'

import {router} from '#/routes'
import {sanitizeDisplayName} from '#/lib/strings/display-names'
import {sanitizeHandle} from '#/lib/strings/handles'
import {getAgent} from '#/state/session'
import {usePreferencesQuery} from '#/state/queries/preferences'
import {STALE} from '#/state/queries'
import {usePreferencesQuery} from '#/state/queries/preferences'
import {getAgent} from '#/state/session'
import {router} from '#/routes'

export type FeedSourceFeedInfo = {
type: 'feed'
Expand Down Expand Up @@ -56,8 +56,9 @@ export type FeedSourceListInfo = {

export type FeedSourceInfo = FeedSourceFeedInfo | FeedSourceListInfo

const feedSourceInfoQueryKeyRoot = 'getFeedSourceInfo'
export const feedSourceInfoQueryKey = ({uri}: {uri: string}) => [
'getFeedSourceInfo',
feedSourceInfoQueryKeyRoot,
uri,
]

Expand Down Expand Up @@ -216,14 +217,16 @@ const FOLLOWING_FEED_STUB: FeedSourceInfo = {
likeUri: '',
}

const pinnedFeedInfosQueryKeyRoot = 'pinnedFeedsInfos'

export function usePinnedFeedsInfos() {
const {data: preferences, isLoading: isLoadingPrefs} = usePreferencesQuery()
const pinnedUris = preferences?.feeds?.pinned ?? []

return useQuery({
staleTime: STALE.INFINITY,
enabled: !isLoadingPrefs,
queryKey: ['pinnedFeedsInfos', pinnedUris.join(',')],
queryKey: [pinnedFeedInfosQueryKeyRoot, pinnedUris.join(',')],
queryFn: async () => {
let resolved = new Map()

Expand Down
13 changes: 9 additions & 4 deletions src/state/queries/handle.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import React from 'react'
import {useQueryClient, useMutation} from '@tanstack/react-query'
import {useMutation, useQueryClient} from '@tanstack/react-query'

import {getAgent} from '#/state/session'
import {STALE} from '#/state/queries'
import {getAgent} from '#/state/session'

const fetchHandleQueryKey = (handleOrDid: string) => ['handle', handleOrDid]
const fetchDidQueryKey = (handleOrDid: string) => ['did', handleOrDid]
const handleQueryKeyRoot = 'handle'
const fetchHandleQueryKey = (handleOrDid: string) => [
handleQueryKeyRoot,
handleOrDid,
]
const didQueryKeyRoot = 'did'
const fetchDidQueryKey = (handleOrDid: string) => [didQueryKeyRoot, handleOrDid]

export function useFetchHandle() {
const queryClient = useQueryClient()
Expand Down
8 changes: 5 additions & 3 deletions src/state/queries/invites.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import {ComAtprotoServerDefs} from '@atproto/api'
import {useQuery} from '@tanstack/react-query'

import {getAgent} from '#/state/session'
import {STALE} from '#/state/queries'
import {cleanError} from '#/lib/strings/errors'
import {STALE} from '#/state/queries'
import {getAgent} from '#/state/session'

function isInviteAvailable(invite: ComAtprotoServerDefs.InviteCode): boolean {
return invite.available - invite.uses.length > 0 && !invite.disabled
}

const inviteCodesQueryKeyRoot = 'inviteCodes'

export type InviteCodesQueryResponse = Exclude<
ReturnType<typeof useInviteCodesQuery>['data'],
undefined
>
export function useInviteCodesQuery() {
return useQuery({
staleTime: STALE.MINUTES.FIVE,
queryKey: ['inviteCodes'],
queryKey: [inviteCodesQueryKeyRoot],
queryFn: async () => {
const res = await getAgent()
.com.atproto.server.getAccountInviteCodes({})
Expand Down
14 changes: 11 additions & 3 deletions src/state/queries/labeler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@ import {AppBskyLabelerDefs} from '@atproto/api'
import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query'
import {z} from 'zod'

import {labelersDetailedInfoQueryKeyRoot} from '#/lib/react-query'
import {STALE} from '#/state/queries'
import {preferencesQueryKey} from '#/state/queries/preferences'
import {getAgent} from '#/state/session'

export const labelerInfoQueryKey = (did: string) => ['labeler-info', did]
const labelerInfoQueryKeyRoot = 'labeler-info'
export const labelerInfoQueryKey = (did: string) => [
labelerInfoQueryKeyRoot,
did,
]

const labelersInfoQueryKeyRoot = 'labelers-info'
export const labelersInfoQueryKey = (dids: string[]) => [
'labelers-info',
labelersInfoQueryKeyRoot,
dids.slice().sort(),
]

export const labelersDetailedInfoQueryKey = (dids: string[]) => [
'labelers-detailed-info',
labelersDetailedInfoQueryKeyRoot,
dids,
]

Expand Down
9 changes: 5 additions & 4 deletions src/state/queries/list-members.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import {AppBskyActorDefs, AppBskyGraphGetList} from '@atproto/api'
import {
useInfiniteQuery,
InfiniteData,
QueryClient,
QueryKey,
useInfiniteQuery,
} from '@tanstack/react-query'

import {getAgent} from '#/state/session'
import {STALE} from '#/state/queries'
import {getAgent} from '#/state/session'

const PAGE_SIZE = 30
type RQPageParam = string | undefined

export const RQKEY = (uri: string) => ['list-members', uri]
const RQKEY_ROOT = 'list-members'
export const RQKEY = (uri: string) => [RQKEY_ROOT, uri]

export function useListMembersQuery(uri: string) {
return useInfiniteQuery<
Expand Down Expand Up @@ -44,7 +45,7 @@ export function* findAllProfilesInQueryData(
const queryDatas = queryClient.getQueriesData<
InfiniteData<AppBskyGraphGetList.OutputSchema>
>({
queryKey: ['list-members'],
queryKey: [RQKEY_ROOT],
})
for (const [_queryKey, queryData] of queryDatas) {
if (!queryData) {
Expand Down
7 changes: 4 additions & 3 deletions src/state/queries/list-memberships.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@
import {AtUri} from '@atproto/api'
import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query'

import {useSession, getAgent} from '#/state/session'
import {RQKEY as LIST_MEMBERS_RQKEY} from '#/state/queries/list-members'
import {STALE} from '#/state/queries'
import {RQKEY as LIST_MEMBERS_RQKEY} from '#/state/queries/list-members'
import {getAgent, useSession} from '#/state/session'

// sanity limit is SANITY_PAGE_LIMIT*PAGE_SIZE total records
const SANITY_PAGE_LIMIT = 1000
const PAGE_SIZE = 100
// ...which comes 100,000k list members

export const RQKEY = () => ['list-memberships']
const RQKEY_ROOT = 'list-memberships'
export const RQKEY = () => [RQKEY_ROOT]

export interface ListMembersip {
membershipUri: string
Expand Down
18 changes: 10 additions & 8 deletions src/state/queries/list.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import {Image as RNImage} from 'react-native-image-crop-picker'
import {
AtUri,
AppBskyGraphDefs,
AppBskyGraphGetList,
AppBskyGraphList,
AppBskyGraphDefs,
AtUri,
Facet,
} from '@atproto/api'
import {Image as RNImage} from 'react-native-image-crop-picker'
import {useQuery, useMutation, useQueryClient} from '@tanstack/react-query'
import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query'
import chunk from 'lodash.chunk'
import {useSession, getAgent} from '../session'
import {invalidate as invalidateMyLists} from './my-lists'
import {RQKEY as PROFILE_LISTS_RQKEY} from './profile-lists'

import {uploadBlob} from '#/lib/api'
import {until} from '#/lib/async/until'
import {STALE} from '#/state/queries'
import {getAgent, useSession} from '../session'
import {invalidate as invalidateMyLists} from './my-lists'
import {RQKEY as PROFILE_LISTS_RQKEY} from './profile-lists'

export const RQKEY = (uri: string) => ['list', uri]
const RQKEY_ROOT = 'list'
export const RQKEY = (uri: string) => [RQKEY_ROOT, uri]

export function useListQuery(uri?: string) {
return useQuery<AppBskyGraphDefs.ListView, Error>({
Expand Down
7 changes: 4 additions & 3 deletions src/state/queries/my-blocked-accounts.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import {AppBskyActorDefs, AppBskyGraphGetBlocks} from '@atproto/api'
import {
useInfiniteQuery,
InfiniteData,
QueryClient,
QueryKey,
useInfiniteQuery,
} from '@tanstack/react-query'

import {getAgent} from '#/state/session'

export const RQKEY = () => ['my-blocked-accounts']
const RQKEY_ROOT = 'my-blocked-accounts'
export const RQKEY = () => [RQKEY_ROOT]
type RQPageParam = string | undefined

export function useMyBlockedAccountsQuery() {
Expand Down Expand Up @@ -39,7 +40,7 @@ export function* findAllProfilesInQueryData(
const queryDatas = queryClient.getQueriesData<
InfiniteData<AppBskyGraphGetBlocks.OutputSchema>
>({
queryKey: ['my-blocked-accounts'],
queryKey: [RQKEY_ROOT],
})
for (const [_queryKey, queryData] of queryDatas) {
if (!queryData?.pages) {
Expand Down
6 changes: 4 additions & 2 deletions src/state/queries/my-follows.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import {AppBskyActorDefs} from '@atproto/api'
import {useQuery} from '@tanstack/react-query'
import {useSession, getAgent} from '../session'

import {STALE} from '#/state/queries'
import {getAgent, useSession} from '../session'

// sanity limit is SANITY_PAGE_LIMIT*PAGE_SIZE total records
const SANITY_PAGE_LIMIT = 1000
const PAGE_SIZE = 100
// ...which comes 10,000k follows

export const RQKEY = () => ['my-follows']
const RQKEY_ROOT = 'my-follows'
export const RQKEY = () => [RQKEY_ROOT]

export function useMyFollowsQuery() {
const {currentAccount} = useSession()
Expand Down
10 changes: 6 additions & 4 deletions src/state/queries/my-lists.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import {AppBskyGraphDefs} from '@atproto/api'
import {useQuery, QueryClient} from '@tanstack/react-query'
import {QueryClient, useQuery} from '@tanstack/react-query'

import {accumulate} from '#/lib/async/accumulate'
import {useSession, getAgent} from '#/state/session'
import {STALE} from '#/state/queries'
import {getAgent, useSession} from '#/state/session'

export type MyListsFilter =
| 'all'
| 'curate'
| 'mod'
| 'all-including-subscribed'
export const RQKEY = (filter: MyListsFilter) => ['my-lists', filter]

const RQKEY_ROOT = 'my-lists'
export const RQKEY = (filter: MyListsFilter) => [RQKEY_ROOT, filter]

export function useMyListsQuery(filter: MyListsFilter) {
const {currentAccount} = useSession()
Expand Down Expand Up @@ -91,6 +93,6 @@ export function invalidate(qc: QueryClient, filter?: MyListsFilter) {
if (filter) {
qc.invalidateQueries({queryKey: RQKEY(filter)})
} else {
qc.invalidateQueries({queryKey: ['my-lists']})
qc.invalidateQueries({queryKey: [RQKEY_ROOT]})
}
}
Loading

0 comments on commit b783745

Please sign in to comment.