mirror of https://github.com/msgbyte/tailchat
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
943 B
TypeScript
27 lines
943 B
TypeScript
import { getCachedUserInfo, isValidStr, useAsync } from '../../index';
|
|
import type { ChatConverseState } from '../slices/chat';
|
|
import { useUserId } from './useUserInfo';
|
|
|
|
export function useDMConverseName(converse: ChatConverseState) {
|
|
const userId = useUserId();
|
|
const otherConverseMembers = converse.members.filter((m) => m !== userId); // 成员Id
|
|
const len = otherConverseMembers.length;
|
|
const { value: otherMembersInfo = [] } = useAsync(() => {
|
|
return Promise.all(
|
|
otherConverseMembers.map((userId) => getCachedUserInfo(userId))
|
|
);
|
|
}, [otherConverseMembers.join(',')]);
|
|
|
|
if (isValidStr(converse.name)) {
|
|
return converse.name;
|
|
}
|
|
|
|
if (len === 1) {
|
|
return otherMembersInfo[0]?.nickname ?? '';
|
|
} else if (len === 2) {
|
|
return `${otherMembersInfo[0]?.nickname}, ${otherMembersInfo[1]?.nickname}`;
|
|
} else {
|
|
return `${otherMembersInfo[0]?.nickname}, ${otherMembersInfo[1]?.nickname} ...`;
|
|
}
|
|
}
|