-
Notifications
You must be signed in to change notification settings - Fork 16
relative: keep player in the centre to avoid layout shift #33
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 all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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
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
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,47 +1,96 @@ | ||
| import { useAutoAnimate } from '@formkit/auto-animate/react'; | ||
| import { DriverInfoRow } from './components/DriverInfoRow/DriverInfoRow'; | ||
| import { useDriverRelatives } from './hooks/useDriverRelatives'; | ||
| import { useRelativeSettings, useDriverRelatives } from './hooks'; | ||
| import { DriverRatingBadge } from './components/DriverRatingBadge/DriverRatingBadge'; | ||
| import { SessionBar } from './components/SessionBar/SessionBar'; | ||
| import { SessionFooter } from './components/SessionFooter/SessionFooter'; | ||
|
|
||
| export const Relative = () => { | ||
| const standings = useDriverRelatives({ buffer: 3 }); | ||
| const config = useRelativeSettings(); | ||
| const buffer = config?.buffer ?? 3; | ||
| const standings = useDriverRelatives({ buffer }); | ||
| const [parent] = useAutoAnimate(); | ||
|
|
||
| // Always render 2 * buffer + 1 rows (buffer above + player + buffer below) | ||
| const totalRows = 2 * buffer + 1; | ||
| const playerIndex = standings.findIndex((result) => result.isPlayer); | ||
|
|
||
| // If no player found, render empty table with consistent height | ||
| if (playerIndex === -1) { | ||
| const emptyRows = Array.from({ length: totalRows }, (_, index) => ( | ||
| <DummyDriverRow key={`empty-${index}`} /> | ||
| )); | ||
|
|
||
| return ( | ||
| <div className="w-full h-full"> | ||
| <SessionBar /> | ||
| <table className="w-full table-auto text-sm border-separate border-spacing-y-0.5 mb-3 mt-3"> | ||
| <tbody ref={parent}>{emptyRows}</tbody> | ||
| </table> | ||
| <SessionFooter /> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| // Create an array of fixed size with placeholder rows | ||
| const rows = Array.from({ length: totalRows }, (_, index) => { | ||
| // Calculate the actual index in the standings array | ||
| // Center the player in the middle of the display | ||
| const centerIndex = Math.floor(totalRows / 2); // buffer | ||
| const actualIndex = index - centerIndex + playerIndex; | ||
| const result = standings[actualIndex]; | ||
|
|
||
| if (!result) { | ||
| // If no result, render a dummy row with visibility hidden | ||
| return <DummyDriverRow key={`placeholder-${index}`} />; | ||
| } | ||
|
|
||
| return ( | ||
| <DriverInfoRow | ||
| key={result.carIdx} | ||
| carIdx={result.carIdx} | ||
| classColor={result.carClass.color} | ||
| carNumber={result.driver?.carNum || ''} | ||
| name={result.driver?.name || ''} | ||
| isPlayer={result.isPlayer} | ||
| hasFastestTime={result.hasFastestTime} | ||
| delta={result.delta} | ||
| position={result.classPosition} | ||
| onPitRoad={result.onPitRoad} | ||
| onTrack={result.onTrack} | ||
| radioActive={result.radioActive} | ||
| isLapped={result.lappedState === 'behind'} | ||
| isLappingAhead={result.lappedState === 'ahead'} | ||
| badge={ | ||
| <DriverRatingBadge | ||
| license={result.driver?.license} | ||
| rating={result.driver?.rating} | ||
| /> | ||
| } | ||
| /> | ||
| ); | ||
| }); | ||
|
|
||
| return ( | ||
| <div className="w-full h-full"> | ||
| <SessionBar /> | ||
| <table className="w-full table-auto text-sm border-separate border-spacing-y-0.5 mb-3 mt-3"> | ||
| <tbody ref={parent}> | ||
| {standings.map((result) => ( | ||
| <DriverInfoRow | ||
| key={result.carIdx} | ||
| carIdx={result.carIdx} | ||
| classColor={result.carClass.color} | ||
| carNumber={result.driver?.carNum || ''} | ||
| name={result.driver?.name || ''} | ||
| isPlayer={result.isPlayer} | ||
| hasFastestTime={result.hasFastestTime} | ||
| delta={result.delta} | ||
| position={result.classPosition} | ||
| onPitRoad={result.onPitRoad} | ||
| onTrack={result.onTrack} | ||
| radioActive={result.radioActive} | ||
| isLapped={result.lappedState === 'behind'} | ||
| isLappingAhead={result.lappedState === 'ahead'} | ||
| badge={ | ||
| <DriverRatingBadge | ||
| license={result.driver?.license} | ||
| rating={result.driver?.rating} | ||
| /> | ||
| } | ||
| /> | ||
| ))} | ||
| </tbody> | ||
| <tbody ref={parent}>{rows}</tbody> | ||
| </table> | ||
|
|
||
| <SessionFooter /> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| // Dummy driver row component with visibility hidden to maintain consistent height | ||
| const DummyDriverRow = () => ( | ||
| <DriverInfoRow | ||
| carIdx={0} | ||
| classColor={0} | ||
| carNumber="33" | ||
| name="Franz Hermann" | ||
| isPlayer={false} | ||
| hasFastestTime={false} | ||
| hidden={true} | ||
| /> | ||
tariknz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ); | ||
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
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
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,5 +1,7 @@ | ||
| export * from './useCarClassStats'; | ||
| export * from './useDriverIncidents'; | ||
| export * from './useDriverStandings'; | ||
| export * from './useDriverRelatives'; | ||
| export * from './useSessionLapCount'; | ||
| export * from './useStandingsSettings'; | ||
| export * from './useRelativeSettings'; |
8 changes: 8 additions & 0 deletions
8
src/frontend/components/Standings/hooks/useRelativeSettings.ts
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 |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { useDashboard } from '@irdashies/context'; | ||
| import { RelativeWidgetSettings } from '../../Settings/types'; | ||
|
|
||
| export const useRelativeSettings = (): RelativeWidgetSettings['config'] => { | ||
| const { currentDashboard } = useDashboard(); | ||
| const widget = currentDashboard?.widgets.find(w => w.id === 'relative')?.config; | ||
| return widget as RelativeWidgetSettings['config']; | ||
| }; | ||
tariknz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.