@@ -2,7 +2,7 @@ import { Checkbox, DirectionalHint, Label, Text } from "@fluentui/react";
22import { Context , FormattedMessage } from "@oursky/react-messageformat" ;
33import cn from "classnames" ;
44import { produce } from "immer" ;
5- import React , { useCallback , useContext , useMemo } from "react" ;
5+ import React , { useCallback , useContext , useMemo , useState } from "react" ;
66import FormTextField from "../../FormTextField" ;
77import {
88 createOAuthSSOProviderItemKey ,
@@ -251,7 +251,7 @@ const OAuthClientIcon: React.VFC<OAuthClientIconProps> =
251251 } ;
252252
253253export function useSingleSignOnConfigurationWidget (
254- alias : string ,
254+ initialAlias : string ,
255255 providerItemKey : OAuthSSOProviderItemKey ,
256256 form : OAuthProviderFormModel ,
257257 oauthSSOFeatureConfig ?: OAuthSSOFeatureConfig
@@ -263,6 +263,19 @@ export function useSingleSignOnConfigurationWidget(
263263
264264 const [ providerType , appType ] = parseOAuthSSOProviderItemKey ( providerItemKey ) ;
265265
266+ const [ providerIndex ] = useState < number > ( ( ) => {
267+ const existingIndex = providers . findIndex ( ( p ) =>
268+ isOAuthSSOProvider ( p . config , providerType , initialAlias , appType )
269+ ) ;
270+ if ( existingIndex !== - 1 ) {
271+ return existingIndex ;
272+ }
273+ // Insert at the end if it does not exist
274+ return providers . length ;
275+ } ) ;
276+
277+ console . log ( "providerIndex" , providerIndex ) ;
278+
266279 const disabled = useMemo ( ( ) => {
267280 const providersConfig = oauthSSOFeatureConfig ?. providers ?? { } ;
268281 const providerConfig = providersConfig [
@@ -275,41 +288,32 @@ export function useSingleSignOnConfigurationWidget(
275288 const newConfig = {
276289 config : {
277290 type : providerType ,
278- alias : alias ,
291+ alias : initialAlias ,
279292 ...( appType && { app_type : appType } ) ,
280293 } ,
281294 secret : {
282295 originalAlias : null ,
283- newAlias : alias ,
296+ newAlias : initialAlias ,
284297 newClientSecret : "" ,
285298 } ,
286299 } satisfies SSOProviderFormState ;
287- return (
288- providers . find ( ( p ) =>
289- isOAuthSSOProvider ( p . config , providerType , alias , appType )
290- ) ?? newConfig
291- ) ;
292- } , [ providers , providerType , alias , appType ] ) ;
300+ return providers . length > providerIndex
301+ ? providers [ providerIndex ]
302+ : newConfig ;
303+ } , [ providerType , initialAlias , appType , providers , providerIndex ] ) ;
293304
294- const index = providers . findIndex ( ( p ) =>
295- isOAuthSSOProvider ( p . config , providerType , alias , appType )
296- ) ;
297305 const jsonPointer = useMemo ( ( ) => {
298- return index >= 0 ? `/identity/oauth/providers/${ index } ` : "" ;
299- } , [ index ] ) ;
300- const clientSecretParentJsonPointer =
301- index >= 0
302- ? new RegExp ( `/secrets/\\d+/data/items/${ index } ` )
303- : / p l a c e h o l d e r / ;
306+ return `/identity/oauth/providers/${ providerIndex } ` ;
307+ } , [ providerIndex ] ) ;
308+ const clientSecretParentJsonPointer = new RegExp (
309+ `/secrets/\\d+/data/items/${ providerIndex } `
310+ ) ;
304311
305312 const onChange = useCallback (
306313 ( config : OAuthSSOProviderConfig , secret : SSOProviderFormSecretViewModel ) =>
307314 setState ( ( state ) =>
308315 produce ( state , ( state ) => {
309- const existingIdx = state . providers . findIndex ( ( p ) =>
310- isOAuthSSOProvider ( p . config , providerType , alias , appType )
311- ) ;
312- if ( existingIdx === - 1 ) {
316+ if ( providerIndex === - 1 ) {
313317 state . providers . push ( {
314318 config,
315319 secret : {
@@ -319,7 +323,7 @@ export function useSingleSignOnConfigurationWidget(
319323 } ,
320324 } ) ;
321325 } else {
322- state . providers [ existingIdx ] = {
326+ state . providers [ providerIndex ] = {
323327 config,
324328 secret : {
325329 originalAlias : secret . originalAlias ,
@@ -330,7 +334,7 @@ export function useSingleSignOnConfigurationWidget(
330334 }
331335 } )
332336 ) ,
333- [ setState , providerType , alias , appType ]
337+ [ setState , providerIndex ]
334338 ) ;
335339
336340 return {
0 commit comments