'use client' import type { Dispatch, SetStateAction } from 'react' import React, { useCallback, useEffect, useMemo, useState } from 'react' import { Trans, useTranslation } from 'react-i18next' import type { OnSelectBlock } from '@/app/components/workflow/types' import type { ViewType } from '@/app/components/workflow/block-selector/view-type-select' import { RiMoreLine } from '@remixicon/react' import Loading from '@/app/components/base/loading' import Link from 'next/link' import { getMarketplaceUrl } from '@/utils/var' import { useRAGRecommendedPlugins } from '@/service/use-tools' import List from './list' import { getFormattedPlugin } from '@/app/components/plugins/marketplace/utils' import { ArrowDownRoundFill } from '@/app/components/base/icons/src/vender/solid/arrows' type RAGToolRecommendationsProps = { viewType: ViewType onSelect: OnSelectBlock onTagsChange: Dispatch> } const STORAGE_KEY = 'workflow_rag_recommendations_collapsed' const RAGToolRecommendations = ({ viewType, onSelect, onTagsChange, }: RAGToolRecommendationsProps) => { const { t } = useTranslation() const [isCollapsed, setIsCollapsed] = useState(() => { if (typeof window === 'undefined') return false const stored = window.localStorage.getItem(STORAGE_KEY) return stored === 'true' }) useEffect(() => { if (typeof window === 'undefined') return const stored = window.localStorage.getItem(STORAGE_KEY) if (stored !== null) setIsCollapsed(stored === 'true') }, []) useEffect(() => { if (typeof window === 'undefined') return window.localStorage.setItem(STORAGE_KEY, String(isCollapsed)) }, [isCollapsed]) const { data: ragRecommendedPlugins, isLoading: isLoadingRAGRecommendedPlugins, isFetching: isFetchingRAGRecommendedPlugins, } = useRAGRecommendedPlugins() const recommendedPlugins = useMemo(() => { if (ragRecommendedPlugins) return ragRecommendedPlugins.installed_recommended_plugins return [] }, [ragRecommendedPlugins]) const unInstalledPlugins = useMemo(() => { if (ragRecommendedPlugins) return (ragRecommendedPlugins.uninstalled_recommended_plugins).map(getFormattedPlugin) return [] }, [ragRecommendedPlugins]) const loadMore = useCallback(() => { onTagsChange((prev) => { if (prev.includes('rag')) return prev return [...prev, 'rag'] }) }, [onTagsChange]) return (
{!isCollapsed && ( <> {/* For first time loading, show loading */} {isLoadingRAGRecommendedPlugins && (
)} {!isFetchingRAGRecommendedPlugins && recommendedPlugins.length === 0 && unInstalledPlugins.length === 0 && (

), }} />

)} {(recommendedPlugins.length > 0 || unInstalledPlugins.length > 0) && ( <>
{t('common.operation.more')}
)} )}
) } export default React.memo(RAGToolRecommendations)