import React, { useEffect } from 'react'; import { FormattedMessage } from 'react-intl'; import { Link } from 'react-router-dom'; import { createSelector } from 'reselect'; import { fetchGroups } from 'soapbox/actions/groups'; import { openModal } from 'soapbox/actions/modals'; import GroupCard from 'soapbox/components/group-card'; import ScrollableList from 'soapbox/components/scrollable-list'; import { Button, Column, Spinner, Stack, Text } from 'soapbox/components/ui'; import { useAppDispatch, useAppSelector } from 'soapbox/hooks'; import { PERMISSION_CREATE_GROUPS, hasPermission } from 'soapbox/utils/permissions'; import PlaceholderGroupCard from '../placeholder/components/placeholder-group-card'; import type { List as ImmutableList } from 'immutable'; import type { RootState } from 'soapbox/store'; import type { Group as GroupEntity } from 'soapbox/types/entities'; const getOrderedGroups = createSelector([ (state: RootState) => state.groups.items, (state: RootState) => state.groups.isLoading, (state: RootState) => state.group_relationships, ], (groups, isLoading, group_relationships) => ({ groups: (groups.toList().filter((item: GroupEntity | false) => !!item) as ImmutableList) .map((item) => item.set('relationship', group_relationships.get(item.id) || null)) .filter((item) => item.relationship?.member) .sort((a, b) => a.display_name.localeCompare(b.display_name)), isLoading, })); const Groups: React.FC = () => { const dispatch = useAppDispatch(); const { groups, isLoading } = useAppSelector((state) => getOrderedGroups(state)); const canCreateGroup = useAppSelector((state) => hasPermission(state, PERMISSION_CREATE_GROUPS)); useEffect(() => { dispatch(fetchGroups()); }, []); const createGroup = () => { dispatch(openModal('MANAGE_GROUP')); }; if (!groups) { return ( ); } const emptyMessage = ( ); return ( {canCreateGroup && ( )} {groups.map((group) => ( ))} ); }; export default Groups;