refactor: 优化用户选择器

pull/49/head
moonrailgun 3 years ago
parent 65b4ec9e2e
commit 31fbd35cdc

@ -0,0 +1,35 @@
import React from 'react';
import { useAppSelector } from 'tailchat-shared';
import { UserPicker } from './UserPicker';
interface FriendPickerProps {
/**
* id
*
*/
withoutUserIds?: string[];
/**
*
* true
*/
withSearch?: boolean;
selectedIds: string[];
onChange: (selectedIds: string[]) => void;
}
export const FriendPicker: React.FC<FriendPickerProps> = React.memo((props) => {
const { withoutUserIds = [], selectedIds, onChange } = props;
const friendIds = useAppSelector((state) =>
state.user.friends.filter((id) => !withoutUserIds.includes(id))
);
return (
<UserPicker
selectedIds={selectedIds}
onChange={onChange}
allUserIds={friendIds}
/>
);
});
FriendPicker.displayName = 'FriendPicker';

@ -1,21 +1,17 @@
import { Checkbox, Input } from 'antd';
import React, { useCallback, useState } from 'react';
import {
getCachedUserInfo,
t,
useAppSelector,
useAsync,
} from 'tailchat-shared';
import { t, useUserInfoList } from 'tailchat-shared';
import _take from 'lodash/take';
import _without from 'lodash/without';
import { Avatar } from './Avatar';
import { Avatar } from 'tailchat-design';
interface FriendPickerProps {
/**
* id
*
*/
withoutUserIds?: string[];
/**
*
*/
interface UserPickerProps {
selectedIds: string[];
onChange: (selectedIds: string[]) => void;
/**
*
@ -23,24 +19,15 @@ interface FriendPickerProps {
*/
withSearch?: boolean;
selectedIds: string[];
onChange: (selectedIds: string[]) => void;
/**
* id
*/
allUserIds: string[];
}
export const FriendPicker: React.FC<FriendPickerProps> = React.memo((props) => {
const {
withoutUserIds = [],
withSearch = true,
selectedIds,
onChange,
} = props;
export const UserPicker: React.FC<UserPickerProps> = React.memo((props) => {
const { withSearch = true, selectedIds, onChange, allUserIds } = props;
const [searchValue, setSearchValue] = useState('');
const friendIds = useAppSelector((state) =>
state.user.friends.filter((id) => !withoutUserIds.includes(id))
);
const { value: friendInfoList = [] } = useAsync(() => {
return Promise.all(friendIds.map((id) => getCachedUserInfo(id)));
}, [friendIds.join(',')]);
const userInfoList = useUserInfoList(allUserIds);
const handleSelectUser = useCallback(
(userId: string, isSelected: boolean) => {
@ -78,8 +65,8 @@ export const FriendPicker: React.FC<FriendPickerProps> = React.memo((props) => {
</div>
{_take(
friendInfoList.filter((info) => info.nickname.includes(searchValue)),
5
userInfoList.filter((info) => info.nickname.includes(searchValue)),
10
).map((info) => {
return (
<div key={info._id} className="my-1">
@ -102,4 +89,4 @@ export const FriendPicker: React.FC<FriendPickerProps> = React.memo((props) => {
</div>
);
});
FriendPicker.displayName = 'FriendPicker';
UserPicker.displayName = 'UserPicker';

@ -1,7 +1,7 @@
import { Button } from 'antd';
import React, { useState } from 'react';
import { appendDMConverseMembers, t, useAsyncFn } from 'tailchat-shared';
import { FriendPicker } from '../FriendPicker';
import { FriendPicker } from '../UserPicker/FriendPicker';
import { closeModal, ModalWrapper } from '../Modal';
interface AppendDMConverseMembersProps {

@ -2,7 +2,7 @@ import { Button } from 'antd';
import React, { useState } from 'react';
import { useHistory } from 'react-router';
import { createDMConverse, t, useAsyncRequest } from 'tailchat-shared';
import { FriendPicker } from '../FriendPicker';
import { FriendPicker } from '../UserPicker/FriendPicker';
import { closeModal, ModalWrapper } from '../Modal';
interface CreateDMConverseProps {

@ -72,7 +72,7 @@ export const GroupRole: React.FC<GroupPermissionProps> = React.memo((props) => {
}, []);
return (
<Loading spinning={loading}>
<Loading spinning={loading} className="h-full">
<div className="flex h-full">
<div className="pr-2 mr-2 w-40 border-r border-white border-opacity-20">
{/* 角色列表 */}

@ -9,7 +9,7 @@ import { useUpdateRef } from 'tailchat-shared';
*
* ?nav=xxx
*/
export function useHistoryNav(
export function useLocationNav(
pattern: string,
callback: (nav: string) => void
) {

@ -5,7 +5,7 @@ import React from 'react';
import { useCallback } from 'react';
import { useHistory } from 'react-router';
import { quitGroup, showAlert, t, useIsGroupOwner } from 'tailchat-shared';
import { useHistoryNav } from '@/hooks/useHistoryNav';
import { useLocationNav } from '@/hooks/useHistoryNav';
/**
* Header hooks
@ -41,7 +41,7 @@ export function useGroupHeaderAction(groupId: string) {
});
}, [groupId, isOwner]);
useHistoryNav('group.*', (nav) => {
useLocationNav('group.*', (nav) => {
if (nav.startsWith('group.detail')) {
handleShowGroupDetail();
}

Loading…
Cancel
Save