1- import { useEffect , useRef , useState } from 'react'
2- import { useEffectCheckEquality } from './use-effect-check-equality'
3- import { uniqBy , uniq } from 'lodash'
4- import { usePersistentLocalState } from 'web/hooks/use-persistent-local-state'
5- import { filterDefined } from 'common/util/array'
6- import { usePersistentInMemoryState } from './use-persistent-in-memory-state'
7- import {
8- DisplayUser ,
9- getDisplayUsers ,
10- getFullUserById ,
11- } from 'web/lib/supabase/users'
12- import { FullUser } from 'common/api/user-types'
1+ import { useEffect , useState } from 'react'
2+ import { useEffectCheckEquality } from './use-effect-check-equality'
3+ import { uniq , uniqBy } from 'lodash'
4+ import { usePersistentLocalState } from 'web/hooks/use-persistent-local-state'
5+ import { filterDefined } from 'common/util/array'
6+ import { usePersistentInMemoryState } from './use-persistent-in-memory-state'
7+ import { DisplayUser , getDisplayUsers , getFullUserById , } from 'web/lib/supabase/users'
8+ import { FullUser } from 'common/api/user-types'
139
1410export function useUserById ( userId : string | undefined ) {
1511 const [ user , setUser ] = usePersistentInMemoryState <
@@ -25,8 +21,8 @@ export function useUserById(userId: string | undefined) {
2521 return user
2622}
2723
28- const cache = new Map < string , DisplayUser | null > ( )
29-
24+ // const cache = new Map<string, DisplayUser | null>()
25+ //
3026// export function useDisplayUserById(userId: string | undefined) {
3127// const [user, setUser] = usePersistentInMemoryState<
3228// DisplayUser | null | undefined
@@ -51,45 +47,48 @@ const cache = new Map<string, DisplayUser | null>()
5147// return user
5248// }
5349
54- export function useUsers ( userIds : string [ ] ) {
55- const [ users , setUsers ] = useState < ( DisplayUser | null ) [ ] | undefined > (
56- undefined
57- )
58-
59- const requestIdRef = useRef ( 0 )
60- useEffectCheckEquality ( ( ) => {
61- const requestId = ++ requestIdRef . current
62-
63- const missing = userIds . filter ( ( id ) => ! cache . has ( id ) )
64-
65- getDisplayUsers ( missing ) . then ( ( users ) => {
66- users . forEach ( ( user ) => {
67- cache . set ( user . id , user )
68- } )
69- if ( requestId !== requestIdRef . current ) return
70- setUsers ( userIds . map ( ( id ) => cache . get ( id ) ?? null ) )
71- } )
72- } , [ userIds ] )
73-
74- return users
75- }
50+ // export function useUsers(userIds: string[]) {
51+ // const [users, setUsers] = useState<(DisplayUser | null)[] | undefined>(
52+ // undefined
53+ // )
54+ //
55+ // const requestIdRef = useRef(0)
56+ // useEffectCheckEquality(() => {
57+ // const requestId = ++requestIdRef.current
58+ //
59+ // const missing = userIds.filter((id) => !cache.has(id))
60+ //
61+ // getDisplayUsers(missing).then((users) => {
62+ // users.forEach((user) => {
63+ // cache.set(user.id, user)
64+ // })
65+ // if (requestId !== requestIdRef.current) return
66+ // setUsers(userIds.map((id) => cache.get(id) ?? null))
67+ // })
68+ // }, [userIds])
69+ //
70+ // return users
71+ // }
7672
7773// TODO: decide whether in-memory or in-localstorage is better and stick to it
7874
7975export function useUsersInStore (
80- userIds : string [ ] ,
76+ userIds : ( string | null ) [ ] ,
8177 key : string ,
8278 limit ?: number
8379) {
80+ const validUserIds = Array . from ( new Set ( userIds . filter ( ( id ) : id is string => ! ! id ) ) )
81+
8482 const [ users , setUsers ] = usePersistentLocalState < DisplayUser [ ] | undefined > (
8583 undefined ,
86- 'use-users-in-local-storage' + key
84+ 'use-users-in-local-storage- ' + key
8785 )
8886
8987 // Fetch all users at least once on load.
9088 const [ userIdsFetched , setUserIdsFetched ] = useState < string [ ] > ( [ ] )
9189 const fetchedSet = new Set ( userIdsFetched )
92- const userIdsNotFetched = userIds . filter ( ( id ) => ! fetchedSet . has ( id ) )
90+
91+ const userIdsNotFetched = validUserIds . filter ( ( id ) => ! fetchedSet . has ( id ) )
9392 const userIdsToFetch = limit
9493 ? userIdsNotFetched . slice ( 0 , limit )
9594 : userIdsNotFetched
@@ -104,5 +103,12 @@ export function useUsersInStore(
104103 } )
105104 } , [ userIdsToFetch ] )
106105
107- return users ?. filter ( ( user ) => userIds . includes ( user ?. id ) )
106+ return users ?. filter ( ( user ) => validUserIds . includes ( user ?. id ) )
107+ }
108+
109+ export function useUserInStore ( userId : string | null ) {
110+ if ( ! userId ) return null // early return avoids invalid key + hook calls are safe
111+
112+ const users = useUsersInStore ( userId ? [ userId ] : [ ] , userId ?? 'empty' )
113+ return users ?. [ 0 ] ?? null
108114}
0 commit comments