Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

global-public-context.tsx 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. 'use client'
  2. import { create } from 'zustand'
  3. import { useQuery } from '@tanstack/react-query'
  4. import type { FC, PropsWithChildren } from 'react'
  5. import { useEffect } from 'react'
  6. import type { SystemFeatures } from '@/types/feature'
  7. import { defaultSystemFeatures } from '@/types/feature'
  8. import { getSystemFeatures } from '@/service/common'
  9. import Loading from '@/app/components/base/loading'
  10. type GlobalPublicStore = {
  11. isGlobalPending: boolean
  12. setIsGlobalPending: (isPending: boolean) => void
  13. systemFeatures: SystemFeatures
  14. setSystemFeatures: (systemFeatures: SystemFeatures) => void
  15. }
  16. export const useGlobalPublicStore = create<GlobalPublicStore>(set => ({
  17. isGlobalPending: true,
  18. setIsGlobalPending: (isPending: boolean) => set(() => ({ isGlobalPending: isPending })),
  19. systemFeatures: defaultSystemFeatures,
  20. setSystemFeatures: (systemFeatures: SystemFeatures) => set(() => ({ systemFeatures })),
  21. }))
  22. const GlobalPublicStoreProvider: FC<PropsWithChildren> = ({
  23. children,
  24. }) => {
  25. const { isPending, data } = useQuery({
  26. queryKey: ['systemFeatures'],
  27. queryFn: getSystemFeatures,
  28. })
  29. const { setSystemFeatures, setIsGlobalPending: setIsPending } = useGlobalPublicStore()
  30. useEffect(() => {
  31. if (data)
  32. setSystemFeatures({ ...defaultSystemFeatures, ...data })
  33. }, [data, setSystemFeatures])
  34. useEffect(() => {
  35. setIsPending(isPending)
  36. }, [isPending, setIsPending])
  37. if (isPending)
  38. return <div className='flex h-screen w-screen items-center justify-center'><Loading /></div>
  39. return <>{children}</>
  40. }
  41. export default GlobalPublicStoreProvider