Skip to content

[f] 205 - Upvoted filter for sidebar#200

Merged
giahung68 merged 5 commits intomainfrom
features/205-upvoted-filter-for-sidebar
Dec 2, 2024
Merged

[f] 205 - Upvoted filter for sidebar#200
giahung68 merged 5 commits intomainfrom
features/205-upvoted-filter-for-sidebar

Conversation

@giahung68
Copy link
Collaborator

@giahung68 giahung68 commented Dec 2, 2024

Summary by CodeRabbit

Release Notes

  • New Features

    • Introduced AudioProvider for centralized audio state management.
    • Added SnippetAudioPlayer component for audio playback functionality.
    • Enhanced SearchInterface with localized sorting options and improved snippet link handling.
    • New filter option for "upvotedBy" in the Sidebar component.
    • Updated translations for new sorting options and user engagement features.
  • Bug Fixes

    • Improved the user interface of the AudioPlayer with updated playback speed options.
  • Documentation

    • Expanded translation capabilities for better internationalization support.

@coderabbitai
Copy link

coderabbitai bot commented Dec 2, 2024

Walkthrough

The pull request involves significant updates to various components within the application, including the introduction of an AudioProvider for managing audio state, modifications to the AudioPlayer and SnippetAudioPlayer components, and enhancements to the SearchInterface and Sidebar for improved filtering and sorting functionalities. Additionally, the translations constant has been expanded to support new user engagement features. The changes collectively enhance user interaction, audio management, and internationalization across the application.

Changes

File Path Change Summary
cypress/e2e/broadcast-dashboard.cy.ts Deleted Cypress end-to-end tests for the broadcast dashboard feature.
src/App.tsx Added AudioProvider component to wrap existing providers, altering the component tree structure.
src/components/AudioPlayer.tsx Modified JSX structure, updated aria-labels, changed playback speed options, and reformatted inline styles.
src/components/SearchInterface.tsx Updated imports, modified handleSnippetClick to handle control/meta key clicks, and localized dropdown options.
src/components/Sidebar.tsx Added new filter option for "upvotedBy" and updated terminology in CountUp component.
src/components/SnippetAudioPlayer.tsx Introduced a new audio player component managing playback state and controls.
src/components/SnippetCard.tsx Integrated SnippetAudioPlayer, updated click handling, and enhanced visual feedback for user interactions.
src/constants/translations.ts Expanded translations for new sorting options and user engagement features in English and Spanish.
src/hooks/useSnippetFilters.tsx Added upvotedBy property to SnippetFilters type and updated related hook functionality.
src/providers/audio.tsx Created AudioContext and AudioProvider for centralized audio state management.

Possibly related PRs

Suggested reviewers

  • nlgthuan

Poem

In the land of code where rabbits play,
New features hop in, brightening the day.
With audio and filters, our tools now shine,
A dashboard of broadcasts, all yours and mine!
So let’s click and listen, let our joy unfurl,
In this wondrous world, let’s give it a whirl! 🐇🎶


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

@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: 5

🧹 Outside diff range and nitpick comments (7)
src/providers/audio.tsx (2)

3-9: Consider improving the state structure

The pause property being nullable (boolean | null) seems unnecessary. Consider defaulting it to false since an audio can only be either playing or paused.

 export const AudioContext = createContext<{
-  currentAudio: { id: string | null; pause: boolean | null }
-  setCurrentAudio: (audio: { id: string | null; pause: boolean | null }) => void
+  currentAudio: { id: string | null; pause: boolean }
+  setCurrentAudio: (audio: { id: string | null; pause: boolean }) => void
 }>({
-  currentAudio: { id: null, pause: null },
+  currentAudio: { id: null, pause: false },
   setCurrentAudio: () => {}
 })

1-1: Optimize React import for types

Use type imports to optimize the bundle size.

-import React, { createContext, useState } from 'react'
+import { createContext, useState } from 'react'
+import type { ReactNode } from 'react'
🧰 Tools
🪛 eslint

[error] 1-1: Import "React" is only used as types.

(@typescript-eslint/consistent-type-imports)

src/hooks/useSnippetFilters.tsx (2)

Line range hint 76-86: Simplify arrow function and fix isEmpty logic

The isEmpty function can be simplified and should check upvotedBy length.

