'use client' import type { CreateAppModalProps } from '../explore/create-app-modal' import type { TryAppSelection } from '@/types/try-app' import { useCallback, useRef, useState } from 'react' import { useTranslation } from 'react-i18next' import { useEducationInit } from '@/app/education-apply/hooks' import AppListContext from '@/context/app-list-context' import useDocumentTitle from '@/hooks/use-document-title' import { useImportDSL } from '@/hooks/use-import-dsl' import { DSLImportMode } from '@/models/app' import dynamic from '@/next/dynamic' import { useRouter, useSearchParams } from '@/next/navigation' import { fetchAppDetail } from '@/service/explore' import { trackCreateApp } from '@/utils/create-app-tracking' import List from './list' const DSLConfirmModal = dynamic(() => import('../app/create-from-dsl-modal/dsl-confirm-modal'), { ssr: false }) const CreateAppModal = dynamic(() => import('../explore/create-app-modal'), { ssr: false }) const TryApp = dynamic(() => import('../explore/try-app'), { ssr: false }) const ImportFromMarketplaceTemplateModal = dynamic(() => import('./import-from-marketplace-template-modal'), { ssr: false }) const Apps = () => { const { t } = useTranslation() const searchParams = useSearchParams() const { replace } = useRouter() const templateId = searchParams.get('template-id') const templateDismissedRef = useRef(false) useDocumentTitle(t('menus.apps', { ns: 'common' })) useEducationInit() const [currentTryAppParams, setCurrentTryAppParams] = useState(undefined) const currentCreateAppModeRef = useRef(null) const currApp = currentTryAppParams?.app const [isShowTryAppPanel, setIsShowTryAppPanel] = useState(false) const hideTryAppPanel = useCallback(() => { setIsShowTryAppPanel(false) }, []) const setShowTryAppPanel = (showTryAppPanel: boolean, params?: TryAppSelection) => { if (showTryAppPanel) setCurrentTryAppParams(params) else setCurrentTryAppParams(undefined) setIsShowTryAppPanel(showTryAppPanel) } const [isShowCreateModal, setIsShowCreateModal] = useState(false) const handleShowFromTryApp = useCallback(() => { setIsShowCreateModal(true) }, []) const trackCurrentCreateApp = useCallback(() => { if (!currentCreateAppModeRef.current) return trackCreateApp({ appMode: currentCreateAppModeRef.current }) }, []) const [controlRefreshList, setControlRefreshList] = useState(0) const [controlHideCreateFromTemplatePanel, setControlHideCreateFromTemplatePanel] = useState(0) const onSuccess = useCallback(() => { setControlRefreshList(prev => prev + 1) setControlHideCreateFromTemplatePanel(prev => prev + 1) }, []) const [showDSLConfirmModal, setShowDSLConfirmModal] = useState(false) const handleCloseTemplateModal = useCallback(() => { templateDismissedRef.current = true const params = new URLSearchParams(searchParams.toString()) params.delete('template-id') const query = params.toString() replace(query ? `?${query}` : window.location.pathname, { scroll: false }) }, [searchParams, replace]) const { handleImportDSL, handleImportDSLConfirm, versions, isFetching, } = useImportDSL() const onConfirmDSL = useCallback(async () => { await handleImportDSLConfirm({ onSuccess: () => { trackCurrentCreateApp() onSuccess() }, }) }, [handleImportDSLConfirm, onSuccess, trackCurrentCreateApp]) const handleMarketplaceTemplateConfirm = useCallback(async (dslContent: string) => { await handleImportDSL({ mode: DSLImportMode.YAML_CONTENT, yaml_content: dslContent, }, { onSuccess: () => { handleCloseTemplateModal() onSuccess() }, onPending: () => { handleCloseTemplateModal() setShowDSLConfirmModal(true) }, }) }, [handleImportDSL, handleCloseTemplateModal, onSuccess]) const onCreate: CreateAppModalProps['onConfirm'] = useCallback(async ({ name, icon_type, icon, icon_background, description, }) => { hideTryAppPanel() const { export_data, mode } = await fetchAppDetail( currApp?.app.id as string, ) currentCreateAppModeRef.current = mode const payload = { mode: DSLImportMode.YAML_CONTENT, yaml_content: export_data, name, icon_type, icon, icon_background, description, } await handleImportDSL(payload, { onSuccess: () => { trackCurrentCreateApp() setIsShowCreateModal(false) }, onPending: () => { setShowDSLConfirmModal(true) }, }) }, [currApp?.app.id, handleImportDSL, hideTryAppPanel, trackCurrentCreateApp]) return (
{isShowTryAppPanel && ( )} { showDSLConfirmModal && ( setShowDSLConfirmModal(false)} onConfirm={onConfirmDSL} confirmDisabled={isFetching} /> ) } {isShowCreateModal && ( setIsShowCreateModal(false)} /> )} {templateId && !templateDismissedRef.current && ( )}
) } export default Apps