Commit d965a280 authored by StyleZhang's avatar StyleZhang

fix: notion-page-selector preview

parent 8f55352c
...@@ -13,6 +13,7 @@ type NotionPageSelectorProps = { ...@@ -13,6 +13,7 @@ type NotionPageSelectorProps = {
value?: string[] value?: string[]
onSelect: (selectedPages: (DataSourceNotionPage & { workspace_id: string })[]) => void onSelect: (selectedPages: (DataSourceNotionPage & { workspace_id: string })[]) => void
canPreview?: boolean canPreview?: boolean
previewPageId?: string
onPreview?: (selectedPage: DataSourceNotionPage & { workspace_id: string }) => void onPreview?: (selectedPage: DataSourceNotionPage & { workspace_id: string }) => void
} }
...@@ -20,6 +21,7 @@ const NotionPageSelector = ({ ...@@ -20,6 +21,7 @@ const NotionPageSelector = ({
value, value,
onSelect, onSelect,
canPreview, canPreview,
previewPageId,
onPreview, onPreview,
}: NotionPageSelectorProps) => { }: NotionPageSelectorProps) => {
const [searchValue, setSearchValue] = useState('') const [searchValue, setSearchValue] = useState('')
...@@ -30,9 +32,9 @@ const NotionPageSelector = ({ ...@@ -30,9 +32,9 @@ const NotionPageSelector = ({
const [currentWorkspaceId, setCurrentWorkspaceId] = useState('') const [currentWorkspaceId, setCurrentWorkspaceId] = useState('')
const currentWorkspace = notionWorkspaces.find(workspace => workspace.id === currentWorkspaceId) const currentWorkspace = notionWorkspaces.find(workspace => workspace.id === currentWorkspaceId)
const handleSearchValueChange = (value: string) => { const handleSearchValueChange = useCallback((value: string) => {
setSearchValue(value) setSearchValue(value)
} }, [])
const handleSelectWorkspace = useCallback((workspaceId: string) => { const handleSelectWorkspace = useCallback((workspaceId: string) => {
setCurrentWorkspaceId(workspaceId) setCurrentWorkspaceId(workspaceId)
}, []) }, [])
...@@ -76,19 +78,16 @@ const NotionPageSelector = ({ ...@@ -76,19 +78,16 @@ const NotionPageSelector = ({
/> />
</div> </div>
<div className='rounded-b-xl overflow-hidden'> <div className='rounded-b-xl overflow-hidden'>
{
currentWorkspace?.source_info.pages.length && (
<PageSelector <PageSelector
key={currentWorkspaceId} key={currentWorkspaceId}
value={value} value={value}
searchValue={searchValue} searchValue={searchValue}
list={currentWorkspace?.source_info.pages} list={currentWorkspace?.source_info.pages || []}
onSelect={handleSelecPages} onSelect={handleSelecPages}
canPreview={canPreview} canPreview={canPreview}
previewPageId={previewPageId}
onPreview={handlePreviewPage} onPreview={handlePreviewPage}
/> />
)
}
</div> </div>
{ {
showDataSourceSetting && ( showDataSourceSetting && (
......
...@@ -9,3 +9,9 @@ ...@@ -9,3 +9,9 @@
.arrow-expand { .arrow-expand {
transform: rotate(0); transform: rotate(0);
} }
.preview-item {
background-color: #eff4ff;
border: 1px solid #D1E0FF;
box-shadow: 0px 1px 2px rgba(16, 24, 40, 0.05);
}
\ No newline at end of file
import { memo, useEffect, useState } from 'react' import { memo, useState } from 'react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import { FixedSizeList as List, areEqual } from 'react-window' import { FixedSizeList as List, areEqual } from 'react-window'
import type { ListChildComponentProps } from 'react-window' import type { ListChildComponentProps } from 'react-window'
...@@ -14,6 +14,7 @@ type PageSelectorProps = { ...@@ -14,6 +14,7 @@ type PageSelectorProps = {
list: DataSourceNotionPage[] list: DataSourceNotionPage[]
onSelect: (selectedPages: DataSourceNotionPage[]) => void onSelect: (selectedPages: DataSourceNotionPage[]) => void
canPreview?: boolean canPreview?: boolean
previewPageId?: string
onPreview?: (selectedPage: DataSourceNotionPage) => void onPreview?: (selectedPage: DataSourceNotionPage) => void
} }
type NotionPageMap = Record<string, DataSourceNotionPage> type NotionPageMap = Record<string, DataSourceNotionPage>
...@@ -73,9 +74,10 @@ const Item = memo(({ index, style, data }: ListChildComponentProps<{ ...@@ -73,9 +74,10 @@ const Item = memo(({ index, style, data }: ListChildComponentProps<{
handlePreview: (index: number) => void handlePreview: (index: number) => void
listMapWithChildrenAndDescendants: NotionPageTreeMap listMapWithChildrenAndDescendants: NotionPageTreeMap
searchValue: string searchValue: string
previewPageId: string
}>) => { }>) => {
const { t } = useTranslation() const { t } = useTranslation()
const { dataList, handleToggle, checkedIds, handleCheck, canPreview, handlePreview, listMapWithChildrenAndDescendants, searchValue } = data const { dataList, handleToggle, checkedIds, handleCheck, canPreview, handlePreview, listMapWithChildrenAndDescendants, searchValue, previewPageId } = data
const current = dataList[index] const current = dataList[index]
const currentWithChildrenAndDescendants = listMapWithChildrenAndDescendants[current.page_id] const currentWithChildrenAndDescendants = listMapWithChildrenAndDescendants[current.page_id]
const hasChild = currentWithChildrenAndDescendants.descendants.size > 0 const hasChild = currentWithChildrenAndDescendants.descendants.size > 0
...@@ -105,7 +107,7 @@ const Item = memo(({ index, style, data }: ListChildComponentProps<{ ...@@ -105,7 +107,7 @@ const Item = memo(({ index, style, data }: ListChildComponentProps<{
return ( return (
<div <div
className='group flex items-center pl-2 pr-[2px] rounded-md hover:bg-gray-100 cursor-pointer' className={cn('group flex items-center pl-2 pr-[2px] rounded-md border border-transparent hover:bg-gray-100 cursor-pointer', previewPageId === current.page_id && s['preview-item'])}
style={{ ...style, top: style.top as number + 8, left: 8, right: 8, width: 'calc(100% - 16px)' }} style={{ ...style, top: style.top as number + 8, left: 8, right: 8, width: 'calc(100% - 16px)' }}
> >
<Checkbox <Checkbox
...@@ -128,7 +130,7 @@ const Item = memo(({ index, style, data }: ListChildComponentProps<{ ...@@ -128,7 +130,7 @@ const Item = memo(({ index, style, data }: ListChildComponentProps<{
{ {
canPreview && ( canPreview && (
<div <div
className='shrink-0 hidden group-hover:flex items-center ml-1 px-2 h-6 rounded-md text-xs font-medium text-gray-700 cursor-pointer hover:bg-gray-50' className='shrink-0 hidden group-hover:flex items-center ml-1 px-2 h-6 rounded-md text-xs font-medium text-gray-500 cursor-pointer hover:bg-gray-50 hover:text-gray-700'
onClick={() => handlePreview(index)}> onClick={() => handlePreview(index)}>
{t('common.dataSource.notion.selector.preview')} {t('common.dataSource.notion.selector.preview')}
</div> </div>
...@@ -154,8 +156,10 @@ const PageSelector = ({ ...@@ -154,8 +156,10 @@ const PageSelector = ({
list, list,
onSelect, onSelect,
canPreview = true, canPreview = true,
previewPageId,
onPreview, onPreview,
}: PageSelectorProps) => { }: PageSelectorProps) => {
const { t } = useTranslation()
const [dataList, setDataList] = useState<NotionPageItem[]>( const [dataList, setDataList] = useState<NotionPageItem[]>(
list.filter(item => item.parent_id === 'root').map((item) => { list.filter(item => item.parent_id === 'root').map((item) => {
return { return {
...@@ -165,8 +169,20 @@ const PageSelector = ({ ...@@ -165,8 +169,20 @@ const PageSelector = ({
} }
}), }),
) )
const [searchDataList, setSearchDataList] = useState<NotionPageItem[]>([]) const searchDataList = list.filter((item) => {
return item.page_name.includes(searchValue)
}).map((item) => {
return {
...item,
expand: false,
deepth: 0,
}
})
const currentDataList = searchValue ? searchDataList : dataList
const [checkedIds, setCheckedIds] = useState<Set<string>>(new Set(value || [])) const [checkedIds, setCheckedIds] = useState<Set<string>>(new Set(value || []))
const [localPreviewPageId, setLocalPreviewPageId] = useState('')
const currentPreviewPageId = previewPageId === undefined ? localPreviewPageId : previewPageId
const listMap = list.reduce((prev: NotionPageMap, next: DataSourceNotionPage) => { const listMap = list.reduce((prev: NotionPageMap, next: DataSourceNotionPage) => {
prev[next.page_id] = next prev[next.page_id] = next
...@@ -210,7 +226,7 @@ const PageSelector = ({ ...@@ -210,7 +226,7 @@ const PageSelector = ({
} }
const handleCheck = (index: number) => { const handleCheck = (index: number) => {
const current = searchValue ? searchDataList[index] : dataList[index] const current = currentDataList[index]
const pageId = current.page_id const pageId = current.page_id
const currentWithChildrenAndDescendants = listMapWithChildrenAndDescendants[pageId] const currentWithChildrenAndDescendants = listMapWithChildrenAndDescendants[pageId]
...@@ -236,35 +252,33 @@ const PageSelector = ({ ...@@ -236,35 +252,33 @@ const PageSelector = ({
} }
const handlePreview = (index: number) => { const handlePreview = (index: number) => {
if (onPreview) { const current = currentDataList[index]
const current = dataList[index]
const pageId = current.page_id const pageId = current.page_id
setLocalPreviewPageId(pageId)
if (onPreview)
onPreview(listMap[pageId]) onPreview(listMap[pageId])
} }
}
useEffect(() => { if (!currentDataList.length) {
setSearchDataList(list.filter((item) => { return (
return item.page_name.includes(searchValue) <div className='flex items-center justify-center h-[296px] text-[13px] text-gray-500'>
}).map((item) => { {t('common.dataSource.notion.selector.noSearchResult')}
return { </div>
...item, )
expand: false,
deepth: 0,
} }
}))
}, [searchValue])
return ( return (
<List <List
className='py-2' className='py-2'
height={296} height={296}
itemCount={searchValue ? searchDataList.length : dataList.length} itemCount={currentDataList.length}
itemSize={28} itemSize={28}
width='100%' width='100%'
itemKey={(index, data) => data.dataList[index].page_id} itemKey={(index, data) => data.dataList[index].page_id}
itemData={{ itemData={{
dataList: searchValue ? searchDataList : dataList, dataList: currentDataList,
handleToggle, handleToggle,
checkedIds, checkedIds,
handleCheck, handleCheck,
...@@ -272,6 +286,7 @@ const PageSelector = ({ ...@@ -272,6 +286,7 @@ const PageSelector = ({
handlePreview, handlePreview,
listMapWithChildrenAndDescendants, listMapWithChildrenAndDescendants,
searchValue, searchValue,
previewPageId: currentPreviewPageId,
}} }}
> >
{Item} {Item}
......
...@@ -16,7 +16,7 @@ const SearchInput = ({ ...@@ -16,7 +16,7 @@ const SearchInput = ({
const handleClear = useCallback(() => { const handleClear = useCallback(() => {
onChange('') onChange('')
}, []) }, [onChange])
return ( return (
<div className={cn(s['input-wrapper'], 'flex items-center px-2 h-7 border border-gray-300 rounded-md', `${value ? 'bg-white' : 'bg-gray-100'}`)}> <div className={cn(s['input-wrapper'], 'flex items-center px-2 h-7 border border-gray-300 rounded-md', `${value ? 'bg-white' : 'bg-gray-100'}`)}>
......
...@@ -190,7 +190,7 @@ const translation = { ...@@ -190,7 +190,7 @@ const translation = {
selector: { selector: {
pageSelected: 'Pages Selected', pageSelected: 'Pages Selected',
searchPages: 'Search pages...', searchPages: 'Search pages...',
noSerachResult: 'No search resluts', noSearchResult: 'No search resluts',
addPages: 'Add pages', addPages: 'Add pages',
preview: 'PREVIEW', preview: 'PREVIEW',
}, },
......
...@@ -191,7 +191,7 @@ const translation = { ...@@ -191,7 +191,7 @@ const translation = {
selector: { selector: {
pageSelected: '已选页面', pageSelected: '已选页面',
searchPages: '搜索页面...', searchPages: '搜索页面...',
noSerachResult: '无搜索结果', noSearchResult: '无搜索结果',
addPages: '添加页面', addPages: '添加页面',
preview: '预览', preview: '预览',
}, },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment