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

64 lines
1.7 KiB
TypeScript
Raw Normal View History

2023-09-12 10:37:47 +10:00
'use client';
import Link from 'next/link';
import { useSession } from 'next-auth/react';
import { match } from 'ts-pattern';
2024-02-12 19:00:47 +11:00
import { formatDocumentsPath } from '@documenso/lib/utils/teams';
import type { Document, Recipient, Team, User } from '@documenso/prisma/client';
2023-09-12 10:37:47 +10:00
export type DataTableTitleProps = {
row: Document & {
User: Pick<User, 'id' | 'name' | 'email'>;
2024-02-12 19:00:47 +11:00
team: Pick<Team, 'url'> | null;
2023-09-12 10:37:47 +10:00
Recipient: Recipient[];
};
2024-02-12 19:00:47 +11:00
teamUrl?: string;
2023-09-12 10:37:47 +10:00
};
2024-02-12 19:00:47 +11:00
export const DataTableTitle = ({ row, teamUrl }: DataTableTitleProps) => {
2023-09-12 10:37:47 +10:00
const { data: session } = useSession();
if (!session) {
return null;
}
const recipient = row.Recipient.find((recipient) => recipient.email === session.user.email);
const isOwner = row.User.id === session.user.id;
const isRecipient = !!recipient;
2024-02-12 19:00:47 +11:00
const isCurrentTeamDocument = teamUrl && row.team?.url === teamUrl;
const documentsPath = formatDocumentsPath(isCurrentTeamDocument ? teamUrl : undefined);
2023-09-12 10:37:47 +10:00
return match({
isOwner,
isRecipient,
2024-02-12 19:00:47 +11:00
isCurrentTeamDocument,
2023-09-12 10:37:47 +10:00
})
2024-02-12 19:00:47 +11:00
.with({ isOwner: true }, { isCurrentTeamDocument: true }, () => (
2023-09-12 10:37:47 +10:00
<Link
2024-02-12 19:00:47 +11:00
href={`${documentsPath}/${row.id}`}
2023-09-12 10:37:47 +10:00
title={row.title}
className="block max-w-[10rem] truncate font-medium hover:underline md:max-w-[20rem]"
>
{row.title}
</Link>
))
.with({ isRecipient: true }, () => (
<Link
href={`/sign/${recipient?.token}`}
title={row.title}
className="block max-w-[10rem] truncate font-medium hover:underline md:max-w-[20rem]"
>
{row.title}
</Link>
))
.otherwise(() => (
<span className="block max-w-[10rem] truncate font-medium hover:underline md:max-w-[20rem]">
{row.title}
</span>
));
};