mirror of
https://github.com/thatmattlove/hyperglass.git
synced 2026-01-17 08:48:05 +00:00
33 lines
1,007 B
TypeScript
33 lines
1,007 B
TypeScript
import { useCallback, useMemo } from 'react';
|
|
import { useConfig } from '~/context';
|
|
|
|
import type { TDeviceVrf } from '~/types';
|
|
import type { TUseVrf } from './types';
|
|
|
|
/**
|
|
* Get a VRF configuration from the global configuration context based on its name.
|
|
*/
|
|
export function useVrf(): TUseVrf {
|
|
const { networks } = useConfig();
|
|
|
|
const vrfs = useMemo(() => networks.map(n => n.locations.map(l => l.vrfs).flat()).flat(), []);
|
|
|
|
function getVrf(id: string): TDeviceVrf {
|
|
const matching = vrfs.find(vrf => vrf._id === id);
|
|
if (typeof matching === 'undefined') {
|
|
if (id === '__hyperglass_default') {
|
|
const anyDefault = vrfs.find(vrf => vrf.default === true);
|
|
if (typeof anyDefault !== 'undefined') {
|
|
return anyDefault;
|
|
} else {
|
|
throw new Error(`No matching VRF found for '${id}'`);
|
|
}
|
|
} else {
|
|
throw new Error(`No matching VRF found for '${id}'`);
|
|
}
|
|
}
|
|
return matching;
|
|
}
|
|
|
|
return useCallback(getVrf, []);
|
|
}
|