Skip to content

Commit

Permalink
ability to change the routing behavior ...
Browse files Browse the repository at this point in the history
  • Loading branch information
burtonator committed Dec 26, 2024
1 parent 86109cf commit d21ffe6
Showing 1 changed file with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import useUserStore from 'client/scripts/state/ui/user';
import { useEffect, useState } from 'react';
import { useCallback, useEffect, useState } from 'react';
import { useNavigate } from 'react-router-dom';

interface ReactNativeWebView {
// allows us to send messages to ReactNative.
Expand Down Expand Up @@ -31,6 +32,24 @@ type UserInfo = {
// darkMode: 'dark' | 'light';
};

type NavigateToLink = {
type: 'navigate-to-link';
link: string;
};

function isNavigateToLink(data: object): data is NavigateToLink {
return (data as any).type === 'navigate-to-link';
}

type NavigateBack = {
type: 'navigate-back';
link: string;
};

function isNavigateBack(data: object): data is NavigateBack {
return (data as any).type === 'navigate-backk';
}

/**
* This acts as a bridge between the react-native client (mobile app) and our
* webapp. Notifications only work with a userId and the react-native client
Expand All @@ -44,9 +63,35 @@ type UserInfo = {
*/
export const ReactNativeBridge = () => {
const user = useUserStore();
const navigate = useNavigate();

const [userInfo, setUserInfo] = useState<UserInfo | null>(null);

const handleMessage = useCallback(
(message: MessageEvent) => {
const obj = JSON.parse(message.data);

if (typeof message.data === 'object') {
if (isNavigateToLink(obj)) {
navigate(obj.link);
}

if (isNavigateBack(obj)) {
navigate(-1);
}
}
},
[navigate],
);

useEffect(() => {
window.addEventListener('message', handleMessage);

return () => {
window.removeEventListener('message', handleMessage);
};
}, [handleMessage]);

useEffect(() => {
if (user.id !== userInfo?.userId) {
setUserInfo({
Expand Down

0 comments on commit d21ffe6

Please sign in to comment.