|  | 
| 1 |  | -import { useMemo } from 'react'; | 
| 2 |  | -import { useAutoAnimate } from '@formkit/auto-animate/react'; | 
| 3 |  | -import { useCarBehind } from './hooks/useCarBehind'; | 
| 4 |  | -import { useFasterCarsSettings } from './hooks/useFasterCarsSettings'; | 
| 5 |  | - | 
| 6 |  | -export const FasterCarsFromBehind = () => { | 
| 7 |  | -  const settings = useFasterCarsSettings(); | 
| 8 |  | -  const carBehind = useCarBehind({distanceThreshold: settings?.distanceThreshold }); | 
| 9 |  | -  const [parent] = useAutoAnimate(); | 
| 10 |  | - | 
| 11 |  | -  const layout = useMemo(() => { | 
| 12 |  | -    const hidden = carBehind.name === null || carBehind.name == undefined ? 'hidden' : '';   | 
| 13 |  | -    const animate = carBehind.distance > -0.3 ? 'animate-pulse' : ''; | 
| 14 |  | -    const red = carBehind.percent; | 
| 15 |  | -    const green = 100 - carBehind.percent; | 
| 16 |  | - | 
| 17 |  | -    return { hidden, animate, red, green }; | 
| 18 |  | -  }, [ | 
| 19 |  | -    carBehind.name, | 
| 20 |  | -    carBehind.distance, | 
| 21 |  | -    carBehind.percent | 
| 22 |  | -  ]); | 
| 23 |  | - | 
| 24 |  | -  return ( | 
| 25 |  | -    <div className={`w-full flex justify-between rounded-sm p-1 pb-2 font-bold relative ${layout.hidden} ${layout.animate} ${carBehind.background}`} | 
| 26 |  | -		     ref={parent}> | 
| 27 |  | -      <div className="rounded-sm bg-gray-700 p-1">{carBehind.name}</div> | 
| 28 |  | -	    <div className="rounded-sm bg-gray-700 p-1">{carBehind.distance}</div> | 
| 29 |  | -	    <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> | 
| 30 |  | -    </div> | 
| 31 |  | -  ); | 
| 32 |  | -}; | 
|  | 1 | +import { useCurrentSessionType } from '@irdashies/context'; | 
|  | 2 | +import { useCarBehind } from './hooks/useCarBehind'; | 
|  | 3 | +import { useFasterCarsSettings } from './hooks/useFasterCarsSettings'; | 
|  | 4 | +import { getTailwindStyle } from '@irdashies/utils/colors'; | 
|  | 5 | + | 
|  | 6 | +export interface FasterCarsFromBehindProps { | 
|  | 7 | +  name?: string; | 
|  | 8 | +  distance?: number; | 
|  | 9 | +  percent?: number; | 
|  | 10 | +  classColor?: number; | 
|  | 11 | +} | 
|  | 12 | + | 
|  | 13 | +export const FasterCarsFromBehind = () => { | 
|  | 14 | +  const settings = useFasterCarsSettings(); | 
|  | 15 | +  const sessionType = useCurrentSessionType(); | 
|  | 16 | +  const carBehind = useCarBehind({ | 
|  | 17 | +    distanceThreshold: settings?.distanceThreshold, | 
|  | 18 | +  }); | 
|  | 19 | + | 
|  | 20 | +  if (sessionType === 'Lone Qualify') { | 
|  | 21 | +    return null; | 
|  | 22 | +  } | 
|  | 23 | + | 
|  | 24 | +  return <FasterCarsFromBehindDisplay {...carBehind} />; | 
|  | 25 | +}; | 
|  | 26 | + | 
|  | 27 | +export const FasterCarsFromBehindDisplay = ({ | 
|  | 28 | +  name, | 
|  | 29 | +  distance, | 
|  | 30 | +  percent, | 
|  | 31 | +  classColor, | 
|  | 32 | +}: FasterCarsFromBehindProps) => { | 
|  | 33 | +  if (!name) { | 
|  | 34 | +    return null; | 
|  | 35 | +  } | 
|  | 36 | + | 
|  | 37 | +  const animate = distance && distance > -0.3 ? 'animate-pulse' : ''; | 
|  | 38 | +  const red = percent || 0; | 
|  | 39 | +  const green = 100 - (percent || 0); | 
|  | 40 | +  const background = getTailwindStyle(classColor).classHeader; | 
|  | 41 | + | 
|  | 42 | +  return ( | 
|  | 43 | +    <div className={`w-full flex justify-between rounded-sm p-1 pb-2 font-bold relative ${background} ${animate}`}> | 
|  | 44 | +      <div className="rounded-sm bg-gray-700 p-1">{name}</div> | 
|  | 45 | +      <div className="rounded-sm bg-gray-700 p-1">{distance}</div> | 
|  | 46 | +      <div | 
|  | 47 | +        className={`absolute bottom-0 left-0 rounded-b-sm bg-white h-1 flex-none`} | 
|  | 48 | +        style={{ | 
|  | 49 | +          width: `${percent ?? 0}%`, | 
|  | 50 | +          backgroundColor: `rgb(${red}%, ${green}%, 0%)`, | 
|  | 51 | +        }} | 
|  | 52 | +      ></div> | 
|  | 53 | +    </div> | 
|  | 54 | +  ); | 
|  | 55 | +}; | 
0 commit comments