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 committed Jan 5, 2024
1 parent 644b8d9 commit ababc35
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 72 deletions.
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();
};
}, [userId, initialized]);

return 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) => boolean;

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,
});
54 changes: 54 additions & 0 deletions utils/onesignal.web.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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(() => {
setInitialized(true);
});

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

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

return initialized;
}

0 comments on commit ababc35

Please sign in to comment.