mirror of https://github.com/msgbyte/tailchat
refactor: 好友列表与会话创建
parent
bb88176b72
commit
ce149bdaa8
@ -0,0 +1,22 @@
|
||||
import { request } from '../api/request';
|
||||
|
||||
interface ConverseInfo {
|
||||
_id: string;
|
||||
name: string;
|
||||
type: 'DM' | 'Group';
|
||||
members: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* 尝试创建私聊会话
|
||||
* 如果已创建则返回之前的
|
||||
*/
|
||||
export async function createDMConverse(
|
||||
targetId: string
|
||||
): Promise<ConverseInfo> {
|
||||
const { data } = await request.post('/api/chat/converse/createDMConverse', {
|
||||
targetId,
|
||||
});
|
||||
|
||||
return data;
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
import React, { useCallback } from 'react';
|
||||
import { createDMConverse, t, useAppSelector } from 'pawchat-shared';
|
||||
import { UserListItem } from '@/components/UserListItem';
|
||||
import { IconBtn } from '@/components/IconBtn';
|
||||
import { Tooltip } from 'antd';
|
||||
import { useHistory } from 'react-router';
|
||||
|
||||
/**
|
||||
* 好友列表
|
||||
*/
|
||||
export const FriendList: React.FC = React.memo(() => {
|
||||
const friends = useAppSelector((state) => state.user.friends);
|
||||
const history = useHistory();
|
||||
|
||||
const handleCreateConverse = useCallback(
|
||||
(targetId: string) => {
|
||||
createDMConverse(targetId).then((converse) => {
|
||||
history.push(`/main/personal/converse/${converse._id}`);
|
||||
});
|
||||
},
|
||||
[history]
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="py-2.5 px-5">
|
||||
<div>好友列表</div>
|
||||
<div>
|
||||
{friends.map((friendId) => (
|
||||
<UserListItem
|
||||
key={friendId}
|
||||
userId={friendId}
|
||||
actions={[
|
||||
<Tooltip key="message" title={t('发送消息')}>
|
||||
<div>
|
||||
<IconBtn
|
||||
icon="mdi-message-text-outline"
|
||||
onClick={() => handleCreateConverse(friendId)}
|
||||
/>
|
||||
</div>
|
||||
</Tooltip>,
|
||||
]}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
});
|
||||
FriendList.displayName = 'FriendList';
|
Loading…
Reference in New Issue