|
| 1 | +import { Fragment, useRef, useState } from "react"; |
| 2 | +import { |
| 3 | + Button, |
| 4 | + Content, |
| 5 | + Flex, |
| 6 | + FlexItem, |
| 7 | + Form, |
| 8 | + FormGroup, |
| 9 | + FormGroupLabelHelp, |
| 10 | + FormHelperText, |
| 11 | + HelperText, |
| 12 | + HelperTextItem, |
| 13 | + PageSection, |
| 14 | + Popover, |
| 15 | + TextInput, |
| 16 | +} from "@patternfly/react-core"; |
| 17 | +import { useFetchArtifactsImageData } from "@app/queries/artifacts"; |
| 18 | +import { LoadingWrapper } from "@app/components/LoadingWrapper"; |
| 19 | +import { ArtifactResults } from "./components/ArtifactResults"; |
| 20 | +import { Controller, useForm } from "react-hook-form"; |
| 21 | + |
| 22 | +import { ExclamationCircleIcon } from "@patternfly/react-icons"; |
| 23 | + |
| 24 | +const PLACEHOLDER_URI = "docker.io/library/nginx:latest"; |
| 25 | + |
| 26 | +interface FormInputs { |
| 27 | + searchInput: string; |
| 28 | +} |
| 29 | + |
| 30 | +export const Artifacts = () => { |
| 31 | + const [artifactUri, setArtifactUri] = useState<string | null>(null); |
| 32 | + const labelHelpRef = useRef(null); |
| 33 | + |
| 34 | + const { |
| 35 | + artifact, |
| 36 | + isFetching: isFetchingArtifactMetadata, |
| 37 | + fetchError: fetchErrorArtifactMetadata, |
| 38 | + } = useFetchArtifactsImageData({ uri: artifactUri }); |
| 39 | + |
| 40 | + const { |
| 41 | + control, |
| 42 | + handleSubmit, |
| 43 | + watch, |
| 44 | + formState: { errors }, |
| 45 | + } = useForm<FormInputs>({ |
| 46 | + mode: "all", |
| 47 | + reValidateMode: "onChange", |
| 48 | + defaultValues: { |
| 49 | + searchInput: "", |
| 50 | + }, |
| 51 | + }); |
| 52 | + |
| 53 | + const onSubmit = (data: FormInputs) => { |
| 54 | + const uri = data.searchInput?.trim(); |
| 55 | + if (!uri) return; |
| 56 | + setArtifactUri(uri); |
| 57 | + }; |
| 58 | + |
| 59 | + const query = watch("searchInput"); |
| 60 | + const isEmpty = query.trim().length === 0; |
| 61 | + |
| 62 | + return ( |
| 63 | + <Fragment> |
| 64 | + <PageSection variant="default"> |
| 65 | + <Content> |
| 66 | + <h1>Artifacts</h1> |
| 67 | + <p>Search for an artifact.</p> |
| 68 | + </Content> |
| 69 | + </PageSection> |
| 70 | + <PageSection> |
| 71 | + <Form onSubmit={(e) => void handleSubmit(onSubmit)(e)}> |
| 72 | + <Flex> |
| 73 | + <Flex direction={{ default: "column" }} flex={{ default: "flex_3" }}> |
| 74 | + <FlexItem> |
| 75 | + <Controller |
| 76 | + name="searchInput" |
| 77 | + control={control} |
| 78 | + rules={{ required: { value: true, message: "A value is required" } }} |
| 79 | + render={({ field, fieldState }) => ( |
| 80 | + <FormGroup |
| 81 | + label="URI" |
| 82 | + labelHelp={ |
| 83 | + <Popover |
| 84 | + triggerRef={labelHelpRef} |
| 85 | + headerContent={<div>URI of the container image</div>} |
| 86 | + bodyContent={<div>e.g., {PLACEHOLDER_URI}</div>} |
| 87 | + > |
| 88 | + <FormGroupLabelHelp ref={labelHelpRef} aria-label="More info for URI field" /> |
| 89 | + </Popover> |
| 90 | + } |
| 91 | + isRequired |
| 92 | + fieldId="uri" |
| 93 | + > |
| 94 | + <TextInput |
| 95 | + aria-label={`uri input field`} |
| 96 | + {...field} |
| 97 | + type="text" |
| 98 | + name="searchInput" |
| 99 | + id="uri" |
| 100 | + aria-describedby="uri-helper" |
| 101 | + aria-invalid={errors.searchInput ? "true" : "false"} |
| 102 | + placeholder={PLACEHOLDER_URI} |
| 103 | + validated={fieldState.invalid ? "error" : "default"} |
| 104 | + /> |
| 105 | + {fieldState.invalid && ( |
| 106 | + <FormHelperText> |
| 107 | + <HelperText> |
| 108 | + <HelperTextItem icon={<ExclamationCircleIcon />} variant={"error"}> |
| 109 | + {fieldState.invalid ? fieldState.error?.message : <span>A value is required</span>} |
| 110 | + </HelperTextItem> |
| 111 | + </HelperText> |
| 112 | + </FormHelperText> |
| 113 | + )} |
| 114 | + </FormGroup> |
| 115 | + )} |
| 116 | + ></Controller> |
| 117 | + </FlexItem> |
| 118 | + </Flex> |
| 119 | + <Flex |
| 120 | + direction={{ default: "column" }} |
| 121 | + alignSelf={{ default: "alignSelfFlexStart" }} |
| 122 | + flex={{ default: "flex_1" }} |
| 123 | + > |
| 124 | + <FlexItem style={{ marginTop: "2em" }}> |
| 125 | + <Button |
| 126 | + variant="primary" |
| 127 | + id="search-form-button" |
| 128 | + isBlock={true} |
| 129 | + isDisabled={isEmpty} |
| 130 | + type="submit" |
| 131 | + spinnerAriaLabel="Loading" |
| 132 | + spinnerAriaLabelledBy="search-form-button" |
| 133 | + > |
| 134 | + Search |
| 135 | + </Button> |
| 136 | + </FlexItem> |
| 137 | + </Flex> |
| 138 | + </Flex> |
| 139 | + </Form> |
| 140 | + </PageSection> |
| 141 | + <PageSection> |
| 142 | + <LoadingWrapper isFetching={isFetchingArtifactMetadata} fetchError={fetchErrorArtifactMetadata}> |
| 143 | + {artifact && <ArtifactResults artifact={artifact} />} |
| 144 | + </LoadingWrapper> |
| 145 | + </PageSection> |
| 146 | + </Fragment> |
| 147 | + ); |
| 148 | +}; |
0 commit comments