@@ -75,7 +75,7 @@ export const parseResource = ({
75
75
formData,
76
76
} : {
77
77
id : string ;
78
- name : string ;
78
+ name ? : string ;
79
79
formData : FormData ;
80
80
} ) => {
81
81
const searchParamNames = formData . getAll ( "search-param-name" ) as string [ ] ;
@@ -84,7 +84,7 @@ export const parseResource = ({
84
84
const headerValues = formData . getAll ( "header-value" ) as string [ ] ;
85
85
return Resource . parse ( {
86
86
id,
87
- name,
87
+ name : name ?? formData . get ( "name" ) ,
88
88
url : formData . get ( "url" ) ,
89
89
searchParams : searchParamNames
90
90
. map ( ( name , index ) => ( { name, value : searchParamValues [ index ] } ) )
@@ -175,17 +175,19 @@ export const UrlField = ({
175
175
}
176
176
try {
177
177
const url = new URL ( value ) ;
178
- const searchParams : Resource [ "searchParams" ] = [ ] ;
179
- for ( const [ name , value ] of url . searchParams ) {
180
- searchParams . push ( { name, value : JSON . stringify ( value ) } ) ;
178
+ if ( url . searchParams . size > 0 ) {
179
+ const searchParams : Resource [ "searchParams" ] = [ ] ;
180
+ for ( const [ name , value ] of url . searchParams ) {
181
+ searchParams . push ( { name, value : JSON . stringify ( value ) } ) ;
182
+ }
183
+ // remove all search params from url
184
+ url . search = "" ;
185
+ // update text value as string literal
186
+ onChange ( JSON . stringify ( url . href ) , searchParams ) ;
187
+ return ;
181
188
}
182
- // remove all search params from url
183
- url . search = "" ;
184
- // update text value as string literal
185
- onChange ( JSON . stringify ( url . href ) , searchParams ) ;
186
- } catch {
187
- onChange ( JSON . stringify ( value ) ) ;
188
- }
189
+ } catch { }
190
+ onChange ( JSON . stringify ( value ) ) ;
189
191
} }
190
192
onBlur = { ( event ) => event . currentTarget . checkValidity ( ) }
191
193
onInvalid = { ( event ) =>
@@ -515,9 +517,9 @@ export const getResourceScopeForInstance = ({
515
517
}
516
518
if ( dataSource ) {
517
519
const name = encodeDataVariableId ( dataSourceId ) ;
518
- variableValues . set ( dataSourceId , value ) ;
519
520
scope [ name ] = value ;
520
521
aliases . set ( name , dataSource . name ) ;
522
+ variableValues . set ( dataSource . name , value ) ;
521
523
}
522
524
}
523
525
}
@@ -544,7 +546,7 @@ const getVariableInstanceKey = ({
544
546
return getInstanceKey ( instancePath [ 0 ] . instanceSelector ) ;
545
547
} ;
546
548
547
- const useScope = ( { variable } : { variable ?: DataSource } ) => {
549
+ export const useResourceScope = ( { variable } : { variable ?: DataSource } ) => {
548
550
return useStore (
549
551
useMemo (
550
552
( ) =>
@@ -561,22 +563,32 @@ const useScope = ({ variable }: { variable?: DataSource }) => {
561
563
variableValuesByInstanceSelector ,
562
564
dataSources
563
565
) => {
564
- const { scope, aliases } = getResourceScopeForInstance ( {
565
- page,
566
- instanceKey : getVariableInstanceKey ( {
567
- variable,
568
- instancePath,
569
- } ) ,
570
- dataSources,
571
- variableValuesByInstanceSelector,
572
- } ) ;
566
+ const { scope, aliases, variableValues } =
567
+ getResourceScopeForInstance ( {
568
+ page,
569
+ instanceKey : getVariableInstanceKey ( {
570
+ variable,
571
+ instancePath,
572
+ } ) ,
573
+ dataSources,
574
+ variableValuesByInstanceSelector,
575
+ } ) ;
573
576
// prevent showing currently edited variable in suggestions
574
577
// to avoid cirular dependeny
575
578
const newScope = { ...scope } ;
579
+ const newAliases = new Map ( aliases ) ;
580
+ const newVariableValues = new Map ( variableValues ) ;
576
581
if ( variable ) {
577
- delete newScope [ encodeDataVariableId ( variable . id ) ] ;
582
+ const key = encodeDataVariableId ( variable . id ) ;
583
+ delete newScope [ key ] ;
584
+ newAliases . delete ( key ) ;
585
+ newVariableValues . delete ( variable . name ) ;
578
586
}
579
- return { scope : newScope , aliases } ;
587
+ return {
588
+ scope : newScope ,
589
+ aliases : newAliases ,
590
+ variableValues : newVariableValues ,
591
+ } ;
580
592
}
581
593
) ,
582
594
[ variable ]
@@ -706,7 +718,7 @@ export const ResourceForm = forwardRef<
706
718
undefined | PanelApi ,
707
719
{ variable ?: DataSource }
708
720
> ( ( { variable } , ref ) => {
709
- const { scope, aliases } = useScope ( { variable } ) ;
721
+ const { scope, aliases } = useResourceScope ( { variable } ) ;
710
722
711
723
const resources = useStore ( $resources ) ;
712
724
const resource =
@@ -739,16 +751,14 @@ export const ResourceForm = forwardRef<
739
751
if ( scopeInstanceId === undefined ) {
740
752
return ;
741
753
}
742
- const name = z . string ( ) . parse ( formData . get ( "name" ) ) ;
743
754
const newResource = parseResource ( {
744
755
id : resource ?. id ?? nanoid ( ) ,
745
- name,
746
756
formData,
747
757
} ) ;
748
758
const newVariable : DataSource = {
749
759
id : variable ?. id ?? nanoid ( ) ,
750
760
scopeInstanceId,
751
- name,
761
+ name : newResource . name ,
752
762
type : "resource" ,
753
763
resourceId : newResource . id ,
754
764
} ;
@@ -942,7 +952,7 @@ export const GraphqlResourceForm = forwardRef<
942
952
undefined | PanelApi ,
943
953
{ variable ?: DataSource }
944
954
> ( ( { variable } , ref ) => {
945
- const { scope, aliases } = useScope ( { variable } ) ;
955
+ const { scope, aliases } = useResourceScope ( { variable } ) ;
946
956
947
957
const resources = useStore ( $resources ) ;
948
958
const resource =
0 commit comments