|
| 1 | +/*! |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +import { Accordion, Box, Field } from "@chakra-ui/react"; |
| 20 | +import { type Control, type FieldValues, type Path, Controller } from "react-hook-form"; |
| 21 | + |
| 22 | +import type { ParamsSpec } from "src/queries/useDagParams"; |
| 23 | +import { useParamStore } from "src/queries/useParamStore"; |
| 24 | + |
| 25 | +import { FlexibleForm, flexibleFormDefaultSection } from "./FlexibleForm"; |
| 26 | +import { JsonEditor } from "./JsonEditor"; |
| 27 | + |
| 28 | +type ConfigFormProps<T extends FieldValues = FieldValues> = { |
| 29 | + readonly children?: React.ReactNode; |
| 30 | + readonly control: Control<T>; |
| 31 | + readonly errors: { |
| 32 | + conf?: string; |
| 33 | + date?: unknown; |
| 34 | + }; |
| 35 | + readonly initialParamsDict: { paramsDict: ParamsSpec }; |
| 36 | + readonly setErrors: React.Dispatch< |
| 37 | + React.SetStateAction<{ |
| 38 | + conf?: string; |
| 39 | + date?: unknown; |
| 40 | + }> |
| 41 | + >; |
| 42 | +}; |
| 43 | + |
| 44 | +const ConfigForm = <T extends FieldValues = FieldValues>({ |
| 45 | + children, |
| 46 | + control, |
| 47 | + errors, |
| 48 | + initialParamsDict, |
| 49 | + setErrors, |
| 50 | +}: ConfigFormProps<T>) => { |
| 51 | + const { conf, setConf } = useParamStore(); |
| 52 | + |
| 53 | + const validateAndPrettifyJson = (value: string) => { |
| 54 | + try { |
| 55 | + const parsedJson = JSON.parse(value) as JSON; |
| 56 | + |
| 57 | + setErrors((prev) => ({ ...prev, conf: undefined })); |
| 58 | + |
| 59 | + const formattedJson = JSON.stringify(parsedJson, undefined, 2); |
| 60 | + |
| 61 | + if (formattedJson !== conf) { |
| 62 | + setConf(formattedJson); // Update only if the value is different |
| 63 | + } |
| 64 | + |
| 65 | + return formattedJson; |
| 66 | + } catch (error) { |
| 67 | + const errorMessage = error instanceof Error ? error.message : "Unknown error occurred."; |
| 68 | + |
| 69 | + setErrors((prev) => ({ |
| 70 | + ...prev, |
| 71 | + conf: `Invalid JSON format: ${errorMessage}`, |
| 72 | + })); |
| 73 | + |
| 74 | + return value; |
| 75 | + } |
| 76 | + }; |
| 77 | + |
| 78 | + return ( |
| 79 | + <Accordion.Root |
| 80 | + collapsible |
| 81 | + defaultValue={[flexibleFormDefaultSection]} |
| 82 | + mb={4} |
| 83 | + size="lg" |
| 84 | + variant="enclosed" |
| 85 | + > |
| 86 | + <FlexibleForm |
| 87 | + flexibleFormDefaultSection={flexibleFormDefaultSection} |
| 88 | + initialParamsDict={initialParamsDict} |
| 89 | + /> |
| 90 | + <Accordion.Item key="advancedOptions" value="advancedOptions"> |
| 91 | + <Accordion.ItemTrigger cursor="button">Advanced Options</Accordion.ItemTrigger> |
| 92 | + <Accordion.ItemContent> |
| 93 | + <Box p={4}> |
| 94 | + {children} |
| 95 | + <Controller |
| 96 | + control={control} |
| 97 | + name={"conf" as Path<T>} |
| 98 | + render={({ field }) => ( |
| 99 | + <Field.Root invalid={Boolean(errors.conf)} mt={6}> |
| 100 | + <Field.Label fontSize="md">Configuration JSON</Field.Label> |
| 101 | + <JsonEditor |
| 102 | + {...field} |
| 103 | + onBlur={() => { |
| 104 | + field.onChange(validateAndPrettifyJson(field.value as string)); |
| 105 | + }} |
| 106 | + /> |
| 107 | + {Boolean(errors.conf) ? <Field.ErrorText>{errors.conf}</Field.ErrorText> : undefined} |
| 108 | + </Field.Root> |
| 109 | + )} |
| 110 | + /> |
| 111 | + </Box> |
| 112 | + </Accordion.ItemContent> |
| 113 | + </Accordion.Item> |
| 114 | + </Accordion.Root> |
| 115 | + ); |
| 116 | +}; |
| 117 | + |
| 118 | +export default ConfigForm; |
0 commit comments