import { useCallback, useMemo } from 'react'; import { useConfig } from '~/context'; import type { Device } from '~/types'; export type UseDeviceReturn = ( /** Device's ID, e.g. the device.name field.*/ deviceId: string, ) => Nullable; /** * Get a device's configuration from the global configuration context based on its name. */ export function useDevice(): UseDeviceReturn { const { devices } = useConfig(); const locations = useMemo(() => devices.flatMap(group => group.locations), [devices]); function getDevice(id: string): Nullable { return locations.find(device => device.id === id) ?? null; } return useCallback(getDevice, [locations]); }