import { useMemo } from 'react' ; import { GroupInfo , GroupPanel , GroupPanelType } from '../../model/group' ; import { isValidStr } from '../../utils/string-helper' ; import { useAppSelector } from './useAppSelector' ; import { useUnread } from './useUnread' ; import { useUserId } from './useUserInfo' ; /**
 * 获取群组信息
 */ export function useGroupInfo ( groupId ) { return useAppSelector ( ( state ) => state . group . groups [ groupId ] ) ?? null ; } /**
 * 获取群组中所有成员的uuid列表
 */ export function useGroupMemberUUIDs ( groupId ) { const groupInfo = useGroupInfo ( groupId ) ; const members = groupInfo ?. members ?? [ ] ; const groupMemberUUIDs = useMemo ( ( ) => members . map ( ( m ) => m . userId ) , [ members ] ) ; return groupMemberUUIDs ; } /**
 * 获取群组面板信息
 */ export function useGroupPanel ( groupId , panelId ) { const groupInfo = useGroupInfo ( groupId ) ; return useMemo ( ( ) => groupInfo ?. panels . find ( ( p ) => p . id === panelId ) ?? null , [ groupInfo , panelId ] ) ; } param userId param userId export function useIsGroupOwner ( groupId , userId ? ) { const groupInfo = useGroupInfo ( groupId ) ; const selfUserId = useUserId ( ) ; if ( isValidStr ( userId ) ) { return groupInfo ?. owner === userId ; } else { return typeof selfUserId === 'string' && groupInfo ?. owner === selfUserId ; } } param groupId export function useGroupUnread ( groupId ) { const group = useGroupInfo ( groupId ) ; const groupTextPanelIds = ( group ?. panels ?? [ ] ) . filter ( ( panel ) => panel . type === GroupPanelType . TEXT ) . map ( ( p ) => p . id ) ; const unread = useUnread ( groupTextPanelIds ) ; return unread . some ( ( u ) => u === true ) ; } param textPanelId export function useGroupTextPanelUnread ( textPanelId ) { const unread = useUnread ( [ textPanelId ] ) ; return unread [ 0 ] ; }