Files

21 lines
564 B
TypeScript
Raw Permalink Normal View History

import { useCallback, useMemo } from 'react';
import { useConfig } from '~/context';
import type { TDevice } from '~/types';
2020-12-31 23:09:54 -07:00
import type { TUseDevice } from './types';
2020-12-31 23:09:54 -07:00
/**
* Get a device's configuration from the global configuration context based on its name.
*/
export function useDevice(): TUseDevice {
const { networks } = useConfig();
2020-12-31 23:09:54 -07:00
2021-01-10 01:15:13 -07:00
const devices = useMemo(() => networks.map(n => n.locations).flat(), []);
function getDevice(id: string): TDevice {
return devices.filter(dev => dev._id === id)[0];
}
2020-12-31 23:09:54 -07:00
return useCallback(getDevice, []);
}