-  const isEmpty = useCallback(() => {
-    return (
-      languages.length === 0 &&
-      states.length === 0 &&
-      sources.length === 0 &&
-      labels.length === 0 &&
-      labeledBy.length === 0 &&
-      starredBy.length === 0 &&
-      !politicalSpectrum
-    )
-  }, [languages, states, sources, labels, labeledBy, starredBy, politicalSpectrum, order_by, upvotedBy])
+  const isEmpty = useCallback(() => (
+    languages.length === 0 &&
+    states.length === 0 &&
+    sources.length === 0 &&
+    labels.length === 0 &&
+    labeledBy.length === 0 &&
+    starredBy.length === 0 &&
+    upvotedBy.length === 0 &&
+    !politicalSpectrum
+  ), [languages, states, sources, labels, labeledBy, starredBy, upvotedBy, politicalSpectrum])

Note: Removed order_by from deps array as it's not used in the function.

🧰 Tools
🪛 eslint

[error] 89-89: Unexpected any. Specify a different type.

(@typescript-eslint/no-explicit-any)


72-73: Consider grouping related filter properties

The order_by property could be grouped with other sorting/ordering related properties for better organization.

   const filters = {
     languages,
     states,
     sources,
     labels,
     labeledBy,
     starredBy,
     politicalSpectrum,
-    order_by,
-    upvotedBy
+    upvotedBy,
+    order_by
   }
src/constants/translations.ts (1)

14-14: Consider using consistent verb/noun forms in translations

The English translation uses a verb form 'Upvote' while the Spanish translation uses a past participle 'Votado'. Consider using either:

  • English: 'Upvoted' to match Spanish 'Votado'
  • Spanish: 'Votar' to match English 'Upvote'

Also applies to: 111-111

src/components/AudioPlayer.tsx (1)

Line range hint 91-144: Consider extracting common audio logic

There's significant duplication of audio handling logic between AudioPlayer and SnippetAudioPlayer. Consider creating a custom hook:

// useAudioPlayer.ts
export const useAudioPlayer = (audioRef: RefObject<HTMLAudioElement>, initialTime?: string) => {
  const [isPlaying, setIsPlaying] = useState(false)
  const [currentTime, setCurrentTime] = useState(0)
  const [duration, setDuration] = useState(0)

  // ... extract common logic here

  return {
    isPlaying,
    currentTime,
    duration,
    togglePlayPause,
    onProgressChange,
    formatTime
  }
}
🧰 Tools
🪛 eslint

[error] 142-144: Empty components are self-closing

(react/self-closing-comp)

src/components/SearchInterface.tsx (1)

148-153: Consider simplifying the nested ternary operators

The nested ternary operators make the code harder to read and maintain.

Consider using a mapping object or switch statement instead:

-{filters.order_by === 'activities'
-  ? t.sortBy.mostRecentActivities
-  : filters.order_by === 'upvotes'
-    ? t.sortBy.mostUpvotes
-    : filters.order_by === 'comments'
-      ? t.sortBy.mostComments
-      : t.sortBy.mostRecentRecordings}
+{
+  const sortLabels = {
+    'activities': t.sortBy.mostRecentActivities,
+    'upvotes': t.sortBy.mostUpvotes,
+    'comments': t.sortBy.mostComments,
+    'latest': t.sortBy.mostRecentRecordings
+  };
+  sortLabels[filters.order_by || 'latest']
+}
🧰 Tools
🪛 eslint

[error] 147-153: Do not nest ternary expressions.

(no-nested-ternary)


[error] 149-153: Do not nest ternary expressions.

(no-nested-ternary)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 5a7d538 and 22a81b0.

📒 Files selected for processing (10)
  • cypress/e2e/broadcast-dashboard.cy.ts (0 hunks)
  • src/App.tsx (1 hunks)
  • src/components/AudioPlayer.tsx (5 hunks)
  • src/components/SearchInterface.tsx (5 hunks)
  • src/components/Sidebar.tsx (3 hunks)
  • src/components/SnippetAudioPlayer.tsx (1 hunks)
  • src/components/SnippetCard.tsx (4 hunks)
  • src/constants/translations.ts (4 hunks)
  • src/hooks/useSnippetFilters.tsx (5 hunks)
  • src/providers/audio.tsx (1 hunks)
💤 Files with no reviewable changes (1)
  • cypress/e2e/broadcast-dashboard.cy.ts
🧰 Additional context used
🪛 eslint
src/hooks/useSnippetFilters.tsx

[error] 76-86: Unexpected block statement surrounding arrow body; move the returned value immediately after the =>.

(arrow-body-style)

src/components/SnippetCard.tsx

[error] 2-2: 'PauseIcon' is defined but never used.

(@typescript-eslint/no-unused-vars)


[error] 2-2: 'PlayIcon' is defined but never used.

(@typescript-eslint/no-unused-vars)

src/components/SearchInterface.tsx

[error] 30-30: Missing file extension for "@/constants/translations"

(import/extensions)


[error] 56-56: Unexpected constant nullishness on the left-hand side of a ?? expression.

