import { Pagination, States, StatesActions, StatesAction, StatesIcon, StatesTitle } from '@rocket.chat/fuselage';
import { useDebouncedValue } from '@rocket.chat/fuselage-hooks';
import { escapeRegExp } from '@rocket.chat/string-helpers';
import { useEndpoint, useRoute, useTranslation, useLayout } from '@rocket.chat/ui-contexts';
import { useQuery } from '@tanstack/react-query';
import React, { useMemo, useCallback, useState } from 'react';
import FilterByText from '../../../components/FilterByText';
import GenericNoResults from '../../../components/GenericNoResults';
import {
GenericTable,
GenericTableBody,
GenericTableHeader,
GenericTableHeaderCell,
GenericTableLoadingTable,
} from '../../../components/GenericTable';
import { usePagination } from '../../../components/GenericTable/hooks/usePagination';
import { useSort } from '../../../components/GenericTable/hooks/useSort';
import IntegrationRow from './IntegrationRow';
const IntegrationsTable = ({ type }: { type?: string }) => {
const t = useTranslation();
const { isMobile } = useLayout();
const [text, setText] = useState('');
const router = useRoute('admin-integrations');
const { sortBy, sortDirection, setSort } = useSort<'name' | 'channel' | '_createdBy' | '_createdAt' | 'username'>('name');
const { current, itemsPerPage, setItemsPerPage: onSetItemsPerPage, setCurrent: onSetCurrent, ...paginationProps } = usePagination();
const query = useDebouncedValue(
useMemo(
() => ({
query: JSON.stringify({ name: { $regex: escapeRegExp(text), $options: 'i' }, type }),
sort: `{ "${sortBy}": ${sortDirection === 'asc' ? 1 : -1} }`,
count: itemsPerPage,
offset: current,
}),
[text, itemsPerPage, current, sortBy, sortDirection, type],
),
500,
);
const getIntegrations = useEndpoint('GET', '/v1/integrations.list');
const { data, isLoading, isSuccess, isError, refetch } = useQuery(['integrations', query], async () => getIntegrations(query));
const onClick = useCallback(
(_id, type) => () =>
router.push({
context: 'edit',
type: type === 'webhook-incoming' ? 'incoming' : 'outgoing',
id: _id,
}),
[router],
);
const headers = (
<>
{t('Name')}
{t('Post_to')}
{t('Created_by')}
{!isMobile && (
{t('Created_at')}
)}
{t('Post_as')}
>
);
return (
<>
setText(text)} />
{isLoading && (
{headers}
)}
{isSuccess && data && data.integrations.length > 0 && (
<>
{headers}
{isSuccess &&
data?.integrations.map((integration) => (
))}
>
)}
{isSuccess && data && data.integrations.length === 0 && }
{isError && (
{t('Something_went_wrong')}
refetch()}>{t('Reload_page')}
)}
>
);
};
export default IntegrationsTable;