Skip to content

Commit d1b3a51

Browse files
authored
chore: refactor GameManager component (#3)
1 parent a360bbd commit d1b3a51

File tree

9 files changed

+30
-21
lines changed

9 files changed

+30
-21
lines changed

src/modules/game/ui/GameBoard/hooks.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const useFocusFirstCardOnMount = () => {
2323
}, [allCardsRegistered, cardRefs])
2424
}
2525

26-
const useFocusNextCardOnSelectCard = () => {
26+
const useFocusNextCardOnCardSelect = () => {
2727
const cardRefs = useCardRefs()
2828
const selectedCards = useSelectedCards()
2929
const allCardsRegistered = useAllCardsRegistered()
@@ -48,7 +48,7 @@ export const useGameBoard = () => {
4848
const { selectCard } = useGameActions()
4949

5050
useFocusFirstCardOnMount()
51-
useFocusNextCardOnSelectCard()
51+
useFocusNextCardOnCardSelect()
5252

5353
return {
5454
cards,

src/modules/game/ui/GameBoardCard/hooks.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
import { RefObject, useCallback, useEffect, useRef } from 'react'
1+
import { useCallback, useEffect, useRef } from 'react'
22

33
import { GameBoardCardProps } from './GameBoardCard'
44
import { useGameActions } from '../..'
55

66
type UseGameBoardCardProps = Pick<GameBoardCardProps, 'card' | 'onClick'>
77

8-
const useRegisterGameCardOnMount = (ref: RefObject<HTMLElement>) => {
8+
const useRegisterGameCardOnMount = () => {
9+
const ref = useRef(null)
910
const { registerCardRef } = useGameActions()
1011

1112
useEffect(() => {
1213
if (ref.current) {
1314
return registerCardRef(ref.current)
1415
}
15-
}, [ref, registerCardRef])
16+
}, [registerCardRef])
17+
18+
return ref
1619
}
1720

1821
export const useGameBoardCard = ({ card, onClick }: UseGameBoardCardProps) => {
19-
const ref = useRef<HTMLButtonElement>(null)
20-
21-
useRegisterGameCardOnMount(ref)
22+
const ref = useRegisterGameCardOnMount()
2223

2324
const handleClick = useCallback(() => {
2425
onClick(card)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { useGameManager } from './hooks'
2+
3+
export const GameManager = () => {
4+
useGameManager()
5+
6+
return null
7+
}
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { useEffect } from 'react'
22
import { useNavigate } from 'react-router-dom'
33

4-
import { useGameActions, listenForAllCardsCollected } from '..'
4+
import { listenForAllCardsCollected, useGameActions } from '../..'
55

6-
export const GameManager = () => {
6+
export const useGameManager = () => {
77
const navigate = useNavigate()
88
const { endGame } = useGameActions()
99

@@ -15,6 +15,4 @@ export const GameManager = () => {
1515
}
1616
})
1717
}, [navigate, endGame])
18-
19-
return null
2018
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './GameManager'

src/modules/game/ui/GameNextTurnButton/GameNextTurnButton.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
import clsx from 'clsx'
2-
import { useRef } from 'react'
32

43
import { useGameActions, useTurn } from '@/modules/game'
54
import { Button } from '@/shared/ui/Button'
65

7-
import { useFocusButtonOnTurnEnd } from './hooks'
6+
import { useFocusElementOnTurnEnd } from './hooks'
87

98
type GameNextTurnButtonProps = {
109
className?: string
1110
}
1211

1312
export const GameNextTurnButton = ({ className }: GameNextTurnButtonProps) => {
14-
const ref = useRef<HTMLButtonElement>(null)
1513
const { isFinished } = useTurn()
1614
const { endTurn } = useGameActions()
15+
const ref = useFocusElementOnTurnEnd<HTMLButtonElement>()
1716

1817
const icon = isFinished ? 'i-lucide-play' : 'i-lucide-hourglass'
1918

20-
useFocusButtonOnTurnEnd(ref)
21-
2219
return (
2320
<Button
2421
ref={ref}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
import { RefObject, useEffect } from 'react'
1+
import { useEffect, useRef } from 'react'
22

33
import { useTurn } from '../..'
44

5-
export const useFocusButtonOnTurnEnd = (ref: RefObject<HTMLElement>) => {
5+
export const useFocusElementOnTurnEnd = <T extends HTMLElement>() => {
6+
const ref = useRef<T>(null)
67
const { isFinished } = useTurn()
78

89
useEffect(() => {
910
if (isFinished) {
1011
ref.current?.focus()
1112
}
1213
}, [isFinished, ref])
14+
15+
return ref
1316
}

src/pages/GamePage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import {
22
GameBoard,
3-
GameManager,
43
GameOverlay,
54
GameNextTurnButton,
65
useAreCardsLoading,
6+
GameManager,
77
} from '@/modules/game'
88
import { Loader } from '@/shared/ui/Loader'
99

src/shared/ui/BoardCard/hooks.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ export const useBoardCard = (forwardedRef: ForwardedRef<HTMLButtonElement>) => {
1212
if (ref.current) {
1313
return registerCardElement(ref.current)
1414
}
15-
}, [ref, registerCardElement])
15+
// ref does not need to be in the dependency array as it's a RefObject
16+
// eslint-disable-next-line react-hooks/exhaustive-deps
17+
}, [registerCardElement])
1618

1719
return { ref }
1820
}

0 commit comments

Comments
 (0)