import { Pagination, States, StatesIcon, StatesActions, StatesAction, StatesTitle } from '@rocket.chat/fuselage'; import { useDebouncedValue } from '@rocket.chat/fuselage-hooks'; import { GenericTable, GenericTableBody, GenericTableHeader, GenericTableHeaderCell, GenericTableLoadingTable, usePagination, useSort, } from '@rocket.chat/ui-client'; import { useTranslation, useEndpoint } from '@rocket.chat/ui-contexts'; import { useQuery } from '@tanstack/react-query'; import type { MutableRefObject } from 'react'; import { useState, useMemo, useEffect } from 'react'; import CustomSoundRow from './CustomSoundRow'; import FilterByText from '../../../../components/FilterByText'; import GenericNoResults from '../../../../components/GenericNoResults'; export type CustomSoundsTableProps = { onClick: (soundId: string) => () => void; reload: MutableRefObject<() => void>; }; const CustomSoundsTable = ({ reload, onClick }: CustomSoundsTableProps) => { const t = useTranslation(); const { sortBy, sortDirection, setSort } = useSort<'name'>('name'); const { current, itemsPerPage, setItemsPerPage: onSetItemsPerPage, setCurrent: onSetCurrent, ...paginationProps } = usePagination(); const [text, setText] = useState(''); const query = useDebouncedValue( useMemo( () => ({ name: text, sort: `{ "${sortBy}": ${sortDirection === 'asc' ? 1 : -1} }`, ...(itemsPerPage && { count: itemsPerPage }), ...(current && { offset: current }), }), [text, itemsPerPage, current, sortBy, sortDirection], ), 500, ); const getSounds = useEndpoint('GET', '/v1/custom-sounds.list'); const { data, refetch, isLoading, isError, isSuccess } = useQuery({ queryKey: ['custom-sounds', query], queryFn: async () => getSounds(query), refetchOnMount: false, }); useEffect(() => { reload.current = refetch; }, [reload, refetch]); const headers = ( <> {t('Name')} ); return ( <> setText(event.target.value)} /> {isLoading && ( {headers} )} {isSuccess && data?.sounds.length > 0 && ( <> {headers} {data.sounds.map((sound) => ( ))} )} {isSuccess && data?.sounds.length === 0 && } {isError && ( {t('Something_went_wrong')} refetch()}>{t('Reload_page')} )} ); }; export default CustomSoundsTable;