diff --git a/services/platform/app/features/automations/components/automation-detail-shell.test.tsx b/services/platform/app/features/automations/components/automation-detail-shell.test.tsx index 0852332687..498787959a 100644 --- a/services/platform/app/features/automations/components/automation-detail-shell.test.tsx +++ b/services/platform/app/features/automations/components/automation-detail-shell.test.tsx @@ -418,6 +418,37 @@ describe('AutomationDetailShell — a deleted automation', () => { ); }); + // Deleted from the list, an automation stays remembered as the rail's + // place; reopening it there must land on the list, not on its banner. + it('drops the stale memory and redirects to the list on a restored arrival', () => { + fixtures.pathname = '/dashboard/org-1/automations/billing__dunning/editor'; + fixtures.state = { navRestore: true }; + window.localStorage.setItem( + 'tale.platform.automations.org-1.lastPath', + JSON.stringify(fixtures.pathname), + ); + renderShell(); + + expect(mockNavigate).toHaveBeenCalledExactlyOnceWith({ + to: '/dashboard/$id/automations', + params: { id: 'org-1' }, + replace: true, + }); + expect( + window.localStorage.getItem('tale.platform.automations.org-1.lastPath'), + ).toBeNull(); + }); + + it('keeps explaining the deletion to a deliberate arrival', () => { + // A shared link or a run link opens the deleted automation on purpose. + fixtures.pathname = + '/dashboard/org-1/automations/billing__dunning/runs/run-1'; + renderShell(); + + expect(mockNavigate).not.toHaveBeenCalled(); + expect(screen.getByRole('alert')).toBeVisible(); + }); + it('keeps plain not-found for a name that was never saved', () => { fixtures.error = { data: { code: 'automation not found' } }; fixtures.pathname = diff --git a/services/platform/app/features/automations/components/automation-detail-shell.tsx b/services/platform/app/features/automations/components/automation-detail-shell.tsx index 0a8069fb31..52284cefc2 100644 --- a/services/platform/app/features/automations/components/automation-detail-shell.tsx +++ b/services/platform/app/features/automations/components/automation-detail-shell.tsx @@ -129,17 +129,19 @@ function AutomationDetailFrame({ // A remembered automation can be deleted between visits. When the rail // RESTORED us here, forget the stale place and fall back to the list - // rather than leaving the user on a not-found they never asked for. + // rather than leaving the user on a not-found, or on a deleted + // automation's banner, they never asked for. const wasRestored = state.navRestore === true; + const isGone = isMissing || deletedAt !== undefined; useEffect(() => { - if (!isMissing || !wasRestored || projectId !== undefined) return; + if (!isGone || !wasRestored || projectId !== undefined) return; clearAutomationMemory(organizationId); void navigate({ to: '/dashboard/$id/automations', params: { id: organizationId }, replace: true, }); - }, [isMissing, wasRestored, projectId, organizationId, navigate]); + }, [isGone, wasRestored, projectId, organizationId, navigate]); const tabs = useMemo(() => { const deleted = deletedAt !== undefined; diff --git a/services/platform/app/features/chat/components/chat-surface.tsx b/services/platform/app/features/chat/components/chat-surface.tsx index 1d755a3608..722ee32899 100644 --- a/services/platform/app/features/chat/components/chat-surface.tsx +++ b/services/platform/app/features/chat/components/chat-surface.tsx @@ -2273,12 +2273,6 @@ function ChatSurfaceInner({ organizationId={organizationId} open={deleteOpen} onOpenChange={setDeleteOpen} - onDeleted={() => - void navigate({ - to: '/dashboard/$id/chat', - params: { id: organizationId }, - }) - } /> )} diff --git a/services/platform/app/features/chat/components/thread-delete-dialog.test.tsx b/services/platform/app/features/chat/components/thread-delete-dialog.test.tsx new file mode 100644 index 0000000000..18f8a9663d --- /dev/null +++ b/services/platform/app/features/chat/components/thread-delete-dialog.test.tsx @@ -0,0 +1,111 @@ +// @vitest-environment jsdom +import '@testing-library/jest-dom/vitest'; +import { beforeEach, describe, expect, it, vi } from 'vitest'; + +import { render, screen, waitFor } from '@/tests/utils/render'; + +import { ThreadDeleteDialog } from './thread-delete-dialog'; + +// Where a delete lands: the conversation on screen is left for a fresh chat, +// never for plain `/chat` — that resumes the most recent chat, and the list +// still names the deleted one until it has read again, so the reader came +// straight back to "This chat is not available". + +const { mockNavigate, mockTrash, mockToast, routeParams } = vi.hoisted(() => ({ + mockNavigate: vi.fn(), + mockTrash: vi.fn<(threadId: string) => Promise>(), + mockToast: vi.fn(), + routeParams: { current: {} as Record }, +})); + +vi.mock('@tanstack/react-router', async (importOriginal) => ({ + ...(await importOriginal()), + useNavigate: () => mockNavigate, + useParams: () => routeParams.current, +})); + +vi.mock('../data/thread-actions', () => ({ + useThreadActions: () => ({ trash: mockTrash }), +})); + +vi.mock('@tale/ui/use-toast', () => ({ + toast: (...args: unknown[]) => mockToast(...args), + useToast: () => ({ toast: mockToast }), +})); + +function renderDialog() { + const onOpenChange = vi.fn(); + const view = render( + , + ); + return { ...view, onOpenChange }; +} + +describe('ThreadDeleteDialog', () => { + beforeEach(() => { + vi.clearAllMocks(); + routeParams.current = { id: 'org-1' }; + }); + + it('leaves the deleted conversation for a fresh chat', async () => { + routeParams.current = { id: 'org-1', threadId: 't1' }; + mockTrash.mockResolvedValueOnce(true); + const { user, onOpenChange } = renderDialog(); + + await user.click(screen.getByRole('button', { name: 'Delete chat' })); + + await waitFor(() => + expect(mockNavigate).toHaveBeenCalledExactlyOnceWith({ + to: '/dashboard/$id/chat', + params: { id: 'org-1' }, + search: { new: true }, + replace: true, + }), + ); + expect(mockTrash).toHaveBeenCalledWith('t1'); + expect(onOpenChange).toHaveBeenCalledWith(false); + }); + + it('stays on another conversation when a row deletes this chat', async () => { + routeParams.current = { id: 'org-1', threadId: 't2' }; + mockTrash.mockResolvedValueOnce(true); + const { user, onOpenChange } = renderDialog(); + + await user.click(screen.getByRole('button', { name: 'Delete chat' })); + + await waitFor(() => expect(onOpenChange).toHaveBeenCalledWith(false)); + expect(mockNavigate).not.toHaveBeenCalled(); + }); + + it('stays where it is off the chat pages', async () => { + mockTrash.mockResolvedValueOnce(true); + const { user, onOpenChange } = renderDialog(); + + await user.click(screen.getByRole('button', { name: 'Delete chat' })); + + await waitFor(() => expect(onOpenChange).toHaveBeenCalledWith(false)); + expect(mockNavigate).not.toHaveBeenCalled(); + }); + + it('keeps the conversation and the dialog when the delete is refused', async () => { + routeParams.current = { id: 'org-1', threadId: 't1' }; + mockTrash.mockResolvedValueOnce(false); + const { user, onOpenChange } = renderDialog(); + + await user.click(screen.getByRole('button', { name: 'Delete chat' })); + + await waitFor(() => + expect(mockToast).toHaveBeenCalledWith({ + title: "Couldn't delete chat", + variant: 'destructive', + }), + ); + expect(onOpenChange).not.toHaveBeenCalled(); + expect(mockNavigate).not.toHaveBeenCalled(); + }); +}); diff --git a/services/platform/app/features/chat/components/thread-delete-dialog.tsx b/services/platform/app/features/chat/components/thread-delete-dialog.tsx index df7d9142de..0bcb283c69 100644 --- a/services/platform/app/features/chat/components/thread-delete-dialog.tsx +++ b/services/platform/app/features/chat/components/thread-delete-dialog.tsx @@ -2,6 +2,7 @@ import { DeleteDialog } from '@tale/ui/dialog/delete-dialog'; import { toast } from '@tale/ui/use-toast'; +import { useNavigate, useParams } from '@tanstack/react-router'; import { useState } from 'react'; import { useT } from '@/lib/i18n/client'; @@ -17,16 +18,17 @@ export function ThreadDeleteDialog({ organizationId, open, onOpenChange, - onDeleted, }: { thread: Pick; organizationId: string; open: boolean; onOpenChange: (open: boolean) => void; - /** Runs after a successful delete — leave the dead conversation. */ - onDeleted?: () => void; }) { const { t } = useT('chat'); + const navigate = useNavigate(); + // The conversation on screen, if any: the header menu deletes it, a row + // menu deletes it or any other chat. + const openThreadId = useParams({ strict: false }).threadId; const actions = useThreadActions(organizationId); const [deleting, setDeleting] = useState(false); @@ -40,7 +42,17 @@ export function ThreadDeleteDialog({ return; } onOpenChange(false); - onDeleted?.(); + // Leave the deleted conversation for a fresh chat, in place of its + // page. Plain `/chat` would resume the most recent chat, and the list + // still names the deleted one until it has read again. + if (openThreadId === thread.id) { + void navigate({ + to: '/dashboard/$id/chat', + params: { id: organizationId }, + search: { new: true }, + replace: true, + }); + } }) .finally(() => setDeleting(false)); }; diff --git a/services/platform/app/features/chat/components/thread-row.test.tsx b/services/platform/app/features/chat/components/thread-row.test.tsx index 14b9309fe0..995fee32e8 100644 --- a/services/platform/app/features/chat/components/thread-row.test.tsx +++ b/services/platform/app/features/chat/components/thread-row.test.tsx @@ -41,6 +41,7 @@ vi.mock('@tanstack/react-router', () => ({ ), useNavigate: () => navigateMock, + useParams: () => ({ id: 'org-1' }), })); const renameMock = vi.hoisted(() => vi.fn(() => Promise.resolve(true))); diff --git a/services/platform/app/features/chat/components/thread-row.tsx b/services/platform/app/features/chat/components/thread-row.tsx index d39d57c64b..8fcaa080b7 100644 --- a/services/platform/app/features/chat/components/thread-row.tsx +++ b/services/platform/app/features/chat/components/thread-row.tsx @@ -464,7 +464,6 @@ export function ThreadRowMenu({ organizationId={organizationId} open={deleteOpen} onOpenChange={setDeleteOpen} - onDeleted={leaveIfActive} /> )} diff --git a/services/platform/app/features/documents/components/breadcrumb-navigation.test.tsx b/services/platform/app/features/documents/components/breadcrumb-navigation.test.tsx index 8e81e395b6..dc16c45437 100644 --- a/services/platform/app/features/documents/components/breadcrumb-navigation.test.tsx +++ b/services/platform/app/features/documents/components/breadcrumb-navigation.test.tsx @@ -1,6 +1,6 @@ // @vitest-environment jsdom import '@testing-library/jest-dom/vitest'; -import { describe, it, vi } from 'vitest'; +import { beforeEach, describe, expect, it, vi } from 'vitest'; import { checkAccessibility } from '@/tests/utils/a11y'; import { render } from '@/tests/utils/render'; @@ -29,19 +29,66 @@ vi.mock('@/app/hooks/use-organization-id', () => ({ useOrganizationId: () => 'org-1', })); +const TRAIL = [ + { _id: 'folder-1', name: 'Documents' }, + { _id: 'folder-2', name: 'Reports' }, +]; + +const { breadcrumbRead } = vi.hoisted(() => ({ + breadcrumbRead: { + current: { data: undefined as unknown, isLoading: false }, + }, +})); + vi.mock('@/app/hooks/use-backend-query', () => ({ - useBackendQuery: () => ({ - data: [ - { _id: 'folder-1', name: 'Documents' }, - { _id: 'folder-2', name: 'Reports' }, - ], - isLoading: false, - }), + useBackendQuery: () => breadcrumbRead.current, })); +import { toast } from '@tale/ui/use-toast'; + import { BreadcrumbNavigation } from './breadcrumb-navigation'; +beforeEach(() => { + vi.clearAllMocks(); + breadcrumbRead.current = { data: TRAIL, isLoading: false }; +}); + describe('BreadcrumbNavigation', () => { + // A deleted folder's address answers no trail. The page leaves it for the + // root in place of the dead entry, so Back does not walk into it again. + it('leaves a folder that is gone for the root, replacing its address', () => { + breadcrumbRead.current = { data: [], isLoading: false }; + const onNavigate = vi.fn(); + render( + , + ); + + expect(onNavigate).toHaveBeenCalledExactlyOnceWith(undefined, { + replace: true, + }); + expect(toast).toHaveBeenCalledWith({ title: 'documents.folderNotFound' }); + }); + + it('stays on a folder whose trail is still loading', () => { + breadcrumbRead.current = { data: undefined, isLoading: true }; + const onNavigate = vi.fn(); + render( + , + ); + + expect(onNavigate).not.toHaveBeenCalled(); + expect(toast).not.toHaveBeenCalled(); + }); + + it('stays on a folder with a trail', () => { + const onNavigate = vi.fn(); + render( + , + ); + + expect(onNavigate).not.toHaveBeenCalled(); + }); + describe('accessibility', () => { it('passes axe audit', async () => { const { container } = render( diff --git a/services/platform/app/features/documents/components/breadcrumb-navigation.tsx b/services/platform/app/features/documents/components/breadcrumb-navigation.tsx index 7ae109e8d3..9f779b29f3 100644 --- a/services/platform/app/features/documents/components/breadcrumb-navigation.tsx +++ b/services/platform/app/features/documents/components/breadcrumb-navigation.tsx @@ -11,7 +11,10 @@ import { useT } from '@/lib/i18n/client'; interface BreadcrumbNavigationProps { folderId: string; - onNavigate: (folderId: string | undefined) => void; + onNavigate: ( + folderId: string | undefined, + options?: { replace?: boolean }, + ) => void; } export function BreadcrumbNavigation({ @@ -29,10 +32,13 @@ export function BreadcrumbNavigation({ organizationId ? { folderId: folderId, organizationId } : 'skip', ); + // A folder with no trail is gone (deleted, here or in another tab) or out + // of reach: leave its address for the root, in place of it, so Back does + // not walk into it again. useEffect(() => { if (!isLoading && breadcrumb !== undefined && breadcrumb.length === 0) { toast({ title: t('folderNotFound') }); - onNavigateRef.current(undefined); + onNavigateRef.current(undefined, { replace: true }); } }, [breadcrumb, isLoading, t]); diff --git a/services/platform/app/features/documents/components/document-row-actions.tsx b/services/platform/app/features/documents/components/document-row-actions.tsx index d9a6c65367..256da82fc2 100644 --- a/services/platform/app/features/documents/components/document-row-actions.tsx +++ b/services/platform/app/features/documents/components/document-row-actions.tsx @@ -60,7 +60,6 @@ interface DocumentRowActionsProps { teamIds?: string[]; /** The folder the row sits in — the move dialog opens on it. */ currentFolderId?: string; - onFolderDeleted?: () => void; parentFolderTeamId?: string; /** Gates the "Reindex" action — terminal `unsupported` files (no text * extractor exists) never get a retry affordance, on the row menu any @@ -85,7 +84,6 @@ export function DocumentRowActions({ sourceProvider, teamIds, currentFolderId, - onFolderDeleted, parentFolderTeamId, ragStatus, record, @@ -156,10 +154,7 @@ export function DocumentRowActions({ const handleDeleteFolderConfirm = useCallback(() => { void deleteFolder({ folderId: documentId }).then( - () => { - dialogs.setOpen.deleteFolder(false); - onFolderDeleted?.(); - }, + () => dialogs.setOpen.deleteFolder(false), (error: unknown) => { console.error('Failed to delete folder:', error); // `AppError.message` is the serialized payload by design; the @@ -176,7 +171,7 @@ export function DocumentRowActions({ }); }, ); - }, [deleteFolder, documentId, dialogs.setOpen, tDocuments, onFolderDeleted]); + }, [deleteFolder, documentId, dialogs.setOpen, tDocuments]); const handleDeleteClick = useCallback(() => { if (itemType === 'folder') { diff --git a/services/platform/app/features/documents/components/documents-table.folder-delete.test.tsx b/services/platform/app/features/documents/components/documents-table.folder-delete.test.tsx new file mode 100644 index 0000000000..33e8c29e48 --- /dev/null +++ b/services/platform/app/features/documents/components/documents-table.folder-delete.test.tsx @@ -0,0 +1,152 @@ +// @vitest-environment jsdom +import '@testing-library/jest-dom/vitest'; +import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; +import type { ReactNode } from 'react'; +import { beforeEach, describe, expect, it, vi } from 'vitest'; + +import { render, screen, waitFor, within } from '@/tests/utils/render'; + +// A folder deleted from its row sat in the folder on screen, and the page +// used to jump to the Documents root after it, losing the place the reader +// was working in. The table, its columns, the row menu and the confirmation +// run for real; the reads, the write and the router are stubbed seams. + +const { navigate, deleteFolder } = vi.hoisted(() => ({ + navigate: vi.fn(), + deleteFolder: vi.fn(), +})); + +vi.mock('@tanstack/react-router', async (importOriginal) => ({ + ...(await importOriginal()), + useNavigate: () => navigate, + useParams: () => ({ id: 'org-1' }), + useLocation: () => ({ pathname: '/dashboard/org-1/documents', search: {} }), + Link: ({ children, to }: { children: ReactNode; to: string }) => ( + {children} + ), +})); + +vi.mock('@tale/ui/use-debounce', () => ({ + useDebounce: (value: string) => value, +})); + +vi.mock('@tale/ui/use-toast', () => ({ + toast: vi.fn(), + useToast: () => ({ toast: vi.fn() }), +})); + +vi.mock('@/app/hooks/use-ability', () => ({ + useAbility: () => ({ can: () => true }), +})); + +vi.mock('@/app/hooks/use-organization-id', () => ({ + useOrganizationId: () => 'org-1', +})); + +vi.mock('@/app/hooks/use-current-user', () => ({ + useCurrentUser: () => ({ data: { userId: 'user-1' } }), +})); + +vi.mock('@/app/features/settings/governance/hooks/queries', () => ({ + useLegalHoldByTarget: () => ({ data: null }), +})); + +vi.mock('@/app/features/settings/teams/hooks/queries', () => ({ + useTeams: () => ({ teams: [], isLoading: false }), + useTeamNames: () => ({ + teams: [], + nameOf: () => undefined, + isLoading: false, + }), +})); + +// The reader is in "Contracts", which holds one subfolder, "Meetings". +vi.mock('../hooks/queries', () => ({ + useApproxDocumentCount: () => ({ data: 0 }), + useFolder: () => ({ data: { _id: 'folder-contracts', name: 'Contracts' } }), + useFolders: () => ({ + data: [ + { + _id: 'folder-meetings', + name: 'Meetings', + _creationTime: 1_700_000_000_000, + parentId: 'folder-contracts', + }, + ], + }), + useListDocumentsPaginated: () => ({ + results: [], + status: 'Exhausted', + loadMore: vi.fn(), + isLoading: false, + error: null, + retry: vi.fn(), + isRetrying: false, + unavailable: false, + errorCount: 0, + }), +})); + +vi.mock('../hooks/mutations', () => ({ + useCancelOneDriveSync: () => ({ mutateAsync: vi.fn(), isPending: false }), + useCancelGoogleDriveSync: () => ({ mutateAsync: vi.fn(), isPending: false }), + useDeleteDocument: () => ({ mutateAsync: vi.fn(), isPending: false }), + useDeleteFolder: () => ({ mutateAsync: deleteFolder, isPending: false }), + useMarkDocumentControlled: () => ({ mutateAsync: vi.fn(), isPending: false }), + useOpenRecordRevision: () => ({ mutateAsync: vi.fn(), isPending: false }), +})); + +vi.mock('../hooks/actions', () => ({ + useRetryRagIndexing: () => ({ mutateAsync: vi.fn(), isPending: false }), +})); + +vi.mock('./documents-action-menu', () => ({ + DocumentsActionMenu: () => null, +})); + +vi.mock('./document-preview-dialog', () => ({ + DocumentPreviewDialog: () => null, +})); + +vi.mock('./breadcrumb-navigation', () => ({ + BreadcrumbNavigation: () =>