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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | 138x 15936x 232x 21x 21x 15913x 15913x 209x 209x 209x 209x 213x 4831x 5098x 5083x 15279x 75x 71x 71x 213x 421x 2x 4x 319x 355x 59x 58x 8x 4x 4x 6x 2x 4x 2x 138x 138x 138x 4x 275x 271x 271x 271x 7x 4x 4x 4x 4x 94x 4x 4x | import { useCallback, useSyncExternalStore } from "react";
// Set by the app's WebEmbed (app/mobile/components/WebEmbed.tsx)
type NativeCapability = "addToCalendar";
interface NativeEmbedInfo {
capabilities?: NativeCapability[];
}
// Handled by the app's useAddToCalendar (app/mobile/hooks/useAddToCalendar.ts)
export interface NativeCalendarEvent {
title: string;
startDate: string; // ISO 8601
endDate: string; // ISO 8601
timeZone: string;
location: string;
url: string;
notes: string;
}
function getReactNativeWebView(): typeof window.ReactNativeWebView {
if (typeof window !== "undefined" && window.ReactNativeWebView) {
return window.ReactNativeWebView;
}
}
function getNativeEmbedInfo(): NativeEmbedInfo | undefined {
try {
return JSON.parse(getReactNativeWebView()?.injectedObjectJson?.() ?? "null") ?? undefined;
} catch {
// Parsing failed or method not available
return undefined;
}
}
// Not exported: calling this during render causes a hydration mismatch (it's always false on the server).
// Components should use useIsNativeEmbed instead.
function isNativeEmbed(): boolean {
const webview = getReactNativeWebView();
if (!webview) return false;
// Android-reliable detection: Check injectedObjectJson() which is more reliable than
// just checking for ReactNativeWebView existence due to timing issues on Android
try {
Eif (webview.injectedObjectJson) {
const injectedData = JSON.parse(webview.injectedObjectJson());
Eif (injectedData?.isNativeEmbed) return true;
}
} catch {
// Parsing failed or method not available - fall through to default behavior
}
// iOS and fallback: If ReactNativeWebView exists, assume we're in native embed
return true;
}
// Always true on web. In the app, only if it handles the feature, since older app versions
// ignore messages they don't know.
function isSupportedInApp(capability: NativeCapability): boolean {
return !isNativeEmbed() || !!getNativeEmbedInfo()?.capabilities?.includes(capability);
}
export function useIsNativeEmbed(): boolean {
return useSyncExternalStore(
// Subscribe function - no-op since value doesn't change after initial load
() => () => {},
// Client snapshot - check if we're in native embed
() => isNativeEmbed(),
// Server snapshot - always false during SSR
() => false,
);
}
export function useIsSupportedInApp(capability: NativeCapability): boolean {
return useSyncExternalStore(
() => () => {},
() => isSupportedInApp(capability),
() => false,
);
}
type MessageType =
| "sendState"
| "clearState"
| "REQUEST_IMAGE_PICK"
| "NATIVE_BACK"
| "LANGUAGE_CHANGE"
| "REQUEST_REVIEW"
| "REQUEST_ADD_TO_CALENDAR"
| "HAPTIC_IMPACT";
function sendToNative(type: MessageType, data: unknown) {
if (!isNativeEmbed()) return;
getReactNativeWebView()!.postMessage(JSON.stringify({ type: type, data: data }));
}
export function sendNativeBack() {
sendToNative("NATIVE_BACK", {});
}
export function sendState<T>(key: string, value: T) {
sendToNative("sendState", { key: key, value: value });
}
export function clearState(key: string) {
sendToNative("clearState", { key: key });
}
export function sendLanguageChange(locale: string) {
sendToNative("LANGUAGE_CHANGE", { locale });
}
export function sendNativeRequestReview() {
sendToNative("REQUEST_REVIEW", {});
}
export function sendNativeAddToCalendar(event: NativeCalendarEvent) {
sendToNative("REQUEST_ADD_TO_CALENDAR", event);
}
export function sendNativeHapticImpact() {
sendToNative("HAPTIC_IMPACT", {});
}
// Image picker bridge for native mobile app
export type ImagePickResult =
| { success: true; imageBase64: string; mimeType: string; fileName?: string }
| { success: false; canceled?: boolean; error?: string };
type ImagePickCallback = (result: ImagePickResult) => void;
let imagePickCallback: ImagePickCallback | null = null;
// Handle messages from native app
Eif (typeof window !== "undefined") {
window.addEventListener("message", (event) => {
try {
const data = typeof event.data === "string" ? JSON.parse(event.data) : event.data;
if (data?.type === "IMAGE_PICK_RESULT" && imagePickCallback) {
imagePickCallback(data.result);
imagePickCallback = null;
}
} catch {
// Ignore non-JSON messages
}
});
}
export function requestNativeImagePick(callback: ImagePickCallback) {
if (!isNativeEmbed()) {
callback({ success: false, error: "Not in native app" });
return;
}
imagePickCallback = callback;
sendToNative("REQUEST_IMAGE_PICK", {});
}
// Hook for components to use
export function useNativeImagePicker() {
const isNative = useIsNativeEmbed();
const pickImage = useCallback((): Promise<ImagePickResult> => {
return new Promise((resolve) => {
requestNativeImagePick(resolve);
});
}, []);
return { isNative, pickImage };
}
// Helper to convert base64 to File object
export function base64ToFile(base64: string, mimeType: string, filename: string): File {
const byteString = atob(base64);
const ab = new ArrayBuffer(byteString.length);
const ia = new Uint8Array(ab);
for (let i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
const blob = new Blob([ab], { type: mimeType });
return new File([blob], filename, { type: mimeType });
}
|