All files / app/platform nativeLink.ts

87.5% Statements 14/16
75% Branches 3/4
100% Functions 7/7
86.66% Lines 13/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 3989x     205x           205x     8x 8x   8x 4x     8x           201x           192x 192x     9x 9x    
import { useEffect, useState } from "react";
 
function getReactNativeWebView(): typeof window.ReactNativeWebView {
  Iif (window && window.ReactNativeWebView) {
    return window.ReactNativeWebView;
  }
}
 
function isNativeEmbed(): boolean {
  return !!getReactNativeWebView();
}
 
export function useIsNativeEmbed(): boolean {
  const [isNative, setIsNative] = useState(false);
 
  useEffect(() => {
    setIsNative(isNativeEmbed());
  }, []);
 
  return isNative;
}
 
type MessageType = "sendState" | "clearState";
 
function sendToNative(type: MessageType, data: unknown) {
  if (!isNativeEmbed()) return;
  getReactNativeWebView()!.postMessage(
    JSON.stringify({ type: type, data: data }),
  );
}
 
export function sendState<T>(key: string, value: T) {
  sendToNative("sendState", { key: key, value: value });
}
 
export function clearState(key: string) {
  sendToNative("clearState", { key: key });
}