'use client' import React from 'react' import cn from 'classnames' import { useRouter } from 'next/navigation' import { useTranslation } from 'react-i18next' import { useContext } from 'use-context-selector' import useSWR from 'swr' import Toast from '../../base/toast' import s from './style.module.css' import ExploreContext from '@/context/explore-context' import type { App } from '@/models/explore' import Category from '@/app/components/explore/category' import AppCard from '@/app/components/explore/app-card' import { fetchAppDetail, fetchAppList } from '@/service/explore' import { importApp } from '@/service/apps' import { useTabSearchParams } from '@/hooks/use-tab-searchparams' import CreateAppModal from '@/app/components/explore/create-app-modal' import type { CreateAppModalProps } from '@/app/components/explore/create-app-modal' import Loading from '@/app/components/base/loading' import { NEED_REFRESH_APP_LIST_KEY } from '@/config' import { useAppContext } from '@/context/app-context' import { getRedirection } from '@/utils/app-redirection' import TabSliderNew from '@/app/components/base/tab-slider-new' import { DotsGrid } from '@/app/components/base/icons/src/vender/line/general' import { Route } from '@/app/components/base/icons/src/vender/line/mapsAndTravel' import { AiText, ChatBot, CuteRobot, } from '@/app/components/base/icons/src/vender/line/communication' type AppsProps = { pageType?: PageType } export enum PageType { EXPLORE = 'explore', CREATE = 'create', } const Apps = ({ pageType = PageType.EXPLORE, }: AppsProps) => { const { t } = useTranslation() const { isCurrentWorkspaceManager } = useAppContext() const { push } = useRouter() const { hasEditPermission } = useContext(ExploreContext) const allCategoriesEn = t('explore.apps.allCategories', { lng: 'en' }) const [currCategory, setCurrCategory] = useTabSearchParams({ defaultTab: allCategoriesEn, disableSearchParams: pageType !== PageType.EXPLORE, }) const options = [ { value: allCategoriesEn, text: t('app.types.all'), icon: }, { value: 'chat', text: t('app.types.chatbot'), icon: }, { value: 'agent-chat', text: t('app.types.agent'), icon: }, { value: 'completion', text: t('app.newApp.completeApp'), icon: }, { value: 'workflow', text: t('app.types.workflow'), icon: }, ] const { data: { categories, allList }, } = useSWR( ['/explore/apps'], () => fetchAppList().then(({ categories, recommended_apps }) => ({ categories, allList: recommended_apps.sort((a, b) => a.position - b.position), })), { fallbackData: { categories: [], allList: [], }, }, ) const currList = currCategory === allCategoriesEn ? allList : allList.filter((item) => { if (pageType === PageType.EXPLORE) return item.category === currCategory else if (currCategory === 'chat') return item.app.mode === 'chat' || item.app.mode === 'advanced-chat' else return item.app.mode === currCategory }) const [currApp, setCurrApp] = React.useState(null) const [isShowCreateModal, setIsShowCreateModal] = React.useState(false) const onCreate: CreateAppModalProps['onConfirm'] = async ({ name, icon, icon_background, description, }) => { const { export_data } = await fetchAppDetail( currApp?.app.id as string, ) try { const app = await importApp({ data: export_data, name, icon, icon_background, description, }) setIsShowCreateModal(false) Toast.notify({ type: 'success', message: t('app.newApp.appCreated'), }) localStorage.setItem(NEED_REFRESH_APP_LIST_KEY, '1') getRedirection(isCurrentWorkspaceManager, app, push) } catch (e) { Toast.notify({ type: 'error', message: t('app.newApp.appCreateFailed') }) } } if (!categories) { return (
) } return (
{pageType === PageType.EXPLORE && (
{t('explore.apps.title')}
{t('explore.apps.description')}
)} {pageType === PageType.EXPLORE && ( )} {pageType !== PageType.EXPLORE && ( )}
{isShowCreateModal && ( setIsShowCreateModal(false)} /> )}
) } export default React.memo(Apps)