2025-05-20 12:07:50 +08:00
|
|
|
'use client'
|
|
|
|
import { create } from 'zustand'
|
|
|
|
import { useQuery } from '@tanstack/react-query'
|
|
|
|
import type { FC, PropsWithChildren } from 'react'
|
|
|
|
import { useEffect } from 'react'
|
|
|
|
import type { SystemFeatures } from '@/types/feature'
|
|
|
|
import { defaultSystemFeatures } from '@/types/feature'
|
|
|
|
import { getSystemFeatures } from '@/service/common'
|
|
|
|
import Loading from '@/app/components/base/loading'
|
2025-06-05 10:55:17 +08:00
|
|
|
import { AccessMode } from '@/models/access-control'
|
2025-05-20 12:07:50 +08:00
|
|
|
|
|
|
|
type GlobalPublicStore = {
|
2025-06-05 10:55:17 +08:00
|
|
|
isGlobalPending: boolean
|
|
|
|
setIsGlobalPending: (isPending: boolean) => void
|
2025-05-20 12:07:50 +08:00
|
|
|
systemFeatures: SystemFeatures
|
|
|
|
setSystemFeatures: (systemFeatures: SystemFeatures) => void
|
2025-06-05 10:55:17 +08:00
|
|
|
webAppAccessMode: AccessMode,
|
|
|
|
setWebAppAccessMode: (webAppAccessMode: AccessMode) => void
|
2025-05-20 12:07:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export const useGlobalPublicStore = create<GlobalPublicStore>(set => ({
|
2025-06-05 10:55:17 +08:00
|
|
|
isGlobalPending: true,
|
|
|
|
setIsGlobalPending: (isPending: boolean) => set(() => ({ isGlobalPending: isPending })),
|
2025-05-20 12:07:50 +08:00
|
|
|
systemFeatures: defaultSystemFeatures,
|
|
|
|
setSystemFeatures: (systemFeatures: SystemFeatures) => set(() => ({ systemFeatures })),
|
2025-06-05 10:55:17 +08:00
|
|
|
webAppAccessMode: AccessMode.PUBLIC,
|
|
|
|
setWebAppAccessMode: (webAppAccessMode: AccessMode) => set(() => ({ webAppAccessMode })),
|
2025-05-20 12:07:50 +08:00
|
|
|
}))
|
|
|
|
|
|
|
|
const GlobalPublicStoreProvider: FC<PropsWithChildren> = ({
|
|
|
|
children,
|
|
|
|
}) => {
|
|
|
|
const { isPending, data } = useQuery({
|
|
|
|
queryKey: ['systemFeatures'],
|
|
|
|
queryFn: getSystemFeatures,
|
|
|
|
})
|
2025-06-05 10:55:17 +08:00
|
|
|
const { setSystemFeatures, setIsGlobalPending: setIsPending } = useGlobalPublicStore()
|
2025-05-20 12:07:50 +08:00
|
|
|
useEffect(() => {
|
|
|
|
if (data)
|
|
|
|
setSystemFeatures({ ...defaultSystemFeatures, ...data })
|
|
|
|
}, [data, setSystemFeatures])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setIsPending(isPending)
|
|
|
|
}, [isPending, setIsPending])
|
|
|
|
|
|
|
|
if (isPending)
|
|
|
|
return <div className='flex h-screen w-screen items-center justify-center'><Loading /></div>
|
|
|
|
return <>{children}</>
|
|
|
|
}
|
|
|
|
export default GlobalPublicStoreProvider
|