import { useMemo, useRef } from 'react' import { toast } from 'sonner' import { useParams } from 'common' import { useFDWsQuery } from 'data/fdw/fdws-query' import { handleErrorOnDelete, useQueryStateWithSelect } from 'hooks/misc/useQueryStateWithSelect' import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject' import { Card, cn, Table, TableBody, TableCell, TableFooter, TableHead, TableHeader, TableRow, } from 'ui' import { INTEGRATIONS } from '../Landing/Integrations.constants' import { WrapperRow } from './WrapperRow' import { wrapperMetaComparator } from './Wrappers.utils' interface WrapperTableProps { isLatest?: boolean } export const WrapperTable = ({ isLatest = false }: WrapperTableProps) => { const { id, ref } = useParams() const { data: project } = useSelectedProjectQuery() const integration = INTEGRATIONS.find((i) => i.id === id) const { data } = useFDWsQuery({ projectRef: ref, connectionString: project?.connectionString, }) const wrappers = useMemo( () => integration && integration.type === 'wrapper' && data ? data.filter((wrapper) => wrapperMetaComparator(integration.meta, wrapper)) : [], [data, integration] ) // Track the ID being deleted to exclude it from error checking const deletingWrapperIdRef = useRef(null) const { setValue: setSelectedWrapperToEdit, value: selectedWrapperToEdit } = useQueryStateWithSelect({ urlKey: 'edit', select: (wrapperId: string) => wrapperId ? wrappers.find((w) => w.id.toString() === wrapperId) : undefined, enabled: !!wrappers.length, onError: () => toast.error(`Wrapper not found`), }) const { setValue: setSelectedWrapperToDelete, value: selectedWrapperToDelete } = useQueryStateWithSelect({ urlKey: 'delete', select: (wrapperId: string) => wrapperId ? wrappers.find((w) => w.id.toString() === wrapperId) : undefined, enabled: !!wrappers.length, onError: (_error, selectedId) => handleErrorOnDelete(deletingWrapperIdRef, selectedId, `Wrapper not found`), }) if (!integration || integration.type !== 'wrapper') { return (

The referenced ID doesn't correspond to a wrapper integration

) } return ( Name Tables Encrypted key Actions {(isLatest ? wrappers.slice(0, 3) : wrappers).map((x) => { return ( ) })} tr>td]:hover:bg-inherit', // Conditionally remove the border-top if there are no wrappers wrappers.length === 0 ? 'border-t-0' : '' )} > {wrappers.length} {integration?.name} {wrappers.length > 1 ? 's' : ''} created
) }