From f46db45e17d83e688f92a06bb0a6612e06bf5980 Mon Sep 17 00:00:00 2001 From: CodingOnStar Date: Wed, 24 Dec 2025 14:21:55 +0800 Subject: [PATCH] refactor: enhance DataSourceSelector component with configuration and generic click handler --- .../create/step-one/data-source-selector.tsx | 135 ++++++++---------- .../datasets/create/step-one/index.tsx | 4 +- 2 files changed, 62 insertions(+), 77 deletions(-) diff --git a/web/app/components/datasets/create/step-one/data-source-selector.tsx b/web/app/components/datasets/create/step-one/data-source-selector.tsx index 3e82db348c..a8c787abba 100644 --- a/web/app/components/datasets/create/step-one/data-source-selector.tsx +++ b/web/app/components/datasets/create/step-one/data-source-selector.tsx @@ -1,11 +1,19 @@ 'use client' import type { DataSourceSelectorProps } from './types' +import { useMemo } from 'react' import { useTranslation } from 'react-i18next' import { ENABLE_WEBSITE_FIRECRAWL, ENABLE_WEBSITE_JINAREADER, ENABLE_WEBSITE_WATERCRAWL } from '@/config' import { DataSourceType } from '@/models/datasets' import { cn } from '@/utils/classnames' import s from './index.module.css' +type DataSourceConfig = { + type: DataSourceType + iconClassName?: string + labelKey: string + isEnabled: boolean +} + /** * Data source type selector component * Allows users to choose between File, Notion, and Web data sources @@ -21,90 +29,67 @@ const DataSourceSelector = ({ const { t } = useTranslation() const isWebEnabled = ENABLE_WEBSITE_FIRECRAWL || ENABLE_WEBSITE_JINAREADER || ENABLE_WEBSITE_WATERCRAWL - const handleFileClick = () => { - if (dataSourceTypeDisable) - return - changeType(DataSourceType.FILE) - onHideNotionPreview() - onHideWebsitePreview() - } + // Configuration for all data source types + const dataSourceConfigs: DataSourceConfig[] = useMemo(() => [ + { + type: DataSourceType.FILE, + labelKey: 'datasetCreation.stepOne.dataSourceType.file', + isEnabled: true, + }, + { + type: DataSourceType.NOTION, + iconClassName: s.notion, + labelKey: 'datasetCreation.stepOne.dataSourceType.notion', + isEnabled: true, + }, + { + type: DataSourceType.WEB, + iconClassName: s.web, + labelKey: 'datasetCreation.stepOne.dataSourceType.web', + isEnabled: isWebEnabled, + }, + ], [isWebEnabled]) - const handleNotionClick = () => { - if (dataSourceTypeDisable) - return - changeType(DataSourceType.NOTION) - onHideFilePreview() - onHideWebsitePreview() - } + // Map of hide preview functions for each data source type + const hidePreviewMap = useMemo(() => ({ + [DataSourceType.FILE]: onHideFilePreview, + [DataSourceType.NOTION]: onHideNotionPreview, + [DataSourceType.WEB]: onHideWebsitePreview, + }), [onHideFilePreview, onHideNotionPreview, onHideWebsitePreview]) - const handleWebClick = () => { + // Generic click handler for all data source types + const handleClick = (type: DataSourceType) => { if (dataSourceTypeDisable) return - changeType(DataSourceType.WEB) - onHideFilePreview() - onHideNotionPreview() + changeType(type) + // Hide previews for other data source types + Object.entries(hidePreviewMap).forEach(([key, hideFn]) => { + if (key !== type) + hideFn() + }) } return (
- {/* File data source */} -
- - - {t('datasetCreation.stepOne.dataSourceType.file')} - -
- - {/* Notion data source */} -
- - - {t('datasetCreation.stepOne.dataSourceType.notion')} - -
- - {/* Web data source */} - {isWebEnabled && ( -
- - config.isEnabled) + .map(config => ( +
handleClick(config.type)} > - {t('datasetCreation.stepOne.dataSourceType.web')} - -
- )} + + + {t(config.labelKey)} + +
+ ))}
) } diff --git a/web/app/components/datasets/create/step-one/index.tsx b/web/app/components/datasets/create/step-one/index.tsx index bebc3a1c34..38dd17acf8 100644 --- a/web/app/components/datasets/create/step-one/index.tsx +++ b/web/app/components/datasets/create/step-one/index.tsx @@ -201,13 +201,13 @@ const StepOne = ({
- {shouldShowDataSourceTypeList && ( + {shouldShowDataSourceTypeList && dataSourceType && ( <>
{t('datasetCreation.steps.one')}