mango-v4-ui/utils/notifications.ts

69 lines
1.8 KiB
TypeScript
Raw Normal View History

2022-11-24 18:39:14 -08:00
import { INITIAL_SOUND_SETTINGS } from '@components/settings/SoundSettings'
2022-09-12 08:53:57 -07:00
import mangoStore from '@store/mangoStore'
2022-11-23 04:40:38 -08:00
import { Howl } from 'howler'
import { SOUND_SETTINGS_KEY } from './constants'
2022-07-05 20:37:49 -07:00
export type TransactionNotification = {
2022-07-05 20:37:49 -07:00
type: 'success' | 'info' | 'error' | 'confirm'
title: string
description?: null | string
txid?: string
show: boolean
id: number
}
2022-12-07 21:05:36 -08:00
const successSound = new Howl({
src: ['/sounds/transaction-success.mp3'],
volume: 0.5,
})
const failSound = new Howl({
src: ['/sounds/transaction-fail.mp3'],
volume: 0.5,
})
2022-11-23 04:40:38 -08:00
2022-07-05 20:37:49 -07:00
export function notify(newNotification: {
type?: 'success' | 'info' | 'error' | 'confirm'
title: string
description?: string
txid?: string
2022-11-23 04:40:38 -08:00
noSound?: boolean
2022-07-05 20:37:49 -07:00
}) {
const setMangoStore = mangoStore.getState().set
const notifications = mangoStore.getState().transactionNotifications
const lastId = mangoStore.getState().transactionNotificationIdCounter
2022-07-05 20:37:49 -07:00
const newId = lastId + 1
2022-11-23 04:40:38 -08:00
const savedSoundSettings = localStorage.getItem(SOUND_SETTINGS_KEY)
const soundSettings = savedSoundSettings
? JSON.parse(savedSoundSettings)
: INITIAL_SOUND_SETTINGS
if (newNotification.type && !newNotification.noSound) {
switch (newNotification.type) {
case 'success': {
if (soundSettings['transaction-success']) {
successSound.play()
}
break
}
case 'error': {
if (soundSettings['transaction-fail']) {
failSound.play()
}
}
}
}
2022-07-05 20:37:49 -07:00
const newNotif: TransactionNotification = {
2022-07-05 20:37:49 -07:00
id: newId,
type: 'success',
show: true,
description: null,
...newNotification,
}
setMangoStore((state) => {
state.transactionNotificationIdCounter = newId
state.transactionNotifications = [...notifications, newNotif]
2022-07-05 20:37:49 -07:00
})
}