'use client' import type { FC } from 'react' import React, { useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import { useContext } from 'use-context-selector' import { useSelectedLayoutSegments } from 'next/navigation' import Link from 'next/link' import Toast from '../../base/toast' import Item from './app-nav-item' import cn from '@/utils/classnames' import { fetchInstalledAppList as doFetchInstalledAppList, uninstallApp, updatePinStatus } from '@/service/explore' import ExploreContext from '@/context/explore-context' import Confirm from '@/app/components/base/confirm' import useBreakpoints, { MediaType } from '@/hooks/use-breakpoints' const SelectedDiscoveryIcon = () => ( ) const DiscoveryIcon = () => ( ) const SelectedChatIcon = () => ( ) const ChatIcon = () => ( ) export type IExploreSideBarProps = { controlUpdateInstalledApps: number } const SideBar: FC = ({ controlUpdateInstalledApps, }) => { const { t } = useTranslation() const segments = useSelectedLayoutSegments() const lastSegment = segments.slice(-1)[0] const isDiscoverySelected = lastSegment === 'apps' const isChatSelected = lastSegment === 'chat' const { installedApps, setInstalledApps } = useContext(ExploreContext) const media = useBreakpoints() const isMobile = media === MediaType.mobile const fetchInstalledAppList = async () => { const { installed_apps }: any = await doFetchInstalledAppList() setInstalledApps(installed_apps) } const [showConfirm, setShowConfirm] = useState(false) const [currId, setCurrId] = useState('') const handleDelete = async () => { const id = currId await uninstallApp(id) setShowConfirm(false) Toast.notify({ type: 'success', message: t('common.api.remove'), }) fetchInstalledAppList() } const handleUpdatePinStatus = async (id: string, isPinned: boolean) => { await updatePinStatus(id, isPinned) Toast.notify({ type: 'success', message: t('common.api.success'), }) fetchInstalledAppList() } useEffect(() => { fetchInstalledAppList() }, []) useEffect(() => { fetchInstalledAppList() }, [controlUpdateInstalledApps]) return ( {isDiscoverySelected ? : } {!isMobile && {t('explore.sidebar.discovery')}} {installedApps.length > 0 && ( {t('explore.sidebar.workspace')} {installedApps.map(({ id, is_pinned, uninstallable, app: { name, icon_type, icon, icon_url, icon_background } }) => { return ( handleUpdatePinStatus(id, !is_pinned)} uninstallable={uninstallable} onDelete={(id) => { setCurrId(id) setShowConfirm(true) }} /> ) })} )} {showConfirm && ( setShowConfirm(false)} /> )} ) } export default React.memo(SideBar)
{t('explore.sidebar.workspace')}