Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
refactor(web): migrate Notion page preview to Console Query (#44252)
Yann Robertoc5a65356fc66
diff --git a/oxlint-suppressions.json b/oxlint-suppressions.jsonindex f6326871a7b..e31c4485c8f 100644--- a/oxlint-suppressions.json+++ b/oxlint-suppressions.json@@ -1102,11 +1102,6 @@"count": 1}},- "web/app/components/datasets/create/notion-page-preview/index.tsx": {- "eslint-react/set-state-in-effect": {- "count": 1- }- },"web/app/components/datasets/create/step-one/components/index.ts": {"no-barrel-files/no-barrel-files": {"count": 3diff --git a/web/app/components/datasets/create/notion-page-preview/__tests__/index.spec.tsx b/web/app/components/datasets/create/notion-page-preview/__tests__/index.spec.tsxindex bb4c8037968..b7263549ad3 100644--- a/web/app/components/datasets/create/notion-page-preview/__tests__/index.spec.tsx+++ b/web/app/components/datasets/create/notion-page-preview/__tests__/index.spec.tsx@@ -1,900 +1,251 @@-import type { MockedFunction } from 'vite-plus/test'+import type { ComponentProps } from 'react'import type { NotionPage } from '@/models/common'-import { act, fireEvent, render, screen, waitFor, within } from '@testing-library/react'-import { fetchNotionPagePreview } from '@/service/datasets'+import { QueryClient } from '@tanstack/react-query'+import { act, render, screen, waitFor } from '@testing-library/react'+import userEvent from '@testing-library/user-event'+import { createQueryClientWrapper } from '@/test/console/query-client'import NotionPagePreview from '../index'-// Mock the fetchNotionPagePreview service-vi.mock('@/service/datasets', () => ({- fetchNotionPagePreview: vi.fn(),+const { request } = vi.hoisted(() => ({+ request: vi.fn<(url: string) => Promise<Response>>(),}))-const mockFetchNotionPagePreview = fetchNotionPagePreview as MockedFunction<- typeof fetchNotionPagePreview->--// Factory function to create mock NotionPage objects-const createMockNotionPage = (overrides: Partial<NotionPage> = {}): NotionPage => {- return {- page_id: 'page-123',- page_name: 'Test Page',- page_icon: null,- parent_id: 'parent-123',- type: 'page',- is_bound: false,- workspace_id: 'workspace-123',- ...overrides,- }-}--// Factory function to create NotionPage with emoji icon-const createMockNotionPageWithEmojiIcon = (- emoji: string,- overrides: Partial<NotionPage> = {},-): NotionPage => {- return createMockNotionPage({- page_icon: {- type: 'emoji',- url: null,- emoji,- },- ...overrides,- })-}+vi.mock('@/service/base', () => ({ request }))++const createPage = (overrides: Partial<NotionPage> = {}): NotionPage => ({+ page_id: 'page-a',+ page_name: 'Page A',+ page_icon: null,+ parent_id: 'parent-id',+ type: 'page',+ is_bound: false,+ workspace_id: 'workspace-id',+ ...overrides,+})-// Factory function to create NotionPage with URL icon-const createMockNotionPageWithUrlIcon = (- url: string,- overrides: Partial<NotionPage> = {},-): NotionPage => {- return createMockNotionPage({- page_icon: {- type: 'url',- url,- emoji: null,- },- ...overrides,+const pendingResponse = () => {+ let resolve!: (response: Response) => void+ let reject!: (error: Error) => void+ const promise = new Promise<Response>((resolveResponse, rejectResponse) => {+ resolve = resolveResponse+ reject = rejectResponse})+ return { promise, resolve, reject }}-// Helper to render NotionPagePreview with default props and wait for async updates-const renderNotionPagePreview = async (- props: Partial<{- currentPage?: NotionPage- notionCredentialId: string- hidePreview: () => void- }> = {},- waitForContent = true,-) => {- const defaultProps = {- currentPage: createMockNotionPage(),- notionCredentialId: 'credential-123',+const renderPreview = (overrides: Partial<ComponentProps<typeof NotionPagePreview>> = {}) => {+ const props = {+ currentPage: createPage(),+ notionCredentialId: 'credential-a',hidePreview: vi.fn(),- ...props,- }- const result = render(<NotionPagePreview {...defaultProps} />)-- // Wait for async state updates to complete if needed- if (waitForContent && defaultProps.currentPage) {- await waitFor(() => {- // Wait for loading to finish- expect(within(result.container).queryByRole('progressbar')).not.toBeInTheDocument()- })+ ...overrides,}-+ const queryClient = new QueryClient({+ defaultOptions: { queries: { gcTime: Number.POSITIVE_INFINITY } },+ })return {- ...result,- props: defaultProps,+ ...render(<NotionPagePreview {...props} />, {+ wrapper: createQueryClientWrapper(queryClient),+ }),+ props,}}-// Helper to find the loading spinner element-const findLoadingSpinner = (container: HTMLElement) => {- return within(container).queryByRole('progressbar')-}--// NotionPagePreview Component Tests-// Note: Branch coverage is ~88% because line 29 (`if (!currentPage) return`)-// is defensive code that cannot be reached - getPreviewContent is only called-// from useEffect when currentPage is truthy.describe('NotionPagePreview', () => {beforeEach(() => {- vi.clearAllMocks()- // Default successful API response- mockFetchNotionPagePreview.mockResolvedValue({ content: 'Preview content here' })+ request+ .mockReset()+ .mockImplementation(async () => Response.json({ content: 'Preview content' }))})- afterEach(async () => {- // Wait for any pending state updates to complete- await act(async () => {- await new Promise((resolve) => setTimeout(resolve, 0))- })- })-- // Rendering Tests - Verify component renders properly- describe('Rendering', () => {- it('should render page preview header', async () => {- await renderNotionPagePreview()-- expect(screen.getByText('datasetCreation.stepOne.pagePreview')).toBeInTheDocument()- })-- it('should render close button with XMarkIcon', async () => {- await renderNotionPagePreview()-- const closeButton = screen.getByRole('button', { name: /operation\.close$/ })- expect(closeButton).toBeInTheDocument()- const xMarkIcon = closeButton?.querySelector('svg')- expect(xMarkIcon).toBeInTheDocument()- })-- it('should render page name', async () => {- const page = createMockNotionPage({ page_name: 'My Notion Page' })-- await renderNotionPagePreview({ currentPage: page })-- expect(screen.getByText('My Notion Page')).toBeInTheDocument()- })-- it('should render NotionIcon component', async () => {- const page = createMockNotionPage()-- const { container } = await renderNotionPagePreview({ currentPage: page })-- // Assert - NotionIcon should be rendered (either as img or div or svg)- const iconContainer = container.querySelector('.mr-1.shrink-0')- expect(iconContainer).toBeInTheDocument()- })+ afterEach(() => {+ vi.restoreAllMocks()})- // NotionIcon Rendering Tests- describe('NotionIcon Rendering', () => {- it('should render default icon when page_icon is null', async () => {- const page = createMockNotionPage({ page_icon: null })-- const { container } = await renderNotionPagePreview({ currentPage: page })-- // Assert - Should render RiFileTextLine icon (svg)- const svgIcon = container.querySelector('svg')- expect(svgIcon).toBeInTheDocument()- })-- it('should render emoji icon when page_icon has emoji type', async () => {- const page = createMockNotionPageWithEmojiIcon('📝')+ it('keeps the selected page content when an earlier page response arrives last', async () => {+ const first = pendingResponse()+ const second = pendingResponse()+ request.mockReturnValueOnce(first.promise).mockReturnValueOnce(second.promise)+ const { props, rerender } = renderPreview()- await renderNotionPagePreview({ currentPage: page })+ expect(screen.getByRole('progressbar')).toBeInTheDocument()+ await waitFor(() => expect(request).toHaveBeenCalledTimes(1))- expect(screen.getByText('📝')).toBeInTheDocument()- })-- it('should render image icon when page_icon has url type', async () => {- const page = createMockNotionPageWithUrlIcon('https://example.com/icon.png')+ rerender(+ <NotionPagePreview+ {...props}+ currentPage={createPage({ page_id: 'page-b', page_name: 'Page B' })}+ />,+ )+ await waitFor(() => expect(request).toHaveBeenCalledTimes(2))+ await act(async () => second.resolve(Response.json({ content: 'Page B content' })))- await renderNotionPagePreview({ currentPage: page })+ expect(await screen.findByText('Page B content')).toBeInTheDocument()+ await act(async () => first.resolve(Response.json({ content: 'Page A content' })))- const img = screen.getByRole('presentation')- expect(img).toBeVisible()- expect(screen.queryByRole('img')).not.toBeInTheDocument()- expect(img).toHaveAttribute('src', 'https://example.com/icon.png')- })+ expect(screen.getByText('Page B')).toBeInTheDocument()+ expect(screen.getByText('Page B content')).toBeInTheDocument()+ expect(screen.queryByText('Page A content')).not.toBeInTheDocument()+ expect(screen.queryByRole('progressbar')).not.toBeInTheDocument()})- // Loading State Tests- describe('Loading State', () => {- it('should show loading indicator initially', async () => {- // Arrange - Delay API response to keep loading state- mockFetchNotionPagePreview.mockImplementation(- () => new Promise((resolve) => setTimeout(() => resolve({ content: 'test' }), 100)),- )-- // Act - Don't wait for content to load- const { container } = await renderNotionPagePreview({}, false)-- // Assert - Loading should be visible initially- const loadingElement = findLoadingSpinner(container)- expect(loadingElement).toBeInTheDocument()- })-- it('should hide loading indicator after content loads', async () => {- mockFetchNotionPagePreview.mockResolvedValue({ content: 'Loaded content' })-- const { container } = await renderNotionPagePreview()-- expect(screen.getByText('Loaded content')).toBeInTheDocument()- // Loading should be gone- const loadingElement = findLoadingSpinner(container)- expect(loadingElement).not.toBeInTheDocument()- })-- it('should show loading when currentPage changes', async () => {- const page1 = createMockNotionPage({ page_id: 'page-1', page_name: 'Page 1' })- const page2 = createMockNotionPage({ page_id: 'page-2', page_name: 'Page 2' })-- let resolveFirst: (value: { content: string }) => void- let resolveSecond: (value: { content: string }) => void-- mockFetchNotionPagePreview- .mockImplementationOnce(- () =>- new Promise((resolve) => {- resolveFirst = resolve- }),- )- .mockImplementationOnce(- () =>- new Promise((resolve) => {- resolveSecond = resolve- }),- )-- // Act - Initial render- const { rerender, container } = render(- <NotionPagePreview- currentPage={page1}- notionCredentialId="cred-123"- hidePreview={vi.fn()}- />,- )-- // First page loading - spinner should be visible- expect(findLoadingSpinner(container)).toBeInTheDocument()-- // Resolve first page- await act(async () => {- resolveFirst({ content: 'Content 1' })- })-- await waitFor(() => {- expect(screen.getByText('Content 1')).toBeInTheDocument()- })+ it.each([+ {+ change: 'page ID',+ page: { page_id: 'page-b' },+ credential: 'credential-a',+ path: '/notion/pages/page-b/page/preview',+ },+ {+ change: 'page type',+ page: { type: 'database' },+ credential: 'credential-a',+ path: '/notion/pages/page-a/database/preview',+ },+ {+ change: 'credential',+ page: undefined,+ credential: 'credential-b',+ path: '/notion/pages/page-a/page/preview',+ },+ ])(+ 'loads a separate preview when the $change changes without showing the previous content',+ async ({ page, credential, path }) => {+ const next = pendingResponse()+ request+ .mockImplementationOnce(async () => Response.json({ content: 'Previous content' }))+ .mockReturnValueOnce(next.promise)+ const { props, rerender } = renderPreview()+ await screen.findByText('Previous content')- // Rerender with new pagererender(<NotionPagePreview- currentPage={page2}- notionCredentialId="cred-123"- hidePreview={vi.fn()}- />,- )-- // Should show loading again- await waitFor(() => {- expect(findLoadingSpinner(container)).toBeInTheDocument()- })-- // Resolve second page- await act(async () => {- resolveSecond({ content: 'Content 2' })- })-- await waitFor(() => {- expect(screen.getByText('Content 2')).toBeInTheDocument()- })- })- })-- // API Call Tests- describe('API Calls', () => {- it('should call fetchNotionPagePreview with correct parameters', async () => {- const page = createMockNotionPage({- page_id: 'test-page-id',- type: 'database',- })-- await renderNotionPagePreview({- currentPage: page,- notionCredentialId: 'test-credential-id',- })-- expect(mockFetchNotionPagePreview).toHaveBeenCalledWith({- pageID: 'test-page-id',- pageType: 'database',- credentialID: 'test-credential-id',- })- })-- it('should not call fetchNotionPagePreview when currentPage is undefined', async () => {- await renderNotionPagePreview({ currentPage: undefined }, false)-- expect(mockFetchNotionPagePreview).not.toHaveBeenCalled()- })-- it('should call fetchNotionPagePreview again when currentPage changes', async () => {- const page1 = createMockNotionPage({ page_id: 'page-1' })- const page2 = createMockNotionPage({ page_id: 'page-2' })-- const { rerender } = render(- <NotionPagePreview- currentPage={page1}- notionCredentialId="cred-123"- hidePreview={vi.fn()}+ {...props}+ currentPage={page ? createPage(page) : props.currentPage}+ notionCredentialId={credential}/>,)- await waitFor(() => {- expect(mockFetchNotionPagePreview).toHaveBeenCalledWith({- pageID: 'page-1',- pageType: 'page',- credentialID: 'cred-123',- })- })-- await act(async () => {- rerender(- <NotionPagePreview- currentPage={page2}- notionCredentialId="cred-123"- hidePreview={vi.fn()}- />,- )- })-- await waitFor(() => {- expect(mockFetchNotionPagePreview).toHaveBeenCalledWith({- pageID: 'page-2',- pageType: 'page',- credentialID: 'cred-123',- })- expect(mockFetchNotionPagePreview).toHaveBeenCalledTimes(2)- })- })-- it('should handle API success and display content', async () => {- mockFetchNotionPagePreview.mockResolvedValue({- content: 'Notion page preview content from API',- })-- await renderNotionPagePreview()-- expect(screen.getByText('Notion page preview content from API')).toBeInTheDocument()- })-- it('should keep the preview header visible when loading fails', async () => {- mockFetchNotionPagePreview.mockRejectedValue(new Error('Network error'))-- await renderNotionPagePreview({}, false)-- await waitFor(() => {- expect(screen.getByText('datasetCreation.stepOne.pagePreview')).toBeInTheDocument()- })- })-- it('should handle empty content response', async () => {- mockFetchNotionPagePreview.mockResolvedValue({ content: '' })-- const { container } = await renderNotionPagePreview()-- // Assert - Should still render without loading- const loadingElement = findLoadingSpinner(container)- expect(loadingElement).not.toBeInTheDocument()- })- })-- describe('User Interactions', () => {- it('should call hidePreview when close button is clicked', async () => {- const hidePreview = vi.fn()- await renderNotionPagePreview({ hidePreview })-- fireEvent.click(screen.getByRole('button', { name: /operation\.close$/ }))-- expect(hidePreview).toHaveBeenCalledTimes(1)- })-- it('should handle multiple clicks on close button', async () => {- const hidePreview = vi.fn()- await renderNotionPagePreview({ hidePreview })-- const closeButton = screen.getByRole('button', { name: /operation\.close$/ })- fireEvent.click(closeButton)- fireEvent.click(closeButton)- fireEvent.click(closeButton)-- expect(hidePreview).toHaveBeenCalledTimes(3)- })- })-- describe('State Management', () => {- it('should initialize with loading state true', async () => {- // Arrange - Keep loading indefinitely (never resolves)- mockFetchNotionPagePreview.mockImplementation(- () =>- new Promise(() => {- /* intentionally empty */- }),- )-- // Act - Don't wait for content- const { container } = await renderNotionPagePreview({}, false)-- const loadingElement = findLoadingSpinner(container)- expect(loadingElement).toBeInTheDocument()- })-- it('should update previewContent state after successful fetch', async () => {- mockFetchNotionPagePreview.mockResolvedValue({ content: 'New preview content' })+ expect(screen.queryByText('Previous content')).not.toBeInTheDocument()+ expect(screen.getByRole('progressbar')).toBeInTheDocument()+ await waitFor(() => expect(request).toHaveBeenCalledTimes(2))+ const url = new URL(request.mock.calls[1]![0])+ expect(url.pathname.endsWith(path)).toBe(true)+ expect(url.searchParams.get('credential_id')).toBe(credential)- await renderNotionPagePreview()-- expect(screen.getByText('New preview content')).toBeInTheDocument()- })+ await act(async () => next.resolve(Response.json({ content: 'Updated content' })))- it('should reset loading to true when currentPage changes', async () => {- const page1 = createMockNotionPage({ page_id: 'page-1' })- const page2 = createMockNotionPage({ page_id: 'page-2' })-- mockFetchNotionPagePreview- .mockResolvedValueOnce({ content: 'Content 1' })- .mockImplementationOnce(- () =>- new Promise(() => {- /* never resolves */- }),- )-- const { rerender, container } = render(- <NotionPagePreview- currentPage={page1}- notionCredentialId="cred-123"- hidePreview={vi.fn()}- />,- )-- await waitFor(() => {- expect(screen.getByText('Content 1')).toBeInTheDocument()- })-- // Change page- await act(async () => {- rerender(- <NotionPagePreview- currentPage={page2}- notionCredentialId="cred-123"- hidePreview={vi.fn()}- />,- )- })-- // Assert - Loading should be shown again- await waitFor(() => {- const loadingElement = findLoadingSpinner(container)- expect(loadingElement).toBeInTheDocument()- })- })-- it('should replace old content with new content when page changes', async () => {- const page1 = createMockNotionPage({ page_id: 'page-1' })- const page2 = createMockNotionPage({ page_id: 'page-2' })-- let resolveSecond: (value: { content: string }) => void+ expect(await screen.findByText('Updated content')).toBeInTheDocument()+ expect(screen.queryByRole('progressbar')).not.toBeInTheDocument()+ },+ )- mockFetchNotionPagePreview- .mockResolvedValueOnce({ content: 'Content 1' })- .mockImplementationOnce(- () =>- new Promise((resolve) => {- resolveSecond = resolve- }),- )+ it('updates page metadata without refetching an unchanged preview identity', async () => {+ const { props, rerender } = renderPreview()+ await screen.findByText('Preview content')- const { rerender } = render(- <NotionPagePreview- currentPage={page1}- notionCredentialId="cred-123"- hidePreview={vi.fn()}- />,+ await act(async () => {+ rerender(+ <NotionPagePreview {...props} currentPage={createPage({ page_name: 'Renamed page' })} />,)-- await waitFor(() => {- expect(screen.getByText('Content 1')).toBeInTheDocument()- })-- // Change page- await act(async () => {- rerender(- <NotionPagePreview- currentPage={page2}- notionCredentialId="cred-123"- hidePreview={vi.fn()}- />,- )- })-- // Resolve second fetch- await act(async () => {- resolveSecond({ content: 'Content 2' })- })-- await waitFor(() => {- expect(screen.getByText('Content 2')).toBeInTheDocument()- expect(screen.queryByText('Content 1')).not.toBeInTheDocument()- })- })- })-- describe('Props', () => {- describe('currentPage prop', () => {- it('should render correctly with currentPage prop', async () => {- const page = createMockNotionPage({ page_name: 'My Test Page' })-- await renderNotionPagePreview({ currentPage: page })-- expect(screen.getByText('My Test Page')).toBeInTheDocument()- })-- it('should render correctly without currentPage prop (undefined)', async () => {- await renderNotionPagePreview({ currentPage: undefined }, false)-- // Assert - Header should still render- expect(screen.getByText('datasetCreation.stepOne.pagePreview')).toBeInTheDocument()- })-- it('should handle page with very long name', async () => {- const longName = 'a'.repeat(200)- const page = createMockNotionPage({ page_name: longName })-- await renderNotionPagePreview({ currentPage: page })-- expect(screen.getByText(longName)).toBeInTheDocument()- })-- it('should handle page with special characters in name', async () => {- const page = createMockNotionPage({ page_name: 'Page with <special> & "chars"' })-- await renderNotionPagePreview({ currentPage: page })-- expect(screen.getByText('Page with <special> & "chars"')).toBeInTheDocument()- })-- it('should handle page with unicode characters in name', async () => {- const page = createMockNotionPage({ page_name: '中文页面名称 🚀 日本語' })-- await renderNotionPagePreview({ currentPage: page })-- expect(screen.getByText('中文页面名称 🚀 日本語')).toBeInTheDocument()- })})- describe('notionCredentialId prop', () => {- it('should pass notionCredentialId to API call', async () => {- const page = createMockNotionPage()-- await renderNotionPagePreview({- currentPage: page,- notionCredentialId: 'my-credential-id',- })-- expect(mockFetchNotionPagePreview).toHaveBeenCalledWith(- expect.objectContaining({ credentialID: 'my-credential-id' }),- )- })- })-- describe('hidePreview prop', () => {- it('should accept hidePreview callback', async () => {- const hidePreview = vi.fn()-- await renderNotionPagePreview({ hidePreview })-- // Assert - No errors thrown- expect(screen.getByText('datasetCreation.stepOne.pagePreview')).toBeInTheDocument()- })- })+ expect(screen.getByText('Renamed page')).toBeInTheDocument()+ expect(screen.getByText('Preview content')).toBeInTheDocument()+ expect(screen.queryByRole('progressbar')).not.toBeInTheDocument()+ expect(request).toHaveBeenCalledTimes(1)})- describe('Edge Cases', () => {- it('should handle page with undefined page_id', async () => {- const page = createMockNotionPage({ page_id: undefined as unknown as string })-- await renderNotionPagePreview({ currentPage: page })-- // Assert - API should still be called (with undefined pageID)- expect(mockFetchNotionPagePreview).toHaveBeenCalled()- })-- it('should handle page with empty string page_id', async () => {- const page = createMockNotionPage({ page_id: '' })-- await renderNotionPagePreview({ currentPage: page })-- expect(mockFetchNotionPagePreview).toHaveBeenCalledWith(- expect.objectContaining({ pageID: '' }),- )- })-- it('should handle very long preview content', async () => {- const longContent = 'x'.repeat(10000)- mockFetchNotionPagePreview.mockResolvedValue({ content: longContent })-- await renderNotionPagePreview()-- expect(screen.getByText(longContent)).toBeInTheDocument()- })-- it('should handle preview content with special characters safely', async () => {- const specialContent = '<script>alert("xss")</script>\n\t& < > "'- mockFetchNotionPagePreview.mockResolvedValue({ content: specialContent })-- const { container } = await renderNotionPagePreview()+ it('keeps retry focused while retrying and after another failure, then recovers', async () => {+ const user = userEvent.setup()+ const retry = pendingResponse()+ vi.spyOn(console, 'error').mockImplementation(() => {})+ request+ .mockRejectedValueOnce(new Error('Preview unavailable'))+ .mockReturnValueOnce(retry.promise)+ renderPreview()- // Assert - Should render as text, not execute scripts- const contentDiv = container.querySelector('[class*="fileContent"]')- expect(contentDiv).toBeInTheDocument()- expect(contentDiv?.textContent).toContain('alert')- })-- it('should handle preview content with unicode', async () => {- const unicodeContent = '中文内容 🚀 émojis & spëcîal çhàrs'- mockFetchNotionPagePreview.mockResolvedValue({ content: unicodeContent })+ expect(await screen.findByRole('alert')).toBeInTheDocument()+ expect(screen.queryByRole('progressbar')).not.toBeInTheDocument()+ expect(screen.getByText('Page A')).toBeInTheDocument()+ expect(request).toHaveBeenCalledTimes(1)- await renderNotionPagePreview()+ const retryButton = screen.getByRole('button', { name: 'common.operation.retry' })+ await user.click(retryButton)- expect(screen.getByText(unicodeContent)).toBeInTheDocument()- })+ await waitFor(() => expect(request).toHaveBeenCalledTimes(2))+ expect(retryButton).toBeInTheDocument()+ expect(retryButton).toHaveFocus()+ expect(retryButton).toHaveAttribute('aria-disabled', 'true')- it('should handle preview content with newlines', async () => {- const multilineContent = 'Line 1\nLine 2\nLine 3'- mockFetchNotionPagePreview.mockResolvedValue({ content: multilineContent })+ await act(async () => retry.reject(new Error('Preview still unavailable')))- const { container } = await renderNotionPagePreview()+ await waitFor(() => expect(retryButton).not.toHaveAttribute('aria-disabled', 'true'))+ expect(screen.getByRole('alert')).toBeInTheDocument()+ expect(retryButton).toHaveFocus()+ expect(request).toHaveBeenCalledTimes(2)- const contentDiv = container.querySelector('[class*="fileContent"]')- expect(contentDiv).toBeInTheDocument()- expect(contentDiv?.textContent).toContain('Line 1')- expect(contentDiv?.textContent).toContain('Line 2')- expect(contentDiv?.textContent).toContain('Line 3')- })+ await user.click(retryButton)- it('should handle different page types', async () => {- const databasePage = createMockNotionPage({ type: 'database' })-- await renderNotionPagePreview({ currentPage: databasePage })-- expect(mockFetchNotionPagePreview).toHaveBeenCalledWith(- expect.objectContaining({ pageType: 'database' }),- )- })+ expect(await screen.findByText('Preview content')).toBeInTheDocument()+ expect(screen.queryByRole('alert')).not.toBeInTheDocument()+ expect(request).toHaveBeenCalledTimes(3)+ expect(request.mock.calls[1]![0]).toBe(request.mock.calls[0]![0])+ expect(request.mock.calls[2]![0]).toBe(request.mock.calls[0]![0])})- // Side Effects and Cleanup Tests- describe('Side Effects and Cleanup', () => {- it('should trigger effect when currentPage prop changes', async () => {- const page1 = createMockNotionPage({ page_id: 'page-1' })- const page2 = createMockNotionPage({ page_id: 'page-2' })-- const { rerender } = render(- <NotionPagePreview- currentPage={page1}- notionCredentialId="cred-123"- hidePreview={vi.fn()}- />,- )-- await waitFor(() => {- expect(mockFetchNotionPagePreview).toHaveBeenCalledTimes(1)- })-- await act(async () => {- rerender(- <NotionPagePreview- currentPage={page2}- notionCredentialId="cred-123"- hidePreview={vi.fn()}- />,- )- })-- await waitFor(() => {- expect(mockFetchNotionPagePreview).toHaveBeenCalledTimes(2)- })- })-- it('should not trigger effect when hidePreview changes', async () => {- const page = createMockNotionPage()- const hidePreview1 = vi.fn()- const hidePreview2 = vi.fn()-- const { rerender } = render(- <NotionPagePreview- currentPage={page}- notionCredentialId="cred-123"- hidePreview={hidePreview1}- />,- )-- await waitFor(() => {- expect(mockFetchNotionPagePreview).toHaveBeenCalledTimes(1)- })-- await act(async () => {- rerender(- <NotionPagePreview- currentPage={page}- notionCredentialId="cred-123"- hidePreview={hidePreview2}- />,- )- })-- // Assert - Should not call API again (currentPage didn't change by reference)- // Note: Since currentPage is the same object, effect should not re-run- expect(mockFetchNotionPagePreview).toHaveBeenCalledTimes(1)- })-- it('should not trigger effect when notionCredentialId changes', async () => {- const page = createMockNotionPage()-- const { rerender } = render(- <NotionPagePreview currentPage={page} notionCredentialId="cred-1" hidePreview={vi.fn()} />,- )-- await waitFor(() => {- expect(mockFetchNotionPagePreview).toHaveBeenCalledTimes(1)- })-- await act(async () => {- rerender(- <NotionPagePreview- currentPage={page}- notionCredentialId="cred-2"- hidePreview={vi.fn()}- />,- )- })-- // Assert - Should not call API again (only currentPage is in dependency array)- expect(mockFetchNotionPagePreview).toHaveBeenCalledTimes(1)- })-- it('should handle rapid page changes', async () => {- const pages = Array.from({ length: 5 }, (_, i) =>- createMockNotionPage({ page_id: `page-${i}` }),- )-- const { rerender } = render(- <NotionPagePreview- currentPage={pages[0]}- notionCredentialId="cred-123"- hidePreview={vi.fn()}- />,- )+ it('waits for a credential without loading, then requests the selected preview', async () => {+ const { props, rerender } = renderPreview({ notionCredentialId: '' })- // Rapidly change pages- for (let i = 1; i < pages.length; i++) {- await act(async () => {- rerender(- <NotionPagePreview- currentPage={pages[i]}- notionCredentialId="cred-123"- hidePreview={vi.fn()}- />,- )- })- }-- // Assert - Should have called API for each page- await waitFor(() => {- expect(mockFetchNotionPagePreview).toHaveBeenCalledTimes(5)- })- })+ expect(request).not.toHaveBeenCalled()+ expect(screen.queryByRole('progressbar')).not.toBeInTheDocument()+ expect(screen.queryByRole('alert')).not.toBeInTheDocument()- it('should handle page changing from defined to undefined', async () => {- const page = createMockNotionPage()+ rerender(<NotionPagePreview {...props} notionCredentialId="credential-b" />)- const { rerender } = render(- <NotionPagePreview- currentPage={page}- notionCredentialId="cred-123"- hidePreview={vi.fn()}- />,- )-- await waitFor(() => {- expect(mockFetchNotionPagePreview).toHaveBeenCalledTimes(1)- })-- await act(async () => {- rerender(- <NotionPagePreview- currentPage={undefined}- notionCredentialId="cred-123"- hidePreview={vi.fn()}- />,- )- })-- expect(mockFetchNotionPagePreview).toHaveBeenCalledTimes(1)- })+ expect(await screen.findByText('Preview content')).toBeInTheDocument()+ expect(request).toHaveBeenCalledTimes(1)+ expect(new URL(request.mock.calls[0]![0]).searchParams.get('credential_id')).toBe(+ 'credential-b',+ )})- // Page Type Variations Tests- describe('Page Type Variations', () => {- it('should handle page type', async () => {- const page = createMockNotionPage({ type: 'page' })-- await renderNotionPagePreview({ currentPage: page })-- expect(mockFetchNotionPagePreview).toHaveBeenCalledWith(- expect.objectContaining({ pageType: 'page' }),- )- })-- it('should handle database type', async () => {- const page = createMockNotionPage({ type: 'database' })-- await renderNotionPagePreview({ currentPage: page })+ it('does not request a preview without a selected page and clears content when selection is removed', async () => {+ const { props, rerender } = renderPreview({ currentPage: undefined })- expect(mockFetchNotionPagePreview).toHaveBeenCalledWith(- expect.objectContaining({ pageType: 'database' }),- )- })+ expect(request).not.toHaveBeenCalled()+ expect(screen.queryByRole('progressbar')).not.toBeInTheDocument()- it('should handle unknown type', async () => {- const page = createMockNotionPage({ type: 'unknown_type' })+ rerender(<NotionPagePreview {...props} currentPage={createPage()} />)+ await screen.findByText('Preview content')- await renderNotionPagePreview({ currentPage: page })+ rerender(<NotionPagePreview {...props} currentPage={undefined} />)- expect(mockFetchNotionPagePreview).toHaveBeenCalledWith(- expect.objectContaining({ pageType: 'unknown_type' }),- )- })+ expect(screen.queryByText('Preview content')).not.toBeInTheDocument()+ expect(screen.queryByRole('progressbar')).not.toBeInTheDocument()+ expect(screen.queryByRole('alert')).not.toBeInTheDocument()+ expect(request).toHaveBeenCalledTimes(1)})- // Icon Type Variations Tests- describe('Icon Type Variations', () => {- it('should handle page with null icon', async () => {- const page = createMockNotionPage({ page_icon: null })+ it('finishes loading successfully when the page has no content', async () => {+ request.mockImplementation(async () => Response.json({ content: '' }))+ renderPreview()- const { container } = await renderNotionPagePreview({ currentPage: page })+ await waitFor(() => expect(screen.queryByRole('progressbar')).not.toBeInTheDocument())- // Assert - Should render default icon- const svgIcon = container.querySelector('svg')- expect(svgIcon).toBeInTheDocument()- })-- it('should handle page with emoji icon object', async () => {- const page = createMockNotionPageWithEmojiIcon('📄')-- await renderNotionPagePreview({ currentPage: page })-- expect(screen.getByText('📄')).toBeInTheDocument()- })-- it('should handle page with url icon object', async () => {- const page = createMockNotionPageWithUrlIcon('https://example.com/custom-icon.png')-- await renderNotionPagePreview({ currentPage: page })-- const img = screen.getByRole('presentation')- expect(img).toBeVisible()- expect(screen.queryByRole('img')).not.toBeInTheDocument()- expect(img).toHaveAttribute('src', 'https://example.com/custom-icon.png')- })+ expect(screen.getByText('Page A')).toBeInTheDocument()+ expect(screen.queryByRole('alert')).not.toBeInTheDocument()+ expect(screen.queryByRole('button', { name: 'common.operation.retry' })).not.toBeInTheDocument()})- // Content Display Tests- describe('Content Display', () => {- it('should preserve whitespace in content', async () => {- const contentWithWhitespace = ' indented content\n more indent'- mockFetchNotionPagePreview.mockResolvedValue({ content: contentWithWhitespace })-- const { container } = await renderNotionPagePreview()-- const contentDiv = container.querySelector('[class*="fileContent"]')- expect(contentDiv).toBeInTheDocument()- // The CSS class has white-space: pre-line- expect(contentDiv?.textContent).toContain('indented content')- })-- it('should display empty string content without loading', async () => {- mockFetchNotionPagePreview.mockResolvedValue({ content: '' })+ it('lets the user close the preview while it is loading', async () => {+ const user = userEvent.setup()+ request.mockReturnValue(pendingResponse().promise)+ const hidePreview = vi.fn()+ renderPreview({ hidePreview })- const { container } = await renderNotionPagePreview()+ await user.click(screen.getByRole('button', { name: 'common.operation.close' }))- const loadingElement = findLoadingSpinner(container)- expect(loadingElement).not.toBeInTheDocument()- const contentDiv = container.querySelector('[class*="fileContent"]')- expect(contentDiv).toBeInTheDocument()- expect(contentDiv?.textContent).toBe('')- })+ expect(hidePreview).toHaveBeenCalledOnce()})})diff --git a/web/app/components/datasets/create/notion-page-preview/index.tsx b/web/app/components/datasets/create/notion-page-preview/index.tsxindex efe6ce28bd0..078676c3832 100644--- a/web/app/components/datasets/create/notion-page-preview/index.tsx+++ b/web/app/components/datasets/create/notion-page-preview/index.tsx@@ -1,13 +1,14 @@'use client'import type { NotionPage } from '@/models/common'import { XMarkIcon } from '@heroicons/react/20/solid'+import { Button } from '@langgenius/dify-ui/button'import { cn } from '@langgenius/dify-ui/cn'+import { skipToken, useQuery } from '@tanstack/react-query'import * as React from 'react'-import { useEffect, useState } from 'react'import { useTranslation } from 'react-i18next'import { LoadingPlaceholder } from '@/app/components/base/loading-placeholder'import NotionIcon from '@/app/components/base/notion-icon'-import { fetchNotionPagePreview } from '@/service/datasets'+import { consoleQuery } from '@/service/console'import s from './index.module.css'type IProps = {@@ -18,28 +19,18 @@ type IProps = {const NotionPagePreview = ({ currentPage, notionCredentialId, hidePreview }: IProps) => {const { t } = useTranslation(['common', 'datasetCreation'])- const [previewContent, setPreviewContent] = useState('')- const [loading, setLoading] = useState(true)-- const getPreviewContent = async () => {- if (!currentPage) return- try {- const res = await fetchNotionPagePreview({- pageID: currentPage.page_id,- pageType: currentPage.type,- credentialID: notionCredentialId,- })- setPreviewContent(res.content)- setLoading(false)- } catch {}- }-- useEffect(() => {- if (currentPage) {- setLoading(true)- getPreviewContent()- }- }, [currentPage])+ const { data, isLoading, isError, isFetching, isFetchedAfterMount, refetch } = useQuery(+ consoleQuery.notion.pages.byPageId.byPageType.preview.get.queryOptions({+ input:+ currentPage && notionCredentialId+ ? {+ params: { page_id: currentPage.page_id, page_type: currentPage.type },+ query: { credential_id: notionCredentialId },+ }+ : skipToken,+ }),+ )+ const hasLoadError = isError || (!data && isFetchedAfterMount)return (<div className={cn(s.filePreview, 'h-full')}>@@ -66,8 +57,20 @@ const NotionPagePreview = ({ currentPage, notionCredentialId, hidePreview }: IPr</div></div><div className={cn(s.previewContent, 'body-md-regular')}>- {loading && <LoadingPlaceholder />}- {!loading && <div className={cn(s.fileContent, 'body-md-regular')}>{previewContent}</div>}+ {isLoading && !hasLoadError && <LoadingPlaceholder />}+ {hasLoadError && (+ <div className="flex flex-col items-start gap-3">+ <p role="alert" className="text-text-secondary">+ {t(($) => $['api.actionFailed'], { ns: 'common' })}+ </p>+ <Button size="small" loading={isFetching} onClick={() => void refetch()}>+ {t(($) => $['operation.retry'], { ns: 'common' })}+ </Button>+ </div>+ )}+ {!hasLoadError && data && (+ <div className={cn(s.fileContent, 'body-md-regular')}>{data.content}</div>+ )}</div></div>)diff --git a/web/service/console/query-policies.ts b/web/service/console/query-policies.tsindex 35f746e6983..60f133dc342 100644--- a/web/service/console/query-policies.ts+++ b/web/service/console/query-policies.ts@@ -84,6 +84,23 @@ export function createConsoleQuery(consoleClient: ConsoleClient) {},},},+ notion: {+ pages: {+ byPageId: {+ byPageType: {+ preview: {+ get: {+ queryOptions: {+ context: { silent: true },+ retry: false,+ staleTime: 0,+ },+ },+ },+ },+ },+ },+ },rag: {pipeline: {templates: {diff --git a/web/service/datasets.ts b/web/service/datasets.tsindex 9df01ac8be3..798462d8701 100644--- a/web/service/datasets.ts+++ b/web/service/datasets.ts@@ -142,21 +142,6 @@ export const downloadDocumentsZip = ({})}-export const fetchNotionPagePreview = ({- pageID,- pageType,- credentialID,-}: {- pageID: string- pageType: string- credentialID: string-}) => {- return consoleClient.notion.pages.byPageId.byPageType.preview.get({- params: { page_id: pageID, page_type: pageType },- query: { credential_id: credentialID },- })-}-type WebsiteCrawlProvider = 'firecrawl' | 'jinareader' | 'watercrawl'type WebsiteCrawlTaskBody = {