import { CheckBox, Table, Tag, Pagination, TableHead, TableRow, TableCell, TableBody } from '@rocket.chat/fuselage'; import type { Dispatch, SetStateAction, ChangeEvent } from 'react'; import { useState, useCallback } from 'react'; import { useTranslation } from 'react-i18next'; import type { ChannelDescriptor } from './ChannelDescriptor'; export type PrepareChannelsProps = { channelsCount: number; channels: ChannelDescriptor[]; setChannels: Dispatch>; }; // TODO: review inner logic const PrepareChannels = ({ channels, channelsCount, setChannels }: PrepareChannelsProps) => { const { t } = useTranslation(); const [current, setCurrent] = useState(0); const [itemsPerPage, setItemsPerPage] = useState<25 | 50 | 100>(25); const showingResultsLabel = useCallback( ({ count, current, itemsPerPage }: { count: number; current: number; itemsPerPage: 25 | 50 | 100 }) => t('Showing_results_of', { from: current + 1, to: Math.min(current + itemsPerPage, count), total: count }), [t], ); const itemsPerPageLabel = useCallback(() => t('Items_per_page:'), [t]); if (!channels.length) { return null; } return ( <> 0} indeterminate={channelsCount > 0 && channelsCount !== channels.length} onChange={(): void => { setChannels((channels) => { const hasCheckedArchivedChannels = channels.some(({ is_archived, do_import }) => is_archived && do_import); const isChecking = channelsCount === 0; if (isChecking) { return channels.map((channel) => ({ ...channel, do_import: true })); } if (hasCheckedArchivedChannels) { return channels.map((channel) => (channel.is_archived ? { ...channel, do_import: false } : channel)); } return channels.map((channel) => ({ ...channel, do_import: false })); }); }} /> {t('Name')} {channels.slice(current, current + itemsPerPage).map((channel) => ( ): void => { const { checked } = event.currentTarget; setChannels((channels) => channels.map((_channel) => (_channel === channel ? { ..._channel, do_import: checked } : _channel)), ); }} /> {channel.name} {channel.is_archived && {t('Importer_Archived')}} ))}
); }; export default PrepareChannels;