import { OrderedSet as ImmutableOrderedSet, is } from 'immutable'; import React, { useEffect, useState } from 'react'; import { defineMessages, useIntl } from 'react-intl'; import { useHistory } from 'react-router-dom'; import { fetchUsers } from 'soapbox/actions/admin'; import { Widget } from 'soapbox/components/ui'; import AccountContainer from 'soapbox/containers/account-container'; import { useAppDispatch, useAppSelector } from 'soapbox/hooks'; import { compareId } from 'soapbox/utils/comparators'; const messages = defineMessages({ title: { id: 'admin.latest_accounts_panel.title', defaultMessage: 'Latest Accounts' }, expand: { id: 'admin.latest_accounts_panel.more', defaultMessage: 'Click to see {count} {count, plural, one {account} other {accounts}}' }, }); interface ILatestAccountsPanel { limit?: number, } const LatestAccountsPanel: React.FC = ({ limit = 5 }) => { const intl = useIntl(); const history = useHistory(); const dispatch = useAppDispatch(); const accountIds = useAppSelector>((state) => state.admin.get('latestUsers').take(limit)); const hasDates = useAppSelector((state) => accountIds.every(id => !!state.accounts.getIn([id, 'created_at']))); const [total, setTotal] = useState(accountIds.size); useEffect(() => { dispatch(fetchUsers(['local', 'active'], 1, null, limit)) .then((value) => { setTotal((value as { count: number }).count); }) .catch(() => {}); }, []); const sortedIds = accountIds.sort(compareId).reverse(); const isSorted = hasDates && is(accountIds, sortedIds); if (!isSorted || !accountIds || accountIds.isEmpty()) { return null; } const handleAction = () => { history.push('/soapbox/admin/users'); }; return ( {accountIds.take(limit).map((account) => ( ))} ); }; export default LatestAccountsPanel;