From 78d37f203c79b586f1cb15275cfe4a99c51c7e19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20S=C3=BC=C3=9F?= Date: Wed, 18 Dec 2024 13:59:04 +0100 Subject: [PATCH] refactor: use react-hook-form directly (#18) --- .../common/AuthenticationEditor.tsx | 57 ++-- .../ComponentEditor/common/RelabelRules.tsx | 255 +++++++++--------- .../ComponentEditor/common/StaticTargets.tsx | 101 ++++--- .../ComponentEditor/common/TargetSelector.tsx | 25 +- .../ComponentEditor/common/TlsBlock.tsx | 33 ++- .../ComponentEditor/common/TlsConfig.tsx | 12 +- .../components/DiscoveryEc2.tsx | 129 +++++---- .../components/DiscoveryKubernetes.tsx | 154 +++++------ .../components/DiscoveryRelabel.tsx | 15 +- .../ComponentEditor/components/ImportGit.tsx | 18 +- .../components/LocalFileMatch.tsx | 103 ++++--- .../components/LokiProcess.tsx | 189 ++++++------- .../components/LokiRelabel.tsx | 18 +- .../components/LokiSourceFile.tsx | 17 +- .../components/LokiSourceJournal.tsx | 26 +- .../components/LokiSourceWindowsEvent.tsx | 38 ++- .../ComponentEditor/components/LokiWrite.tsx | 42 +-- .../components/OTelColExporterLoki.tsx | 8 +- .../components/OTelColExporterPrometheus.tsx | 20 +- .../components/OTelColProcessorBatch.tsx | 18 +- .../components/OTelColReceiverOTLP.tsx | 11 +- .../components/PrometheusExporterGithub.tsx | 27 +- .../components/PrometheusExporterMysql.tsx | 17 +- .../components/PrometheusExporterRedis.tsx | 97 ++++--- .../components/PrometheusExporterUnix.tsx | 39 ++- .../components/PrometheusExporterWindows.tsx | 12 +- .../components/PrometheusRelabel.tsx | 15 +- .../components/PrometheusRemoteWrite.tsx | 93 +++---- .../components/PrometheusScrape.tsx | 57 ++-- .../components/PyroscopeScrape.tsx | 41 ++- .../modules/GrafanaCloudAutoConfigure.tsx | 17 +- src/components/ComponentEditor/index.tsx | 103 ++++--- .../inputs/GenericStringMap.tsx | 72 +++-- .../ComponentEditor/inputs/LabelsInput.tsx | 70 +++-- .../ComponentEditor/inputs/MultiBlock.tsx | 59 ++-- .../inputs/ReferenceMultiSelect.tsx | 6 +- .../inputs/ReferenceSelect.tsx | 6 +- .../ComponentEditor/inputs/TypedInput.tsx | 17 +- src/lib/components.ts | 7 +- 39 files changed, 982 insertions(+), 1062 deletions(-) diff --git a/src/components/ComponentEditor/common/AuthenticationEditor.tsx b/src/components/ComponentEditor/common/AuthenticationEditor.tsx index 41e1a5f..ffcaa97 100644 --- a/src/components/ComponentEditor/common/AuthenticationEditor.tsx +++ b/src/components/ComponentEditor/common/AuthenticationEditor.tsx @@ -2,12 +2,11 @@ import { SelectableValue } from "@grafana/data"; import { Alert, FieldSet, - FormAPI, InlineField, - InputControl, MultiSelect, RadioButtonGroup, } from "@grafana/ui"; +import { Controller, useFormContext } from "react-hook-form"; import TypedInput from "../inputs/TypedInput"; type AuthenticationType = @@ -18,16 +17,15 @@ type AuthenticationType = | "authorization" | "oauth2"; const Component = ({ - methods, options, defaultValue, parent, }: { - methods: FormAPI>; options?: AuthenticationType[]; defaultValue?: AuthenticationType; parent?: string; }) => { + const { watch, control } = useFormContext(); const commonOptions = { labelWidth: 24, }; @@ -38,12 +36,12 @@ const Component = ({ defaultValue = "none"; } parent = parent ? parent + "." : ""; - const watchAuthType = methods.watch(`${parent}auth_type` as const); + const watchAuthType = watch(`${parent}auth_type` as const); return (
- ( @@ -114,20 +112,14 @@ const Component = ({ tooltip="Bearer token to authenticate with." {...commonOptions} > - + - + )} @@ -140,19 +132,19 @@ const Component = ({ > @@ -160,27 +152,24 @@ const Component = ({ {watchAuthType === "oauth2" && ( <> - + - ( - + - + )} diff --git a/src/components/ComponentEditor/common/RelabelRules.tsx b/src/components/ComponentEditor/common/RelabelRules.tsx index 6ed6f76..02e1487 100644 --- a/src/components/ComponentEditor/common/RelabelRules.tsx +++ b/src/components/ComponentEditor/common/RelabelRules.tsx @@ -2,16 +2,14 @@ import { css } from "@emotion/css"; import { GrafanaTheme2, SelectableValue } from "@grafana/data"; import { Button, - FieldArray, FieldSet, - FormAPI, InlineField, Input, - InputControl, MultiSelect, Select, VerticalGroup, } from "@grafana/ui"; +import { Controller, useFieldArray, useFormContext } from "react-hook-form"; import { useStyles } from "../../../theme"; @@ -84,139 +82,134 @@ const relabelActions = [ }, ]; -export const RelabelRules = ({ - methods, -}: { - methods: FormAPI>; -}) => { +export const RelabelRules = () => { + const { control, register } = useFormContext<{ + [key: string]: any; + rule: Array>; + }>(); const styles = useStyles(getStyles); const commonOptions = { labelWidth: 24 }; + + const { fields, append, remove } = useFieldArray({ + control, + name: "rule", + }); return (
- - {({ fields, append, remove }) => ( - <> - {fields.map((field, index) => ( -
- - - ( - - )} - /> - - - - - - - - - - - - - - - - - - ( - + + + + + + + + + + + + + + + ( +