(no-constant-binary-expression)


[error] 59-59: Unexpected constant nullishness on the left-hand side of a ?? expression.

(no-constant-binary-expression)


[error] 149-153: Do not nest ternary expressions.

(no-nested-ternary)

src/providers/audio.tsx

[error] 1-1: Import "React" is only used as types.

(@typescript-eslint/consistent-type-imports)


[error] 19-22: The object passed as the value prop to the Context provider (at line 19) changes every render. To fix this consider wrapping it in a useMemo hook.

(react/jsx-no-constructed-context-values)

src/components/SnippetAudioPlayer.tsx

[error] 1-1: Import "FC" is only used as types.

(@typescript-eslint/consistent-type-imports)


[error] 2-2: Missing file extension for "@/components/ui/button"

(import/extensions)


[error] 3-3: Missing file extension for "@/components/ui/slider"

(import/extensions)


[error] 5-5: Missing file extension for "@/providers/audio"

(import/extensions)


[error] 7-120: Prefer default export on a file with single export.

(import/prefer-default-export)


[error] 60-66: Arrow function expected no return value.

(consistent-return)


[error] 105-105: Media elements such as and

(jsx-a11y/media-has-caption)

src/components/AudioPlayer.tsx

[error] 142-144: Empty components are self-closing

(react/self-closing-comp)

🔇 Additional comments (10)
src/App.tsx (1)

38-58: LGTM! Provider hierarchy is well-structured

The AudioProvider is correctly placed in the provider hierarchy:

  • Inside AuthProvider for global auth state access
  • Wrapping UI-related providers (Sidebar, Language) which might need audio state
src/constants/translations.ts (1)

59-63: LGTM! Well-structured sorting options

The sorting options are well-organized and consistently translated between English and Spanish.

Also applies to: 123-127

src/components/AudioPlayer.tsx (2)

134-137: LGTM! Improved playback speed options

The updated playback speed options (75%, 100%, 125%, 150%) provide a good range of control.


92-92: ⚠️ Potential issue

Add captions support for accessibility

Similar to SnippetAudioPlayer, add caption support here as well.

🧰 Tools
🪛 eslint

[error] 92-92: Media elements such as and

(jsx-a11y/media-has-caption)

src/components/Sidebar.tsx (3)

27-27: LGTM: Filter declaration follows existing pattern

The upvotedBy filter is correctly added to the destructured filters object, maintaining consistency with other filter declarations.


171-182: LGTM: Upvoted filter section implementation

The implementation follows the established pattern used for labeled and starred sections, ensuring consistency in the UI and behavior.


86-88: LGTM: Improved terminology consistency

Changed "items" to "snippets" in the count formatting, providing more precise and consistent terminology.

src/components/SearchInterface.tsx (1)

54-61: LGTM: Enhanced snippet click handling

Added support for opening snippets in new tabs using Ctrl/Cmd+Click, improving user experience.

🧰 Tools
🪛 eslint

[error] 56-56: Unexpected constant nullishness on the left-hand side of a ?? expression.

(no-constant-binary-expression)


[error] 59-59: Unexpected constant nullishness on the left-hand side of a ?? expression.

(no-constant-binary-expression)

src/components/SnippetCard.tsx (2)

145-147: LGTM: Enhanced snippet card interaction

The changes improve the card's visual feedback with a smooth border transition on hover and proper click handling.


194-194: LGTM: Audio player integration

The SnippetAudioPlayer is correctly integrated with the required props.

Comment on lines +17 to +25
return (
<AudioContext.Provider
value={{
currentAudio,
setCurrentAudio: setCurrentAudio as (audio: { id: string | null; pause: boolean | null }) => void
}}>
{children}
</AudioContext.Provider>
)
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Optimize context value to prevent unnecessary re-renders

The context value object is recreated on every render. Wrap it in useMemo to optimize performance.

+import { createContext, useState, useMemo } from 'react'

 return (
   <AudioContext.Provider
-    value={{
-      currentAudio,
-      setCurrentAudio: setCurrentAudio as (audio: { id: string | null; pause: boolean | null }) => void
-    }}>
+    value={useMemo(() => ({
+      currentAudio,
+      setCurrentAudio: setCurrentAudio as (audio: { id: string | null; pause: boolean }) => void
+    }), [currentAudio])}>
     {children}
   </AudioContext.Provider>
 )
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
return (
<AudioContext.Provider
value={{
currentAudio,
setCurrentAudio: setCurrentAudio as (audio: { id: string | null; pause: boolean | null }) => void
}}>
{children}
</AudioContext.Provider>
)
return (
<AudioContext.Provider
value={useMemo(() => ({
currentAudio,
setCurrentAudio: setCurrentAudio as (audio: { id: string | null; pause: boolean | null }) => void
}), [currentAudio])}>
{children}
</AudioContext.Provider>
)
🧰 Tools
🪛 eslint

