From 3e2d00e95ebc7bc820c1f5ec905159fa994795e1 Mon Sep 17 00:00:00 2001 From: Andrei O Date: Wed, 22 Feb 2023 14:31:37 +0200 Subject: [PATCH] rf: last reward notif --- src/background/index.ts | 17 +++++++++-------- src/utils/storage.ts | 31 +++++++++++++++++++++---------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/background/index.ts b/src/background/index.ts index c3313b1..e759c2a 100644 --- a/src/background/index.ts +++ b/src/background/index.ts @@ -1,11 +1,11 @@ import { SEND_AUTH_NOTIF } from '@/constants/messeges'; import { initStorage } from '@/utils/storage' -import { getStore, setProfile, setNotifStorageNotifs, setSettings, getNotifStorage, setNotifStorageLastRewardNotif } from '@/utils/storage' +import { getStore, setProfile, setNotifStorageNotifs, setSettings, setNotifStorageLastRewardNotif, getNotifStorageLastRewardNotif } from '@/utils/storage' import type { Notification } from '@/utils/types'; import { API_BASE } from '@/constants/config'; import { getNotifications } from '@/utils/notifications'; import { setBadge } from '@/utils/chrome-misc' -import { closeTo, getTimeRemaining } from '@/utils/time'; +import { closeTo } from '@/utils/time'; import { getActionUsage } from '@/utils/user'; // Disable conflict with yup extension const yupExtensionId = 'nhmeoaahigiljjdkoagafdccikgojjoi' @@ -65,18 +65,19 @@ const alarmHandler = async () => { if (store.settings?.chromeNotifWhenReward && notSeen.some(notif => notif.action === 'reward')) { const rewardNotif = notSeen.find(notif => notif.action === 'reward') if (rewardNotif) { - const storeReward = (await getNotifStorage()).lastRewardNotif - if (!storeReward || (storeReward.id !== rewardNotif._id && - !closeTo(new Date(storeReward.createdAt), new Date(rewardNotif.createdAt), 2e4))) { + const storeReward = (await getNotifStorageLastRewardNotif()) + + if (!storeReward || (storeReward.id !== rewardNotif._id + && !closeTo(new Date(storeReward.createdAt), new Date(rewardNotif.createdAt), 2e4) + )) { { - setNotifStorageLastRewardNotif({ createdAt: rewardNotif.createdAt, id: rewardNotif._id }).then(() => { - chrome.notifications.create({ + await setNotifStorageLastRewardNotif({ createdAt: rewardNotif.createdAt, id: rewardNotif._id }); + await chrome.notifications.create({ type: 'basic', iconUrl: chrome.runtime.getURL('src/assets/icons/yup_ext_128.png'), title: 'Yup Live Extension', message: `You have been alocated a future reward of ${rewardNotif.quantity} YUP`, }) - }).catch(console.error) } } } diff --git a/src/utils/storage.ts b/src/utils/storage.ts index 19d4bcc..7e9e111 100644 --- a/src/utils/storage.ts +++ b/src/utils/storage.ts @@ -51,13 +51,16 @@ export const storageDefault = { } export const storageNotifsDefault = { - lastRewardNotif: { - createdAt: 0, - id: '', - }, notifs: [] } +export const lastRewardNotifDefault = { + createdAt: 0, + id: '', + } + + + export type StorageType = typeof storageDefault export const wipeStorage = async () => { @@ -75,12 +78,15 @@ export const initStorage = async () => { console.info('Storage initialized') } const notifs = await chrome.storage.local.get('notifs') - const updateNotifs = { ...storageNotifsDefault, ...notifs } - const updateNotifsCondition = !notifs.notifs || !notifs.lastRewardNotif - if(updateNotifsCondition) { - await chrome.storage.local.set({ notifs: updateNotifs }) + const lastRewardNotif = await chrome.storage.local.get('lastRewardNotif') + if(!notifs.notifs) { + await chrome.storage.local.set({ notifs: storageNotifsDefault }) console.info('Notifs storage initialized') } + if(!lastRewardNotif.lastRewardNotif) { + await chrome.storage.local.set({ lastRewardNotif: lastRewardNotifDefault }) + console.info('Last reward notif storage initialized') + } } export const getNotifStorage = async () => { @@ -93,9 +99,14 @@ export const setNotifStorageNotifs = async (notifs: any[]) => { await chrome.storage.local.set({ ...notifsStorage, notifs: notifs }) } +export const getNotifStorageLastRewardNotif = async () => { + const notifsStorage = await chrome.storage.local.get('lastRewardNotif') + return notifsStorage ? notifsStorage.lastRewardNotif : lastRewardNotifDefault +} + export const setNotifStorageLastRewardNotif = async (lastRewardNotif) => { - const notifsStorage = await getNotifStorage() - await chrome.storage.local.set({ ...notifsStorage, ...{ notifs: { lastRewardNotif } } }) + const lastReward = await getNotifStorageLastRewardNotif() + chrome.storage.local.set({ lastRewardNotif: { ...lastReward, ...lastRewardNotif } }) } export const setAuth = async (auth) => {