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

Conversation

cpvalente
Copy link
Owner

@cpvalente cpvalente commented Oct 7, 2024

We would still need to let the users activate and deactivate this
We also need a better sound file

We may run into issues with autoplay permissions and should investigate how to direct users around this
https://developer.chrome.com/blog/autoplay

Copy link
Contributor

coderabbitai bot commented Oct 7, 2024

Warning

Rate limit exceeded

@cpvalente has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 14 minutes and 56 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Files that changed from the base of the PR and between d02940b and f554605.

Walkthrough

This pull request introduces a new custom hook, useTimerPhase, which extracts the phase from the timer state in the RuntimeStore. Additionally, it enhances the Timer component by implementing a new hook, usePhaseEvent, that utilizes useTimerPhase to manage audio playback based on timer phase transitions. The changes include the addition of audio functionality that plays a sound when the timer enters the Overtime phase, integrating this feature into the component's lifecycle.

Changes

File Path Change Summary
apps/client/src/common/hooks/useSocket.ts - New hook added: useTimerPhase to extract phase from the timer state in RuntimeStore.
apps/client/src/features/viewers/timer/Timer.tsx - New hook added: usePhaseEvent to track current and previous timer phases and manage audio playback.
- Updated Timer component to integrate usePhaseEvent and added audio playback functionality.

Possibly related PRs

  • Keep offset when taking over playback with roll v2 #1184: The addition of the timerPhase property to the usePlaybackControl hook in useSocket.ts is directly related to the new useTimerPhase hook introduced in the main PR, as both involve managing and accessing the timer phase state.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 4

🧹 Outside diff range and nitpick comments (4)
apps/client/src/common/hooks/useSocket.ts (2)

179-185: LGTM! Consider adding a type annotation for consistency.

The useTimerPhase hook is well-implemented and follows the established pattern in the file. It provides a clean way to access the timer phase, which will be useful for the new audio playback feature.

For consistency with other hooks in the file, consider adding a type annotation to the featureSelector function:

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

  return useRuntimeStore(featureSelector);
};

179-185: Consider adding documentation for the new hook.

The addition of the useTimerPhase hook is well-placed and consistent with the file's structure. It will be useful for components that need to react to timer phase changes, supporting the new audio playback feature mentioned in the PR objectives.

To improve maintainability, consider adding a brief JSDoc comment explaining the purpose of the hook and its return value. For example:

/**
 * Hook to access the current timer phase from the runtime store.
 * @returns An object containing the current timer phase.
 */
export const useTimerPhase = () => {
  // ... (existing implementation)
};
apps/client/src/features/viewers/timer/Timer.tsx (2)

82-82: Ensure custom hooks follow React conventions

Calling usePhaseEvent() directly inside the component without handling its lifecycle can lead to unexpected side effects during rendering. It's recommended to place the call inside a useEffect in the Timer component or adjust the hook to properly manage side effects.

Consider this adjustment:

-export default function Timer(props: TimerProps) {
+export default function Timer(props: TimerProps) {
+  usePhaseEvent();

Adjusted usePhaseEvent:

const usePhaseEvent = () => {
  const { phase } = useTimerPhase();
  const previousValue = usePrevious(phase);

  useEffect(() => {
    if (
      previousValue !== TimerPhase.None &&
      previousValue !== phase &&
      phase === TimerPhase.Overtime
    ) {
      const audio = new Audio(sound);
      audio.play();
    }
  }, [phase, previousValue]);
};

This adheres to React's best practices by handling side effects appropriately.


65-74: Handle user interaction for audio playback

Modern browsers often restrict autoplay of audio without user interaction. To ensure the sound plays reliably, you may need to handle user gestures or provide a play button.

Consider prompting the user to enable sound or incorporating user interaction before playing audio.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 86e6f8b and 8538b3a.

⛔ Files ignored due to path filters (1)
  • apps/client/src/assets/sounds/buzzer.mp3 is excluded by !**/*.mp3 and included by apps/client/src/**
📒 Files selected for processing (2)
  • apps/client/src/common/hooks/useSocket.ts (1 hunks)
  • apps/client/src/features/viewers/timer/Timer.tsx (4 hunks)
🧰 Additional context used
🔇 Additional comments (1)
apps/client/src/common/hooks/useSocket.ts (1)

179-185: Reminder: Implement activation/deactivation functionality for the sound feature.

As mentioned in the PR description, the ability to activate and deactivate the sound feature is still pending. Consider how this functionality will be implemented, possibly through additional hooks or state management.

To help plan the next steps, let's check for any existing audio-related code:

Would you like assistance in designing the activation/deactivation feature for the sound playback?

apps/client/src/features/viewers/timer/Timer.tsx Outdated Show resolved Hide resolved
apps/client/src/features/viewers/timer/Timer.tsx Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (1)
apps/client/src/features/viewers/timer/Timer.tsx (1)

Line range hint 1-280: Summary of review and PR objectives

This implementation is a good start towards playing a sound when the timer enters the Overtime phase. However, there are a few areas that need attention to fully meet the PR objectives and ensure best practices:

  1. The sound feature toggle needs to be implemented as mentioned in the PR objectives.
  2. The usePhaseEvent hook should be refactored to use useEffect for better integration with React's lifecycle.
  3. The unnecessary <audio> element should be removed from the JSX.

Regarding the concern about autoplay permissions, you might want to add a user interaction (e.g., a button click) to trigger the initial audio playback. This can help bypass autoplay restrictions in some browsers.

As for the need for a better sound file, consider adding a TODO comment or creating a separate issue to track this requirement.

Overall, with the suggested changes, this implementation will be closer to meeting all the PR objectives and following React best practices.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 8538b3a and d02940b.

⛔ Files ignored due to path filters (1)
  • apps/client/src/assets/sounds/buzzer.mp3 is excluded by !**/*.mp3 and included by apps/client/src/**
📒 Files selected for processing (2)
  • apps/client/src/common/hooks/useSocket.ts (1 hunks)
  • apps/client/src/features/viewers/timer/Timer.tsx (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • apps/client/src/common/hooks/useSocket.ts
🧰 Additional context used
🔇 Additional comments (2)
apps/client/src/features/viewers/timer/Timer.tsx (2)

1-1: LGTM: New imports for audio functionality

The new imports for useMemo, usePrevious, and the sound file are appropriate for implementing the audio playback feature.

Also applies to: 3-3, 17-17


195-195: ⚠️ Potential issue

Remove unnecessary <audio> element

The <audio> element added to the JSX is not required since audio playback is managed programmatically within the usePhaseEvent hook. Including it may cause redundancy or conflicts in audio playback.

You can safely remove this line:

-<audio controls src='../../assets/sounds/buzzer.mp3' />

This will ensure that audio is played only through the programmatic approach implemented in the usePhaseEvent hook.

Likely invalid or redundant comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant