From e407258f72635f88d4b9aa5f4b06cbc8082cf224 Mon Sep 17 00:00:00 2001 From: checktheroads Date: Fri, 25 Dec 2020 00:45:23 -0700 Subject: [PATCH] continue typescript & chakra v1 migrations [skip ci] --- hyperglass/ui/components/buttons/path.tsx | 4 +- hyperglass/ui/components/buttons/submit.tsx | 35 ++++---- .../ui/components/form/communitySelect.tsx | 57 ------------- hyperglass/ui/components/form/index.ts | 1 - hyperglass/ui/components/form/queryTarget.tsx | 85 +++++++++++++++---- hyperglass/ui/components/form/types.ts | 3 +- hyperglass/ui/components/help/modal.tsx | 8 +- hyperglass/ui/components/lookingGlass.tsx | 38 ++++----- hyperglass/ui/components/path/chart.tsx | 46 ++++++---- hyperglass/ui/components/path/controls.tsx | 27 ++++++ hyperglass/ui/components/path/path.tsx | 4 +- hyperglass/ui/components/results/group.tsx | 1 + 12 files changed, 167 insertions(+), 142 deletions(-) delete mode 100644 hyperglass/ui/components/form/communitySelect.tsx create mode 100644 hyperglass/ui/components/path/controls.tsx diff --git a/hyperglass/ui/components/buttons/path.tsx b/hyperglass/ui/components/buttons/path.tsx index 7c5762c..68bf21b 100644 --- a/hyperglass/ui/components/buttons/path.tsx +++ b/hyperglass/ui/components/buttons/path.tsx @@ -3,7 +3,9 @@ import { Button, Icon, Tooltip } from '@chakra-ui/react'; import type { TPathButton } from './types'; -const PathIcon = dynamic(() => import('@meronex/icons/gr').then(i => i.GrNetwork)); +const PathIcon = dynamic(() => + import('@meronex/icons/bi').then(i => i.BisNetworkChart), +); export const PathButton = (props: TPathButton) => { const { onOpen } = props; diff --git a/hyperglass/ui/components/buttons/submit.tsx b/hyperglass/ui/components/buttons/submit.tsx index a634ff6..8b91282 100644 --- a/hyperglass/ui/components/buttons/submit.tsx +++ b/hyperglass/ui/components/buttons/submit.tsx @@ -1,3 +1,4 @@ +import { forwardRef } from 'react'; import { Modal, Popover, @@ -18,24 +19,26 @@ import { useMobile } from '~/context'; import { useLGState } from '~/hooks'; import type { IconButtonProps } from '@chakra-ui/react'; -import type { OnChangeArgs } from '~/types'; import type { TSubmitButton, TRSubmitButton } from './types'; -const SubmitIcon = (props: Omit) => { - const { isLoading } = props; - return ( - } - title="Submit Query" - colorScheme="primary" - isLoading={isLoading} - aria-label="Submit Query" - /> - ); -}; +const SubmitIcon = forwardRef>( + (props, ref) => { + const { isLoading } = props; + return ( + } + title="Submit Query" + colorScheme="primary" + isLoading={isLoading} + aria-label="Submit Query" + /> + ); + }, +); /** * Mobile Submit Button diff --git a/hyperglass/ui/components/form/communitySelect.tsx b/hyperglass/ui/components/form/communitySelect.tsx deleted file mode 100644 index 85f45c8..0000000 --- a/hyperglass/ui/components/form/communitySelect.tsx +++ /dev/null @@ -1,57 +0,0 @@ -import { useEffect, useMemo } from 'react'; -import { Text } from '@chakra-ui/react'; -import { components } from 'react-select'; -import { Select } from '~/components'; - -import type { OptionProps } from 'react-select'; -import type { TBGPCommunity, TSelectOption } from '~/types'; -import type { TCommunitySelect } from './types'; - -function buildOptions(communities: TBGPCommunity[]): TSelectOption[] { - return communities.map(c => ({ - value: c.community, - label: c.display_name, - description: c.description, - })); -} - -const Option = (props: OptionProps) => { - const { label, data } = props; - return ( - - {label} -
- - {data.description} - -
- ); -}; - -export const CommunitySelect = (props: TCommunitySelect) => { - const { name, communities, onChange, register, unregister } = props; - - const options = useMemo(() => buildOptions(communities), [communities.length]); - - function handleChange(e: TSelectOption | TSelectOption[]): void { - if (!Array.isArray(e) && e !== null) { - onChange({ field: name, value: e.value }); - } - } - - useEffect(() => { - register({ name }); - return () => unregister(name); - }, [name, register, unregister]); - - return ( - - + + + ); }; diff --git a/hyperglass/ui/components/form/types.ts b/hyperglass/ui/components/form/types.ts index fc5eada..8cff3b8 100644 --- a/hyperglass/ui/components/form/types.ts +++ b/hyperglass/ui/components/form/types.ts @@ -29,7 +29,6 @@ export interface TCommunitySelect { onChange: OnChange; communities: TBGPCommunity[]; register: Control['register']; - unregister: Control['unregister']; } export interface TQueryTarget { @@ -37,7 +36,7 @@ export interface TQueryTarget { placeholder: string; resolveTarget: boolean; register: Control['register']; - setTarget(e: OnChangeArgs): void; + onChange(e: OnChangeArgs): void; } export interface TResolvedTarget { diff --git a/hyperglass/ui/components/help/modal.tsx b/hyperglass/ui/components/help/modal.tsx index 4e367f8..393e5b9 100644 --- a/hyperglass/ui/components/help/modal.tsx +++ b/hyperglass/ui/components/help/modal.tsx @@ -31,19 +31,15 @@ export const HelpModal = (props: THelpModal) => { animate={{ opacity: 1, scale: 1 }} initial={{ opacity: 0, scale: 0.3 }}> } onClick={onOpen} - colorScheme="primary" + colorScheme="blue" aria-label={`${name}_help`} /> diff --git a/hyperglass/ui/components/lookingGlass.tsx b/hyperglass/ui/components/lookingGlass.tsx index 0ad0937..4f36e5d 100644 --- a/hyperglass/ui/components/lookingGlass.tsx +++ b/hyperglass/ui/components/lookingGlass.tsx @@ -15,7 +15,6 @@ import { QueryTarget, SubmitButton, QueryLocation, - CommunitySelect, } from '~/components'; import { useConfig } from '~/context'; import { useStrf, useGreeting, useDevice, useLGState } from '~/hooks'; @@ -37,16 +36,17 @@ export const HyperglassForm = () => { const formSchema = yup.object().shape({ query_location: yup.array().of(yup.string()).required(noQueryLoc), + query_target: yup.string().required(noQueryTarget), query_type: yup.string().required(noQueryType), query_vrf: yup.string(), - query_target: yup.string().required(noQueryTarget), }); const formInstance = useForm({ resolver: yupResolver(formSchema), defaultValues: { query_vrf: 'default', query_target: '', query_location: [], query_type: '' }, }); - const { handleSubmit, register, unregister, setValue, errors } = formInstance; + + const { handleSubmit, register, unregister, setValue, getValues } = formInstance; const { queryVrf, @@ -63,6 +63,8 @@ export const HyperglassForm = () => { } = useLGState(); function submitHandler(values: TFormData) { + console.dir(values); + console.dir(formData.value); if (!greetingAck && web.greeting.required) { window.location.reload(false); setGreetingAck(false); @@ -141,6 +143,8 @@ export const HyperglassForm = () => { } else if (e.field === 'query_target' && isString(e.value)) { queryTarget.set(e.value); } + console.log(e.field, e.value); + console.dir(getValues()); } const vrfContent = useMemo(() => { @@ -155,9 +159,9 @@ export const HyperglassForm = () => { useEffect(() => { register({ name: 'query_location', required: true }); + register({ name: 'query_target', required: true }); register({ name: 'query_type', required: true }); register({ name: 'query_vrf' }); - register({ name: 'query_target', required: true }); }, [register]); return ( @@ -192,25 +196,13 @@ export const HyperglassForm = () => { - - - - - - + diff --git a/hyperglass/ui/components/path/chart.tsx b/hyperglass/ui/components/path/chart.tsx index 85995cb..1b13d47 100644 --- a/hyperglass/ui/components/path/chart.tsx +++ b/hyperglass/ui/components/path/chart.tsx @@ -1,40 +1,52 @@ import { useMemo } from 'react'; -import { Box, Flex, Icon, Badge, VStack } from '@chakra-ui/react'; -import { BeatLoader } from 'react-spinners'; +import { Box, Flex, SkeletonText, Badge, VStack } from '@chakra-ui/react'; import ReactFlow from 'react-flow-renderer'; -import { Background, Controls } from 'react-flow-renderer'; +import { Background, ReactFlowProvider } from 'react-flow-renderer'; import { Handle, Position } from 'react-flow-renderer'; -import { useConfig, useColorValue, useColorToken } from '~/context'; +import { useConfig, useColorValue, useColorToken, useBreakpointValue } from '~/context'; import { useASNDetail } from '~/hooks'; +import { Controls } from './controls'; import { buildElements } from './util'; +import type { ReactFlowProps } from 'react-flow-renderer'; import type { TChart, TNode, TNodeData } from './types'; export const Chart = (props: TChart) => { const { data } = props; const { primary_asn, org_name } = useConfig(); - // const elements = useMemo(() => [...buildElements({ asn: primary_asn, name: org_name }, data)], [ - // data, - // ]); - const elements = [...buildElements({ asn: primary_asn, name: org_name }, data)]; const dots = useColorToken('blackAlpha.500', 'whiteAlpha.400'); + + const flowProps = useBreakpointValue>({ + base: { defaultPosition: [0, 300], defaultZoom: 0 }, + lg: { defaultPosition: [500, 450] }, + }); + + const elements = useMemo(() => [...buildElements({ asn: primary_asn, name: org_name }, data)], [ + data, + ]); + return ( - - - - - - + + + + + + + + ); }; const TestNode = (props: TNode) => { const { data } = props; const { asn, name, hasChildren, hasParents } = data; + const color = useColorValue('black', 'white'); - const bg = useColorValue('white', 'blackAlpha.800'); + const bg = useColorValue('white', 'whiteAlpha.100'); + const { data: asnData, isError, isLoading } = useASNDetail(asn); + return ( <> {hasChildren && } @@ -42,7 +54,9 @@ const TestNode = (props: TNode) => { {isLoading ? ( - + + + ) : !isError && asnData?.data?.description_short ? ( asnData.data.description_short ) : ( diff --git a/hyperglass/ui/components/path/controls.tsx b/hyperglass/ui/components/path/controls.tsx new file mode 100644 index 0000000..5d9dd43 --- /dev/null +++ b/hyperglass/ui/components/path/controls.tsx @@ -0,0 +1,27 @@ +import dynamic from 'next/dynamic'; +import { ButtonGroup, IconButton } from '@chakra-ui/react'; +import { useZoomPanHelper } from 'react-flow-renderer'; + +const Plus = dynamic(() => import('@meronex/icons/fi').then(i => i.FiPlus)); +const Minus = dynamic(() => import('@meronex/icons/fi').then(i => i.FiMinus)); +const Square = dynamic(() => import('@meronex/icons/fi').then(i => i.FiSquare)); + +export const Controls = () => { + const { fitView, zoomIn, zoomOut } = useZoomPanHelper(); + return ( + + } onClick={() => zoomIn()} aria-label="Zoom In" /> + } onClick={() => zoomOut()} aria-label="Zoom Out" /> + } onClick={() => fitView()} aria-label="Fit Nodes" /> + + ); +}; diff --git a/hyperglass/ui/components/path/path.tsx b/hyperglass/ui/components/path/path.tsx index 3e5b6f9..8cf53d0 100644 --- a/hyperglass/ui/components/path/path.tsx +++ b/hyperglass/ui/components/path/path.tsx @@ -22,13 +22,13 @@ export const Path = (props: TPath) => { const { displayTarget } = useLGState(); const response = getResponse(device); const output = response?.output as TStructuredResponse; - const bg = useColorValue('white', 'blackFaded.800'); + const bg = useColorValue('whiteFaded.50', 'blackFaded.900'); return ( <> - + {`Path to ${displayTarget.value}`} diff --git a/hyperglass/ui/components/results/group.tsx b/hyperglass/ui/components/results/group.tsx index c68294f..791d984 100644 --- a/hyperglass/ui/components/results/group.tsx +++ b/hyperglass/ui/components/results/group.tsx @@ -148,6 +148,7 @@ export const Results = () => { const device = getDevice(loc.value); return (