2023-10-02 16:55:04 +01:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import { useTransition } from 'react';
|
|
|
|
|
|
|
|
|
|
import Link from 'next/link';
|
|
|
|
|
|
2023-10-12 17:07:54 +03:00
|
|
|
import { Loader } from 'lucide-react';
|
2023-10-02 16:55:04 +01:00
|
|
|
|
|
|
|
|
import { useUpdateSearchParams } from '@documenso/lib/client-only/hooks/use-update-search-params';
|
|
|
|
|
import { FindResultSet } from '@documenso/lib/types/find-result-set';
|
2023-10-16 17:36:12 +03:00
|
|
|
import { recipientInitials } from '@documenso/lib/utils/recipient-formatter';
|
2023-10-10 16:52:58 +03:00
|
|
|
import { Document, User } from '@documenso/prisma/client';
|
2023-10-06 15:48:05 +03:00
|
|
|
import { Avatar, AvatarFallback } from '@documenso/ui/primitives/avatar';
|
2023-10-02 16:55:04 +01:00
|
|
|
import { DataTable } from '@documenso/ui/primitives/data-table';
|
|
|
|
|
import { DataTablePagination } from '@documenso/ui/primitives/data-table-pagination';
|
|
|
|
|
|
|
|
|
|
import { DocumentStatus } from '~/components/formatter/document-status';
|
|
|
|
|
import { LocaleDate } from '~/components/formatter/locale-date';
|
|
|
|
|
|
|
|
|
|
export type DocumentsDataTableProps = {
|
|
|
|
|
results: FindResultSet<
|
|
|
|
|
Document & {
|
|
|
|
|
User: Pick<User, 'id' | 'name' | 'email'>;
|
|
|
|
|
}
|
|
|
|
|
>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const DocumentsDataTable = ({ results }: DocumentsDataTableProps) => {
|
|
|
|
|
const [isPending, startTransition] = useTransition();
|
|
|
|
|
|
|
|
|
|
const updateSearchParams = useUpdateSearchParams();
|
|
|
|
|
|
|
|
|
|
const onPaginationChange = (page: number, perPage: number) => {
|
|
|
|
|
startTransition(() => {
|
|
|
|
|
updateSearchParams({
|
|
|
|
|
page,
|
|
|
|
|
perPage,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="relative">
|
|
|
|
|
<DataTable
|
|
|
|
|
columns={[
|
|
|
|
|
{
|
|
|
|
|
header: 'Created',
|
|
|
|
|
accessorKey: 'createdAt',
|
|
|
|
|
cell: ({ row }) => <LocaleDate date={row.original.createdAt} />,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
header: 'Title',
|
2023-10-10 13:50:50 +03:00
|
|
|
accessorKey: 'title',
|
|
|
|
|
cell: ({ row }) => {
|
2023-10-12 17:07:54 +03:00
|
|
|
return <div>{row.original.title}</div>;
|
2023-10-10 13:50:50 +03:00
|
|
|
},
|
2023-10-02 16:55:04 +01:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
header: 'Owner',
|
|
|
|
|
accessorKey: 'owner',
|
|
|
|
|
cell: ({ row }) => {
|
|
|
|
|
return (
|
|
|
|
|
<Link href={`/admin/users/${row.original.User.id}`}>
|
2023-10-06 15:48:05 +03:00
|
|
|
<Avatar className="dark:border-border h-12 w-12 border-2 border-solid border-white">
|
|
|
|
|
<AvatarFallback className="text-gray-400">
|
2023-10-16 17:36:12 +03:00
|
|
|
<span className="text-sm">
|
|
|
|
|
{recipientInitials(row.original.User.name ?? '')}
|
|
|
|
|
</span>
|
2023-10-06 15:48:05 +03:00
|
|
|
</AvatarFallback>
|
|
|
|
|
</Avatar>
|
2023-10-02 16:55:04 +01:00
|
|
|
</Link>
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
},
|
2023-10-12 17:07:54 +03:00
|
|
|
{
|
|
|
|
|
header: 'Last updated',
|
|
|
|
|
accessorKey: 'updatedAt',
|
|
|
|
|
cell: ({ row }) => <LocaleDate date={row.original.updatedAt} />,
|
|
|
|
|
},
|
2023-10-02 16:55:04 +01:00
|
|
|
{
|
|
|
|
|
header: 'Status',
|
|
|
|
|
accessorKey: 'status',
|
2023-10-10 16:52:58 +03:00
|
|
|
cell: ({ row }) => <DocumentStatus status={row.original.status} />,
|
2023-10-02 16:55:04 +01:00
|
|
|
},
|
|
|
|
|
]}
|
|
|
|
|
data={results.data}
|
|
|
|
|
perPage={results.perPage}
|
|
|
|
|
currentPage={results.currentPage}
|
|
|
|
|
totalPages={results.totalPages}
|
|
|
|
|
onPaginationChange={onPaginationChange}
|
|
|
|
|
>
|
|
|
|
|
{(table) => <DataTablePagination additionalInformation="VisibleCount" table={table} />}
|
|
|
|
|
</DataTable>
|
|
|
|
|
|
|
|
|
|
{isPending && (
|
|
|
|
|
<div className="absolute inset-0 flex items-center justify-center bg-white/50">
|
|
|
|
|
<Loader className="h-8 w-8 animate-spin text-gray-500" />
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|