This repository has been archived by the owner on Jun 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
55 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,41 @@ | ||
import { Middleware, isAnyOf } from '@reduxjs/toolkit' | ||
import { getCowSoundSuccess, getCowSoundSend } from 'utils/sound' | ||
import { AppState } from 'state' | ||
import { finalizeTransaction } from '../enhancedTransactions/actions' | ||
import { finalizeTransaction, addTransaction } from '../enhancedTransactions/actions' | ||
import { setClaimStatus, ClaimStatus } from './actions' | ||
|
||
const isFinalizeTransaction = isAnyOf(finalizeTransaction) | ||
const isAddTransaction = isAnyOf(addTransaction) | ||
|
||
// Watch for claim tx being finalized and triggers a change of status | ||
export const claimMinedMiddleware: Middleware<Record<string, unknown>, AppState> = (store) => (next) => (action) => { | ||
const result = next(action) | ||
|
||
if (isFinalizeTransaction(action)) { | ||
let cowSound | ||
if (isAddTransaction(action)) { | ||
const { chainId, hash } = action.payload | ||
const transaction = store.getState().transactions[chainId][hash] | ||
|
||
if (transaction.claim) { | ||
console.debug('[stat:claim:middleware] Claim transaction sent', transaction.hash, transaction.claim) | ||
cowSound = getCowSoundSend() | ||
} | ||
} else if (isFinalizeTransaction(action)) { | ||
const { chainId, hash } = action.payload | ||
const transaction = store.getState().transactions[chainId][hash] | ||
|
||
if (transaction.claim) { | ||
console.debug('[stat:claim:middleware] Claim transaction finalized', transaction.hash, transaction.claim) | ||
store.dispatch(setClaimStatus(ClaimStatus.CONFIRMED)) | ||
cowSound = getCowSoundSuccess() | ||
} | ||
} | ||
|
||
if (cowSound) { | ||
cowSound.play().catch((e) => { | ||
console.error('🐮 [Claiming] Moooooo cannot be played', e) | ||
}) | ||
} | ||
|
||
return result | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
type SoundType = 'SEND' | 'SUCCESS' | 'ERROR' | ||
type Sounds = Record<SoundType, string> | ||
|
||
const COW_SOUNDS: Sounds = { | ||
SEND: '/audio/mooooo-send__lower-90.mp3', | ||
SUCCESS: '/audio/mooooo-success__ben__lower-90.mp3', | ||
ERROR: '/audio/mooooo-error__lower-90.mp3', | ||
} | ||
|
||
const SOUND_CACHE: Record<string, HTMLAudioElement | undefined> = {} | ||
|
||
function getAudio(type: SoundType): HTMLAudioElement { | ||
const soundPath = COW_SOUNDS[type] | ||
let sound = SOUND_CACHE[soundPath] | ||
|
||
if (!sound) { | ||
sound = new Audio(soundPath) | ||
SOUND_CACHE[soundPath] = sound | ||
} | ||
|
||
return sound | ||
} | ||
|
||
export function getCowSoundSend(): HTMLAudioElement { | ||
return getAudio('SEND') | ||
} | ||
|
||
export function getCowSoundSuccess(): HTMLAudioElement { | ||
return getAudio('SUCCESS') | ||
} | ||
|
||
export function getCowSoundError(): HTMLAudioElement { | ||
return getAudio('ERROR') | ||
} |