Skip to content

Commit

Permalink
React strictness.
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 40f869822cdfa901f53bf9bdf2f31f104bb69734
  • Loading branch information
cpojer committed Jan 23, 2025
1 parent a685bed commit 67bac07
Show file tree
Hide file tree
Showing 20 changed files with 92 additions and 45 deletions.
13 changes: 11 additions & 2 deletions hera/Building.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,17 @@ const BuildingTile = memo(function BuildingTile({

animate();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [animation]);
}, [
animation,
animationConfig,
height,
isBeingCreated,
position,
positionY,
requestFrame,
scheduleTimer,
size,
]);

const showHighlight = highlight && player > 0;
const isCompleted = building.isCompleted();
Expand Down
1 change: 0 additions & 1 deletion hera/Cursor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export default memo(function Cursor({
useCallback((tick: number) => {
ref.current?.style.setProperty(vars.set('tick'), String(tick % 4));
}, []),
[],
);

return (
Expand Down
1 change: 1 addition & 0 deletions hera/Decorators.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export default memo(function Decorators({
}
});
}
// eslint-disable-next-line react-compiler/react-compiler
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [biome, hasSprites, isVisible, map.size, map.decorators, paused, size]);

Expand Down
1 change: 1 addition & 0 deletions hera/Mask.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ export default memo(function Mask({
))}
</div>
);
// eslint-disable-next-line react-compiler/react-compiler
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
attackable,
Expand Down
1 change: 1 addition & 0 deletions hera/MaskWithSubtiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export default memo(function MaskWithSubtiles({
.flat()}
</div>
),
// eslint-disable-next-line react-compiler/react-compiler
// eslint-disable-next-line react-hooks/exhaustive-deps
[decoratorSize, enter, map.size, select, zIndex],
);
Expand Down
1 change: 0 additions & 1 deletion hera/Tick.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export default function Tick({
);
}
}, []),
[],
);

return (
Expand Down
1 change: 1 addition & 0 deletions hera/Tiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ export default memo(function Tiles({
});
}

// eslint-disable-next-line react-compiler/react-compiler
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
biome,
Expand Down
30 changes: 28 additions & 2 deletions hera/Unit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -892,8 +892,34 @@ export default function UnitTile({
}
}, animationConfig.ExplosionStep / 2);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [animation]);
}, [
animation,
animationConfig.AnimationDuration,
animationConfig.ExplosionStep,
animationConfig.UnitAnimationStep,
animationConfig.UnitMoveDuration,
animationKey,
animationOffset,
backgroundPositionY,
getLayer,
hasAttackStance,
info,
isAnimating,
isMoving,
isSpawning,
onAnimationComplete,
player,
position,
positionOffset.x,
positionOffset.y,
requestFrame,
scheduleTimer,
size,
spritePosition,
tile,
unit,
unitDirection,
]);

const isCompleted = unit.isCompleted();
const animationStyle =
Expand Down
7 changes: 4 additions & 3 deletions hera/audio/Music.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import AudioPlayer from '@deities/ui/AudioPlayer.tsx';
import { NativeTimeout } from '@deities/ui/controls/throttle.tsx';
import useLocation from '@deities/ui/hooks/useLocation.tsx';
import useVisibilityState from '@deities/ui/hooks/useVisibilityState.tsx';
import { createContext, ReactNode, useContext, useEffect } from 'react';
import { createContext, ReactNode, useContext, useEffect, useRef } from 'react';

type MusicContext = {
songByRoute: Map<string, SongName | null>;
Expand Down Expand Up @@ -64,11 +64,12 @@ const onVisibilityChange = (isVisible: boolean) => {

export function usePlayMusic(dep?: unknown) {
const { pathname } = useLocation();
const context = useContext(Context);
const contextRef = useRef(useContext(Context));

useVisibilityState(onVisibilityChange);

useEffect(() => {
const context = contextRef.current;
if (context.timer != null) {
clearTimeout(context.timer);
}
Expand All @@ -82,7 +83,7 @@ export function usePlayMusic(dep?: unknown) {
? 3000
: 100,
);
}, [context, pathname, dep]);
}, [pathname, dep]);
}

