|
| 1 | +import { |
| 2 | + Alert, |
| 3 | + AlertVariant, |
| 4 | + Drawer, |
| 5 | + DrawerContent, |
| 6 | + DrawerContentBody, |
| 7 | + PageSection, |
| 8 | + PageSectionVariants, |
| 9 | + Title, |
| 10 | +} from '@patternfly/react-core'; |
| 11 | +import React, { useEffect, useState } from 'react'; |
| 12 | +import { useHistory, useLocation } from 'react-router-dom'; |
| 13 | + |
| 14 | +import { IOptions, TType } from '../../utils/interfaces'; |
| 15 | +import { IPluginPageProps } from '@kobsio/plugin-core'; |
| 16 | +import PageList from './PageList'; |
| 17 | +import PageToolbar from './PageToolbar'; |
| 18 | +import { getOptionsFromSearch } from '../../utils/helpers'; |
| 19 | + |
| 20 | +const Page: React.FunctionComponent<IPluginPageProps> = ({ name, displayName, description }: IPluginPageProps) => { |
| 21 | + const history = useHistory(); |
| 22 | + const location = useLocation(); |
| 23 | + const [options, setOptions] = useState<IOptions>(getOptionsFromSearch(location.search)); |
| 24 | + const [details, setDetails] = useState<React.ReactNode>(undefined); |
| 25 | + |
| 26 | + const changeOptions = (type: TType, cluster: string): void => { |
| 27 | + history.push({ |
| 28 | + pathname: location.pathname, |
| 29 | + search: `?type=${type}&cluster=${cluster}`, |
| 30 | + }); |
| 31 | + }; |
| 32 | + |
| 33 | + // useEffect is used to change the resources state everytime the location.search parameter changes. |
| 34 | + useEffect(() => { |
| 35 | + setOptions(getOptionsFromSearch(location.search)); |
| 36 | + }, [location.search]); |
| 37 | + |
| 38 | + return ( |
| 39 | + <React.Fragment> |
| 40 | + <PageSection variant={PageSectionVariants.light}> |
| 41 | + <Title headingLevel="h6" size="xl"> |
| 42 | + {displayName} |
| 43 | + </Title> |
| 44 | + <p>{description}</p> |
| 45 | + <PageToolbar options={options} setOptions={changeOptions} /> |
| 46 | + </PageSection> |
| 47 | + |
| 48 | + <Drawer isExpanded={details !== undefined}> |
| 49 | + <DrawerContent panelContent={details}> |
| 50 | + <DrawerContentBody> |
| 51 | + <PageSection style={{ minHeight: '100%' }} variant={PageSectionVariants.default}> |
| 52 | + {!options || !options.cluster ? ( |
| 53 | + <Alert variant={AlertVariant.info} title="Select a cluster and type"> |
| 54 | + <p>Select a cluster and type from the toolbar.</p> |
| 55 | + </Alert> |
| 56 | + ) : ( |
| 57 | + <React.Fragment> |
| 58 | + {options.type === 'sources' ? ( |
| 59 | + <React.Fragment> |
| 60 | + <PageList |
| 61 | + name={name} |
| 62 | + cluster={options.cluster} |
| 63 | + type="gitrepositories.source.toolkit.fluxcd.io/v1beta1" |
| 64 | + title="Git Repos" |
| 65 | + showDetails={setDetails} |
| 66 | + /> |
| 67 | + <p> </p> |
| 68 | + <PageList |
| 69 | + name={name} |
| 70 | + cluster={options.cluster} |
| 71 | + type="helmrepositories.source.toolkit.fluxcd.io/v1beta1" |
| 72 | + title="Helm Repos" |
| 73 | + showDetails={setDetails} |
| 74 | + /> |
| 75 | + <p> </p> |
| 76 | + <PageList |
| 77 | + name={name} |
| 78 | + cluster={options.cluster} |
| 79 | + type="buckets.source.toolkit.fluxcd.io/v1beta1" |
| 80 | + title="Buckets" |
| 81 | + showDetails={setDetails} |
| 82 | + /> |
| 83 | + </React.Fragment> |
| 84 | + ) : options.type === 'kustomizations' ? ( |
| 85 | + <PageList |
| 86 | + name={name} |
| 87 | + cluster={options.cluster} |
| 88 | + type="kustomizations.kustomize.toolkit.fluxcd.io/v1beta1" |
| 89 | + title="Kustomizations" |
| 90 | + showDetails={setDetails} |
| 91 | + /> |
| 92 | + ) : options.type === 'helmreleases' ? ( |
| 93 | + <PageList |
| 94 | + name={name} |
| 95 | + cluster={options.cluster} |
| 96 | + type="helmreleases.helm.toolkit.fluxcd.io/v2beta1" |
| 97 | + title="Helm Releases" |
| 98 | + showDetails={setDetails} |
| 99 | + /> |
| 100 | + ) : null} |
| 101 | + </React.Fragment> |
| 102 | + )} |
| 103 | + </PageSection> |
| 104 | + </DrawerContentBody> |
| 105 | + </DrawerContent> |
| 106 | + </Drawer> |
| 107 | + </React.Fragment> |
| 108 | + ); |
| 109 | +}; |
| 110 | + |
| 111 | +export default Page; |
0 commit comments