about summary refs log tree commit diff
path: root/src/state/shell/light-status-bar.tsx
diff options
context:
space:
mode:
Diffstat (limited to 'src/state/shell/light-status-bar.tsx')
-rw-r--r--src/state/shell/light-status-bar.tsx45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/state/shell/light-status-bar.tsx b/src/state/shell/light-status-bar.tsx
new file mode 100644
index 000000000..eb213adb9
--- /dev/null
+++ b/src/state/shell/light-status-bar.tsx
@@ -0,0 +1,45 @@
+import {createContext, useContext, useEffect, useState} from 'react'
+
+import {isWeb} from '#/platform/detection'
+import {IS_DEV} from '#/env'
+
+const LightStatusBarRefCountContext = createContext<boolean>(false)
+const SetLightStatusBarRefCountContext = createContext<React.Dispatch<
+  React.SetStateAction<number>
+> | null>(null)
+
+export function useLightStatusBar() {
+  return useContext(LightStatusBarRefCountContext)
+}
+
+export function useSetLightStatusBar(enabled: boolean) {
+  const setRefCount = useContext(SetLightStatusBarRefCountContext)
+  useEffect(() => {
+    // noop on web -sfn
+    if (isWeb) return
+
+    if (!setRefCount) {
+      if (IS_DEV)
+        console.error(
+          'useLightStatusBar was used without a SetLightStatusBarRefCountContext provider',
+        )
+      return
+    }
+    if (enabled) {
+      setRefCount(prev => prev + 1)
+      return () => setRefCount(prev => prev - 1)
+    }
+  }, [enabled, setRefCount])
+}
+
+export function Provider({children}: React.PropsWithChildren<{}>) {
+  const [refCount, setRefCount] = useState(0)
+
+  return (
+    <SetLightStatusBarRefCountContext.Provider value={setRefCount}>
+      <LightStatusBarRefCountContext.Provider value={refCount > 0}>
+        {children}
+      </LightStatusBarRefCountContext.Provider>
+    </SetLightStatusBarRefCountContext.Provider>
+  )
+}