export function biomeToSong(
Expand Down
6 changes: 3 additions & 3 deletions hera/campaign/CampaignEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export default function CampaignEditor({
const { slug } = data;
const [hasChanges, setHasChanges] = useState(!data.id);
const [mapHasChanges, setMapHasChanges] = useState(false);
const [origin, setOrigin] = useState('');
const [difficulty, setDifficulty] = useState<AttributeRangeWithZero>(
validateAttributeRange(data.difficulty) ? data.difficulty : 0,
);
Expand Down Expand Up @@ -285,6 +286,7 @@ export default function CampaignEditor({
navigate(getMapRoute(node.slug, 'edit'));
return;
}
setOrigin(transformRef.current);
setShowAllDialogue(false);
setCampaignEditorState({
map: node,
Expand Down Expand Up @@ -547,9 +549,7 @@ export default function CampaignEditor({
transform: 'scale(0)',
}}
style={{
transformOrigin: transformRef.current?.length
? transformRef.current
: 'center center',
transformOrigin: origin?.length ? origin : 'center center',
}}
transition={{
duration: 0.25,
Expand Down
1 change: 1 addition & 0 deletions hera/editor/MapEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,7 @@ export default function MapEditor({
// Disabling the context menu is handled globally in production,
// so this is only needed for the Map Editor in development.
if (process.env.NODE_ENV === 'development') {
// eslint-disable-next-line react-compiler/react-compiler
// eslint-disable-next-line react-hooks/rules-of-hooks
useEffect(() => {
const listener = (event: Event) => event.preventDefault();
Expand Down
9 changes: 2 additions & 7 deletions hera/lib/tick.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,11 @@ export default function tick(fn: TickFunction) {
};
}

export function useTick(
paused: boolean | undefined,
fn: TickFunction,
dependencies: Array<unknown>,
) {
export function useTick(paused: boolean | undefined, fn: TickFunction) {
const isVisible = useVisibilityState();
useEffect(() => {
if (!paused && isVisible) {
return tick(fn);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [fn, isVisible, paused, ...dependencies]);
}, [fn, isVisible, paused]);
}
3 changes: 1 addition & 2 deletions hera/ui/CharacterMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ const MessageComponent = ({
}, 100);
window.addEventListener('resize', listener);
return () => window.removeEventListener('resize', listener);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [currentLine, clientWidth, lines]);
}, [currentLine, clientWidth, lines, onComplete]);

useEffect(() => {
AudioPlayer.playSound(sound);
Expand Down
6 changes: 2 additions & 4 deletions hera/ui/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ const MessageComponent = ({
}, 100);
window.addEventListener('resize', listener);
return () => window.removeEventListener('resize', listener);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [currentLine, clientWidth, lines]);
}, [currentLine, clientWidth, lines, onComplete]);

// Avoid double invocations of onComplete.
const done = useRef(false);
Expand All @@ -106,8 +105,7 @@ const MessageComponent = ({
done.current = true;
onComplete();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [timer, animationComplete, hasNext, clearTimer, currentLine]);
}, [timer, animationComplete, hasNext, clearTimer, currentLine, onComplete]);

useInput('accept', next);
useEffect(() => {
Expand Down
3 changes: 1 addition & 2 deletions hera/ui/lib/useSkipAnimation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ export default function useSkipAnimation({
if (animationConfig.Instant) {
scheduleTimer(onComplete, 0);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
}, [animationConfig.Instant, onComplete, scheduleTimer]);

return animationConfig.Instant;
}
12 changes: 8 additions & 4 deletions tests/display.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ window.renderMap = (url: string) => {
root.render(<DisplayMap url={url} />);
};

const initializeHasRendered = (gameActionResponses: ReadonlyArray<string>) => {
// Initialize global state for listeners.
gameActionResponses?.forEach((_, index) => {
window.MapHasRendered[index] = false;
});
};

const animationSpeed = {
human: InstantAnimationConfig,
regular: InstantAnimationConfig,
Expand Down Expand Up @@ -64,10 +71,7 @@ const DisplayMap = ({ url: initialURL }: { url: string }) => {
[maps],
);

// Initialize global state for listeners.
gameActionResponses?.forEach((_, index) => {
window.MapHasRendered[index] = false;
});
initializeHasRendered(gameActionResponses);

useEffect(() => {
if (gameActionResponses?.length) {
Expand Down
9 changes: 5 additions & 4 deletions ui/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
useEffect,
useLayoutEffect,
useMemo,
useRef,
useState,
} from 'react';
import { App } from './App.tsx';
Expand Down Expand Up @@ -41,16 +42,16 @@ export const MenuContext = ({ children }: { children: ReactNode }) => {
};

export const useDisableOpenMenuOnCancel = () => {
const context = useContext(Context);
const context = useRef(useContext(Context));
useEffect(() => {
context.current = false;
context.current.current = false;
}, [context]);
};

export const useOpenMenuOnCancel = () => {
const context = useContext(Context);
const context = useRef(useContext(Context));
useEffect(() => {
context.current = true;
context.current.current = true;
}, [context]);
};

Expand Down
2 changes: 1 addition & 1 deletion ui/Reload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default function Reload({ reload = _reload }: { reload?: () => void }) {

return (
<>
{useMemo(hasGamepad, []) ? (
{useMemo(() => hasGamepad(), []) ? (
<fbt desc="Reload description">
Press A, B, L1 and R1 to reload the game.
</fbt>
Expand Down
22 changes: 17 additions & 5 deletions ui/controls/useMenuNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,23 @@ export default function useMenuNavigation(
initialCount: number,
layer: InputLayer = 'menu',
nowrap = false,
initial = -1,
initial: 'selected' | number = -1,
): [selected: number, active: number, reset: () => void] {
const [count, setCount] = useState(initialCount);
const [selected, setSelected] = useState(
isValid(count, initial) ? initial : -1,
initial !== 'selected' && isValid(count, initial) ? initial : -1,
);
const [active, resetActive] = useAcceptNavigation(count, layer, selected);

if (count !== initialCount) {
setCount(initialCount);
setSelected(isValid(initialCount, initial) ? initial : -1);
setSelected(
initial === 'selected'
? selected
: isValid(initialCount, initial)
? initial
: -1,
);
if (active !== -1) {
resetActive();
}
Expand Down Expand Up @@ -57,8 +63,14 @@ export default function useMenuNavigation(
selected,
active,
useCallback(() => {
setSelected(isValid(count, initial) ? initial : -1);
setSelected(
initial === 'selected'
? selected
: isValid(count, initial)
? initial
: -1,
);
resetActive();
}, [count, initial, resetActive]),
}, [count, initial, resetActive, selected]),
];
}
8 changes: 4 additions & 4 deletions ui/hooks/useScrollRestore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export const ScrollRestoreContext = ({ children }: { children: ReactNode }) => (

export default function useScrollRestore() {
const { hash, pathname } = useLocation();
const context = useContext(Context);
const context = useRef(useContext(Context));
useEffect(() => {
let timer = setTimeout(() => {
if (context.current) {
context.current = false;
context.current.current = false;
} else if (hash.length > 1) {
timer = setTimeout(() => {
document.getElementById(hash.slice(1))?.scrollIntoView({
Expand All @@ -41,8 +41,8 @@ export default function useScrollRestore() {
}

export function useSkipScrollRestore() {
const scrollRestoreContext = useContext(ScrollRestore);
const scrollRestoreContext = useRef(useContext(ScrollRestore));
useEffect(() => {
scrollRestoreContext.current = true;
scrollRestoreContext.current.current = true;
}, [scrollRestoreContext]);
}

0 comments on commit 67bac07

Please sign in to comment.