From 204388888d30da894bd00f4089c7eddc1263f2ae Mon Sep 17 00:00:00 2001 From: Catalin Pit <25515812+catalinpit@users.noreply.github.com> Date: Thu, 18 Jan 2024 09:38:42 +0200 Subject: [PATCH] fix: fix bug for completed document shortcut (#839) When you're in the `/documents` page in the dashboard, if you hover over a draft and a completed document, you'll see different URLs. At the moment, the shortcut tries to go to the following URL for a completed document `/documents/{doc-id}`. However, that's the wrong URL, since the URL for a completed doc is `/sign/{token}` when the user is the recipient, not the one that sent the document for signing. If it's the document owner & the document is completed, the URL is fine as `/documents/{doc-id}`. --------- Co-authored-by: Lucas Smith --- .../(dashboard)/common/command-menu.tsx | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/apps/web/src/components/(dashboard)/common/command-menu.tsx b/apps/web/src/components/(dashboard)/common/command-menu.tsx index ffbd213a4..93f7fa729 100644 --- a/apps/web/src/components/(dashboard)/common/command-menu.tsx +++ b/apps/web/src/components/(dashboard)/common/command-menu.tsx @@ -5,6 +5,7 @@ import { useCallback, useMemo, useState } from 'react'; import { useRouter } from 'next/navigation'; import { Loader, Monitor, Moon, Sun } from 'lucide-react'; +import { useSession } from 'next-auth/react'; import { useTheme } from 'next-themes'; import { useHotkeys } from 'react-hotkeys-hook'; @@ -13,6 +14,7 @@ import { SETTINGS_PAGE_SHORTCUT, TEMPLATES_PAGE_SHORTCUT, } from '@documenso/lib/constants/keyboard-shortcuts'; +import type { Document, Recipient } from '@documenso/prisma/client'; import { trpc as trpcReact } from '@documenso/trpc/react'; import { CommandDialog, @@ -65,6 +67,8 @@ export type CommandMenuProps = { export function CommandMenu({ open, onOpenChange }: CommandMenuProps) { const { setTheme } = useTheme(); + const { data: session } = useSession(); + const router = useRouter(); const [isOpen, setIsOpen] = useState(() => open ?? false); @@ -81,6 +85,17 @@ export function CommandMenu({ open, onOpenChange }: CommandMenuProps) { }, ); + const isOwner = useCallback( + (document: Document) => document.userId === session?.user.id, + [session?.user.id], + ); + + const getSigningLink = useCallback( + (recipients: Recipient[]) => + `/sign/${recipients.find((r) => r.email === session?.user.email)?.token}`, + [session?.user.email], + ); + const searchResults = useMemo(() => { if (!searchDocumentsData) { return []; @@ -88,10 +103,10 @@ export function CommandMenu({ open, onOpenChange }: CommandMenuProps) { return searchDocumentsData.map((document) => ({ label: document.title, - path: `/documents/${document.id}`, + path: isOwner(document) ? `/documents/${document.id}` : getSigningLink(document.Recipient), value: [document.id, document.title, ...document.Recipient.map((r) => r.email)].join(' '), })); - }, [searchDocumentsData]); + }, [searchDocumentsData, isOwner, getSigningLink]); const currentPage = pages[pages.length - 1];