Skip to content

Commit

Permalink
Fix extra renders
Browse files Browse the repository at this point in the history
  • Loading branch information
wyne committed Apr 21, 2024
1 parent 1dd6b76 commit 327077d
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 31 deletions.
8 changes: 4 additions & 4 deletions redux/GamesSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,19 +207,19 @@ export const selectSortSelectorKey = (state: RootState, gameId: string) => {
export const makeSelectPlayerColors = () => createSelector(
[
(state: RootState, gameId: string) => state.games.entities[gameId],
(state: RootState, gameId: string, playerId: string) => state.players.entities[playerId],
(state: RootState, gameId: string, playerId: string) => playerId,
],
(game, player) => {
(game, playerId) => {
// TODO: Get player color if it exists

if (!game || !player) {
if (!game || !playerId) {
return ['#000000', '#FFFFFF'];
}

const paletteName = game.palette;
const playerIds = game.playerIds;

const index = playerIds.indexOf(player.id);
const index = playerIds.indexOf(playerId);

const palette = getPalette(paletteName || 'original');

Expand Down
11 changes: 10 additions & 1 deletion redux/PlayersSlice.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import crashlytics from '@react-native-firebase/crashlytics';
import { PayloadAction, createEntityAdapter, createSlice } from '@reduxjs/toolkit';
import { PayloadAction, createEntityAdapter, createSelector, createSlice } from '@reduxjs/toolkit';

import { RootState } from './store';

type RoundIndex = number;

Expand Down Expand Up @@ -72,3 +74,10 @@ export const {
selectIds: selectPlayerIds,
// Pass in a selector that returns the posts slice of state
} = playersAdapter.getSelectors((state: PlayersSlice) => state.players);

export const selectPlayerNameById = createSelector(
[
(state: RootState, playerId: string) => state.players.entities[playerId]
],
(player) => player?.playerName
);
6 changes: 4 additions & 2 deletions src/components/PlayerListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { NativeStackNavigationProp } from '@react-navigation/native-stack';
import { Alert, StyleSheet, Text, TouchableOpacity, View } from 'react-native';
import { Icon, ListItem } from 'react-native-elements';

import { selectGameById, selectPlayerColors, updateGame } from '../../redux/GamesSlice';
import { makeSelectPlayerColors, selectGameById, updateGame } from '../../redux/GamesSlice';
import { useAppDispatch, useAppSelector } from '../../redux/hooks';
import { removePlayer, selectPlayerById } from '../../redux/PlayersSlice';
import { selectCurrentGame } from '../../redux/selectors';
Expand All @@ -31,7 +31,9 @@ const PlayerListItem: React.FunctionComponent<Props> = ({
const currentGameId = useAppSelector(state => selectCurrentGame(state)?.id);
const playerIds = useAppSelector(state => selectGameById(state, currentGameId || '')?.playerIds);
const player = useAppSelector(state => selectPlayerById(state, playerId));
const playerColors = useAppSelector(state => selectPlayerColors(state, currentGameId || '', index || 0));

const selectPlayerColors = makeSelectPlayerColors();
const playerColors = useAppSelector(state => selectPlayerColors(state, currentGameId || '', playerId));

if (currentGameId == '' || currentGameId === undefined) return null;
if (playerIds === undefined) return null;
Expand Down
11 changes: 4 additions & 7 deletions src/components/Rounds.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { NativeStackNavigationProp } from '@react-navigation/native-stack';
import { LayoutChangeEvent, Platform, ScrollView, StyleSheet, View } from 'react-native';
import { TouchableOpacity } from 'react-native-gesture-handler';

import { selectGameById, selectSortSelectorKey, setSortSelector } from '../../redux/GamesSlice';
import { selectGameById, setSortSelector } from '../../redux/GamesSlice';
import { useAppDispatch, useAppSelector } from '../../redux/hooks';

import PlayerNameColumn from './ScoreLog/PlayerNameColumn';
import RoundScoreColumn from './ScoreLog/RoundScoreColumn';
import { SortSelectorKey, sortSelectors } from './ScoreLog/SortHelper';
import { SortSelectorKey } from './ScoreLog/SortHelper';
import TotalScoreColumn from './ScoreLog/TotalScoreColumn';

interface Props {
Expand Down Expand Up @@ -61,9 +61,6 @@ const Rounds: React.FunctionComponent<Props> = ({ }) => {

const dispatch = useAppDispatch();

const sortSelectorKey = useAppSelector(state => selectSortSelectorKey(state, currentGameId));
const sortSelector = sortSelectors[sortSelectorKey];

const sortByPlayerIndex = () => {
dispatch(setSortSelector({ gameId: currentGameId, sortSelector: SortSelectorKey.ByIndex }));
};
Expand All @@ -88,10 +85,10 @@ const Rounds: React.FunctionComponent<Props> = ({ }) => {
{roundsIterator.map((item, round) => (
<View key={round} onLayout={e => onLayoutHandler(e, round)}>
<MemoizedRoundScoreColumn
sortSelector={sortSelector}
round={round}
key={round}
isCurrentRound={round == roundCurrent} />
isCurrentRound={round == roundCurrent}
/>
</View>
))}
</ScrollView>
Expand Down
13 changes: 8 additions & 5 deletions src/components/ScoreLog/PlayerNameColumn.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react';
import React, { useMemo } from 'react';

import { StyleSheet, Text, View } from 'react-native';
import { Icon } from 'react-native-elements/dist/icons/Icon';

import { makeSelectPlayerColors } from '../../../redux/GamesSlice';
import { useAppSelector } from '../../../redux/hooks';
import { selectPlayerById } from '../../../redux/PlayersSlice';
import { selectPlayerNameById } from '../../../redux/PlayersSlice';
import { selectCurrentGame } from '../../../redux/selectors';

import { SortDirectionKey, SortSelectorKey, sortSelectors } from './SortHelper';
Expand All @@ -18,9 +18,11 @@ interface CellProps {

const PlayerNameCell: React.FunctionComponent<CellProps> = ({ index, playerId }) => {
const currentGameId = useAppSelector(state => selectCurrentGame(state)?.id);
const selectPlayerColors = makeSelectPlayerColors();

const selectPlayerColors = useMemo(() => makeSelectPlayerColors(), []);
const playerColors = useAppSelector(state => selectPlayerColors(state, currentGameId || '', playerId));
const playerName = useAppSelector(state => selectPlayerById(state, playerId)?.playerName);

const playerName = useAppSelector(state => selectPlayerNameById(state, playerId));

return (
<View key={index} style={{ paddingLeft: 5, borderLeftWidth: 5, borderColor: playerColors[0] }}>
Expand Down Expand Up @@ -56,6 +58,7 @@ const PlayerHeaderCell: React.FunctionComponent = () => {
);
};

const MemoizedPlayerNameCell = React.memo(PlayerNameCell);

const PlayerNameColumn: React.FunctionComponent = () => {
const sortKey = useAppSelector(state => selectCurrentGame(state)?.sortSelectorKey);
Expand All @@ -68,7 +71,7 @@ const PlayerNameColumn: React.FunctionComponent = () => {
<PlayerHeaderCell />

{sortedPlayerIds.map((playerId, index) => (
playerId && <PlayerNameCell key={index} index={index} playerId={playerId} />
playerId && <MemoizedPlayerNameCell key={index} index={index} playerId={playerId} />
))}
</View>
);
Expand Down
9 changes: 5 additions & 4 deletions src/components/ScoreLog/RoundScoreColumn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,32 @@ import React, { memo, useCallback } from 'react';
import analytics from '@react-native-firebase/analytics';
import { LayoutChangeEvent, Text, TouchableWithoutFeedback, View } from 'react-native';

import { updateGame } from '../../../redux/GamesSlice';
import { selectSortSelectorKey, updateGame } from '../../../redux/GamesSlice';
import { useAppDispatch, useAppSelector } from '../../../redux/hooks';
import { RootState } from '../../../redux/store';

import RoundScoreCell from './RoundScoreCell';
import { sortSelectors } from './SortHelper';

interface Props {
round: number;
isCurrentRound: boolean;
disabled?: boolean;
sortSelector: (state: RootState) => string[];
onLayout?: (event: LayoutChangeEvent, round: number) => void;
}

const RoundScoreColumn: React.FunctionComponent<Props> = ({
round,
isCurrentRound,
disabled = false,
sortSelector,
onLayout,
}) => {
const dispatch = useAppDispatch();

const currentGameId = useAppSelector(state => state.settings.currentGameId);

const sortSelectorKey = useAppSelector(state => selectSortSelectorKey(state, currentGameId || ''));
const sortSelector = sortSelectors[sortSelectorKey];

const sortedPlayerIds = useAppSelector(sortSelector);

const onPressHandler = useCallback(async () => {
Expand Down
11 changes: 3 additions & 8 deletions src/screens/ShareScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ import { Button, Icon } from 'react-native-elements';
import { SafeAreaView } from 'react-native-safe-area-context';
import { captureRef } from 'react-native-view-shot';

import { selectGameById, selectSortSelectorKey } from '../../redux/GamesSlice';
import { selectGameById } from '../../redux/GamesSlice';
import { useAppSelector } from '../../redux/hooks';
import { selectCurrentGame } from '../../redux/selectors';
import PlayerNameColumn from '../components/ScoreLog/PlayerNameColumn';
import RoundScoreColumn from '../components/ScoreLog/RoundScoreColumn';
import { sortSelectors } from '../components/ScoreLog/SortHelper';
import TotalScoreColumn from '../components/ScoreLog/TotalScoreColumn';
import { systemBlue } from '../constants';

Expand Down Expand Up @@ -54,9 +53,6 @@ const ShareScreen: React.FunctionComponent<Props> = ({ navigation }) => {
await analytics().logEvent('share_image');
};

const sortSelectorKey = useAppSelector(state => selectSortSelectorKey(state, currentGameId));
const sortSelector = sortSelectors[sortSelectorKey];

return (
<SafeAreaView edges={['right', 'left']}
style={[styles.contentContainer, { height: 'auto' }]}>
Expand Down Expand Up @@ -98,12 +94,11 @@ const ShareScreen: React.FunctionComponent<Props> = ({ navigation }) => {
</Text>
</View>
<View style={{ flexDirection: 'row' }}>
<PlayerNameColumn sortSelector={sortSelector} sortSelectorKey={sortSelectorKey} />
<TotalScoreColumn sortSelector={sortSelector} sortSelectorKey={sortSelectorKey} />
<PlayerNameColumn />
<TotalScoreColumn />
{roundsIterator.map((item, round) => (
<View key={round}>
<RoundScoreColumn
sortSelector={sortSelector}
round={round}
key={round}
isCurrentRound={false}
Expand Down

0 comments on commit 327077d

Please sign in to comment.