import { useQuery } from 'react-query' ; import { fetchUserInfo , getUserOnlineStatus , UserBaseInfo , } from '../model/user' ; import { isValidStr } from '../utils/string-helper' ; /**
 * 用户缓存
 */ export function useCachedUserInfo ( userId , refetch = false ) { const { data } = useQuery ( [ 'user' , userId ] , ( ) => { if ( ! isValidStr ( userId ) ) { return { } ; } return fetchUserInfo ( userId ) ; } , { staleTime : 2 * 60 * 60 * 1000 , refetchOnMount : refetch ? 'always' : true } ) ; return data ?? { } ; } /**
 * 用户登录状态
 */ export function useCachedOnlineStatus ( ids ) { const { data } = useQuery ( [ 'onlineStatus' , ids . join ( ',' ) ] , ( ) => getUserOnlineStatus ( ids ) , { staleTime : 10 * 1000 } ) ; return data ?? ids . map ( ( ) => false ) ; }