2023-03-22 11:47:18 -07:00
|
|
|
import clsx from 'clsx';
|
2023-03-13 06:47:23 -07:00
|
|
|
import React, { useMemo } from 'react';
|
2022-12-16 15:07:35 -08:00
|
|
|
|
2023-03-20 14:26:40 -07:00
|
|
|
import { PendingItemsRow } from 'soapbox/components/pending-items-row';
|
2022-12-16 15:07:35 -08:00
|
|
|
import ScrollableList from 'soapbox/components/scrollable-list';
|
2023-04-04 11:05:08 -07:00
|
|
|
import { useFeatures } from 'soapbox/hooks';
|
2023-03-23 05:43:41 -07:00
|
|
|
import { useGroup } from 'soapbox/hooks/api';
|
2023-03-20 14:03:41 -07:00
|
|
|
import { useGroupMembershipRequests } from 'soapbox/hooks/api/groups/useGroupMembershipRequests';
|
2023-03-13 06:47:23 -07:00
|
|
|
import { useGroupMembers } from 'soapbox/hooks/api/useGroupMembers';
|
2023-03-20 05:53:38 -07:00
|
|
|
import { GroupRoles } from 'soapbox/schemas/group-member';
|
2022-12-16 15:07:35 -08:00
|
|
|
|
2022-12-16 15:41:22 -08:00
|
|
|
import PlaceholderAccount from '../placeholder/components/placeholder-account';
|
|
|
|
|
2023-03-13 06:47:23 -07:00
|
|
|
import GroupMemberListItem from './components/group-member-list-item';
|
2022-12-16 15:07:35 -08:00
|
|
|
|
2023-03-13 06:47:23 -07:00
|
|
|
import type { Group } from 'soapbox/types/entities';
|
2022-12-17 08:57:43 -08:00
|
|
|
|
|
|
|
interface IGroupMembers {
|
2023-04-17 12:28:20 -07:00
|
|
|
params: { groupId: string }
|
2022-12-17 08:57:43 -08:00
|
|
|
}
|
|
|
|
|
2023-04-04 11:05:08 -07:00
|
|
|
export const MAX_ADMIN_COUNT = 5;
|
|
|
|
|
2022-12-16 15:07:35 -08:00
|
|
|
const GroupMembers: React.FC<IGroupMembers> = (props) => {
|
2023-04-17 12:28:20 -07:00
|
|
|
const { groupId } = props.params;
|
2022-12-16 15:07:35 -08:00
|
|
|
|
2023-04-04 11:05:08 -07:00
|
|
|
const features = useFeatures();
|
|
|
|
|
2023-03-13 06:47:23 -07:00
|
|
|
const { group, isFetching: isFetchingGroup } = useGroup(groupId);
|
2023-03-20 05:53:38 -07:00
|
|
|
const { groupMembers: owners, isFetching: isFetchingOwners } = useGroupMembers(groupId, GroupRoles.OWNER);
|
|
|
|
const { groupMembers: admins, isFetching: isFetchingAdmins } = useGroupMembers(groupId, GroupRoles.ADMIN);
|
|
|
|
const { groupMembers: users, isFetching: isFetchingUsers, fetchNextPage, hasNextPage } = useGroupMembers(groupId, GroupRoles.USER);
|
2023-03-22 19:24:53 -07:00
|
|
|
const { isFetching: isFetchingPending, count: pendingCount } = useGroupMembershipRequests(groupId);
|
2022-12-16 15:07:35 -08:00
|
|
|
|
2023-03-20 14:03:41 -07:00
|
|
|
const isLoading = isFetchingGroup || isFetchingOwners || isFetchingAdmins || isFetchingUsers || isFetchingPending;
|
2022-12-16 15:07:35 -08:00
|
|
|
|
2023-03-13 06:47:23 -07:00
|
|
|
const members = useMemo(() => [
|
2023-03-20 05:53:38 -07:00
|
|
|
...owners,
|
2023-03-13 06:47:23 -07:00
|
|
|
...admins,
|
|
|
|
...users,
|
2023-03-20 05:53:38 -07:00
|
|
|
], [owners, admins, users]);
|
2022-12-16 15:07:35 -08:00
|
|
|
|
2023-04-04 11:05:08 -07:00
|
|
|
const canPromoteToAdmin = features.groupsAdminMax
|
|
|
|
? members.filter((member) => member.role === GroupRoles.ADMIN).length < MAX_ADMIN_COUNT
|
|
|
|
: true;
|
|
|
|
|
2022-12-16 15:07:35 -08:00
|
|
|
return (
|
|
|
|
<>
|
2023-03-13 06:47:23 -07:00
|
|
|
<ScrollableList
|
|
|
|
scrollKey='group-members'
|
|
|
|
hasMore={hasNextPage}
|
|
|
|
onLoadMore={fetchNextPage}
|
2023-03-22 18:26:53 -07:00
|
|
|
isLoading={!group || isLoading}
|
|
|
|
showLoading={!group || isFetchingPending || isLoading && members.length === 0}
|
2023-03-13 06:47:23 -07:00
|
|
|
placeholderComponent={PlaceholderAccount}
|
|
|
|
placeholderCount={3}
|
2023-03-22 11:47:18 -07:00
|
|
|
className='divide-y divide-solid divide-gray-200 dark:divide-gray-800'
|
2023-03-13 06:47:23 -07:00
|
|
|
itemClassName='py-3 last:pb-0'
|
2023-03-22 17:16:19 -07:00
|
|
|
prepend={(pendingCount > 0) && (
|
2023-03-22 11:47:18 -07:00
|
|
|
<div className={clsx('py-3', { 'border-b border-gray-200 dark:border-gray-800': members.length })}>
|
2023-03-22 17:16:19 -07:00
|
|
|
<PendingItemsRow to={`/groups/${groupId}/manage/requests`} count={pendingCount} />
|
2023-03-22 11:47:18 -07:00
|
|
|
</div>
|
2023-03-20 14:03:41 -07:00
|
|
|
)}
|
2023-03-22 11:47:18 -07:00
|
|
|
>
|
2023-03-13 06:47:23 -07:00
|
|
|
{members.map((member) => (
|
|
|
|
<GroupMemberListItem
|
|
|
|
group={group as Group}
|
|
|
|
member={member}
|
2023-03-15 11:53:17 -07:00
|
|
|
key={member.account.id}
|
2023-04-04 11:05:08 -07:00
|
|
|
canPromoteToAdmin={canPromoteToAdmin}
|
2023-03-13 06:47:23 -07:00
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</ScrollableList>
|
2022-12-16 15:07:35 -08:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default GroupMembers;
|