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 (