mirror of https://github.com/msgbyte/tailchat
feat: add friend nickname support in everywhere
parent
36608ed8f3
commit
20c16adeec
@ -1,17 +1,19 @@
|
|||||||
import { getDMConverseName } from '../../helper/converse-helper';
|
import { getDMConverseName } from '../../helper/converse-helper';
|
||||||
import { isValidStr, useAsync } from '../../index';
|
import { isValidStr, useAppSelector, useAsync } from '../../index';
|
||||||
import type { ChatConverseState } from '../slices/chat';
|
import type { ChatConverseState } from '../slices/chat';
|
||||||
import { useUserId } from './useUserInfo';
|
import { useUserId } from './useUserInfo';
|
||||||
|
import type { FriendInfo } from '../slices/user';
|
||||||
|
|
||||||
export function useDMConverseName(converse: ChatConverseState) {
|
export function useDMConverseName(converse: ChatConverseState) {
|
||||||
const userId = useUserId();
|
const userId = useUserId();
|
||||||
|
const friends: FriendInfo[] = useAppSelector((state) => state.user.friends);
|
||||||
const { value: name = '' } = useAsync(async () => {
|
const { value: name = '' } = useAsync(async () => {
|
||||||
if (!isValidStr(userId)) {
|
if (!isValidStr(userId)) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
return getDMConverseName(userId, converse);
|
return getDMConverseName(userId, converse);
|
||||||
}, [userId, converse.name, converse.members.join(',')]);
|
}, [userId, converse.name, converse.members.join(','), friends]);
|
||||||
|
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,33 @@
|
|||||||
|
import { useMemo } from 'react';
|
||||||
|
import { buildFriendNicknameMap } from '../../helper/converse-helper';
|
||||||
|
import { isValidStr } from '../../utils/string-helper';
|
||||||
|
import { useAppSelector } from './useAppSelector';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取好友自定义的昵称
|
||||||
|
* 用于覆盖原始昵称
|
||||||
|
*
|
||||||
|
* @param userId 用户id
|
||||||
|
*/
|
||||||
|
export function useFriendNickname(userId: string): string | null {
|
||||||
|
const nickname = useAppSelector(
|
||||||
|
(state) => state.user.friends.find((f) => f.id === userId)?.nickname
|
||||||
|
);
|
||||||
|
|
||||||
|
if (isValidStr(nickname)) {
|
||||||
|
return nickname;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useFriendNicknameMap(): Record<string, string> {
|
||||||
|
const friends = useAppSelector((state) => state.user.friends);
|
||||||
|
|
||||||
|
const friendNicknameMap = useMemo(
|
||||||
|
() => buildFriendNicknameMap(friends),
|
||||||
|
[friends]
|
||||||
|
);
|
||||||
|
|
||||||
|
return friendNicknameMap;
|
||||||
|
}
|
@ -1,20 +1,66 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { useCachedUserInfo } from 'tailchat-shared';
|
import { useCachedUserInfo, useFriendNickname } from 'tailchat-shared';
|
||||||
|
|
||||||
interface UserNameProps {
|
interface UserNameProps {
|
||||||
userId: string;
|
userId: string;
|
||||||
className?: string;
|
className?: string;
|
||||||
style?: React.CSSProperties;
|
style?: React.CSSProperties;
|
||||||
|
showDiscriminator?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 纯净版的 UserName, 无需redux上下文
|
||||||
|
*/
|
||||||
|
export const UserNamePure: React.FC<UserNameProps> = React.memo((props) => {
|
||||||
|
const { userId, showDiscriminator, className, style } = props;
|
||||||
|
const cachedUserInfo = useCachedUserInfo(userId);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<span className={className} style={style}>
|
||||||
|
{cachedUserInfo.nickname ?? <span> </span>}
|
||||||
|
|
||||||
|
{showDiscriminator && (
|
||||||
|
<UserNameDiscriminator discriminator={cachedUserInfo.discriminator} />
|
||||||
|
)}
|
||||||
|
</span>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
UserNamePure.displayName = 'UserNamePure';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 增加好友名称patch的 UserName
|
||||||
|
*/
|
||||||
export const UserName: React.FC<UserNameProps> = React.memo((props) => {
|
export const UserName: React.FC<UserNameProps> = React.memo((props) => {
|
||||||
const { userId, className, style } = props;
|
const { userId, showDiscriminator, className, style } = props;
|
||||||
const cachedUserInfo = useCachedUserInfo(userId);
|
const cachedUserInfo = useCachedUserInfo(userId);
|
||||||
|
const friendNickname = useFriendNickname(userId);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<span className={className} style={style}>
|
<span className={className} style={style}>
|
||||||
{cachedUserInfo.nickname}
|
{friendNickname ? (
|
||||||
|
<>
|
||||||
|
{friendNickname}
|
||||||
|
<span className="opacity-60">({cachedUserInfo.nickname})</span>
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
cachedUserInfo.nickname ?? <span> </span>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{showDiscriminator && (
|
||||||
|
<UserNameDiscriminator discriminator={cachedUserInfo.discriminator} />
|
||||||
|
)}
|
||||||
</span>
|
</span>
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
UserName.displayName = 'UserName';
|
UserName.displayName = 'UserName';
|
||||||
|
|
||||||
|
const UserNameDiscriminator: React.FC<{ discriminator: string }> = React.memo(
|
||||||
|
({ discriminator }) => {
|
||||||
|
return (
|
||||||
|
<span className="text-gray-500 dark:text-gray-300 opacity-0 group-hover:opacity-100">
|
||||||
|
#{discriminator}
|
||||||
|
</span>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
UserNameDiscriminator.displayName = 'UserNameDiscriminator';
|
||||||
|
Loading…
Reference in New Issue