import { useCallback, useEffect, useMemo, useState } from 'react' import useSWR from 'swr' import s from './base.module.css' import WorkspaceSelector from './workspace-selector' import SearchInput from './search-input' import PageSelector from './page-selector' import cn from '@/utils/classnames' import { preImportNotionPages } from '@/service/datasets' import { NotionConnector } from '@/app/components/datasets/create/step-one' import type { DataSourceNotionPageMap, DataSourceNotionWorkspace, NotionPage } from '@/models/common' import { useModalContext } from '@/context/modal-context' type NotionPageSelectorProps = { value?: string[] onSelect: (selectedPages: NotionPage[]) => void canPreview?: boolean previewPageId?: string onPreview?: (selectedPage: NotionPage) => void datasetId?: string } const NotionPageSelector = ({ value, onSelect, canPreview, previewPageId, onPreview, datasetId = '', }: NotionPageSelectorProps) => { const { data, mutate } = useSWR({ url: '/notion/pre-import/pages', datasetId }, preImportNotionPages) const [prevData, setPrevData] = useState(data) const [searchValue, setSearchValue] = useState('') const [currentWorkspaceId, setCurrentWorkspaceId] = useState('') const { setShowAccountSettingModal } = useModalContext() const notionWorkspaces = useMemo(() => { return data?.notion_info || [] }, [data?.notion_info]) const firstWorkspaceId = notionWorkspaces[0]?.workspace_id const currentWorkspace = notionWorkspaces.find(workspace => workspace.workspace_id === currentWorkspaceId) const getPagesMapAndSelectedPagesId: [DataSourceNotionPageMap, Set, Set] = useMemo(() => { const selectedPagesId = new Set() const boundPagesId = new Set() const pagesMap = notionWorkspaces.reduce((prev: DataSourceNotionPageMap, next: DataSourceNotionWorkspace) => { next.pages.forEach((page) => { if (page.is_bound) { selectedPagesId.add(page.page_id) boundPagesId.add(page.page_id) } prev[page.page_id] = { ...page, workspace_id: next.workspace_id, } }) return prev }, {}) return [pagesMap, selectedPagesId, boundPagesId] }, [notionWorkspaces]) const defaultSelectedPagesId = [...Array.from(getPagesMapAndSelectedPagesId[1]), ...(value || [])] const [selectedPagesId, setSelectedPagesId] = useState>(new Set(defaultSelectedPagesId)) if (prevData !== data) { setPrevData(data) setSelectedPagesId(new Set(defaultSelectedPagesId)) } const handleSearchValueChange = useCallback((value: string) => { setSearchValue(value) }, []) const handleSelectWorkspace = useCallback((workspaceId: string) => { setCurrentWorkspaceId(workspaceId) }, []) const handleSelectPages = (newSelectedPagesId: Set) => { const selectedPages = Array.from(newSelectedPagesId).map(pageId => getPagesMapAndSelectedPagesId[0][pageId]) setSelectedPagesId(new Set(Array.from(newSelectedPagesId))) onSelect(selectedPages) } const handlePreviewPage = (previewPageId: string) => { if (onPreview) onPreview(getPagesMapAndSelectedPagesId[0][previewPageId]) } useEffect(() => { setCurrentWorkspaceId(firstWorkspaceId) }, [firstWorkspaceId]) return (
{ data?.notion_info?.length ? ( <>
setShowAccountSettingModal({ payload: 'data-source', onCancelCallback: mutate })} />
) : ( setShowAccountSettingModal({ payload: 'data-source', onCancelCallback: mutate })} /> ) }
) } export default NotionPageSelector