app-context.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 initialLangeniusVersionInfo = {
  21. current_env: '',
  22. current_version: '',
  23. latest_version: '',
  24. release_date: '',
  25. release_notes: '',
  26. version: '',
  27. can_auto_update: false,
  28. }
  29. const AppContext = createContext<AppContextValue>({
  30. apps: [],
  31. mutateApps: () => { },
  32. userProfile: {
  33. id: '',
  34. name: '',
  35. email: '',
  36. avatar: '',
  37. is_password_set: false,
  38. },
  39. mutateUserProfile: () => { },
  40. pageContainerRef: createRef(),
  41. langeniusVersionInfo: initialLangeniusVersionInfo,
  42. useSelector,
  43. })
  44. export function useSelector<T>(selector: (value: AppContextValue) => T): T {
  45. return useContextSelector(AppContext, selector)
  46. }
  47. export type AppContextProviderProps = {
  48. children: ReactNode
  49. }
  50. export const AppContextProvider: FC<AppContextProviderProps> = ({ children }) => {
  51. const pageContainerRef = useRef<HTMLDivElement>(null)
  52. const { data: appList, mutate: mutateApps } = useSWR({ url: '/apps', params: { page: 1 } }, fetchAppList)
  53. const { data: userProfileResponse, mutate: mutateUserProfile } = useSWR({ url: '/account/profile', params: {} }, fetchUserProfile)
  54. const [userProfile, setUserProfile] = useState<UserProfileResponse>()
  55. const [langeniusVersionInfo, setLangeniusVersionInfo] = useState<LangGeniusVersionResponse>(initialLangeniusVersionInfo)
  56. const updateUserProfileAndVersion = async () => {
  57. if (userProfileResponse && !userProfileResponse.bodyUsed) {
  58. const result = await userProfileResponse.json()
  59. setUserProfile(result)
  60. const current_version = userProfileResponse.headers.get('x-version')
  61. const current_env = process.env.NODE_ENV === 'development' ? 'DEVELOPMENT' : userProfileResponse.headers.get('x-env')
  62. const versionData = await fetchLanggeniusVersion({ url: '/version', params: { current_version } })
  63. setLangeniusVersionInfo({ ...versionData, current_version, latest_version: versionData.version, current_env })
  64. }
  65. }
  66. useEffect(() => {
  67. updateUserProfileAndVersion()
  68. }, [userProfileResponse])
  69. if (!appList || !userProfile)
  70. return <Loading type='app' />
  71. return (
  72. <AppContext.Provider value={{ apps: appList.data, mutateApps, userProfile, mutateUserProfile, pageContainerRef, langeniusVersionInfo, useSelector }}>
  73. <div ref={pageContainerRef} className='relative flex flex-col h-full overflow-auto bg-gray-100'>
  74. {children}
  75. </div>
  76. </AppContext.Provider>
  77. )
  78. }
  79. export const useAppContext = () => useContext(AppContext)
  80. export default AppContext