-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
π app: fix onesignal web/native abstraction
- Loading branch information
1 parent
297ddc1
commit 3301b7c
Showing
4 changed files
with
61 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { useState, useEffect } from "react"; | ||
import { Platform } from "react-native"; | ||
import type OneSignalNative from "react-native-onesignal"; | ||
import type OneSignalWeb from "react-onesignal"; | ||
|
||
import { oneSignalAppId as appId } from "./constants"; | ||
import handleError from "./handleError"; | ||
|
||
const { initialization, login, logout } = ( | ||
Platform.OS === "web" | ||
? () => { | ||
const OneSignal = (require("react-onesignal") as { default: typeof OneSignalWeb }).default; // eslint-disable-line @typescript-eslint/no-var-requires, unicorn/prefer-module | ||
return { | ||
initialization: | ||
appId === undefined ? Promise.resolve() : OneSignal.init({ appId, allowLocalhostAsSecureOrigin: __DEV__ }), | ||
login: (userId: string) => OneSignal.login(userId), | ||
logout: () => OneSignal.logout(), | ||
}; | ||
} | ||
: () => { | ||
const { OneSignal } = require("react-native-onesignal") as typeof OneSignalNative; // eslint-disable-line @typescript-eslint/no-var-requires, unicorn/prefer-module | ||
return { | ||
initialization: (() => { | ||
if (appId !== undefined) OneSignal.initialize(appId); | ||
return Promise.resolve(); | ||
})(), | ||
login: (userId: string) => { | ||
OneSignal.login(userId); | ||
return Promise.resolve(); | ||
}, | ||
logout: () => { | ||
OneSignal.logout(); | ||
return Promise.resolve(); | ||
}, | ||
}; | ||
} | ||
)(); | ||
|
||
export default function useOneSignal({ userId }: { userId?: string } = {}) { | ||
const [initialized, setInitialized] = useState(false); | ||
|
||
useEffect(() => { | ||
initialization | ||
.then(() => { | ||
setInitialized(true); | ||
}) | ||
.catch(handleError); | ||
|
||
if (userId && initialized) login(userId).catch(handleError); | ||
|
||
return () => { | ||
if (userId) logout().catch(handleError); | ||
}; | ||
}, [userId, initialized]); | ||
|
||
return initialized; | ||
} |