Skip to content

Commit

Permalink
πŸ› onesignal: fix onesignal web being imported on android
Browse files Browse the repository at this point in the history
  • Loading branch information
jgalat authored and cruzdanilo committed Jan 9, 2024
1 parent ec111c5 commit 35c7539
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 75 deletions.
6 changes: 3 additions & 3 deletions app/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export default function RootLayout() {
...FontAwesome.font,
});

useOneSignal({});

useEffect(() => {
if (error) throw error;
}, [error]);
Expand All @@ -65,9 +67,7 @@ export default function RootLayout() {
reconnect(wagmiConfig).catch(handleError);
}, []);

const initialized = useOneSignal({});

if (!loaded || !initialized) return;
if (!loaded) return;

return (
<>
Expand Down
47 changes: 47 additions & 0 deletions utils/onesignal.native.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { useState, useEffect, useRef } from "react";
import { OneSignal as RNOneSignal } from "react-native-onesignal";

import { oneSignalAPPId } from "./constants";

type OneSignalProperties = {
userId?: string;
};

type Instance = typeof RNOneSignal;

export default function useOneSignal({ userId }: OneSignalProperties) {
const instance = useRef<Instance | null>(null);
const [initialized, setInitialized] = useState(false);

useEffect(() => {
const load = function () {
if (!oneSignalAPPId) {
setInitialized(true);
return;
}

if (!initialized) {
RNOneSignal.initialize(oneSignalAPPId);
instance.current = RNOneSignal;

setInitialized(true);
}

if (instance.current && userId) {
instance.current.login(userId);
}
};

load();

return () => {
if (!userId || !instance.current) {
return;
}

instance.current.logout();

setInitialized(false);
};
}, [userId, initialized]);
}
84 changes: 12 additions & 72 deletions utils/onesignal.ts
Original file line number Diff line number Diff line change
@@ -1,78 +1,18 @@
import { useState, useEffect, useRef } from "react";
import { Platform } from "react-native";
import { OneSignal as RNOneSignal } from "react-native-onesignal";
import ROneSignal from "react-onesignal";

import { oneSignalAPPId } from "./constants";

type OneSignalProperties = {
userId?: string;
};

type Instance =
| {
type: "native";
value: typeof RNOneSignal;
}
| {
type: "web";
value: typeof ROneSignal;
};

export default function useOneSignal({ userId }: OneSignalProperties) {
const instance = useRef<Instance | null>(null);
const [initialized, setInitialized] = useState(false);

useEffect(() => {
const load = async function () {
if (!oneSignalAPPId) {
setInitialized(true);
return;
}

if (!initialized) {
switch (Platform.OS) {
case "web": {
await ROneSignal.init({
appId: oneSignalAPPId,
allowLocalhostAsSecureOrigin: true,
});
instance.current = { type: "web", value: ROneSignal };
break;
}
case "ios":
case "android": {
RNOneSignal.initialize(oneSignalAPPId);
instance.current = { type: "native", value: RNOneSignal };
break;
}
}

setInitialized(true);
}

if (instance.current && userId) {
await instance.current.value.login(userId);
}
};

load().catch(() => {
setInitialized(true);
});

return () => {
if (!userId || !instance.current) {
return;
}

const logout = instance.current.value.logout();
if (logout instanceof Promise) {
logout.catch(() => {
// ignore
});
}
};
}, [userId, initialized]);

return initialized;
}
type OneSignal = (parameters: OneSignalProperties) => void;

export default Platform.select<OneSignal>({
// eslint-disable-next-line @typescript-eslint/no-var-requires, unicorn/prefer-module
web: require("./onesignal.web") as OneSignal,
// eslint-disable-next-line @typescript-eslint/no-var-requires, unicorn/prefer-module
android: require("./onesignal.native") as OneSignal,
// eslint-disable-next-line @typescript-eslint/no-var-requires, unicorn/prefer-module
ios: require("./onesignal.native") as OneSignal,
// eslint-disable-next-line @typescript-eslint/no-var-requires, unicorn/prefer-module
default: require("./onesignal.native") as OneSignal,
});
48 changes: 48 additions & 0 deletions utils/onesignal.web.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { useState, useEffect, useRef } from "react";
import OneSignal from "react-onesignal";

import { oneSignalAPPId } from "./constants";

type OneSignalProperties = {
userId?: string;
};

type Instance = typeof OneSignal;

export default function useOneSignal({ userId }: OneSignalProperties) {
const instance = useRef<Instance | null>(null);
const [initialized, setInitialized] = useState(false);

useEffect(() => {
const load = async function () {
if (!oneSignalAPPId) {
setInitialized(true);
return;
}

if (!initialized) {
await OneSignal.init({
appId: oneSignalAPPId,
allowLocalhostAsSecureOrigin: true,
});
instance.current = OneSignal;

setInitialized(true);
}

if (instance.current && userId) {
await instance.current.login(userId);
}
};

load().catch(() => {});

return () => {
if (!userId || !instance.current) {
return;
}

instance.current.logout().catch(() => {});
};
}, [userId, initialized]);
}

0 comments on commit 35c7539

Please sign in to comment.