From e412d970e2724992be058577559b9be78957edbe Mon Sep 17 00:00:00 2001 From: Justin Wyne <1986068+wyne@users.noreply.github.com> Date: Sat, 13 Apr 2024 02:22:36 -0700 Subject: [PATCH] Rounds render optimizations --- src/components/Rounds.tsx | 11 +++++----- src/components/ScoreLog/PlayerNameColumn.tsx | 22 +++++++++++++------- src/components/ScoreLog/RoundScoreColumn.tsx | 22 +++++++++++++------- 3 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/components/Rounds.tsx b/src/components/Rounds.tsx index b5864d7a..0a3b98fc 100644 --- a/src/components/Rounds.tsx +++ b/src/components/Rounds.tsx @@ -67,12 +67,11 @@ const Rounds: React.FunctionComponent = ({ }) => { contentContainerStyle={{ flexDirection: 'row' }} ref={roundsScrollViewEl}> {roundsIterator.map((item, round) => ( - onLayoutHandler(e, round)}> - - + onLayoutHandler(e, round) : undefined} + round={round} + key={round} + isCurrentRound={round == roundCurrent} /> ))} diff --git a/src/components/ScoreLog/PlayerNameColumn.tsx b/src/components/ScoreLog/PlayerNameColumn.tsx index 011ea6a7..ac47862b 100644 --- a/src/components/ScoreLog/PlayerNameColumn.tsx +++ b/src/components/ScoreLog/PlayerNameColumn.tsx @@ -1,21 +1,27 @@ import React from 'react'; +import { createSelector } from '@reduxjs/toolkit'; import { StyleSheet, Text, View } from 'react-native'; import { Icon } from 'react-native-elements/dist/icons/Icon'; -import { selectSortedPlayers } from '../../../redux/GamesSlice'; import { useAppSelector } from '../../../redux/hooks'; import { selectCurrentGame } from '../../../redux/selectors'; +import { RootState } from '../../../redux/store'; import { palette, systemBlue } from '../../constants'; -const PlayerNameColumn: React.FunctionComponent = ({ }) => { - const currentGame = useAppSelector(selectCurrentGame); +const selectPlayerEntities = (state: RootState) => state.players.entities; - if (currentGame == undefined) return null; +const selectPlayerIds = (_: RootState, playerIds: string[]) => playerIds; - const players = useAppSelector(selectSortedPlayers); +const selectPlayerNamesByIds = createSelector( + [selectPlayerEntities, selectPlayerIds], + (players, playerIds) => playerIds.map(id => players[id]?.playerName) +); + +const PlayerNameColumn: React.FunctionComponent = () => { + const playerIds = useAppSelector(state => selectCurrentGame(state)?.playerIds) || []; + const playerNames = useAppSelector(state => selectPlayerNamesByIds(state, playerIds)); - if (players == undefined) return null; return ( @@ -26,11 +32,11 @@ const PlayerNameColumn: React.FunctionComponent = ({ }) => { size={19} color='white' /> - {players.map((player, index) => ( + {playerNames.map((name, index) => ( {player.playerName} + >{name} ))} diff --git a/src/components/ScoreLog/RoundScoreColumn.tsx b/src/components/ScoreLog/RoundScoreColumn.tsx index 5310fc74..a553d2c0 100644 --- a/src/components/ScoreLog/RoundScoreColumn.tsx +++ b/src/components/ScoreLog/RoundScoreColumn.tsx @@ -1,7 +1,7 @@ import React, { memo, useCallback } from 'react'; import analytics from '@react-native-firebase/analytics'; -import { Text, TouchableWithoutFeedback, View } from 'react-native'; +import { LayoutChangeEvent, Text, TouchableWithoutFeedback, View } from 'react-native'; import { updateGame } from '../../../redux/GamesSlice'; import { useAppDispatch, useAppSelector } from '../../../redux/hooks'; @@ -13,9 +13,15 @@ interface Props { round: number; isCurrentRound: boolean; disabled?: boolean; + onLayout?: (event: LayoutChangeEvent, round: number) => void; } -const RoundScoreColumn: React.FunctionComponent = ({ round, isCurrentRound, disabled = false }) => { +const RoundScoreColumn: React.FunctionComponent = ({ + round, + isCurrentRound, + disabled = false, + onLayout, +}) => { const dispatch = useAppDispatch(); const currentGameId = useAppSelector(state => state.settings.currentGameId); @@ -46,11 +52,13 @@ const RoundScoreColumn: React.FunctionComponent = ({ round, isCurrentRoun return ( - + onLayout?.(e, round)} + style={{ + padding: 10, + paddingBottom: 0, + backgroundColor: backgroundColor, + }}>