Skip to content

Commit

Permalink
extract getEventData function
Browse files Browse the repository at this point in the history
  • Loading branch information
feildmaster committed Jul 7, 2024
1 parent 3253a19 commit 9cb2fd6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/routes/Game/hooks/useEventData.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { useMemo } from 'react';
import { useGameState } from '~/hooks/useGame';
import { Actions } from '~/types/game';
import { getEventData } from '~/utils/getEventData';

export function useEventData<A extends keyof Actions>(action: A): Actions[A] | null {
const state = useGameState();
return useMemo(() => {
if (state.action !== action || state.extra === undefined) return null;
return state.extra as Actions[A];
}, [action, state]);
return useMemo(
() => getEventData(action, state),
[action, state],
);
}
9 changes: 9 additions & 0 deletions src/utils/getEventData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Actions, GameStateExpanded } from '~/types/game';

export function getEventData<A extends keyof Actions>(
action: A,
state: GameStateExpanded,
): Actions[A] | null {
if (state.action !== action || state.extra === undefined) return null;
return state.extra as Actions[A];
}

0 comments on commit 9cb2fd6

Please sign in to comment.