()
| 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. |
| 49 | export 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 | }; |
no test coverage detected