'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' type AppsProps = { pageType?: PageType } export enum PageType { EXPLORE = 'explore', CREATE = 'create', } const Apps = ({ pageType = PageType.EXPLORE, }: AppsProps) => { const { t } = useTranslation() const { isCurrentWorkspaceManager } = useAppContext() const router = useRouter() const { hasEditPermission } = useContext(ExploreContext) const allCategoriesEn = t('explore.apps.allCategories', { lng: 'en' }) const [currCategory, setCurrCategory] = useTabSearchParams({ defaultTab: allCategoriesEn, }) 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 => item.category === 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') if (!isCurrentWorkspaceManager) { router.push(`/app/${app.id}/overview`) } else { if (app.mode === 'workflow' || app.mode === 'advanced-chat') router.push(`/app/${app.id}/workflow`) router.push(`/app/${app.id}/configuration`) } } 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')}
)}
{isShowCreateModal && ( setIsShowCreateModal(false)} /> )}
) } export default React.memo(Apps)