- 
                Notifications
    You must be signed in to change notification settings 
- Fork 16
feat: hide FasterCarsFromBehind during Lone Qualify #44
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
          
     Merged
      
      
    
  
     Merged
                    Changes from 2 commits
      Commits
    
    
            Show all changes
          
          
            5 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      3e25ab0
              
                abstract display component, tidy up animations, and hide in lone qualify
              
              
                tariknz b789aa9
              
                tidy up
              
              
                tariknz 441cb58
              
                Update src/frontend/components/FasterCarsFromBehind/hooks/useCarBehin…
              
              
                tariknz 5f93f04
              
                Update src/frontend/components/FasterCarsFromBehind/hooks/useCarBehin…
              
              
                tariknz 310d9af
              
                Merge branch 'main' into fix/fastercars-tidy
              
              
                tariknz File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
        
          
          
            30 changes: 22 additions & 8 deletions
          
          30 
        
  src/frontend/components/FasterCarsFromBehind/FasterCarsFromBehind.stories.tsx
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,18 +1,32 @@ | ||
| import { Meta, StoryObj } from '@storybook/react-vite'; | ||
| import { FasterCarsFromBehind } from './FasterCarsFromBehind'; | ||
| import { FasterCarsFromBehind, FasterCarsFromBehindDisplay } from './FasterCarsFromBehind'; | ||
| import { TelemetryDecorator } from '@irdashies/storybook'; | ||
|  | ||
| export default { | ||
| component: FasterCarsFromBehind, | ||
| parameters: { | ||
| controls: { | ||
| exclude: ['telemetryPath'], | ||
| } | ||
| component: FasterCarsFromBehindDisplay, | ||
| argTypes: { | ||
| classColor: { | ||
| options: [undefined, 0xffda59, 0x33ceff, 0xff5888, 0xae6bff, 0xffffff], | ||
| control: { type: 'select' }, | ||
| }, | ||
| percent: { | ||
| control: { type: 'range', min: 0, max: 100 }, | ||
| }, | ||
| } | ||
| } as Meta<typeof FasterCarsFromBehind>; | ||
| } as Meta<typeof FasterCarsFromBehindDisplay>; | ||
|  | ||
| type Story = StoryObj<typeof FasterCarsFromBehind>; | ||
| type Story = StoryObj<typeof FasterCarsFromBehindDisplay>; | ||
|  | ||
| export const Primary: Story = { | ||
| render: () => <FasterCarsFromBehind />, | ||
| decorators: [TelemetryDecorator('/test-data/1747384033336')], | ||
| }; | ||
|  | ||
| export const Display: Story = { | ||
| args: { | ||
| name: 'Tom Wilson', | ||
| distance: -1.0, | ||
| percent: 50, | ||
| classColor: 0xffda59, | ||
| }, | ||
| }; | 
        
          
          
            87 changes: 55 additions & 32 deletions
          
          87 
        
  src/frontend/components/FasterCarsFromBehind/FasterCarsFromBehind.tsx
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,32 +1,55 @@ | ||
| import { useMemo } from 'react'; | ||
| import { useAutoAnimate } from '@formkit/auto-animate/react'; | ||
| import { useCarBehind } from './hooks/useCarBehind'; | ||
| import { useFasterCarsSettings } from './hooks/useFasterCarsSettings'; | ||
|  | ||
| export const FasterCarsFromBehind = () => { | ||
| const settings = useFasterCarsSettings(); | ||
| const carBehind = useCarBehind({distanceThreshold: settings?.distanceThreshold }); | ||
| const [parent] = useAutoAnimate(); | ||
|  | ||
| const layout = useMemo(() => { | ||
| const hidden = carBehind.name === null || carBehind.name == undefined ? 'hidden' : ''; | ||
| const animate = carBehind.distance > -0.3 ? 'animate-pulse' : ''; | ||
| const red = carBehind.percent; | ||
| const green = 100 - carBehind.percent; | ||
|  | ||
| return { hidden, animate, red, green }; | ||
| }, [ | ||
| carBehind.name, | ||
| carBehind.distance, | ||
| carBehind.percent | ||
| ]); | ||
|  | ||
| return ( | ||
| <div className={`w-full flex justify-between rounded-sm p-1 pb-2 font-bold relative ${layout.hidden} ${layout.animate} ${carBehind.background}`} | ||
| ref={parent}> | ||
| <div className="rounded-sm bg-gray-700 p-1">{carBehind.name}</div> | ||
| <div className="rounded-sm bg-gray-700 p-1">{carBehind.distance}</div> | ||
| <div className={`absolute bottom-0 left-0 rounded-b-sm bg-white h-1 flex-none`} style={{width: carBehind.percent+'%', backgroundColor: `rgb(${layout.red}%, ${layout.green}%, 0%)`}}></div> | ||
| </div> | ||
| ); | ||
| }; | ||
| import { useCurrentSessionType } from '@irdashies/context'; | ||
| import { useCarBehind } from './hooks/useCarBehind'; | ||
| import { useFasterCarsSettings } from './hooks/useFasterCarsSettings'; | ||
| import { getTailwindStyle } from '@irdashies/utils/colors'; | ||
|  | ||
| export interface FasterCarsFromBehindProps { | ||
| name?: string; | ||
| distance?: number; | ||
| percent?: number; | ||
| classColor?: number; | ||
| } | ||
|  | ||
| export const FasterCarsFromBehind = () => { | ||
| const settings = useFasterCarsSettings(); | ||
| const sessionType = useCurrentSessionType(); | ||
| const carBehind = useCarBehind({ | ||
| distanceThreshold: settings?.distanceThreshold, | ||
| }); | ||
|  | ||
| if (sessionType === 'Lone Qualify') { | ||
| return null; | ||
| } | ||
|  | ||
| return <FasterCarsFromBehindDisplay {...carBehind} />; | ||
| }; | ||
|  | ||
| export const FasterCarsFromBehindDisplay = ({ | ||
| name, | ||
| distance, | ||
| percent, | ||
| classColor, | ||
| }: FasterCarsFromBehindProps) => { | ||
| if (!name) { | ||
| return null; | ||
| } | ||
|  | ||
| const animate = distance && distance > -0.3 ? 'animate-pulse' : ''; | ||
| const red = percent || 0; | ||
| const green = 100 - (percent || 0); | ||
| const background = getTailwindStyle(classColor).classHeader; | ||
|  | ||
| return ( | ||
| <div className={`w-full flex justify-between rounded-sm p-1 pb-2 font-bold relative ${background} ${animate}`}> | ||
| <div className="rounded-sm bg-gray-700 p-1">{name}</div> | ||
| <div className="rounded-sm bg-gray-700 p-1">{distance}</div> | ||
| <div | ||
| className={`absolute bottom-0 left-0 rounded-b-sm bg-white h-1 flex-none`} | ||
| style={{ | ||
| width: `${percent ?? 0}%`, | ||
| backgroundColor: `rgb(${red}%, ${green}%, 0%)`, | ||
| }} | ||
| ></div> | ||
| </div> | ||
| ); | ||
| }; | 
        
          
          
            43 changes: 29 additions & 14 deletions
          
          43 
        
  src/frontend/components/FasterCarsFromBehind/hooks/useCarBehind.tsx
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,21 +1,36 @@ | ||
| import { useMemo } from 'react'; | ||
| import { useDriverRelatives } from '../../Standings/hooks/useDriverRelatives'; | ||
| import { getTailwindStyle } from '@irdashies/utils/colors'; | ||
|  | ||
| export const useCarBehind = ({ distanceThreshold }:{ distanceThreshold?: number }) => { | ||
| export const useCarBehind = ({ | ||
| distanceThreshold, | ||
| }: { | ||
| distanceThreshold?: number; | ||
| }) => { | ||
| const drivers = useDriverRelatives({ buffer: 1 }); | ||
| const carBehind = drivers[2]; | ||
| const myCar = drivers[1]; | ||
| const myCar = drivers[1]; | ||
| const threshold = distanceThreshold ?? -3; | ||
|  | ||
| const background = getTailwindStyle(carBehind?.carClass?.color).classHeader; | ||
|  | ||
| const FasterCarFromBehind = useMemo(() => { | ||
| const percent = parseInt((100 - (Math.abs(carBehind?.delta) / 3 * 100)).toFixed(0)); | ||
|  | ||
| return { name: carBehind?.driver?.name, distance: parseFloat(carBehind?.delta.toFixed(1)), background: background, percent : percent }; | ||
| }, [carBehind?.delta, carBehind?.driver?.name, background]); | ||
|  | ||
| if(carBehind?.carClass?.relativeSpeed <= myCar?.carClass?.relativeSpeed || carBehind?.delta < threshold) return {name: null, distance: 0, background: null, percent: 0}; | ||
| return FasterCarFromBehind; | ||
| const classColor = carBehind?.carClass?.color; | ||
|  | ||
| const fasterCarFromBehind = useMemo(() => { | ||
| const percent = parseInt( | ||
| (100 - (Math.abs(carBehind?.delta) / 3) * 100).toFixed(0) | ||
| ); | ||
|  | ||
| return { | ||
| name: carBehind?.driver?.name, | ||
| distance: parseFloat(carBehind?.delta.toFixed(1)), | ||
|         
                  tariknz marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| classColor, | ||
| percent: percent, | ||
| }; | ||
| }, [carBehind?.delta, carBehind?.driver?.name, classColor]); | ||
|  | ||
| if ( | ||
| carBehind?.carClass?.relativeSpeed <= myCar?.carClass?.relativeSpeed || | ||
| carBehind?.delta < threshold | ||
| ) { | ||
| return { name: undefined, distance: 0, classColor: undefined, percent: 0 }; | ||
| } | ||
|  | ||
| return fasterCarFromBehind; | ||
| }; | ||
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.