base.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { useCallback, useEffect, useMemo, useState } from 'react'
  2. import useSWR from 'swr'
  3. import s from './base.module.css'
  4. import WorkspaceSelector from './workspace-selector'
  5. import SearchInput from './search-input'
  6. import PageSelector from './page-selector'
  7. import cn from '@/utils/classnames'
  8. import { preImportNotionPages } from '@/service/datasets'
  9. import { NotionConnector } from '@/app/components/datasets/create/step-one'
  10. import type { DataSourceNotionPageMap, DataSourceNotionWorkspace, NotionPage } from '@/models/common'
  11. import { useModalContext } from '@/context/modal-context'
  12. type NotionPageSelectorProps = {
  13. value?: string[]
  14. onSelect: (selectedPages: NotionPage[]) => void
  15. canPreview?: boolean
  16. previewPageId?: string
  17. onPreview?: (selectedPage: NotionPage) => void
  18. datasetId?: string
  19. }
  20. const NotionPageSelector = ({
  21. value,
  22. onSelect,
  23. canPreview,
  24. previewPageId,
  25. onPreview,
  26. datasetId = '',
  27. }: NotionPageSelectorProps) => {
  28. const { data, mutate } = useSWR({ url: '/notion/pre-import/pages', datasetId }, preImportNotionPages)
  29. const [prevData, setPrevData] = useState(data)
  30. const [searchValue, setSearchValue] = useState('')
  31. const [currentWorkspaceId, setCurrentWorkspaceId] = useState('')
  32. const { setShowAccountSettingModal } = useModalContext()
  33. const notionWorkspaces = useMemo(() => {
  34. return data?.notion_info || []
  35. }, [data?.notion_info])
  36. const firstWorkspaceId = notionWorkspaces[0]?.workspace_id
  37. const currentWorkspace = notionWorkspaces.find(workspace => workspace.workspace_id === currentWorkspaceId)
  38. const getPagesMapAndSelectedPagesId: [DataSourceNotionPageMap, Set<string>, Set<string>] = useMemo(() => {
  39. const selectedPagesId = new Set<string>()
  40. const boundPagesId = new Set<string>()
  41. const pagesMap = notionWorkspaces.reduce((prev: DataSourceNotionPageMap, next: DataSourceNotionWorkspace) => {
  42. next.pages.forEach((page) => {
  43. if (page.is_bound) {
  44. selectedPagesId.add(page.page_id)
  45. boundPagesId.add(page.page_id)
  46. }
  47. prev[page.page_id] = {
  48. ...page,
  49. workspace_id: next.workspace_id,
  50. }
  51. })
  52. return prev
  53. }, {})
  54. return [pagesMap, selectedPagesId, boundPagesId]
  55. }, [notionWorkspaces])
  56. const defaultSelectedPagesId = [...Array.from(getPagesMapAndSelectedPagesId[1]), ...(value || [])]
  57. const [selectedPagesId, setSelectedPagesId] = useState<Set<string>>(new Set(defaultSelectedPagesId))
  58. if (prevData !== data) {
  59. setPrevData(data)
  60. setSelectedPagesId(new Set(defaultSelectedPagesId))
  61. }
  62. const handleSearchValueChange = useCallback((value: string) => {
  63. setSearchValue(value)
  64. }, [])
  65. const handleSelectWorkspace = useCallback((workspaceId: string) => {
  66. setCurrentWorkspaceId(workspaceId)
  67. }, [])
  68. const handleSelectPages = (newSelectedPagesId: Set<string>) => {
  69. const selectedPages = Array.from(newSelectedPagesId).map(pageId => getPagesMapAndSelectedPagesId[0][pageId])
  70. setSelectedPagesId(new Set(Array.from(newSelectedPagesId)))
  71. onSelect(selectedPages)
  72. }
  73. const handlePreviewPage = (previewPageId: string) => {
  74. if (onPreview)
  75. onPreview(getPagesMapAndSelectedPagesId[0][previewPageId])
  76. }
  77. useEffect(() => {
  78. setCurrentWorkspaceId(firstWorkspaceId)
  79. }, [firstWorkspaceId])
  80. return (
  81. <div className='bg-gray-25 border border-gray-200 rounded-xl'>
  82. {
  83. data?.notion_info?.length
  84. ? (
  85. <>
  86. <div className='flex items-center pl-[10px] pr-2 h-11 bg-white border-b border-b-gray-200 rounded-t-xl'>
  87. <WorkspaceSelector
  88. value={currentWorkspaceId || firstWorkspaceId}
  89. items={notionWorkspaces}
  90. onSelect={handleSelectWorkspace}
  91. />
  92. <div className='mx-1 w-[1px] h-3 bg-gray-200' />
  93. <div
  94. className={cn(s['setting-icon'], 'w-6 h-6 cursor-pointer')}
  95. onClick={() => setShowAccountSettingModal({ payload: 'data-source', onCancelCallback: mutate })}
  96. />
  97. <div className='grow' />
  98. <SearchInput
  99. value={searchValue}
  100. onChange={handleSearchValueChange}
  101. />
  102. </div>
  103. <div className='rounded-b-xl overflow-hidden'>
  104. <PageSelector
  105. value={selectedPagesId}
  106. disabledValue={getPagesMapAndSelectedPagesId[2]}
  107. searchValue={searchValue}
  108. list={currentWorkspace?.pages || []}
  109. pagesMap={getPagesMapAndSelectedPagesId[0]}
  110. onSelect={handleSelectPages}
  111. canPreview={canPreview}
  112. previewPageId={previewPageId}
  113. onPreview={handlePreviewPage}
  114. />
  115. </div>
  116. </>
  117. )
  118. : (
  119. <NotionConnector onSetting={() => setShowAccountSettingModal({ payload: 'data-source', onCancelCallback: mutate })} />
  120. )
  121. }
  122. </div>
  123. )
  124. }
  125. export default NotionPageSelector