[error] 19-22: The object passed as the value prop to the Context provider (at line 19) changes every render. To fix this consider wrapping it in a useMemo hook.

(react/jsx-no-constructed-context-values)

Comment on lines +76 to +80
if (currentAudio.id && currentAudio.id !== id && currentAudio.pause) {
currentAudio.pause()
}
audio.play()
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add error handling for audio playback

The play operation should handle potential failures:

 if (currentAudio.id && currentAudio.id !== id && currentAudio.pause) {
   currentAudio.pause()
 }
-audio.play()
+audio.play().catch(error => {
+  console.error('Failed to play audio:', error)
+  setIsPlaying(false)
+})
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (currentAudio.id && currentAudio.id !== id && currentAudio.pause) {
currentAudio.pause()
}
audio.play()
}
if (currentAudio.id && currentAudio.id !== id && currentAudio.pause) {
currentAudio.pause()
}
audio.play().catch(error => {
console.error('Failed to play audio:', error)
setIsPlaying(false)
})
}

Comment on lines +23 to +28
if (initialStartTime) {
const [hours, minutes, seconds] = initialStartTime.split(':').map(Number)
const startSeconds = hours * 3600 + minutes * 60 + seconds
audio.currentTime = startSeconds
setCurrentTime(startSeconds)
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add error handling for time parsing

The time parsing logic could fail if initialStartTime is in an invalid format. Consider adding error handling:

 if (initialStartTime) {
+  try {
     const [hours, minutes, seconds] = initialStartTime.split(':').map(Number)
+    if (isNaN(hours) || isNaN(minutes) || isNaN(seconds)) {
+      throw new Error('Invalid time format')
+    }
     const startSeconds = hours * 3600 + minutes * 60 + seconds
     audio.currentTime = startSeconds
     setCurrentTime(startSeconds)
+  } catch (error) {
+    console.error('Failed to parse initial start time:', error)
+  }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (initialStartTime) {
const [hours, minutes, seconds] = initialStartTime.split(':').map(Number)
const startSeconds = hours * 3600 + minutes * 60 + seconds
audio.currentTime = startSeconds
setCurrentTime(startSeconds)
}
if (initialStartTime) {
try {
const [hours, minutes, seconds] = initialStartTime.split(':').map(Number)
if (isNaN(hours) || isNaN(minutes) || isNaN(seconds)) {
throw new Error('Invalid time format')
}
const startSeconds = hours * 3600 + minutes * 60 + seconds
audio.currentTime = startSeconds
setCurrentTime(startSeconds)
} catch (error) {
console.error('Failed to parse initial start time:', error)
}
}


return (
<div onClick={handleClick} className='my-4'>
<audio ref={audioRef} src={`${import.meta.env.VITE_AUDIO_BASE_URL}/${path}`} />
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add captions support for accessibility

The audio element should support captions for accessibility compliance.

-<audio ref={audioRef} src={`${import.meta.env.VITE_AUDIO_BASE_URL}/${path}`} />
+<audio ref={audioRef} src={`${import.meta.env.VITE_AUDIO_BASE_URL}/${path}`}>
+  <track kind="captions" src={`${import.meta.env.VITE_AUDIO_BASE_URL}/${path.replace(/\.[^/.]+$/, '.vtt')}`} label="English" />
+</audio>

Committable suggestion skipped: line range outside the PR's diff.

🧰 Tools
🪛 eslint

[error] 105-105: Media elements such as and

(jsx-a11y/media-has-caption)

@@ -1,5 +1,5 @@
import React, { useState, useEffect } from 'react'
import { ThumbsUp, ThumbsDown } from 'lucide-react'
import { ThumbsUp, ThumbsDown, PauseIcon, PlayIcon } from 'lucide-react'
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Remove unused imports

The PauseIcon and PlayIcon imports are not used in this file.

-import { ThumbsUp, ThumbsDown, PauseIcon, PlayIcon } from 'lucide-react'
+import { ThumbsUp, ThumbsDown } from 'lucide-react'
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
import { ThumbsUp, ThumbsDown, PauseIcon, PlayIcon } from 'lucide-react'
import { ThumbsUp, ThumbsDown } from 'lucide-react'
🧰 Tools
🪛 eslint

[error] 2-2: 'PauseIcon' is defined but never used.

(@typescript-eslint/no-unused-vars)


[error] 2-2: 'PlayIcon' is defined but never used.

(@typescript-eslint/no-unused-vars)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant