rf: last reward notif

This commit is contained in:
Andrei O 2023-02-22 14:31:37 +02:00
parent 3343a2ff57
commit 3e2d00e95e
No known key found for this signature in database
GPG Key ID: B961E5B68389457E
2 changed files with 30 additions and 18 deletions

View File

@ -1,11 +1,11 @@
import { SEND_AUTH_NOTIF } from '@/constants/messeges'; import { SEND_AUTH_NOTIF } from '@/constants/messeges';
import { initStorage } from '@/utils/storage' 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 type { Notification } from '@/utils/types';
import { API_BASE } from '@/constants/config'; import { API_BASE } from '@/constants/config';
import { getNotifications } from '@/utils/notifications'; import { getNotifications } from '@/utils/notifications';
import { setBadge } from '@/utils/chrome-misc' import { setBadge } from '@/utils/chrome-misc'
import { closeTo, getTimeRemaining } from '@/utils/time'; import { closeTo } from '@/utils/time';
import { getActionUsage } from '@/utils/user'; import { getActionUsage } from '@/utils/user';
// Disable conflict with yup extension // Disable conflict with yup extension
const yupExtensionId = 'nhmeoaahigiljjdkoagafdccikgojjoi' const yupExtensionId = 'nhmeoaahigiljjdkoagafdccikgojjoi'
@ -65,18 +65,19 @@ const alarmHandler = async () => {
if (store.settings?.chromeNotifWhenReward && notSeen.some(notif => notif.action === 'reward')) { if (store.settings?.chromeNotifWhenReward && notSeen.some(notif => notif.action === 'reward')) {
const rewardNotif = notSeen.find(notif => notif.action === 'reward') const rewardNotif = notSeen.find(notif => notif.action === 'reward')
if (rewardNotif) { if (rewardNotif) {
const storeReward = (await getNotifStorage()).lastRewardNotif const storeReward = (await getNotifStorageLastRewardNotif())
if (!storeReward || (storeReward.id !== rewardNotif._id &&
!closeTo(new Date(storeReward.createdAt), new Date(rewardNotif.createdAt), 2e4))) { if (!storeReward || (storeReward.id !== rewardNotif._id
&& !closeTo(new Date(storeReward.createdAt), new Date(rewardNotif.createdAt), 2e4)
)) {
{ {
setNotifStorageLastRewardNotif({ createdAt: rewardNotif.createdAt, id: rewardNotif._id }).then(() => { await setNotifStorageLastRewardNotif({ createdAt: rewardNotif.createdAt, id: rewardNotif._id });
chrome.notifications.create({ await chrome.notifications.create({
type: 'basic', type: 'basic',
iconUrl: chrome.runtime.getURL('src/assets/icons/yup_ext_128.png'), iconUrl: chrome.runtime.getURL('src/assets/icons/yup_ext_128.png'),
title: 'Yup Live Extension', title: 'Yup Live Extension',
message: `You have been alocated a future reward of ${rewardNotif.quantity} YUP`, message: `You have been alocated a future reward of ${rewardNotif.quantity} YUP`,
}) })
}).catch(console.error)
} }
} }
} }

View File

@ -51,13 +51,16 @@ export const storageDefault = {
} }
export const storageNotifsDefault = { export const storageNotifsDefault = {
lastRewardNotif: {
createdAt: 0,
id: '',
},
notifs: [] notifs: []
} }
export const lastRewardNotifDefault = {
createdAt: 0,
id: '',
}
export type StorageType = typeof storageDefault export type StorageType = typeof storageDefault
export const wipeStorage = async () => { export const wipeStorage = async () => {
@ -75,12 +78,15 @@ export const initStorage = async () => {
console.info('Storage initialized') console.info('Storage initialized')
} }
const notifs = await chrome.storage.local.get('notifs') const notifs = await chrome.storage.local.get('notifs')
const updateNotifs = { ...storageNotifsDefault, ...notifs } const lastRewardNotif = await chrome.storage.local.get('lastRewardNotif')
const updateNotifsCondition = !notifs.notifs || !notifs.lastRewardNotif if(!notifs.notifs) {
if(updateNotifsCondition) { await chrome.storage.local.set({ notifs: storageNotifsDefault })
await chrome.storage.local.set({ notifs: updateNotifs })
console.info('Notifs storage initialized') 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 () => { export const getNotifStorage = async () => {
@ -93,9 +99,14 @@ export const setNotifStorageNotifs = async (notifs: any[]) => {
await chrome.storage.local.set({ ...notifsStorage, notifs: notifs }) 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) => { export const setNotifStorageLastRewardNotif = async (lastRewardNotif) => {
const notifsStorage = await getNotifStorage() const lastReward = await getNotifStorageLastRewardNotif()
await chrome.storage.local.set({ ...notifsStorage, ...{ notifs: { lastRewardNotif } } }) chrome.storage.local.set({ lastRewardNotif: { ...lastReward, ...lastRewardNotif } })
} }
export const setAuth = async (auth) => { export const setAuth = async (auth) => {