Fix policy overlay logic (#8793)
* Only enable policy update overlay once the actual Overlay mounts (after onboarding and all that) * Disable policy overlay in e2e * Add comments * Add extra insurance * Rm log
This commit is contained in:
@@ -1,32 +1,67 @@
|
|||||||
import {createContext, type ReactNode, useContext} from 'react'
|
import {
|
||||||
|
createContext,
|
||||||
|
type ReactNode,
|
||||||
|
useContext,
|
||||||
|
useMemo,
|
||||||
|
useState,
|
||||||
|
} from 'react'
|
||||||
|
|
||||||
|
import {useSession} from '#/state/session'
|
||||||
import {Provider as PortalProvider} from '#/components/PolicyUpdateOverlay/Portal'
|
import {Provider as PortalProvider} from '#/components/PolicyUpdateOverlay/Portal'
|
||||||
import {
|
import {
|
||||||
type PolicyUpdateState,
|
type PolicyUpdateState,
|
||||||
usePolicyUpdateState,
|
usePolicyUpdateState,
|
||||||
} from '#/components/PolicyUpdateOverlay/usePolicyUpdateState'
|
} from '#/components/PolicyUpdateOverlay/usePolicyUpdateState'
|
||||||
|
|
||||||
const Context = createContext<PolicyUpdateState>({
|
const Context = createContext<{
|
||||||
|
state: PolicyUpdateState
|
||||||
|
setIsReadyToShowOverlay: () => void
|
||||||
|
}>({
|
||||||
|
state: {
|
||||||
completed: true,
|
completed: true,
|
||||||
complete: () => {},
|
complete: () => {},
|
||||||
|
},
|
||||||
|
/**
|
||||||
|
* Although our data will be ready to go when the app shell mounts, we don't
|
||||||
|
* want to show the overlay until we actually render it, which happens after
|
||||||
|
* sigin/signup/onboarding in `createNativeStackNavigatorWithAuth`.
|
||||||
|
*/
|
||||||
|
setIsReadyToShowOverlay: () => {},
|
||||||
})
|
})
|
||||||
|
|
||||||
export function usePolicyUpdateStateContext() {
|
export function usePolicyUpdateContext() {
|
||||||
const context = useContext(Context)
|
const context = useContext(Context)
|
||||||
if (!context) {
|
if (!context) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'usePolicyUpdateStateContext must be used within a PolicyUpdateProvider',
|
'usePolicyUpdateContext must be used within a PolicyUpdateProvider',
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
return context
|
return context
|
||||||
}
|
}
|
||||||
|
|
||||||
export function Provider({children}: {children?: ReactNode}) {
|
export function Provider({children}: {children?: ReactNode}) {
|
||||||
const state = usePolicyUpdateState()
|
const {hasSession} = useSession()
|
||||||
|
const [isReadyToShowOverlay, setIsReadyToShowOverlay] = useState(false)
|
||||||
|
const state = usePolicyUpdateState({
|
||||||
|
// only enable the policy update overlay in non-test environments
|
||||||
|
enabled:
|
||||||
|
isReadyToShowOverlay && hasSession && process.env.NODE_ENV !== 'test',
|
||||||
|
})
|
||||||
|
|
||||||
|
const ctx = useMemo(
|
||||||
|
() => ({
|
||||||
|
state,
|
||||||
|
setIsReadyToShowOverlay() {
|
||||||
|
if (isReadyToShowOverlay) return
|
||||||
|
setIsReadyToShowOverlay(true)
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
[state, isReadyToShowOverlay, setIsReadyToShowOverlay],
|
||||||
|
)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<PortalProvider>
|
<PortalProvider>
|
||||||
<Context.Provider value={state}>{children}</Context.Provider>
|
<Context.Provider value={ctx}>{children}</Context.Provider>
|
||||||
</PortalProvider>
|
</PortalProvider>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,26 @@
|
|||||||
|
import {useEffect} from 'react'
|
||||||
import {View} from 'react-native'
|
import {View} from 'react-native'
|
||||||
|
|
||||||
import {isIOS} from '#/platform/detection'
|
import {isIOS} from '#/platform/detection'
|
||||||
import {atoms as a} from '#/alf'
|
import {atoms as a} from '#/alf'
|
||||||
import {FullWindowOverlay} from '#/components/FullWindowOverlay'
|
import {FullWindowOverlay} from '#/components/FullWindowOverlay'
|
||||||
import {usePolicyUpdateStateContext} from '#/components/PolicyUpdateOverlay/context'
|
import {usePolicyUpdateContext} from '#/components/PolicyUpdateOverlay/context'
|
||||||
import {Portal} from '#/components/PolicyUpdateOverlay/Portal'
|
import {Portal} from '#/components/PolicyUpdateOverlay/Portal'
|
||||||
import {Content} from '#/components/PolicyUpdateOverlay/updates/202508'
|
import {Content} from '#/components/PolicyUpdateOverlay/updates/202508'
|
||||||
|
|
||||||
export {Provider} from '#/components/PolicyUpdateOverlay/context'
|
export {Provider} from '#/components/PolicyUpdateOverlay/context'
|
||||||
export {usePolicyUpdateStateContext} from '#/components/PolicyUpdateOverlay/context'
|
export {usePolicyUpdateContext} from '#/components/PolicyUpdateOverlay/context'
|
||||||
export {Outlet} from '#/components/PolicyUpdateOverlay/Portal'
|
export {Outlet} from '#/components/PolicyUpdateOverlay/Portal'
|
||||||
|
|
||||||
export function PolicyUpdateOverlay() {
|
export function PolicyUpdateOverlay() {
|
||||||
const state = usePolicyUpdateStateContext()
|
const {state, setIsReadyToShowOverlay} = usePolicyUpdateContext()
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
/**
|
||||||
|
* Tell the context that we are ready to show the overlay.
|
||||||
|
*/
|
||||||
|
setIsReadyToShowOverlay()
|
||||||
|
}, [setIsReadyToShowOverlay])
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* See `window.clearNux` example in `/state/queries/nuxs` for a way to clear
|
* See `window.clearNux` example in `/state/queries/nuxs` for a way to clear
|
||||||
|
|||||||
@@ -11,13 +11,33 @@ export type PolicyUpdateState = {
|
|||||||
complete: () => void
|
complete: () => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export function usePolicyUpdateState() {
|
export function usePolicyUpdateState({
|
||||||
|
enabled,
|
||||||
|
}: {
|
||||||
|
/**
|
||||||
|
* Used to skip the policy update overlay until we're actually ready to
|
||||||
|
* show it.
|
||||||
|
*/
|
||||||
|
enabled: boolean
|
||||||
|
}) {
|
||||||
const nux = useNux(ACTIVE_UPDATE_ID)
|
const nux = useNux(ACTIVE_UPDATE_ID)
|
||||||
const {mutate: save, variables} = useSaveNux()
|
const {mutate: save, variables} = useSaveNux()
|
||||||
const deviceStorage = useStorage(device, [ACTIVE_UPDATE_ID])
|
const deviceStorage = useStorage(device, [ACTIVE_UPDATE_ID])
|
||||||
const debugOverride =
|
const debugOverride =
|
||||||
!!useStorage(device, ['policyUpdateDebugOverride'])[0] && IS_DEV
|
!!useStorage(device, ['policyUpdateDebugOverride'])[0] && IS_DEV
|
||||||
|
|
||||||
return useMemo(() => {
|
return useMemo(() => {
|
||||||
|
/**
|
||||||
|
* If not enabled, then just return a completed state so the app functions
|
||||||
|
* as normal.
|
||||||
|
*/
|
||||||
|
if (!enabled) {
|
||||||
|
return {
|
||||||
|
completed: true,
|
||||||
|
complete() {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const nuxIsReady = nux.status === 'ready'
|
const nuxIsReady = nux.status === 'ready'
|
||||||
const nuxIsCompleted = nux.nux?.completed === true
|
const nuxIsCompleted = nux.nux?.completed === true
|
||||||
const nuxIsOptimisticallyCompleted = !!variables?.completed
|
const nuxIsOptimisticallyCompleted = !!variables?.completed
|
||||||
@@ -59,7 +79,7 @@ export function usePolicyUpdateState() {
|
|||||||
setCompletedForDevice(true)
|
setCompletedForDevice(true)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}, [nux, save, variables, deviceStorage, debugOverride])
|
}, [enabled, nux, save, variables, deviceStorage, debugOverride])
|
||||||
}
|
}
|
||||||
|
|
||||||
export function computeCompletedState({
|
export function computeCompletedState({
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ import {MutedWordsDialog} from '#/components/dialogs/MutedWords'
|
|||||||
import {SigninDialog} from '#/components/dialogs/Signin'
|
import {SigninDialog} from '#/components/dialogs/Signin'
|
||||||
import {
|
import {
|
||||||
Outlet as PolicyUpdateOverlayPortalOutlet,
|
Outlet as PolicyUpdateOverlayPortalOutlet,
|
||||||
usePolicyUpdateStateContext,
|
usePolicyUpdateContext,
|
||||||
} from '#/components/PolicyUpdateOverlay'
|
} from '#/components/PolicyUpdateOverlay'
|
||||||
import {Outlet as PortalOutlet} from '#/components/Portal'
|
import {Outlet as PortalOutlet} from '#/components/Portal'
|
||||||
import {RoutesContainer, TabsNavigator} from '#/Navigation'
|
import {RoutesContainer, TabsNavigator} from '#/Navigation'
|
||||||
@@ -49,7 +49,7 @@ function ShellInner() {
|
|||||||
const setIsDrawerOpen = useSetDrawerOpen()
|
const setIsDrawerOpen = useSetDrawerOpen()
|
||||||
const winDim = useWindowDimensions()
|
const winDim = useWindowDimensions()
|
||||||
const insets = useSafeAreaInsets()
|
const insets = useSafeAreaInsets()
|
||||||
const policyUpdateState = usePolicyUpdateStateContext()
|
const {state: policyUpdateState} = usePolicyUpdateContext()
|
||||||
|
|
||||||
const renderDrawerContent = useCallback(() => <DrawerContent />, [])
|
const renderDrawerContent = useCallback(() => <DrawerContent />, [])
|
||||||
const onOpenDrawer = useCallback(
|
const onOpenDrawer = useCallback(
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ import {MutedWordsDialog} from '#/components/dialogs/MutedWords'
|
|||||||
import {SigninDialog} from '#/components/dialogs/Signin'
|
import {SigninDialog} from '#/components/dialogs/Signin'
|
||||||
import {
|
import {
|
||||||
Outlet as PolicyUpdateOverlayPortalOutlet,
|
Outlet as PolicyUpdateOverlayPortalOutlet,
|
||||||
usePolicyUpdateStateContext,
|
usePolicyUpdateContext,
|
||||||
} from '#/components/PolicyUpdateOverlay'
|
} from '#/components/PolicyUpdateOverlay'
|
||||||
import {Outlet as PortalOutlet} from '#/components/Portal'
|
import {Outlet as PortalOutlet} from '#/components/Portal'
|
||||||
import {FlatNavigator, RoutesContainer} from '#/Navigation'
|
import {FlatNavigator, RoutesContainer} from '#/Navigation'
|
||||||
@@ -41,7 +41,7 @@ function ShellInner() {
|
|||||||
const {_} = useLingui()
|
const {_} = useLingui()
|
||||||
const showDrawer = !isDesktop && isDrawerOpen
|
const showDrawer = !isDesktop && isDrawerOpen
|
||||||
const [showDrawerDelayedExit, setShowDrawerDelayedExit] = useState(showDrawer)
|
const [showDrawerDelayedExit, setShowDrawerDelayedExit] = useState(showDrawer)
|
||||||
const policyUpdateState = usePolicyUpdateStateContext()
|
const {state: policyUpdateState} = usePolicyUpdateContext()
|
||||||
|
|
||||||
useLayoutEffect(() => {
|
useLayoutEffect(() => {
|
||||||
if (showDrawer !== showDrawerDelayedExit) {
|
if (showDrawer !== showDrawerDelayedExit) {
|
||||||
|
|||||||
Reference in New Issue
Block a user