-
-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
wip: poc for playing a sound on time end #1245
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import { useMemo } from 'react'; | ||
import { useSearchParams } from 'react-router-dom'; | ||
import { usePrevious } from '@mantine/hooks'; | ||
import { AnimatePresence, motion } from 'framer-motion'; | ||
import { | ||
CustomFields, | ||
|
@@ -12,12 +14,14 @@ import { | |
ViewSettings, | ||
} from 'ontime-types'; | ||
|
||
import sound from '../../../assets/sounds/buzzer.mp3'; | ||
import { overrideStylesURL } from '../../../common/api/constants'; | ||
import { FitText } from '../../../common/components/fit-text/FitText'; | ||
import MultiPartProgressBar from '../../../common/components/multi-part-progress-bar/MultiPartProgressBar'; | ||
import TitleCard from '../../../common/components/title-card/TitleCard'; | ||
import ViewParamsEditor from '../../../common/components/view-params-editor/ViewParamsEditor'; | ||
import { useRuntimeStylesheet } from '../../../common/hooks/useRuntimeStylesheet'; | ||
import { useTimerPhase } from '../../../common/hooks/useSocket'; | ||
import { useWindowTitle } from '../../../common/hooks/useWindowTitle'; | ||
import { ViewExtendedTimer } from '../../../common/models/TimeManager.type'; | ||
import { formatTime, getDefaultFormat } from '../../../common/utils/time'; | ||
|
@@ -59,12 +63,28 @@ interface TimerProps { | |
viewSettings: ViewSettings; | ||
} | ||
|
||
const usePhaseEvent = () => { | ||
const { phase } = useTimerPhase(); | ||
const previousValue = usePrevious(phase); | ||
|
||
const audio = useMemo(() => new Audio(sound), []); | ||
|
||
if (previousValue !== TimerPhase.None && previousValue !== phase && phase === TimerPhase.Overtime) { | ||
try { | ||
audio.play(); | ||
} catch (error) { | ||
console.error('Audio playback prevented', error); | ||
} | ||
} | ||
}; | ||
|
||
export default function Timer(props: TimerProps) { | ||
const { auxTimer, customFields, eventNow, eventNext, isMirrored, message, settings, time, viewSettings } = props; | ||
|
||
const { shouldRender } = useRuntimeStylesheet(viewSettings?.overrideStyles && overrideStylesURL); | ||
const { getLocalizedString } = useTranslation(); | ||
const [searchParams] = useSearchParams(); | ||
usePhaseEvent(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Implement sound feature toggle in Timer component To fulfill the PR objective of allowing users to activate/deactivate the sound feature, consider the following changes:
These changes will allow users to control the sound feature as requested in the PR objectives. Would you like assistance in implementing this feature? |
||
|
||
useWindowTitle('Timer'); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Refactor
usePhaseEvent
hook for better React integrationThe
usePhaseEvent
hook implementation can be improved:useEffect
to handle side effects (audio playback) instead of doing it during render.Here's a suggested refactor:
This refactor ensures that side effects are handled properly and allows for enabling/disabling the sound feature.