Rm abstraction
This commit is contained in:
+14
-12
@@ -22,7 +22,11 @@ import {
|
||||
import {DISCOVER_FEED_URI, DISCOVER_SAVED_FEED} from '#/lib/constants'
|
||||
import {sanitizeDisplayName} from '#/lib/strings/display-names'
|
||||
import {sanitizeHandle} from '#/lib/strings/handles'
|
||||
import {persist, STALE} from '#/state/queries'
|
||||
import {
|
||||
PERSISTED_QUERY_GCTIME,
|
||||
PERSISTED_QUERY_ROOT,
|
||||
STALE,
|
||||
} from '#/state/queries'
|
||||
import {RQKEY as listQueryKey} from '#/state/queries/list'
|
||||
import {usePreferencesQuery} from '#/state/queries/preferences'
|
||||
import {useAgent, useSession} from '#/state/session'
|
||||
@@ -415,7 +419,7 @@ const PWI_DISCOVER_FEED_STUB: SavedFeedSourceInfo = {
|
||||
const createPinnedFeedInfosQueryKeyRoot = (
|
||||
kind: 'pinned' | 'saved',
|
||||
feedUris: string[],
|
||||
) => ['feed-info', kind, feedUris]
|
||||
) => [PERSISTED_QUERY_ROOT, 'feed-info', kind, feedUris]
|
||||
|
||||
export function usePinnedFeedsInfos() {
|
||||
const {hasSession} = useSession()
|
||||
@@ -424,12 +428,11 @@ export function usePinnedFeedsInfos() {
|
||||
const pinnedItems = preferences?.savedFeeds.filter(feed => feed.pinned) ?? []
|
||||
|
||||
return useQuery({
|
||||
...persist(
|
||||
createPinnedFeedInfosQueryKeyRoot(
|
||||
'pinned',
|
||||
pinnedItems.map(f => f.value),
|
||||
),
|
||||
queryKey: createPinnedFeedInfosQueryKeyRoot(
|
||||
'pinned',
|
||||
pinnedItems.map(f => f.value),
|
||||
),
|
||||
gcTime: PERSISTED_QUERY_GCTIME,
|
||||
staleTime: STALE.INFINITY,
|
||||
enabled: !isLoadingPrefs,
|
||||
queryFn: async () => {
|
||||
@@ -533,12 +536,11 @@ export function useSavedFeeds() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useQuery({
|
||||
...persist(
|
||||
createPinnedFeedInfosQueryKeyRoot(
|
||||
'saved',
|
||||
savedItems.map(f => f.value),
|
||||
),
|
||||
queryKey: createPinnedFeedInfosQueryKeyRoot(
|
||||
'saved',
|
||||
savedItems.map(f => f.value),
|
||||
),
|
||||
gcTime: PERSISTED_QUERY_GCTIME,
|
||||
staleTime: STALE.INFINITY,
|
||||
enabled: !isLoadingPrefs,
|
||||
placeholderData: previousData => {
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import {type QueryKey, queryOptions} from '@tanstack/react-query'
|
||||
|
||||
const SECOND = 1e3
|
||||
const MINUTE = SECOND * 60
|
||||
const HOUR = MINUTE * 60
|
||||
@@ -40,28 +38,3 @@ export const STALE = {
|
||||
*/
|
||||
export const PERSISTED_QUERY_ROOT = 'PERSISTED'
|
||||
export const PERSISTED_QUERY_GCTIME = Infinity
|
||||
|
||||
/**
|
||||
* Sets up a query to be persisted automatically by the `PersistQueryClientProvider`.
|
||||
* This automatically adjusts the query key, and sets the `gcTime` to be infinite.
|
||||
*
|
||||
* Be careful when using this, since it will change the query key and may
|
||||
* break any cases where we call `invalidateQueries` or `refetchQueries`
|
||||
* with the old key.
|
||||
*
|
||||
* Also, only use this for queries that are safe to persist between
|
||||
* app launches (like user preferences).
|
||||
*
|
||||
* Use like this:
|
||||
* ```ts
|
||||
* useQuery({
|
||||
* ...persist(['user', userId]),
|
||||
* queryFn: () => fetchUser(userId),
|
||||
* })
|
||||
*/
|
||||
export function persist<T extends QueryKey>(key: T) {
|
||||
return queryOptions({
|
||||
queryKey: [PERSISTED_QUERY_ROOT, ...key],
|
||||
gcTime: PERSISTED_QUERY_GCTIME,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,7 +3,11 @@ import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query'
|
||||
import {z} from 'zod'
|
||||
|
||||
import {MAX_LABELERS} from '#/lib/constants'
|
||||
import {persist, STALE} from '#/state/queries'
|
||||
import {
|
||||
PERSISTED_QUERY_GCTIME,
|
||||
PERSISTED_QUERY_ROOT,
|
||||
STALE,
|
||||
} from '#/state/queries'
|
||||
import {
|
||||
preferencesQueryKey,
|
||||
usePreferencesQuery,
|
||||
@@ -23,6 +27,7 @@ export const labelersInfoQueryKey = (dids: string[]) => [
|
||||
]
|
||||
|
||||
const persistedLabelersDetailedInfoQueryKey = (dids: string[]) => [
|
||||
PERSISTED_QUERY_ROOT,
|
||||
'labelers-detailed-info',
|
||||
dids,
|
||||
]
|
||||
@@ -63,9 +68,9 @@ export function useLabelersInfoQuery({dids}: {dids: string[]}) {
|
||||
export function useLabelersDetailedInfoQuery({dids}: {dids: string[]}) {
|
||||
const agent = useAgent()
|
||||
return useQuery({
|
||||
...persist(persistedLabelersDetailedInfoQueryKey(dids)),
|
||||
enabled: !!dids.length,
|
||||
gcTime: 1000 * 60 * 60 * 6, // 6 hours
|
||||
queryKey: persistedLabelersDetailedInfoQueryKey(dids),
|
||||
gcTime: PERSISTED_QUERY_GCTIME,
|
||||
staleTime: STALE.MINUTES.ONE,
|
||||
queryFn: async () => {
|
||||
const res = await agent.app.bsky.labeler.getServices({
|
||||
|
||||
Reference in New Issue
Block a user