All files / lib/providers/GameItem index.tsx

30% Statements 3/10
100% Branches 0/0
16.66% Functions 1/6
28.57% Lines 2/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26        1x                 15x                        
import { Game } from '@uniquegood/realworld-web-interface/lib/models/game';
import { Item } from '@uniquegood/realworld-web-interface/lib/models/game/Item';
import React, { useContext, createContext } from 'react';
 
const GameItemContext = createContext<{
  getItemById: (itemId: string) => Item | undefined;
}>({
  getItemById: () => {
    // pass
    return undefined;
  }
});
 
export const useGameItemContext = () => useContext(GameItemContext);
 
export function GameItemProvider({ children, game }: { children: React.ReactNode; game: Game }) {
  const getItemById = React.useCallback(
    (itemId: string) => game.itemList.find((item) => item.id === itemId),
    [game]
  );
 
  const value = React.useMemo(() => ({ getItemById }), [getItemById]);
 
  return <GameItemContext.Provider value={value}>{children}</GameItemContext.Provider>;
}