Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added apps/client/src/assets/sounds/buzzer.mp3
Binary file not shown.
8 changes: 8 additions & 0 deletions apps/client/src/common/hooks/useSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ export const useTimer = () => {
return useRuntimeStore(featureSelector);
};

export const useTimerPhase = () => {
const featureSelector = (state: RuntimeStore) => ({
phase: state.timer.phase,
});

return useRuntimeStore(featureSelector);
};

export const useClock = () => {
const featureSelector = (state: RuntimeStore) => ({
clock: state.clock,
Expand Down
20 changes: 20 additions & 0 deletions apps/client/src/features/viewers/timer/Timer.tsx
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,
Expand All @@ -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';
Expand Down Expand Up @@ -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);
}
}
};
Comment on lines +66 to +79
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

⚠️ Potential issue

Refactor usePhaseEvent hook for better React integration

The usePhaseEvent hook implementation can be improved:

  1. Use useEffect to handle side effects (audio playback) instead of doing it during render.
  2. Implement a way to enable/disable the sound feature as mentioned in the PR objectives.

Here's a suggested refactor:

const usePhaseEvent = (soundEnabled: boolean) => {
  const { phase } = useTimerPhase();
  const previousPhase = usePrevious(phase);
  const audio = useMemo(() => new Audio(sound), []);

  useEffect(() => {
    if (
      soundEnabled &&
      previousPhase !== TimerPhase.None &&
      previousPhase !== phase &&
      phase === TimerPhase.Overtime
    ) {
      try {
        audio.play();
      } catch (error) {
        console.error('Audio playback prevented', error);
      }
    }
  }, [phase, previousPhase, soundEnabled, audio]);
};

This refactor ensures that side effects are handled properly and allows for enabling/disabling the sound feature.


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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

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:

  1. Add a state variable for the sound toggle:

    const [soundEnabled, setSoundEnabled] = useState(false);
  2. Pass the state to the usePhaseEvent hook:

    usePhaseEvent(soundEnabled);
  3. Add a UI element (e.g., a checkbox) to toggle the sound feature:

    <label>
      <input
        type="checkbox"
        checked={soundEnabled}
        onChange={(e) => setSoundEnabled(e.target.checked)}
      />
      Enable sound
    </label>

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');

Expand Down