Commit 29bef1e3 authored by JzoNg's avatar JzoNg

app sidebar auto collapse

parent e36d62f0
...@@ -13,6 +13,7 @@ import { useAppContext } from '@/context/app-context' ...@@ -13,6 +13,7 @@ import { useAppContext } from '@/context/app-context'
import Loading from '@/app/components/base/loading' import Loading from '@/app/components/base/loading'
import { BarChartSquare02, FileHeart02, PromptEngineering, TerminalSquare } from '@/app/components/base/icons/src/vender/line/development' import { BarChartSquare02, FileHeart02, PromptEngineering, TerminalSquare } from '@/app/components/base/icons/src/vender/line/development'
import { BarChartSquare02 as BarChartSquare02Solid, FileHeart02 as FileHeart02Solid, PromptEngineering as PromptEngineeringSolid, TerminalSquare as TerminalSquareSolid } from '@/app/components/base/icons/src/vender/solid/development' import { BarChartSquare02 as BarChartSquare02Solid, FileHeart02 as FileHeart02Solid, PromptEngineering as PromptEngineeringSolid, TerminalSquare as TerminalSquareSolid } from '@/app/components/base/icons/src/vender/solid/development'
import useBreakpoints, { MediaType } from '@/hooks/use-breakpoints'
export type IAppDetailLayoutProps = { export type IAppDetailLayoutProps = {
children: React.ReactNode children: React.ReactNode
...@@ -27,8 +28,10 @@ const AppDetailLayout: FC<IAppDetailLayoutProps> = (props) => { ...@@ -27,8 +28,10 @@ const AppDetailLayout: FC<IAppDetailLayoutProps> = (props) => {
const { t } = useTranslation() const { t } = useTranslation()
const router = useRouter() const router = useRouter()
const pathname = usePathname() const pathname = usePathname()
const media = useBreakpoints()
const isMobile = media === MediaType.mobile
const { isCurrentWorkspaceManager } = useAppContext() const { isCurrentWorkspaceManager } = useAppContext()
const { appDetail, setAppDetail } = useStore() const { appDetail, setAppDetail, setAppSiderbarExpand } = useStore()
const [navigation, setNavigation] = useState<Array<{ const [navigation, setNavigation] = useState<Array<{
name: string name: string
href: string href: string
...@@ -72,9 +75,15 @@ const AppDetailLayout: FC<IAppDetailLayoutProps> = (props) => { ...@@ -72,9 +75,15 @@ const AppDetailLayout: FC<IAppDetailLayoutProps> = (props) => {
}, [t]) }, [t])
useEffect(() => { useEffect(() => {
if (appDetail) if (appDetail) {
document.title = `${(appDetail.name || 'App')} - Dify` document.title = `${(appDetail.name || 'App')} - Dify`
}, [appDetail]) const localeMode = localStorage.getItem('app-detail-collapse-or-expand') || 'expand'
const mode = isMobile ? 'collapse' : 'expand'
setAppSiderbarExpand(isMobile ? mode : localeMode)
if (appDetail.mode === 'advanced-chat' || appDetail.mode === 'workflow')
setAppSiderbarExpand('collapse')
}
}, [appDetail, isMobile, setAppSiderbarExpand])
useEffect(() => { useEffect(() => {
setAppDetail() setAppDetail()
......
...@@ -156,7 +156,7 @@ const AppInfo = ({ expand }: IAppInfoProps) => { ...@@ -156,7 +156,7 @@ const AppInfo = ({ expand }: IAppInfoProps) => {
> >
<div className={cn('flex cursor-pointer p-1 rounded-lg hover:bg-gray-100', open && 'bg-gray-100')}> <div className={cn('flex cursor-pointer p-1 rounded-lg hover:bg-gray-100', open && 'bg-gray-100')}>
<div className='shrink-0 mr-2'> <div className='shrink-0 mr-2'>
<AppIcon size="large" icon={appDetail.icon} background={appDetail.icon_background} /> <AppIcon size={expand ? 'large' : 'medium'} icon={appDetail.icon} background={appDetail.icon_background} />
</div> </div>
{expand && ( {expand && (
<div className="grow w-0"> <div className="grow w-0">
...@@ -244,7 +244,7 @@ const AppInfo = ({ expand }: IAppInfoProps) => { ...@@ -244,7 +244,7 @@ const AppInfo = ({ expand }: IAppInfoProps) => {
}}> }}>
<span className='text-gray-700 text-sm leading-5'>{t('app.editApp')}</span> <span className='text-gray-700 text-sm leading-5'>{t('app.editApp')}</span>
</div> </div>
{appDetail.mode !== 'completion' && appDetail.model_config.prompt_type !== 'advanced' && ( {appDetail.mode !== 'completion' && appDetail.model_config?.prompt_type !== 'advanced' && (
<> <>
<div className='h-9 py-2 px-3 mx-1 flex items-center hover:bg-gray-50 rounded-lg cursor-pointer' onClick={() => { <div className='h-9 py-2 px-3 mx-1 flex items-center hover:bg-gray-50 rounded-lg cursor-pointer' onClick={() => {
setOpen(false) setOpen(false)
...@@ -257,7 +257,7 @@ const AppInfo = ({ expand }: IAppInfoProps) => { ...@@ -257,7 +257,7 @@ const AppInfo = ({ expand }: IAppInfoProps) => {
</div> </div>
</> </>
)} )}
{(appDetail.mode === 'completion' || appDetail.model_config.prompt_type === 'advanced') && ( {(appDetail.mode === 'completion' || appDetail.model_config?.prompt_type === 'advanced') && (
<> <>
<Divider className="!my-1" /> <Divider className="!my-1" />
<div <div
......
import React, { useCallback, useState } from 'react' import React, { useCallback, useEffect, useState } from 'react'
import NavLink from './navLink' import NavLink from './navLink'
import type { NavIcon } from './navLink' import type { NavIcon } from './navLink'
import AppBasic from './basic' import AppBasic from './basic'
...@@ -8,8 +8,7 @@ import { ...@@ -8,8 +8,7 @@ import {
AlignLeft01, AlignLeft01,
AlignRight01, AlignRight01,
} from '@/app/components/base/icons/src/vender/line/layout' } from '@/app/components/base/icons/src/vender/line/layout'
import { useEventEmitterContextContext } from '@/context/event-emitter' import { useStore as useAppStore } from '@/app/components/app/store'
import { APP_SIDEBAR_SHOULD_COLLAPSE } from '@/app/components/app/configuration/debug/types'
export type IAppDetailNavProps = { export type IAppDetailNavProps = {
iconType?: 'app' | 'dataset' | 'notion' iconType?: 'app' | 'dataset' | 'notion'
...@@ -27,28 +26,26 @@ export type IAppDetailNavProps = { ...@@ -27,28 +26,26 @@ export type IAppDetailNavProps = {
} }
const AppDetailNav = ({ title, desc, icon, icon_background, navigation, extraInfo, iconType = 'app' }: IAppDetailNavProps) => { const AppDetailNav = ({ title, desc, icon, icon_background, navigation, extraInfo, iconType = 'app' }: IAppDetailNavProps) => {
const localeMode = localStorage.getItem('app-detail-collapse-or-expand') || 'expand' const { appSidebarExpand, setAppSiderbarExpand } = useAppStore()
const media = useBreakpoints() const media = useBreakpoints()
const isMobile = media === MediaType.mobile const isMobile = media === MediaType.mobile
const mode = isMobile ? 'collapse' : 'expand' const [modeState, setModeState] = useState(appSidebarExpand)
const [modeState, setModeState] = useState(isMobile ? mode : localeMode)
const expand = modeState === 'expand' const expand = modeState === 'expand'
const handleToggle = useCallback(() => { const handleToggle = useCallback(() => {
setModeState((prev) => { setModeState((prev) => {
const next = prev === 'expand' ? 'collapse' : 'expand' const next = prev === 'expand' ? 'collapse' : 'expand'
localStorage.setItem('app-detail-collapse-or-expand', next) setAppSiderbarExpand(next)
return next return next
}) })
}, []) }, [setAppSiderbarExpand])
const { eventEmitter } = useEventEmitterContextContext() useEffect(() => {
eventEmitter?.useSubscription((v: any) => { if (appSidebarExpand) {
if (v.type === APP_SIDEBAR_SHOULD_COLLAPSE) { localStorage.setItem('app-detail-collapse-or-expand', appSidebarExpand)
setModeState('collapse') setModeState(appSidebarExpand)
localStorage.setItem('app-detail-collapse-or-expand', 'collapse')
} }
}) }, [appSidebarExpand])
return ( return (
<div <div
......
...@@ -15,5 +15,4 @@ export type DebugWithSingleOrMultipleModelConfigs = { ...@@ -15,5 +15,4 @@ export type DebugWithSingleOrMultipleModelConfigs = {
} }
export const APP_CHAT_WITH_MULTIPLE_MODEL = 'APP_CHAT_WITH_MULTIPLE_MODEL' export const APP_CHAT_WITH_MULTIPLE_MODEL = 'APP_CHAT_WITH_MULTIPLE_MODEL'
export const APP_CHAT_WITH_MULTIPLE_MODEL_RESTART = 'APP_CHAT_WITH_MULTIPLE_MODEL_RESTART' export const APP_CHAT_WITH_MULTIPLE_MODEL_RESTART = 'APP_CHAT_WITH_MULTIPLE_MODEL_RESTART'
export const APP_SIDEBAR_SHOULD_COLLAPSE = 'APP_SIDEBAR_SHOULD_COLLAPSE'
export const ORCHESTRATE_CHANGED = 'ORCHESTRATE_CHANGED' export const ORCHESTRATE_CHANGED = 'ORCHESTRATE_CHANGED'
...@@ -19,7 +19,6 @@ import { ...@@ -19,7 +19,6 @@ import {
useFormattingChangedDispatcher, useFormattingChangedDispatcher,
} from './debug/hooks' } from './debug/hooks'
import type { ModelAndParameter } from './debug/types' import type { ModelAndParameter } from './debug/types'
import { APP_SIDEBAR_SHOULD_COLLAPSE } from './debug/types'
import PublishWithMultipleModel from './debug/debug-with-multiple-model/publish-with-multiple-model' import PublishWithMultipleModel from './debug/debug-with-multiple-model/publish-with-multiple-model'
import type { import type {
AnnotationReplyConfig, AnnotationReplyConfig,
...@@ -57,7 +56,7 @@ import type { FormValue } from '@/app/components/header/account-setting/model-pr ...@@ -57,7 +56,7 @@ import type { FormValue } from '@/app/components/header/account-setting/model-pr
import { useTextGenerationCurrentProviderAndModelAndModelList } from '@/app/components/header/account-setting/model-provider-page/hooks' import { useTextGenerationCurrentProviderAndModelAndModelList } from '@/app/components/header/account-setting/model-provider-page/hooks'
import { fetchCollectionList } from '@/service/tools' import { fetchCollectionList } from '@/service/tools'
import { type Collection } from '@/app/components/tools/types' import { type Collection } from '@/app/components/tools/types'
import { useEventEmitterContextContext } from '@/context/event-emitter' import { useStore as useAppStore } from '@/app/components/app/store'
type PublichConfig = { type PublichConfig = {
modelConfig: ModelConfig modelConfig: ModelConfig
...@@ -67,6 +66,7 @@ type PublichConfig = { ...@@ -67,6 +66,7 @@ type PublichConfig = {
const Configuration: FC = () => { const Configuration: FC = () => {
const { t } = useTranslation() const { t } = useTranslation()
const { notify } = useContext(ToastContext) const { notify } = useContext(ToastContext)
const { setAppSiderbarExpand } = useAppStore()
const [formattingChanged, setFormattingChanged] = useState(false) const [formattingChanged, setFormattingChanged] = useState(false)
const { setShowAccountSettingModal } = useModalContext() const { setShowAccountSettingModal } = useModalContext()
const [hasFetchedDetail, setHasFetchedDetail] = useState(false) const [hasFetchedDetail, setHasFetchedDetail] = useState(false)
...@@ -645,7 +645,6 @@ const Configuration: FC = () => { ...@@ -645,7 +645,6 @@ const Configuration: FC = () => {
const [showUseGPT4Confirm, setShowUseGPT4Confirm] = useState(false) const [showUseGPT4Confirm, setShowUseGPT4Confirm] = useState(false)
const { eventEmitter } = useEventEmitterContextContext()
const { const {
debugWithMultipleModel, debugWithMultipleModel,
multipleModelConfigs, multipleModelConfigs,
...@@ -660,9 +659,7 @@ const Configuration: FC = () => { ...@@ -660,9 +659,7 @@ const Configuration: FC = () => {
{ id: `${Date.now()}-no-repeat`, model: '', provider: '', parameters: {} }, { id: `${Date.now()}-no-repeat`, model: '', provider: '', parameters: {} },
], ],
) )
eventEmitter?.emit({ setAppSiderbarExpand('collapse')
type: APP_SIDEBAR_SHOULD_COLLAPSE,
} as any)
} }
if (isLoading) { if (isLoading) {
......
...@@ -3,13 +3,17 @@ import type { App } from '@/types/app' ...@@ -3,13 +3,17 @@ import type { App } from '@/types/app'
type State = { type State = {
appDetail?: App appDetail?: App
appSidebarExpand: string
} }
type Action = { type Action = {
setAppDetail: (appDetail?: App) => void setAppDetail: (appDetail?: App) => void
setAppSiderbarExpand: (state: string) => void
} }
export const useStore = create<State & Action>(set => ({ export const useStore = create<State & Action>(set => ({
appDetail: undefined, appDetail: undefined,
setAppDetail: appDetail => set(() => ({ appDetail })), setAppDetail: appDetail => set(() => ({ appDetail })),
appSidebarExpand: '',
setAppSiderbarExpand: appSidebarExpand => set(() => ({ appSidebarExpand })),
})) }))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment