app-context.tsx 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. 'use client'
  2. import { createRef, useEffect, useRef, useState } from 'react'
  3. import useSWR from 'swr'
  4. import { createContext, useContext, useContextSelector } from 'use-context-selector'
  5. import type { FC, ReactNode } from 'react'
  6. import { fetchAppList } from '@/service/apps'
  7. import Loading from '@/app/components/base/loading'
  8. import { fetchLanggeniusVersion, fetchUserProfile } from '@/service/common'
  9. import type { App } from '@/types/app'
  10. import type { LangGeniusVersionResponse, UserProfileResponse } from '@/models/common'
  11. export type AppContextValue = {
  12. apps: App[]
  13. mutateApps: () => void
  14. userProfile: UserProfileResponse
  15. mutateUserProfile: () => void
  16. pageContainerRef: React.RefObject<HTMLDivElement>
  17. langeniusVersionInfo: LangGeniusVersionResponse
  18. useSelector: typeof useSelector
  19. }
  20. const AppContext = createContext<AppContextValue>({
  21. apps: [],
  22. mutateApps: () => { },
  23. userProfile: {
  24. id: '',
  25. name: '',
  26. email: '',
  27. },
  28. mutateUserProfile: () => { },
  29. pageContainerRef: createRef(),
  30. langeniusVersionInfo: {
  31. current_env: '',
  32. current_version: '',
  33. latest_version: '',
  34. release_date: '',
  35. release_notes: '',
  36. version: '',
  37. can_auto_update: false,
  38. },
  39. useSelector,
  40. })
  41. export function useSelector<T>(selector: (value: AppContextValue) => T): T {
  42. return useContextSelector(AppContext, selector)
  43. }
  44. export type AppContextProviderProps = {
  45. children: ReactNode
  46. }
  47. export const AppContextProvider: FC<AppContextProviderProps> = ({ children }) => {
  48. const pageContainerRef = useRef<HTMLDivElement>(null)
  49. const { data: appList, mutate: mutateApps } = useSWR({ url: '/apps', params: { page: 1 } }, fetchAppList)
  50. const { data: userProfileResponse, mutate: mutateUserProfile } = useSWR({ url: '/account/profile', params: {} }, fetchUserProfile)
  51. const [userProfile, setUserProfile] = useState<UserProfileResponse>()
  52. const [langeniusVersionInfo, setLangeniusVersionInfo] = useState<LangGeniusVersionResponse>()
  53. const updateUserProfileAndVersion = async () => {
  54. if (userProfileResponse && !userProfileResponse.bodyUsed) {
  55. const result = await userProfileResponse.json()
  56. setUserProfile(result)
  57. const current_version = userProfileResponse.headers.get('x-version')
  58. const current_env = userProfileResponse.headers.get('x-env')
  59. const versionData = await fetchLanggeniusVersion({ url: '/version', params: { current_version } })
  60. setLangeniusVersionInfo({ ...versionData, current_version, latest_version: versionData.version, current_env })
  61. }
  62. }
  63. useEffect(() => {
  64. updateUserProfileAndVersion()
  65. }, [userProfileResponse])
  66. if (!appList || !userProfile || !langeniusVersionInfo)
  67. return <Loading type='app' />
  68. return (
  69. <AppContext.Provider value={{ apps: appList.data, mutateApps, userProfile, mutateUserProfile, pageContainerRef, langeniusVersionInfo, useSelector }}>
  70. <div ref={pageContainerRef} className='relative flex flex-col h-full overflow-auto bg-gray-100'>
  71. {children}
  72. </div>
  73. </AppContext.Provider>
  74. )
  75. }
  76. export const useAppContext = () => useContext(AppContext)
  77. export default AppContext