dify/web/context/app-context.tsx

139 lines
4.9 KiB
TypeScript
Raw Normal View History

'use client'
2023-08-15 13:35:47 +08:00
import { createRef, useCallback, useEffect, useMemo, useRef, useState } from 'react'
2023-06-29 15:30:12 +08:00
import useSWR from 'swr'
import { createContext, useContext, useContextSelector } from 'use-context-selector'
2023-06-29 15:30:12 +08:00
import type { FC, ReactNode } from 'react'
import { fetchAppList } from '@/service/apps'
import Loading from '@/app/components/base/loading'
2023-08-15 13:35:47 +08:00
import { fetchCurrentWorkspace, fetchLanggeniusVersion, fetchUserProfile } from '@/service/common'
import type { App } from '@/types/app'
2023-08-15 13:35:47 +08:00
import type { ICurrentWorkspace, LangGeniusVersionResponse, UserProfileResponse } from '@/models/common'
2023-08-25 19:38:52 +08:00
import MaintenanceNotice from '@/app/components/header/maintenance-notice'
export type AppContextValue = {
apps: App[]
2023-08-15 13:35:47 +08:00
mutateApps: VoidFunction
userProfile: UserProfileResponse
2023-08-15 13:35:47 +08:00
mutateUserProfile: VoidFunction
currentWorkspace: ICurrentWorkspace
isCurrentWorkspaceManager: boolean
2023-12-18 16:54:47 +08:00
isCurrentWorkspaceOwner: boolean
2023-08-15 13:35:47 +08:00
mutateCurrentWorkspace: VoidFunction
2023-06-29 15:30:12 +08:00
pageContainerRef: React.RefObject<HTMLDivElement>
langeniusVersionInfo: LangGeniusVersionResponse
useSelector: typeof useSelector
}
2023-07-05 11:11:38 +08:00
const initialLangeniusVersionInfo = {
current_env: '',
current_version: '',
latest_version: '',
release_date: '',
release_notes: '',
version: '',
can_auto_update: false,
}
2023-08-15 13:35:47 +08:00
const initialWorkspaceInfo: ICurrentWorkspace = {
id: '',
name: '',
plan: '',
status: '',
created_at: 0,
role: 'normal',
providers: [],
in_trail: true,
}
const AppContext = createContext<AppContextValue>({
apps: [],
mutateApps: () => { },
userProfile: {
id: '',
name: '',
email: '',
avatar: '',
is_password_set: false,
},
2023-08-15 13:35:47 +08:00
currentWorkspace: initialWorkspaceInfo,
isCurrentWorkspaceManager: false,
isCurrentWorkspaceOwner: false,
mutateUserProfile: () => { },
2023-08-15 13:35:47 +08:00
mutateCurrentWorkspace: () => { },
pageContainerRef: createRef(),
2023-07-05 11:11:38 +08:00
langeniusVersionInfo: initialLangeniusVersionInfo,
useSelector,
})
2023-06-29 15:30:12 +08:00
export function useSelector<T>(selector: (value: AppContextValue) => T): T {
return useContextSelector(AppContext, selector)
}
export type AppContextProviderProps = {
children: ReactNode
}
export const AppContextProvider: FC<AppContextProviderProps> = ({ children }) => {
const pageContainerRef = useRef<HTMLDivElement>(null)
const { data: appList, mutate: mutateApps } = useSWR({ url: '/apps', params: { page: 1, limit: 30, name: '' } }, fetchAppList)
2023-06-29 15:30:12 +08:00
const { data: userProfileResponse, mutate: mutateUserProfile } = useSWR({ url: '/account/profile', params: {} }, fetchUserProfile)
2023-08-15 13:35:47 +08:00
const { data: currentWorkspaceResponse, mutate: mutateCurrentWorkspace } = useSWR({ url: '/workspaces/current', params: {} }, fetchCurrentWorkspace)
2023-06-29 15:30:12 +08:00
const [userProfile, setUserProfile] = useState<UserProfileResponse>()
2023-07-05 11:11:38 +08:00
const [langeniusVersionInfo, setLangeniusVersionInfo] = useState<LangGeniusVersionResponse>(initialLangeniusVersionInfo)
2023-08-15 13:35:47 +08:00
const [currentWorkspace, setCurrentWorkspace] = useState<ICurrentWorkspace>(initialWorkspaceInfo)
const isCurrentWorkspaceManager = useMemo(() => ['owner', 'admin'].includes(currentWorkspace.role), [currentWorkspace.role])
2023-12-18 16:54:47 +08:00
const isCurrentWorkspaceOwner = useMemo(() => currentWorkspace.role === 'owner', [currentWorkspace.role])
2023-08-15 13:35:47 +08:00
const updateUserProfileAndVersion = useCallback(async () => {
2023-06-29 15:30:12 +08:00
if (userProfileResponse && !userProfileResponse.bodyUsed) {
const result = await userProfileResponse.json()
setUserProfile(result)
const current_version = userProfileResponse.headers.get('x-version')
2023-07-18 16:57:14 +08:00
const current_env = process.env.NODE_ENV === 'development' ? 'DEVELOPMENT' : userProfileResponse.headers.get('x-env')
2023-06-29 15:30:12 +08:00
const versionData = await fetchLanggeniusVersion({ url: '/version', params: { current_version } })
setLangeniusVersionInfo({ ...versionData, current_version, latest_version: versionData.version, current_env })
}
2023-08-15 13:35:47 +08:00
}, [userProfileResponse])
2023-06-29 15:30:12 +08:00
useEffect(() => {
updateUserProfileAndVersion()
2023-08-15 13:35:47 +08:00
}, [updateUserProfileAndVersion, userProfileResponse])
useEffect(() => {
if (currentWorkspaceResponse)
setCurrentWorkspace(currentWorkspaceResponse)
}, [currentWorkspaceResponse])
2023-07-05 11:11:38 +08:00
if (!appList || !userProfile)
2023-06-29 15:30:12 +08:00
return <Loading type='app' />
return (
2023-08-15 13:35:47 +08:00
<AppContext.Provider value={{
apps: appList.data,
mutateApps,
userProfile,
mutateUserProfile,
pageContainerRef,
langeniusVersionInfo,
useSelector,
currentWorkspace,
isCurrentWorkspaceManager,
2023-12-18 16:54:47 +08:00
isCurrentWorkspaceOwner,
2023-08-15 13:35:47 +08:00
mutateCurrentWorkspace,
}}>
<div className='flex flex-col h-full overflow-y-auto'>
2023-08-25 19:38:52 +08:00
{globalThis.document?.body?.getAttribute('data-public-maintenance-notice') && <MaintenanceNotice />}
<div ref={pageContainerRef} className='grow relative flex flex-col overflow-y-auto overflow-x-hidden bg-gray-100'>
2023-08-25 19:38:52 +08:00
{children}
</div>
2023-06-29 15:30:12 +08:00
</div>
</AppContext.Provider>
)
}
export const useAppContext = () => useContext(AppContext)
export default AppContext