+24





![autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)
![Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)


-LAN-
GitHub
Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Yunlu Wen
Joel
GareArc
NFish
Davide Delbianco
minglu7
Ponder
crazywoola
gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
heyszt
Asuka Minato
Guangdong Liu
Eric Guo
NeatGuyCoding
XlKsyt
Dhruv Gorasiya
crazywoola
github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
lyzno1
hj24
GuanMu
非法操作
Copilot
Tonlo
Yusuke Yamada
Novice
kenwoodjw
Ademílson Tonato
znn
yangzheli
9a5f214623
Signed-off-by: NeatGuyCoding <[email protected]> Signed-off-by: lyzno1 <[email protected]> Signed-off-by: kenwoodjw <[email protected]> Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Yunlu Wen <[email protected]> Co-authored-by: Joel <[email protected]> Co-authored-by: GareArc <[email protected]> Co-authored-by: NFish <[email protected]> Co-authored-by: Davide Delbianco <[email protected]> Co-authored-by: minglu7 <[email protected]> Co-authored-by: Ponder <[email protected]> Co-authored-by: crazywoola <[email protected]> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: heyszt <[email protected]> Co-authored-by: Asuka Minato <[email protected]> Co-authored-by: Guangdong Liu <[email protected]> Co-authored-by: Eric Guo <[email protected]> Co-authored-by: NeatGuyCoding <[email protected]> Co-authored-by: XlKsyt <[email protected]> Co-authored-by: Dhruv Gorasiya <[email protected]> Co-authored-by: crazywoola <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: lyzno1 <[email protected]> Co-authored-by: hj24 <[email protected]> Co-authored-by: GuanMu <[email protected]> Co-authored-by: 非法操作 <[email protected]> Co-authored-by: Copilot <[email protected]> Co-authored-by: Tonlo <[email protected]> Co-authored-by: Yusuke Yamada <[email protected]> Co-authored-by: Novice <[email protected]> Co-authored-by: kenwoodjw <[email protected]> Co-authored-by: Ademílson Tonato <[email protected]> Co-authored-by: znn <[email protected]> Co-authored-by: yangzheli <[email protected]>
90 lines
3.3 KiB
TypeScript
90 lines
3.3 KiB
TypeScript
'use client'
|
|
|
|
import type { ChatConfig } from '@/app/components/base/chat/types'
|
|
import Loading from '@/app/components/base/loading'
|
|
import { AccessMode } from '@/models/access-control'
|
|
import type { AppData, AppMeta } from '@/models/share'
|
|
import { useGetWebAppAccessModeByCode } from '@/service/use-share'
|
|
import { usePathname, useSearchParams } from 'next/navigation'
|
|
import type { FC, PropsWithChildren } from 'react'
|
|
import { useEffect } from 'react'
|
|
import { create } from 'zustand'
|
|
import { useGlobalPublicStore } from './global-public-context'
|
|
|
|
type WebAppStore = {
|
|
shareCode: string | null
|
|
updateShareCode: (shareCode: string | null) => void
|
|
appInfo: AppData | null
|
|
updateAppInfo: (appInfo: AppData | null) => void
|
|
appParams: ChatConfig | null
|
|
updateAppParams: (appParams: ChatConfig | null) => void
|
|
webAppAccessMode: AccessMode
|
|
updateWebAppAccessMode: (accessMode: AccessMode) => void
|
|
appMeta: AppMeta | null
|
|
updateWebAppMeta: (appMeta: AppMeta | null) => void
|
|
userCanAccessApp: boolean
|
|
updateUserCanAccessApp: (canAccess: boolean) => void
|
|
}
|
|
|
|
export const useWebAppStore = create<WebAppStore>(set => ({
|
|
shareCode: null,
|
|
updateShareCode: (shareCode: string | null) => set(() => ({ shareCode })),
|
|
appInfo: null,
|
|
updateAppInfo: (appInfo: AppData | null) => set(() => ({ appInfo })),
|
|
appParams: null,
|
|
updateAppParams: (appParams: ChatConfig | null) => set(() => ({ appParams })),
|
|
webAppAccessMode: AccessMode.SPECIFIC_GROUPS_MEMBERS,
|
|
updateWebAppAccessMode: (accessMode: AccessMode) => set(() => ({ webAppAccessMode: accessMode })),
|
|
appMeta: null,
|
|
updateWebAppMeta: (appMeta: AppMeta | null) => set(() => ({ appMeta })),
|
|
userCanAccessApp: false,
|
|
updateUserCanAccessApp: (canAccess: boolean) => set(() => ({ userCanAccessApp: canAccess })),
|
|
}))
|
|
|
|
const getShareCodeFromRedirectUrl = (redirectUrl: string | null): string | null => {
|
|
if (!redirectUrl || redirectUrl.length === 0)
|
|
return null
|
|
const url = new URL(`${window.location.origin}${decodeURIComponent(redirectUrl)}`)
|
|
return url.pathname.split('/').pop() || null
|
|
}
|
|
const getShareCodeFromPathname = (pathname: string): string | null => {
|
|
const code = pathname.split('/').pop() || null
|
|
if (code === 'webapp-signin')
|
|
return null
|
|
return code
|
|
}
|
|
|
|
const WebAppStoreProvider: FC<PropsWithChildren> = ({ children }) => {
|
|
const isGlobalPending = useGlobalPublicStore(s => s.isGlobalPending)
|
|
const updateWebAppAccessMode = useWebAppStore(state => state.updateWebAppAccessMode)
|
|
const updateShareCode = useWebAppStore(state => state.updateShareCode)
|
|
const pathname = usePathname()
|
|
const searchParams = useSearchParams()
|
|
const redirectUrlParam = searchParams.get('redirect_url')
|
|
|
|
// Compute shareCode directly
|
|
const shareCode = getShareCodeFromRedirectUrl(redirectUrlParam) || getShareCodeFromPathname(pathname)
|
|
useEffect(() => {
|
|
updateShareCode(shareCode)
|
|
}, [shareCode, updateShareCode])
|
|
|
|
const { isFetching, data: accessModeResult } = useGetWebAppAccessModeByCode(shareCode)
|
|
|
|
useEffect(() => {
|
|
if (accessModeResult?.accessMode)
|
|
updateWebAppAccessMode(accessModeResult.accessMode)
|
|
}, [accessModeResult, updateWebAppAccessMode, shareCode])
|
|
|
|
if (isGlobalPending || isFetching) {
|
|
return <div className='flex h-full w-full items-center justify-center'>
|
|
<Loading />
|
|
</div>
|
|
}
|
|
return (
|
|
<>
|
|
{children}
|
|
</>
|
|
)
|
|
}
|
|
export default WebAppStoreProvider
|