import { useTranslation } from 'react-i18next' import { useRouter } from 'next/navigation' import { useContext, useContextSelector } from 'use-context-selector' import { RiArrowDownSLine } from '@remixicon/react' import React, { useCallback, useState } from 'react' import AppIcon from '../base/app-icon' import SwitchAppModal from '../app/switch-app-modal' import s from './style.module.css' import cn from '@/utils/classnames' import { PortalToFollowElem, PortalToFollowElemContent, PortalToFollowElemTrigger, } from '@/app/components/base/portal-to-follow-elem' import Divider from '@/app/components/base/divider' import Confirm from '@/app/components/base/confirm' import { useStore as useAppStore } from '@/app/components/app/store' import { ToastContext } from '@/app/components/base/toast' import AppsContext, { useAppContext } from '@/context/app-context' import { useProviderContext } from '@/context/provider-context' import { copyApp, deleteApp, exportAppConfig, updateAppInfo } from '@/service/apps' import DuplicateAppModal from '@/app/components/app/duplicate-modal' import type { DuplicateAppModalProps } from '@/app/components/app/duplicate-modal' import CreateAppModal from '@/app/components/explore/create-app-modal' import { AiText, ChatBot, CuteRobot } from '@/app/components/base/icons/src/vender/solid/communication' import { Route } from '@/app/components/base/icons/src/vender/solid/mapsAndTravel' import type { CreateAppModalProps } from '@/app/components/explore/create-app-modal' import { NEED_REFRESH_APP_LIST_KEY } from '@/config' import { getRedirection } from '@/utils/app-redirection' import UpdateDSLModal from '@/app/components/workflow/update-dsl-modal' import type { EnvironmentVariable } from '@/app/components/workflow/types' import DSLExportConfirmModal from '@/app/components/workflow/dsl-export-confirm-modal' import { fetchWorkflowDraft } from '@/service/workflow' export type IAppInfoProps = { expand: boolean } const AppInfo = ({ expand }: IAppInfoProps) => { const { t } = useTranslation() const { notify } = useContext(ToastContext) const { replace } = useRouter() const { onPlanInfoChanged } = useProviderContext() const appDetail = useAppStore(state => state.appDetail) const setAppDetail = useAppStore(state => state.setAppDetail) const [open, setOpen] = useState(false) const [showEditModal, setShowEditModal] = useState(false) const [showDuplicateModal, setShowDuplicateModal] = useState(false) const [showConfirmDelete, setShowConfirmDelete] = useState(false) const [showSwitchTip, setShowSwitchTip] = useState('') const [showSwitchModal, setShowSwitchModal] = useState(false) const [showImportDSLModal, setShowImportDSLModal] = useState(false) const [secretEnvList, setSecretEnvList] = useState([]) const mutateApps = useContextSelector( AppsContext, state => state.mutateApps, ) const onEdit: CreateAppModalProps['onConfirm'] = useCallback(async ({ name, icon_type, icon, icon_background, description, use_icon_as_answer_icon, }) => { if (!appDetail) return try { const app = await updateAppInfo({ appID: appDetail.id, name, icon_type, icon, icon_background, description, use_icon_as_answer_icon, }) setShowEditModal(false) notify({ type: 'success', message: t('app.editDone'), }) setAppDetail(app) mutateApps() } catch (e) { notify({ type: 'error', message: t('app.editFailed') }) } }, [appDetail, mutateApps, notify, setAppDetail, t]) const onCopy: DuplicateAppModalProps['onConfirm'] = async ({ name, icon_type, icon, icon_background }) => { if (!appDetail) return try { const newApp = await copyApp({ appID: appDetail.id, name, icon_type, icon, icon_background, mode: appDetail.mode, }) setShowDuplicateModal(false) notify({ type: 'success', message: t('app.newApp.appCreated'), }) localStorage.setItem(NEED_REFRESH_APP_LIST_KEY, '1') mutateApps() onPlanInfoChanged() getRedirection(true, newApp, replace) } catch (e) { notify({ type: 'error', message: t('app.newApp.appCreateFailed') }) } } const onExport = async (include = false) => { if (!appDetail) return try { const { data } = await exportAppConfig({ appID: appDetail.id, include, }) const a = document.createElement('a') const file = new Blob([data], { type: 'application/yaml' }) a.href = URL.createObjectURL(file) a.download = `${appDetail.name}.yml` a.click() } catch (e) { notify({ type: 'error', message: t('app.exportFailed') }) } } const exportCheck = async () => { if (!appDetail) return if (appDetail.mode !== 'workflow' && appDetail.mode !== 'advanced-chat') { onExport() return } try { const workflowDraft = await fetchWorkflowDraft(`/apps/${appDetail.id}/workflows/draft`) const list = (workflowDraft.environment_variables || []).filter(env => env.value_type === 'secret') if (list.length === 0) { onExport() return } setSecretEnvList(list) } catch (e) { notify({ type: 'error', message: t('app.exportFailed') }) } } const onConfirmDelete = useCallback(async () => { if (!appDetail) return try { await deleteApp(appDetail.id) notify({ type: 'success', message: t('app.appDeleted') }) mutateApps() onPlanInfoChanged() setAppDetail() replace('/apps') } catch (e: any) { notify({ type: 'error', message: `${t('app.appDeleteFailed')}${'message' in e ? `: ${e.message}` : ''}`, }) } setShowConfirmDelete(false) }, [appDetail, mutateApps, notify, onPlanInfoChanged, replace, t]) const { isCurrentWorkspaceEditor } = useAppContext() if (!appDetail) return null return (
{ if (isCurrentWorkspaceEditor) setOpen(v => !v) }} className='block' >
{appDetail.mode === 'advanced-chat' && ( )} {appDetail.mode === 'agent-chat' && ( )} {appDetail.mode === 'chat' && ( )} {appDetail.mode === 'completion' && ( )} {appDetail.mode === 'workflow' && ( )}
{expand && (
{appDetail.name}
{isCurrentWorkspaceEditor && }
{appDetail.mode === 'advanced-chat' && ( <>
{t('app.types.chatbot').toUpperCase()}
{t('app.newApp.advanced').toUpperCase()}
)} {appDetail.mode === 'agent-chat' && (
{t('app.types.agent').toUpperCase()}
)} {appDetail.mode === 'chat' && ( <>
{t('app.types.chatbot').toUpperCase()}
{(t('app.newApp.basic').toUpperCase())}
)} {appDetail.mode === 'completion' && ( <>
{t('app.types.completion').toUpperCase()}
{(t('app.newApp.basic').toUpperCase())}
)} {appDetail.mode === 'workflow' && (
{t('app.types.workflow').toUpperCase()}
)}
)}
{/* header */}
{appDetail.mode === 'advanced-chat' && ( )} {appDetail.mode === 'agent-chat' && ( )} {appDetail.mode === 'chat' && ( )} {appDetail.mode === 'completion' && ( )} {appDetail.mode === 'workflow' && ( )}
{appDetail.name}
{appDetail.mode === 'advanced-chat' && ( <>
{t('app.types.chatbot').toUpperCase()}
{t('app.newApp.advanced').toUpperCase()}
)} {appDetail.mode === 'agent-chat' && (
{t('app.types.agent').toUpperCase()}
)} {appDetail.mode === 'chat' && ( <>
{t('app.types.chatbot').toUpperCase()}
{(t('app.newApp.basic').toUpperCase())}
)} {appDetail.mode === 'completion' && ( <>
{t('app.types.completion').toUpperCase()}
{(t('app.newApp.basic').toUpperCase())}
)} {appDetail.mode === 'workflow' && (
{t('app.types.workflow').toUpperCase()}
)}
{/* description */} {appDetail.description && (
{appDetail.description}
)} {/* operations */}
{ setOpen(false) setShowEditModal(true) }}> {t('app.editApp')}
{ setOpen(false) setShowDuplicateModal(true) }}> {t('app.duplicate')}
{(appDetail.mode === 'completion' || appDetail.mode === 'chat') && ( <>
setShowSwitchTip(appDetail.mode)} onMouseLeave={() => setShowSwitchTip('')} onClick={() => { setOpen(false) setShowSwitchModal(true) }} > {t('app.switch')}
)}
{t('app.export')}
{ (appDetail.mode === 'advanced-chat' || appDetail.mode === 'workflow') && (
{ setOpen(false) setShowImportDSLModal(true) }}> {t('workflow.common.importDSL')}
) }
{ setOpen(false) setShowConfirmDelete(true) }}> {t('common.operation.delete')}
{/* switch tip */}
{showSwitchTip === 'chat' ? t('app.newApp.advanced') : t('app.types.workflow')} BETA
{t('app.newApp.advancedFor').toLocaleUpperCase()}
{t('app.newApp.advancedDescription')}
{showSwitchModal && ( setShowSwitchModal(false)} onSuccess={() => setShowSwitchModal(false)} /> )} {showEditModal && ( setShowEditModal(false)} /> )} {showDuplicateModal && ( setShowDuplicateModal(false)} /> )} {showConfirmDelete && ( setShowConfirmDelete(false)} /> )} {showImportDSLModal && ( setShowImportDSLModal(false)} onBackup={exportCheck} /> )} {secretEnvList.length > 0 && ( setSecretEnvList([])} /> )}
) } export default React.memo(AppInfo)