diff --git a/packages/yoroi-extension/app/UI/features/notifications/useCases/NotificationsSettings/NotificationsSettings.tsx b/packages/yoroi-extension/app/UI/features/notifications/useCases/NotificationsSettings/NotificationsSettings.tsx index f50d442bf4..2e588a24b5 100644 --- a/packages/yoroi-extension/app/UI/features/notifications/useCases/NotificationsSettings/NotificationsSettings.tsx +++ b/packages/yoroi-extension/app/UI/features/notifications/useCases/NotificationsSettings/NotificationsSettings.tsx @@ -7,25 +7,45 @@ import { ampli } from '../../../../../../ampli'; const NotificationsSettings = ({ intl }) => { const strings = useStrings(intl); - const [notificationsEnabled, setNotificationsEnabled] = React.useState(true) + const [notificationsEnabled, setNotificationsEnabled] = React.useState(true); + const [selectedWalletId, setSelectedWalletId] = React.useState(""); const lsApi = new LocalStorageApi(); + async function getNotificationsSetting(checkCurrentWallet: boolean = false) { + const notifSettingsStr = await lsApi.getNotificationsSetting(); + const notifSettings = JSON.parse(notifSettingsStr || "{}"); + + if (checkCurrentWallet) { + const selectedWalletId = await lsApi.getSelectedWalletId(); + setSelectedWalletId(selectedWalletId); + + return notifSettings[selectedWalletId] !== undefined ? notifSettings[selectedWalletId] : true; + } + + return notifSettings; + } + + async function setNotificationsSetting(enabled: boolean) { + const notifSettings = await getNotificationsSetting(); + lsApi.setNotificationsSetting(JSON.stringify({ ...notifSettings, [selectedWalletId]: enabled })); + } + + // get initial state from localstorage React.useEffect(() => { - async function getNotifStatus() { - const notifEnabled = await lsApi.getNotificationsSetting(); - if (notifEnabled === "true" && !notificationsEnabled) { - setNotificationsEnabled(true); - } + async function initialNotifStatus() { + const notifEnabled = await getNotificationsSetting(true); + setNotificationsEnabled(notifEnabled); } - getNotifStatus(); + initialNotifStatus(); }, []) - const handleNotificationsChange = (event) => { - setNotificationsEnabled(prev => !prev); - lsApi.setNotificationsSetting(String(event.target.checked)); - + // handle checkbox change event + const handleNotificationsChange = async (event) => { + const enabled = event.target.checked; + setNotificationsEnabled(enabled); + setNotificationsSetting(enabled); ampli.settingsInAppNotificationsStatusUpdated({ status: event.target.checked ? "enabled" : "disabled" }) diff --git a/packages/yoroi-extension/app/api/localStorage/index.js b/packages/yoroi-extension/app/api/localStorage/index.js index 904e186196..0335d11ef6 100644 --- a/packages/yoroi-extension/app/api/localStorage/index.js +++ b/packages/yoroi-extension/app/api/localStorage/index.js @@ -33,7 +33,7 @@ const storageKeys = { FLAGS: networkForLocalStorage + '-FLAGS', USER_THEME: networkForLocalStorage + '-USER-THEME', PORTFOLIO_FIAT_PAIR: networkForLocalStorage + '-PORTFOLIO_FIAT_PAIR', - NOTIFICATIONS_SETTING: networkForLocalStorage + '-NOTIFICATIONS_SETTING', + NOTIFICATIONS_ENABLED: networkForLocalStorage + '-NOTIFICATIONS_ENABLED_PER_WALLET', BUY_SELL_DISCLAIMER: networkForLocalStorage + '-BUY_SELL_DISCLAIMER', // ========== CONNECTOR ========== // DAPP_CONNECTOR_WHITELIST: 'connector_whitelist', @@ -109,11 +109,11 @@ export default class LocalStorageApi { // ========== Notifications Setting ========== // - getNotificationsSetting: void => Promise = () => getLocalItem(storageKeys.NOTIFICATIONS_SETTING); + getNotificationsSetting: void => Promise = () => getLocalItem(storageKeys.NOTIFICATIONS_ENABLED); - setNotificationsSetting: string => Promise = allowed => setLocalItem(storageKeys.NOTIFICATIONS_SETTING, allowed); + setNotificationsSetting: string => Promise = allowed => setLocalItem(storageKeys.NOTIFICATIONS_ENABLED, allowed); - unsetNotificationsSetting: void => Promise = () => removeLocalItem(storageKeys.NOTIFICATIONS_SETTING); + unsetNotificationsSetting: void => Promise = () => removeLocalItem(storageKeys.NOTIFICATIONS_ENABLED); // ========== Buy/Sell Disclaimer ========== // getBuySellDisclaimer: void => Promise = () => getLocalItem(storageKeys.BUY_SELL_DISCLAIMER);