Skip to content

Commit

Permalink
feat: add sound when limit is over (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
uigywnkiub authored Feb 4, 2025
1 parent 2d36156 commit a9376f0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app/ui/home/transaction-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ function TransactionForm({ currency, userCategories }: TProps) {
useEffect(() => setCategory(newCategoryState), [newCategoryState])
const categoryName = Array.from(category)[0]?.toString()
useEffect(() => {
if (categoryName) {
if (categoryName && categoryName !== DEFAULT_CATEGORY) {
setInLocalStorage(LOCAL_STORAGE_KEY.SELECTED_CATEGORY_NAME, categoryName)
}

Expand Down
19 changes: 12 additions & 7 deletions app/ui/limit-toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { useEffect } from 'react'
import toast from 'react-hot-toast'
import { useDebounce } from 'react-use'
import { useAudio, useDebounce } from 'react-use'

import DEFAULT_CATEGORIES from '@/public/data/default-categories.json'

Expand Down Expand Up @@ -31,13 +31,17 @@ type TProps = {
}

export default function LimitToast({ triggerBy }: TProps) {
const [audio, , audioControls] = useAudio({
src: '/sounds/error-limit-over.mp3',
})

const init = async () => {
try {
const selectedCategoryName = getFromLocalStorage(
LOCAL_STORAGE_KEY.SELECTED_CATEGORY_NAME,
)
if (!selectedCategoryName) return null
const selectedCategoryName = getFromLocalStorage(
LOCAL_STORAGE_KEY.SELECTED_CATEGORY_NAME,
)
if (!selectedCategoryName) return

try {
const session = await getCachedAuthSession()
const userId = session?.user?.email
const [limitsRaw, transactions] = await Promise.all([
Expand Down Expand Up @@ -85,6 +89,7 @@ export default function LimitToast({ triggerBy }: TProps) {
),
duration: TOAST_DURATION * 1.5,
})
audioControls.play()
}
} catch (err) {
throw err
Expand All @@ -98,5 +103,5 @@ export default function LimitToast({ triggerBy }: TProps) {
if (!isReady()) cancel()
}, [cancel, isReady])

return null
return audio
}
15 changes: 10 additions & 5 deletions app/ui/limits/limits.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,9 @@ function Limits({ userId, currency, transactions, userCategories }: TProps) {
items={userCategories}
selectedKeys={category}
onSelectionChange={setCategory}
disabledKeys={disabledCategories}
disabledKeys={disabledCategories.concat(
DEFAULT_CATEGORY,
)}
>
{userCategories.map((category, idx, arr) => (
<SelectSection
Expand Down Expand Up @@ -519,6 +521,9 @@ function Limits({ userId, currency, transactions, userCategories }: TProps) {
/>
}
description='Edit category limit'
classNames={{
description: 'text-default-500',
}}
>
Edit
</DropdownItem>
Expand All @@ -538,6 +543,9 @@ function Limits({ userId, currency, transactions, userCategories }: TProps) {
/>
}
description='Permanently delete category limit'
classNames={{
description: 'text-default-500',
}}
>
Delete
</DropdownItem>
Expand All @@ -550,10 +558,7 @@ function Limits({ userId, currency, transactions, userCategories }: TProps) {
})}
</ul>
<div className='mt-4 flex flex-col gap-2 text-left md:mt-8'>
<InfoText
withDoubleAsterisk
text='The calculation of limits is based on transactions by the current month.'
/>
<InfoText text='The calculation of limits is based on transactions by the current month.' />
<InfoText text='Your limits will not be deleted automatically in the new month.' />
</div>

Expand Down
Binary file added public/sounds/error-limit-over.mp3
Binary file not shown.

0 comments on commit a9376f0

Please sign in to comment.