| 
									
										
										
										
											2023-05-20 21:55:47 +08:00
										 |  |  | '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' | 
					
						
							| 
									
										
										
										
											2023-05-20 21:55:47 +08:00
										 |  |  | 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' | 
					
						
							| 
									
										
										
										
											2025-05-20 12:07:50 +08:00
										 |  |  | import { fetchCurrentWorkspace, fetchLanggeniusVersion, fetchUserProfile } from '@/service/common' | 
					
						
							| 
									
										
										
										
											2023-05-20 21:55:47 +08:00
										 |  |  | 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' | 
					
						
							| 
									
										
										
										
											2025-04-06 17:56:08 +08:00
										 |  |  | import { noop } from 'lodash-es' | 
					
						
							| 
									
										
										
										
											2023-05-20 21:55:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export type AppContextValue = { | 
					
						
							|  |  |  |   apps: App[] | 
					
						
							| 
									
										
										
										
											2023-08-15 13:35:47 +08:00
										 |  |  |   mutateApps: VoidFunction | 
					
						
							| 
									
										
										
										
											2023-05-20 21:55:47 +08:00
										 |  |  |   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 | 
					
						
							| 
									
										
										
										
											2024-06-14 07:34:25 -05:00
										 |  |  |   isCurrentWorkspaceEditor: boolean | 
					
						
							| 
									
										
										
										
											2024-07-09 17:47:54 +08:00
										 |  |  |   isCurrentWorkspaceDatasetOperator: 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 | 
					
						
							| 
									
										
										
										
											2024-12-18 09:37:22 +09:00
										 |  |  |   isLoadingCurrentWorkspace: boolean | 
					
						
							| 
									
										
										
										
											2023-05-20 21:55:47 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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: [], | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-20 21:55:47 +08:00
										 |  |  | const AppContext = createContext<AppContextValue>({ | 
					
						
							|  |  |  |   apps: [], | 
					
						
							| 
									
										
										
										
											2025-04-06 17:56:08 +08:00
										 |  |  |   mutateApps: noop, | 
					
						
							| 
									
										
										
										
											2023-05-20 21:55:47 +08:00
										 |  |  |   userProfile: { | 
					
						
							|  |  |  |     id: '', | 
					
						
							|  |  |  |     name: '', | 
					
						
							|  |  |  |     email: '', | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  |     avatar: '', | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |     avatar_url: '', | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  |     is_password_set: false, | 
					
						
							| 
									
										
										
										
											2023-05-20 21:55:47 +08:00
										 |  |  |   }, | 
					
						
							| 
									
										
										
										
											2023-08-15 13:35:47 +08:00
										 |  |  |   currentWorkspace: initialWorkspaceInfo, | 
					
						
							|  |  |  |   isCurrentWorkspaceManager: false, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   isCurrentWorkspaceOwner: false, | 
					
						
							| 
									
										
										
										
											2024-06-14 07:34:25 -05:00
										 |  |  |   isCurrentWorkspaceEditor: false, | 
					
						
							| 
									
										
										
										
											2024-07-09 17:47:54 +08:00
										 |  |  |   isCurrentWorkspaceDatasetOperator: false, | 
					
						
							| 
									
										
										
										
											2025-04-06 17:56:08 +08:00
										 |  |  |   mutateUserProfile: noop, | 
					
						
							|  |  |  |   mutateCurrentWorkspace: noop, | 
					
						
							| 
									
										
										
										
											2023-05-20 21:55:47 +08:00
										 |  |  |   pageContainerRef: createRef(), | 
					
						
							| 
									
										
										
										
											2023-07-05 11:11:38 +08:00
										 |  |  |   langeniusVersionInfo: initialLangeniusVersionInfo, | 
					
						
							| 
									
										
										
										
											2023-05-20 21:55:47 +08:00
										 |  |  |   useSelector, | 
					
						
							| 
									
										
										
										
											2024-12-18 09:37:22 +09:00
										 |  |  |   isLoadingCurrentWorkspace: false, | 
					
						
							| 
									
										
										
										
											2023-05-20 21:55:47 +08:00
										 |  |  | }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   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) | 
					
						
							| 
									
										
										
										
											2024-12-18 09:37:22 +09:00
										 |  |  |   const { data: currentWorkspaceResponse, mutate: mutateCurrentWorkspace, isLoading: isLoadingCurrentWorkspace } = 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]) | 
					
						
							| 
									
										
										
										
											2024-06-14 07:34:25 -05:00
										 |  |  |   const isCurrentWorkspaceEditor = useMemo(() => ['owner', 'admin', 'editor'].includes(currentWorkspace.role), [currentWorkspace.role]) | 
					
						
							| 
									
										
										
										
											2024-07-09 17:47:54 +08:00
										 |  |  |   const isCurrentWorkspaceDatasetOperator = useMemo(() => currentWorkspace.role === 'dataset_operator', [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-05-20 21:55:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							| 
									
										
										
										
											2024-06-14 07:34:25 -05:00
										 |  |  |       isCurrentWorkspaceEditor, | 
					
						
							| 
									
										
										
										
											2024-07-09 17:47:54 +08:00
										 |  |  |       isCurrentWorkspaceDatasetOperator, | 
					
						
							| 
									
										
										
										
											2023-08-15 13:35:47 +08:00
										 |  |  |       mutateCurrentWorkspace, | 
					
						
							| 
									
										
										
										
											2024-12-18 09:37:22 +09:00
										 |  |  |       isLoadingCurrentWorkspace, | 
					
						
							| 
									
										
										
										
											2023-08-15 13:35:47 +08:00
										 |  |  |     }}> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |       <div className='flex h-full flex-col overflow-y-auto'> | 
					
						
							| 
									
										
										
										
											2023-08-25 19:38:52 +08:00
										 |  |  |         {globalThis.document?.body?.getAttribute('data-public-maintenance-notice') && <MaintenanceNotice />} | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |         <div ref={pageContainerRef} className='relative flex grow flex-col overflow-y-auto overflow-x-hidden bg-background-body'> | 
					
						
							| 
									
										
										
										
											2023-08-25 19:38:52 +08:00
										 |  |  |           {children} | 
					
						
							|  |  |  |         </div> | 
					
						
							| 
									
										
										
										
											2023-06-29 15:30:12 +08:00
										 |  |  |       </div> | 
					
						
							|  |  |  |     </AppContext.Provider> | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-05-20 21:55:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export const useAppContext = () => useContext(AppContext) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default AppContext |