From 768ec4c2c5cc37b9938d18144005af24615b075a Mon Sep 17 00:00:00 2001 From: Misi Date: Tue, 18 Feb 2025 12:42:09 +0100 Subject: [PATCH] Auth: Fix AzureAD config UI's ClientAuthentication dropdown (#100752) * wip * Address feedback --- .../features/auth-config/FieldRenderer.tsx | 19 +++++++++++++++++-- .../auth-config/ProviderConfigForm.tsx | 2 ++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/public/app/features/auth-config/FieldRenderer.tsx b/public/app/features/auth-config/FieldRenderer.tsx index de12cc004bf..feb4f9aeb40 100644 --- a/public/app/features/auth-config/FieldRenderer.tsx +++ b/public/app/features/auth-config/FieldRenderer.tsx @@ -2,6 +2,7 @@ import { css } from '@emotion/css'; import { useEffect, useState } from 'react'; import { UseFormReturn, Controller } from 'react-hook-form'; +import { SelectableValue } from '@grafana/data'; import { Checkbox, Field, Input, SecretInput, Select, Switch, useTheme2 } from '@grafana/ui'; import { fieldMap } from './fields'; @@ -9,7 +10,10 @@ import { SSOProviderDTO, SSOSettingsField } from './types'; import { isSelectableValue } from './utils/guards'; interface FieldRendererProps - extends Pick, 'register' | 'control' | 'watch' | 'setValue' | 'unregister'> { + extends Pick< + UseFormReturn, + 'register' | 'control' | 'watch' | 'setValue' | 'getValues' | 'unregister' + > { field: SSOSettingsField; errors: UseFormReturn['formState']['errors']; secretConfigured: boolean; @@ -22,6 +26,7 @@ export const FieldRenderer = ({ errors, watch, setValue, + getValues, control, unregister, secretConfigured, @@ -42,9 +47,19 @@ export const FieldRenderer = ({ } }, [unregister, name, parentValue, isDependantField]); + const isNotEmptySelectableValueArray = ( + current: string | boolean | Record | Array> | undefined + ): current is Array> => { + return Array.isArray(current) && current.length > 0 && 'value' in current[0]; + }; + useEffect(() => { if (fieldData.defaultValue) { - setValue(name, fieldData.defaultValue.value); + const current = getValues(name); + const obj = fieldData.options?.find( + (option) => option.value === (isNotEmptySelectableValueArray(current) ? current[0].value : undefined) + ); + setValue(name, obj?.value || fieldData.defaultValue.value); } // eslint-disable-next-line react-hooks/exhaustive-deps }, []); diff --git a/public/app/features/auth-config/ProviderConfigForm.tsx b/public/app/features/auth-config/ProviderConfigForm.tsx index bdaeb1e0bc7..0fb3567a16b 100644 --- a/public/app/features/auth-config/ProviderConfigForm.tsx +++ b/public/app/features/auth-config/ProviderConfigForm.tsx @@ -41,6 +41,7 @@ export const ProviderConfigForm = ({ config, provider, isLoading }: ProviderConf reset, watch, setValue, + getValues, unregister, formState: { errors, dirtyFields, isSubmitted }, } = useForm({ defaultValues: dataToDTO(config), mode: 'onSubmit', reValidateMode: 'onChange' }); @@ -181,6 +182,7 @@ export const ProviderConfigForm = ({ config, provider, isLoading }: ProviderConf control={control} errors={errors} setValue={setValue} + getValues={getValues} register={register} watch={watch} unregister={unregister}