Add `packages/app/src/constants/platform.ts` with canonical gates (isWeb, isNative, getIsElectron) and refactor all ~100 ad-hoc Platform.OS checks across 69 files to use shared imports. Fix sidebar hover crash on native (onPointerEnter → onHoverIn), fix tooltip to use breakpoint instead of platform detection, make sidebar action buttons always visible on native where hover doesn't work, and document the platform gating decision matrix in CLAUDE.md.
51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
import { useEffect, useSyncExternalStore } from "react";
|
|
import { AppState } from "react-native";
|
|
import { getIsAppActivelyVisible } from "@/utils/app-visibility";
|
|
import { isWeb } from "@/constants/platform";
|
|
|
|
let current = getIsAppActivelyVisible();
|
|
const listeners = new Set<() => void>();
|
|
|
|
function notify(): void {
|
|
const next = getIsAppActivelyVisible();
|
|
if (next === current) {
|
|
return;
|
|
}
|
|
current = next;
|
|
for (const listener of listeners) {
|
|
listener();
|
|
}
|
|
}
|
|
|
|
function subscribe(listener: () => void): () => void {
|
|
listeners.add(listener);
|
|
return () => listeners.delete(listener);
|
|
}
|
|
|
|
function getSnapshot(): boolean {
|
|
return current;
|
|
}
|
|
|
|
export function useAppVisible(): boolean {
|
|
useEffect(() => {
|
|
const appStateSubscription = AppState.addEventListener("change", notify);
|
|
|
|
if (isWeb && typeof document !== "undefined") {
|
|
document.addEventListener("visibilitychange", notify);
|
|
window.addEventListener("focus", notify);
|
|
window.addEventListener("blur", notify);
|
|
}
|
|
|
|
return () => {
|
|
appStateSubscription.remove();
|
|
if (isWeb && typeof document !== "undefined") {
|
|
document.removeEventListener("visibilitychange", notify);
|
|
window.removeEventListener("focus", notify);
|
|
window.removeEventListener("blur", notify);
|
|
}
|
|
};
|
|
}, []);
|
|
|
|
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
}
|