beerbuddy/react-native/app/messageHandler.ts
2024-02-10 13:52:47 -08:00

58 lines
1.5 KiB
TypeScript

import { Alert } from "react-native";
import { WebViewMessageEvent } from "react-native-webview";
import { router } from "expo-router";
import WebView from "react-native-webview";
const messageHandler = (event: WebViewMessageEvent, element: WebView) => {
if (typeof event.nativeEvent.data == "string") {
const message = event.nativeEvent.data;
if (message === "new pin start") {
return Alert.alert(
"New Pin",
`Please select location for new pin then press "OK"`,
[
{
text: "OK",
onPress: () => {
element.injectJavaScript(`window.placePin(); true()`);
},
},
]
);
}
if (message.startsWith(`create@`)) {
const coords = message.slice(7).split(",");
return router.push({
pathname: "./store/new/[coords]",
params: {
coords: `${coords[0]}+${coords[1]}`,
},
});
}
if (message.startsWith(`open@`)) {
const coords = message.slice(5).split(",");
return router.push({
pathname: "./store/[coords]",
params: {
coords: `${coords[0]}+${coords[1]}`,
},
});
}
if (message.startsWith(`search@`)) {
const chunks = message.slice(7).split(":");
const coords = chunks[0].split(",");
return router.push({
pathname: "./search/[slug]",
params: {
slug: `${coords[0]}+${coords[1]}+${chunks[1]}`,
},
});
}
}
};
export default messageHandler;