Cache with react query instead of memoize

This commit is contained in:
Sinclair Chen 2022-09-08 12:47:44 -07:00
parent cc8de39eff
commit 07c1693eaf
2 changed files with 11 additions and 7 deletions

View File

@ -1,16 +1,13 @@
import type { MentionOptions } from '@tiptap/extension-mention'
import { ReactRenderer } from '@tiptap/react'
import { searchInAny } from 'common/util/parse'
import { memoize, orderBy } from 'lodash'
import { orderBy } from 'lodash'
import tippy from 'tippy.js'
import { getUsers } from 'web/lib/firebase/users'
import { getCachedUsers } from 'web/hooks/use-users'
import { MentionList } from './mention-list'
type Suggestion = MentionOptions['suggestion']
const users = memoize(getUsers)
users() // prefetch
const beginsWith = (text: string, query: string) =>
text.toLocaleLowerCase().startsWith(query.toLocaleLowerCase())
@ -18,7 +15,9 @@ const beginsWith = (text: string, query: string) =>
export const mentionSuggestion: Suggestion = {
items: async ({ query }) =>
orderBy(
(await users()).filter((u) => searchInAny(query, u.username, u.name)),
(await getCachedUsers()).filter((u) =>
searchInAny(query, u.username, u.name)
),
[
(u) => [u.name, u.username].some((s) => beginsWith(s, query)),
'followerCountCached',

View File

@ -6,7 +6,8 @@ import { useFollows } from './use-follows'
import { useUser } from './use-user'
import { useFirestoreQueryData } from '@react-query-firebase/firestore'
import { DocumentData } from 'firebase/firestore'
import { users, privateUsers } from 'web/lib/firebase/users'
import { users, privateUsers, getUsers } from 'web/lib/firebase/users'
import { QueryClient } from 'react-query'
export const useUsers = () => {
const result = useFirestoreQueryData<DocumentData, User[]>(['users'], users, {
@ -16,6 +17,10 @@ export const useUsers = () => {
return result.data ?? []
}
const q = new QueryClient()
export const getCachedUsers = async () =>
q.fetchQuery(['users'], getUsers, { staleTime: Infinity })
export const usePrivateUsers = () => {
const result = useFirestoreQueryData<DocumentData, PrivateUser[]>(
['private users'],