2020-12-13 01:49:13 -07:00
|
|
|
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-13 01:49:13 -07:00
|
|
|
|
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 {
|
2020-12-13 01:49:13 -07:00
|
|
|
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(), []);
|
2020-12-13 01:49:13 -07:00
|
|
|
|
|
|
|
|
function getDevice(id: string): TDevice {
|
2021-02-10 00:43:40 -07:00
|
|
|
return devices.filter(dev => dev._id === id)[0];
|
2020-12-13 01:49:13 -07:00
|
|
|
}
|
2020-12-31 23:09:54 -07:00
|
|
|
|
2020-12-13 01:49:13 -07:00
|
|
|
return useCallback(getDevice, []);
|
|
|
|
|
}
|