MCPcopy Create free account
hub / github.com/dailydotdev/apps / useNativeBridge

Function useNativeBridge

packages/shared/src/hooks/useNativeBridge.ts:49–94  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

47// (refresh, back), and what the page tells them (where it is, and a light
48// impact when the streak goes up). Silent until a wrapper takes the message.
49export const useNativeBridge = (): void => {
50 const router = useRouter();
51 const queryClient = useQueryClient();
52 const { isAndroidApp } = useAuthContext();
53 const { streak, isStreaksEnabled } = useReadingStreak();
54 const lastStreak = useRef<number>();
55 const [inWrapper, setInWrapper] = useState(false);
56
57 useEffect(() => {
58 setInWrapper(isAndroidApp || isIOSNative());
59 }, [isAndroidApp]);
60
61 useEffect(() => {
62 if (!inWrapper) {
63 return undefined;
64 }
65 setNativeHandlers({
66 refresh: () => refreshPage(queryClient).then(() => true),
67 back: closeTopLayer,
68 });
69 return () => setNativeHandlers(null);
70 }, [inWrapper, queryClient]);
71
72 useEffect(() => {
73 if (!inWrapper) {
74 return;
75 }
76 const root = owningRoot(router.pathname);
77 postNativeMessage(WebKitMessageHandlers.NavState, {
78 path: router.asPath.split(/[?#]/)[0],
79 isRoot: isRootView(root, router.pathname),
80 isHome: root === ShellRoot.Home && isRootView(root, router.pathname),
81 });
82 }, [inWrapper, router.asPath, router.pathname]);
83
84 const current = isStreaksEnabled ? streak?.current : undefined;
85 useEffect(() => {
86 if (!inWrapper || current === undefined) {
87 return;
88 }
89 if (lastStreak.current !== undefined && current > lastStreak.current) {
90 playHaptic(HapticStyle.Success);
91 }
92 lastStreak.current = current;
93 }, [inWrapper, current]);
94};

Callers 1

InternalAppFunction · 0.90

Calls 10

useAuthContextFunction · 0.90
useReadingStreakFunction · 0.90
isIOSNativeFunction · 0.90
setNativeHandlersFunction · 0.90
refreshPageFunction · 0.90
owningRootFunction · 0.90
postNativeMessageFunction · 0.90
isRootViewFunction · 0.90
playHapticFunction · 0.90
useRouterFunction · 0.50

Tested by

no test coverage detected