import { createContext, type Dispatch, useState, type SetStateAction, useEffect } from 'react'; import { defaultMaker, type Maker, Garage } from '../models'; import { systemClient } from '../services/System'; export interface UseGarageStoreType { garage: Garage; maker: Maker; setMaker: Dispatch>; badOrder?: string; setBadOrder: Dispatch>; robotUpdatedAt: string; orderUpdatedAt: string; } export const initialGarageContext: UseGarageStoreType = { garage: new Garage(), maker: defaultMaker, setMaker: () => {}, badOrder: undefined, setBadOrder: () => {}, robotUpdatedAt: '', orderUpdatedAt: '', }; export const GarageContext = createContext(initialGarageContext); export const useGarageStore = (): UseGarageStoreType => { // All garage data structured const [garage] = useState(initialGarageContext.garage); const [maker, setMaker] = useState(initialGarageContext.maker); const [badOrder, setBadOrder] = useState(); const [robotUpdatedAt, setRobotUpdatedAt] = useState(new Date().toISOString()); const [orderUpdatedAt, setOrderUpdatedAt] = useState(new Date().toISOString()); const onRobotUpdated = (): void => { setRobotUpdatedAt(new Date().toISOString()); }; const onOrderUpdate = (): void => { setOrderUpdatedAt(new Date().toISOString()); }; useEffect(() => { garage.registerHook('onRobotUpdate', onRobotUpdated); garage.registerHook('onOrderUpdate', onOrderUpdate); }, []); useEffect(() => { if (window.NativeRobosats !== undefined && !systemClient.loading) { garage.loadSlots(); } }, [systemClient.loading]); return { garage, maker, setMaker, badOrder, setBadOrder, robotUpdatedAt, orderUpdatedAt, }; };