lookingglass/hyperglass/ui/hooks/useDevice.ts
2021-09-24 00:16:26 -07:00

20 lines
601 B
TypeScript

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