All files / widgets/EventDataTable/context EventTableContext.tsx

52.94% Statements 9/17
0% Branches 0/4
40% Functions 2/5
56.25% Lines 9/16

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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71                                        1x   1x           7x 7x   7x                                 1x                     7x   7x             7x    
import React, { createContext, useContext, useState } from 'react';
import { eventTableStore } from '@sb/stores';
 
type EventTableContextType = {
  favorite: boolean;
  topAlarm: boolean;
  isFavorite: string[];
  handleOnFavoriteClick: (itemId: string) => void;
  showLoading: boolean;
  activeTorAlarm: string[];
  addTorAlarm: (id: string) => void;
};
 
type EventTableContextProps = {
  children: React.ReactNode;
  favorite: boolean;
  topAlarm: boolean;
  showLoading: boolean;
};
 
const EventTableContext = createContext<EventTableContextType | null>(null);
 
const EventTableProvider = ({
  children,
  favorite,
  topAlarm,
  showLoading,
}: EventTableContextProps) => {
  const { isFavorite, handleOnFavoriteClick } = useFavorite();
  const { activeTorAlarm, addTorAlarm } = eventTableStore();
 
  return (
    <EventTableContext.Provider
      value={{
        favorite,
        topAlarm,
        isFavorite,
        handleOnFavoriteClick,
        showLoading,
        activeTorAlarm,
        addTorAlarm,
      }}
    >
      {children}
    </EventTableContext.Provider>
  );
};
 
const useEventTable = () => {
  const context = useContext(EventTableContext);
  if (!context) {
    throw new Error('useEventTable must be used within an EventTableProvider');
  }
  return context;
};
 
export { EventTableProvider, useEventTable };
 
function useFavorite() {
  const [isFavorite, setIsFavorite] = useState<string[]>([]);
 
  const handleOnFavoriteClick = (itemId: string) => {
    if (isFavorite.includes(itemId)) {
      setIsFavorite(isFavorite.filter((id) => id !== itemId));
    } else {
      setIsFavorite([...isFavorite, itemId]);
    }
  };
  return { isFavorite, handleOnFavoriteClick };
}