From 78fbbded1ad8fa1e209314c2990c8e5abff40ddc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E4=BC=9F=E5=BC=BA?= Date: Thu, 18 May 2023 13:58:10 +0800 Subject: [PATCH] feat: add to workspace api --- web/app/(commonLayout)/explore/layout.tsx | 10 ++--- web/app/components/explore/app-list/index.tsx | 16 ++++++-- web/app/components/explore/index.tsx | 33 ++++++++++++++++ .../explore/installed-app/index.tsx | 1 - web/app/components/explore/sidebar/index.tsx | 39 ++++++++++++------- web/context/explore-context.ts | 13 +++++++ web/models/explore.ts | 6 +++ web/service/explore.ts | 16 +++++++- 8 files changed, 105 insertions(+), 29 deletions(-) create mode 100644 web/app/components/explore/index.tsx create mode 100644 web/context/explore-context.ts diff --git a/web/app/(commonLayout)/explore/layout.tsx b/web/app/(commonLayout)/explore/layout.tsx index 3dbed4d505..3af7bb1e07 100644 --- a/web/app/(commonLayout)/explore/layout.tsx +++ b/web/app/(commonLayout)/explore/layout.tsx @@ -1,19 +1,15 @@ import type { FC } from 'react' import React from 'react' -import Sidebar from '@/app/components/explore/sidebar' - +import ExploreClient from '@/app/components/explore' export type IAppDetail = { children: React.ReactNode } const AppDetail: FC = ({ children }) => { return ( -
- -
+ {children} -
-
+ ) } diff --git a/web/app/components/explore/app-list/index.tsx b/web/app/components/explore/app-list/index.tsx index 0c5c5658c9..ae9e93a50d 100644 --- a/web/app/components/explore/app-list/index.tsx +++ b/web/app/components/explore/app-list/index.tsx @@ -1,18 +1,21 @@ 'use client' import React, { FC, useEffect } from 'react' import { useTranslation } from 'react-i18next' +import { useContext } from 'use-context-selector' +import ExploreContext from '@/context/explore-context' import { App } from '@/models/explore' import Category from '@/app/components/explore/category' import AppCard from '@/app/components/explore/app-card' -import { fetchAppList } from '@/service/explore' +import { fetchAppList, installApp } from '@/service/explore' import CreateAppModal from '@/app/components/explore/create-app-modal' import Loading from '@/app/components/base/loading' import s from './style.module.css' +import Toast from '../../base/toast' const Apps: FC = ({ }) => { const { t } = useTranslation() - + const { setControlUpdateInstalledApps } = useContext(ExploreContext) const [currCategory, setCurrCategory] = React.useState('') const [allList, setAllList] = React.useState([]) const [isLoaded, setIsLoaded] = React.useState(false) @@ -31,8 +34,13 @@ const Apps: FC = ({ }) => { })() }, []) - const handleAddToWorkspace = (appId: string) => { - console.log('handleAddToWorkspace', appId) + const handleAddToWorkspace = async (appId: string) => { + await installApp(appId) + Toast.notify({ + type: 'success', + message: t('common.api.success'), + }) + setControlUpdateInstalledApps(Date.now()) } const [currApp, setCurrApp] = React.useState(null) diff --git a/web/app/components/explore/index.tsx b/web/app/components/explore/index.tsx new file mode 100644 index 0000000000..c098bcfa0e --- /dev/null +++ b/web/app/components/explore/index.tsx @@ -0,0 +1,33 @@ +'use client' +import React, { FC } from 'react' +import ExploreContext from '@/context/explore-context' +import Sidebar from '@/app/components/explore/sidebar' + + +export interface IExploreProps { + children: React.ReactNode +} + +const Explore: FC = ({ + children +}) => { + const [controlUpdateInstalledApps, setControlUpdateInstalledApps] = React.useState(0) + return ( +
+ + +
+ {children} +
+
+
+ ) +} +export default React.memo(Explore) diff --git a/web/app/components/explore/installed-app/index.tsx b/web/app/components/explore/installed-app/index.tsx index e681e5b86f..fa09501584 100644 --- a/web/app/components/explore/installed-app/index.tsx +++ b/web/app/components/explore/installed-app/index.tsx @@ -2,7 +2,6 @@ import React, { FC, useEffect } from 'react' import { App } from '@/types/app' import ChatApp from '@/app/components/share/chat' - export interface IInstalledAppProps { id: string } diff --git a/web/app/components/explore/sidebar/index.tsx b/web/app/components/explore/sidebar/index.tsx index 886dc00ff6..546bdcd51c 100644 --- a/web/app/components/explore/sidebar/index.tsx +++ b/web/app/components/explore/sidebar/index.tsx @@ -1,21 +1,12 @@ 'use client' -import React, { FC } from 'react' +import React, { FC, useEffect } from 'react' import { useTranslation } from 'react-i18next' import cn from 'classnames' import { useSelectedLayoutSegments } from 'next/navigation' import Link from 'next/link' import Item from './app-nav-item' - -const list = [ - { - id: '1', - name: 'Story Bot', - }, - { - id: '2', - name: 'Fancy title' - } -] +import { InstalledApp } from '@/models/explore' +import { fetchInstalledAppList as doFetchInstalledAppList } from '@/service/explore' const SelectedDiscoveryIcon = () => ( @@ -29,11 +20,29 @@ const DiscoveryIcon = () => ( ) -const SideBar: FC = () => { +const SideBar: FC<{ + controlUpdateInstalledApps: number +}> = ({ + controlUpdateInstalledApps +}) => { const { t } = useTranslation() const segments = useSelectedLayoutSegments() const lastSegment = segments.slice(-1)[0] const isDiscoverySelected = lastSegment === 'apps' + const [installedApps, setInstalledApps] = React.useState([]) + + const fetchInstalledAppList = async () => { + const {installed_apps} : any = await doFetchInstalledAppList() + setInstalledApps(installed_apps) + } + + useEffect(() => { + fetchInstalledAppList() + }, []) + + useEffect(() => { + fetchInstalledAppList() + }, [controlUpdateInstalledApps]) return (
@@ -47,11 +56,11 @@ const SideBar: FC = () => {
{t('explore.sidebar.discovery')}
- {list.length > 0 && ( + {installedApps.length > 0 && (
{t('explore.sidebar.workspace')}
- {list.map(({id, name}) => { + {installedApps.map(({app : {id, name}}) => { return ( ) diff --git a/web/context/explore-context.ts b/web/context/explore-context.ts new file mode 100644 index 0000000000..e819d30cb2 --- /dev/null +++ b/web/context/explore-context.ts @@ -0,0 +1,13 @@ +import { createContext } from 'use-context-selector' + +type IExplore = { + controlUpdateInstalledApps: number + setControlUpdateInstalledApps: (controlUpdateInstalledApps: number) => void +} + +const ExploreContext = createContext({ + controlUpdateInstalledApps: 0, + setControlUpdateInstalledApps: () => { }, +}) + +export default ExploreContext diff --git a/web/models/explore.ts b/web/models/explore.ts index ac3ae2a40b..65f2e175fb 100644 --- a/web/models/explore.ts +++ b/web/models/explore.ts @@ -20,4 +20,10 @@ export type App = { install_count: number; installed: boolean; editable: boolean; +} + +export type InstalledApp = { + app: AppBasicInfo; + id: string; + uninstallable: boolean } \ No newline at end of file diff --git a/web/service/explore.ts b/web/service/explore.ts index 093e83f267..39be48fbc9 100644 --- a/web/service/explore.ts +++ b/web/service/explore.ts @@ -1,5 +1,17 @@ -import { get } from './base' +import { get, post } from './base' export const fetchAppList = () => { return get('/explore/apps') -} \ No newline at end of file +} + +export const fetchInstalledAppList = () => { + return get('/installed-apps') +} + +export const installApp = (id: string) => { + return post('/installed-apps', { + body: { + app_id: id + } + }) +}