Files
sign/apps/web/src/app/(dashboard)/documents/data-table.tsx

130 lines
4.1 KiB
TypeScript
Raw Normal View History

2023-06-09 18:21:18 +10:00
'use client';
import { useTransition } from 'react';
2023-08-29 14:30:57 +10:00
import { Loader } from 'lucide-react';
2024-04-29 04:28:13 +05:30
import { DateTime } from 'luxon';
2023-09-12 10:37:47 +10:00
import { useSession } from 'next-auth/react';
2023-06-09 18:21:18 +10:00
import { useUpdateSearchParams } from '@documenso/lib/client-only/hooks/use-update-search-params';
import type { FindResultSet } from '@documenso/lib/types/find-result-set';
import type { Document, Recipient, Team, User } from '@documenso/prisma/client';
import { ExtendedDocumentStatus } from '@documenso/prisma/types/extended-document-status';
2023-06-09 18:21:18 +10:00
import { DataTable } from '@documenso/ui/primitives/data-table';
import { DataTablePagination } from '@documenso/ui/primitives/data-table-pagination';
import { StackAvatarsWithTooltip } from '~/components/(dashboard)/avatar/stack-avatars-with-tooltip';
2023-06-09 18:21:18 +10:00
import { DocumentStatus } from '~/components/formatter/document-status';
import { LocaleDate } from '~/components/formatter/locale-date';
2023-08-29 14:30:57 +10:00
import { DataTableActionButton } from './data-table-action-button';
import { DataTableActionDropdown } from './data-table-action-dropdown';
2023-09-12 10:37:47 +10:00
import { DataTableTitle } from './data-table-title';
2023-08-29 14:30:57 +10:00
2023-06-09 18:21:18 +10:00
export type DocumentsDataTableProps = {
2023-08-29 14:30:57 +10:00
results: FindResultSet<
Document & {
Recipient: Recipient[];
User: Pick<User, 'id' | 'name' | 'email'>;
team: Pick<Team, 'id' | 'url'> | null;
2023-08-29 14:30:57 +10:00
}
>;
showSenderColumn?: boolean;
team?: Pick<Team, 'id' | 'url'> & { teamEmail?: string };
2023-06-09 18:21:18 +10:00
};
export const DocumentsDataTable = ({
results,
showSenderColumn,
team,
}: DocumentsDataTableProps) => {
2023-09-12 10:37:47 +10:00
const { data: session } = useSession();
2023-06-09 18:21:18 +10:00
const [isPending, startTransition] = useTransition();
const updateSearchParams = useUpdateSearchParams();
const onPaginationChange = (page: number, perPage: number) => {
startTransition(() => {
updateSearchParams({
page,
perPage,
});
});
};
2023-09-12 10:37:47 +10:00
if (!session) {
return null;
}
2023-06-09 18:21:18 +10:00
return (
<div className="relative">
<DataTable
columns={[
{
header: 'Created',
2023-09-14 14:50:17 +10:00
accessorKey: 'createdAt',
2024-04-29 04:28:13 +05:30
cell: ({ row }) => (
2024-05-08 19:17:47 +05:30
<LocaleDate
date={row.original.createdAt}
format={{ ...DateTime.DATETIME_SHORT, hourCycle: 'h12' }}
/>
2024-04-29 04:28:13 +05:30
),
2023-06-09 18:21:18 +10:00
},
{
header: 'Title',
2024-02-12 19:00:47 +11:00
cell: ({ row }) => <DataTableTitle row={row.original} teamUrl={team?.url} />,
2023-06-09 18:21:18 +10:00
},
{
id: 'sender',
header: 'Sender',
cell: ({ row }) => row.original.User.name ?? row.original.User.email,
},
{
header: 'Recipient',
accessorKey: 'recipient',
cell: ({ row }) => (
<StackAvatarsWithTooltip
recipients={row.original.Recipient}
documentStatus={row.original.status}
/>
),
},
2023-06-09 18:21:18 +10:00
{
header: 'Status',
accessorKey: 'status',
cell: ({ row }) => <DocumentStatus status={row.getValue('status')} />,
2024-02-08 13:31:38 +05:30
size: 140,
2023-06-09 18:21:18 +10:00
},
2023-08-24 16:50:40 +10:00
{
header: 'Actions',
cell: ({ row }) =>
(!row.original.deletedAt ||
row.original.status === ExtendedDocumentStatus.COMPLETED) && (
<div className="flex items-center gap-x-4">
<DataTableActionButton team={team} row={row.original} />
<DataTableActionDropdown team={team} row={row.original} />
</div>
),
2023-08-24 16:50:40 +10:00
},
2023-06-09 18:21:18 +10:00
]}
data={results.data}
perPage={results.perPage}
currentPage={results.currentPage}
totalPages={results.totalPages}
onPaginationChange={onPaginationChange}
columnVisibility={{
sender: Boolean(showSenderColumn),
}}
2023-06-09 18:21:18 +10:00
>
{(table) => <DataTablePagination additionalInformation="VisibleCount" table={table} />}
2023-06-09 18:21:18 +10:00
</DataTable>
{isPending && (
2023-09-24 14:45:50 +10:00
<div className="bg-background/50 absolute inset-0 flex items-center justify-center">
<Loader className="text-muted-foreground h-8 w-8 animate-spin" />
2023-06-09 18:21:18 +10:00
</div>
)}
</div>
);
};