about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorEric Bailey <git@esb.lol>2025-08-07 14:55:19 -0500
committerGitHub <noreply@github.com>2025-08-07 14:55:19 -0500
commit11c9931fd2bc9db8a6e4e71ae04d71051f63191c (patch)
treeed2de0262a18a8971d23214009afa6bdc172f9c1 /src
parentc0593e49792af987b0c7accd6301f235d132030f (diff)
downloadvoidsky-11c9931fd2bc9db8a6e4e71ae04d71051f63191c.tar.zst
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
Diffstat (limited to 'src')
-rw-r--r--src/components/PolicyUpdateOverlay/context.tsx51
-rw-r--r--src/components/PolicyUpdateOverlay/index.tsx14
-rw-r--r--src/components/PolicyUpdateOverlay/usePolicyUpdateState.ts24
-rw-r--r--src/view/shell/index.tsx4
-rw-r--r--src/view/shell/index.web.tsx4
5 files changed, 80 insertions, 17 deletions
diff --git a/src/components/PolicyUpdateOverlay/context.tsx b/src/components/PolicyUpdateOverlay/context.tsx
index 68ae7bbd8..4f6e56e21 100644
--- a/src/components/PolicyUpdateOverlay/context.tsx
+++ b/src/components/PolicyUpdateOverlay/context.tsx
@@ -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 {
   type PolicyUpdateState,
   usePolicyUpdateState,
 } from '#/components/PolicyUpdateOverlay/usePolicyUpdateState'
 
-const Context = createContext<PolicyUpdateState>({
-  completed: true,
-  complete: () => {},
+const Context = createContext<{
+  state: PolicyUpdateState
+  setIsReadyToShowOverlay: () => void
+}>({
+  state: {
+    completed: true,
+    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)
   if (!context) {
     throw new Error(
-      'usePolicyUpdateStateContext must be used within a PolicyUpdateProvider',
+      'usePolicyUpdateContext must be used within a PolicyUpdateProvider',
     )
   }
   return context
 }
 
 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 (
     <PortalProvider>
-      <Context.Provider value={state}>{children}</Context.Provider>
+      <Context.Provider value={ctx}>{children}</Context.Provider>
     </PortalProvider>
   )
 }
diff --git a/src/components/PolicyUpdateOverlay/index.tsx b/src/components/PolicyUpdateOverlay/index.tsx
index 1900dc27f..f30da4334 100644
--- a/src/components/PolicyUpdateOverlay/index.tsx
+++ b/src/components/PolicyUpdateOverlay/index.tsx
@@ -1,18 +1,26 @@
+import {useEffect} from 'react'
 import {View} from 'react-native'
 
 import {isIOS} from '#/platform/detection'
 import {atoms as a} from '#/alf'
 import {FullWindowOverlay} from '#/components/FullWindowOverlay'
-import {usePolicyUpdateStateContext} from '#/components/PolicyUpdateOverlay/context'
+import {usePolicyUpdateContext} from '#/components/PolicyUpdateOverlay/context'
 import {Portal} from '#/components/PolicyUpdateOverlay/Portal'
 import {Content} from '#/components/PolicyUpdateOverlay/updates/202508'
 
 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 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
diff --git a/src/components/PolicyUpdateOverlay/usePolicyUpdateState.ts b/src/components/PolicyUpdateOverlay/usePolicyUpdateState.ts
index 29d8afe06..32b948af9 100644
--- a/src/components/PolicyUpdateOverlay/usePolicyUpdateState.ts
+++ b/src/components/PolicyUpdateOverlay/usePolicyUpdateState.ts
@@ -11,13 +11,33 @@ export type PolicyUpdateState = {
   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 {mutate: save, variables} = useSaveNux()
   const deviceStorage = useStorage(device, [ACTIVE_UPDATE_ID])
   const debugOverride =
     !!useStorage(device, ['policyUpdateDebugOverride'])[0] && IS_DEV
+
   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 nuxIsCompleted = nux.nux?.completed === true
     const nuxIsOptimisticallyCompleted = !!variables?.completed
@@ -59,7 +79,7 @@ export function usePolicyUpdateState() {
         setCompletedForDevice(true)
       },
     }
-  }, [nux, save, variables, deviceStorage, debugOverride])
+  }, [enabled, nux, save, variables, deviceStorage, debugOverride])
 }
 
 export function computeCompletedState({
diff --git a/src/view/shell/index.tsx b/src/view/shell/index.tsx
index 0d8c24566..543009a55 100644
--- a/src/view/shell/index.tsx
+++ b/src/view/shell/index.tsx
@@ -33,7 +33,7 @@ import {MutedWordsDialog} from '#/components/dialogs/MutedWords'
 import {SigninDialog} from '#/components/dialogs/Signin'
 import {
   Outlet as PolicyUpdateOverlayPortalOutlet,
-  usePolicyUpdateStateContext,
+  usePolicyUpdateContext,
 } from '#/components/PolicyUpdateOverlay'
 import {Outlet as PortalOutlet} from '#/components/Portal'
 import {RoutesContainer, TabsNavigator} from '#/Navigation'
@@ -49,7 +49,7 @@ function ShellInner() {
   const setIsDrawerOpen = useSetDrawerOpen()
   const winDim = useWindowDimensions()
   const insets = useSafeAreaInsets()
-  const policyUpdateState = usePolicyUpdateStateContext()
+  const {state: policyUpdateState} = usePolicyUpdateContext()
 
   const renderDrawerContent = useCallback(() => <DrawerContent />, [])
   const onOpenDrawer = useCallback(
diff --git a/src/view/shell/index.web.tsx b/src/view/shell/index.web.tsx
index c1565e8ee..3c2bc58ab 100644
--- a/src/view/shell/index.web.tsx
+++ b/src/view/shell/index.web.tsx
@@ -24,7 +24,7 @@ import {MutedWordsDialog} from '#/components/dialogs/MutedWords'
 import {SigninDialog} from '#/components/dialogs/Signin'
 import {
   Outlet as PolicyUpdateOverlayPortalOutlet,
-  usePolicyUpdateStateContext,
+  usePolicyUpdateContext,
 } from '#/components/PolicyUpdateOverlay'
 import {Outlet as PortalOutlet} from '#/components/Portal'
 import {FlatNavigator, RoutesContainer} from '#/Navigation'
@@ -41,7 +41,7 @@ function ShellInner() {
   const {_} = useLingui()
   const showDrawer = !isDesktop && isDrawerOpen
   const [showDrawerDelayedExit, setShowDrawerDelayedExit] = useState(showDrawer)
-  const policyUpdateState = usePolicyUpdateStateContext()
+  const {state: policyUpdateState} = usePolicyUpdateContext()
 
   useLayoutEffect(() => {
     if (showDrawer !== showDrawerDelayedExit) {