You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

layout.tsx 1.9KB

2 years ago
2 years ago
2 years ago
2 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. 'use client'
  2. import React, { useEffect, useState } from 'react'
  3. import type { FC } from 'react'
  4. import { usePathname, useSearchParams } from 'next/navigation'
  5. import Loading from '../components/base/loading'
  6. import { useGlobalPublicStore } from '@/context/global-public-context'
  7. import { AccessMode } from '@/models/access-control'
  8. import { getAppAccessModeByAppCode } from '@/service/share'
  9. const Layout: FC<{
  10. children: React.ReactNode
  11. }> = ({ children }) => {
  12. const isGlobalPending = useGlobalPublicStore(s => s.isGlobalPending)
  13. const setWebAppAccessMode = useGlobalPublicStore(s => s.setWebAppAccessMode)
  14. const systemFeatures = useGlobalPublicStore(s => s.systemFeatures)
  15. const pathname = usePathname()
  16. const searchParams = useSearchParams()
  17. const redirectUrl = searchParams.get('redirect_url')
  18. const [isLoading, setIsLoading] = useState(true)
  19. useEffect(() => {
  20. (async () => {
  21. if (!isGlobalPending && !systemFeatures.webapp_auth.enabled) {
  22. setIsLoading(false)
  23. return
  24. }
  25. let appCode: string | null = null
  26. if (redirectUrl) {
  27. const url = new URL(`${window.location.origin}${decodeURIComponent(redirectUrl)}`)
  28. appCode = url.pathname.split('/').pop() || null
  29. }
  30. else {
  31. appCode = pathname.split('/').pop() || null
  32. }
  33. if (!appCode)
  34. return
  35. setIsLoading(true)
  36. const ret = await getAppAccessModeByAppCode(appCode)
  37. setWebAppAccessMode(ret?.accessMode || AccessMode.PUBLIC)
  38. setIsLoading(false)
  39. })()
  40. }, [pathname, redirectUrl, setWebAppAccessMode, isGlobalPending, systemFeatures.webapp_auth.enabled])
  41. if (isLoading || isGlobalPending) {
  42. return <div className='flex h-full w-full items-center justify-center'>
  43. <Loading />
  44. </div>
  45. }
  46. return (
  47. <div className="h-full min-w-[300px] pb-[env(safe-area-inset-bottom)]">
  48. {children}
  49. </div>
  50. )
  51. }
  52. export default Layout