app-context.tsx 3.1 KB

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