@@ -36,7 +36,6 @@ import {
36
36
import { TrashIcon , InfoCircleIcon , PlusIcon } from "@webstudio-is/icons" ;
37
37
import { isFeatureEnabled } from "@webstudio-is/feature-flags" ;
38
38
import { humanizeString } from "~/shared/string-utils" ;
39
- import { serverSyncStore } from "~/shared/sync" ;
40
39
import {
41
40
$dataSources ,
42
41
$resources ,
@@ -55,10 +54,12 @@ import {
55
54
} from "~/builder/shared/code-editor-base" ;
56
55
import { parseCurl , type CurlRequest } from "./curl" ;
57
56
import {
58
- $selectedInstance ,
59
57
$selectedInstanceKey ,
58
+ $selectedInstancePath ,
60
59
$selectedPage ,
61
60
} from "~/shared/awareness" ;
61
+ import { updateWebstudioData } from "~/shared/instance-utils" ;
62
+ import { restoreTreeVariablesMutable } from "~/shared/data-variables" ;
62
63
63
64
const validateUrl = ( value : string , scope : Record < string , unknown > ) => {
64
65
const evaluatedValue = evaluateExpressionWithinScope ( value , scope ) ;
@@ -582,10 +583,11 @@ export const ResourceForm = forwardRef<
582
583
583
584
useImperativeHandle ( ref , ( ) => ( {
584
585
save : ( formData ) => {
585
- const instanceId = $selectedInstance . get ( ) ?. id ;
586
- if ( instanceId === undefined ) {
586
+ const instancePath = $selectedInstancePath . get ( ) ;
587
+ if ( instancePath === undefined ) {
587
588
return ;
588
589
}
590
+ const [ { instance } ] = instancePath ;
589
591
const name = z . string ( ) . parse ( formData . get ( "name" ) ) ;
590
592
const newResource : Resource = {
591
593
id : resource ?. id ?? nanoid ( ) ,
@@ -598,18 +600,16 @@ export const ResourceForm = forwardRef<
598
600
const newVariable : DataSource = {
599
601
id : variable ?. id ?? nanoid ( ) ,
600
602
// preserve existing instance scope when edit
601
- scopeInstanceId : variable ?. scopeInstanceId ?? instanceId ,
603
+ scopeInstanceId : variable ?. scopeInstanceId ?? instance . id ,
602
604
name,
603
605
type : "resource" ,
604
606
resourceId : newResource . id ,
605
607
} ;
606
- serverSyncStore . createTransaction (
607
- [ $dataSources , $resources ] ,
608
- ( dataSources , resources ) => {
609
- dataSources . set ( newVariable . id , newVariable ) ;
610
- resources . set ( newResource . id , newResource ) ;
611
- }
612
- ) ;
608
+ updateWebstudioData ( ( data ) => {
609
+ data . dataSources . set ( newVariable . id , newVariable ) ;
610
+ data . resources . set ( newResource . id , newResource ) ;
611
+ restoreTreeVariablesMutable ( { instancePath, ...data } ) ;
612
+ } ) ;
613
613
} ,
614
614
} ) ) ;
615
615
@@ -715,10 +715,11 @@ export const SystemResourceForm = forwardRef<
715
715
716
716
useImperativeHandle ( ref , ( ) => ( {
717
717
save : ( formData ) => {
718
- const instanceId = $selectedInstance . get ( ) ?. id ;
719
- if ( instanceId === undefined ) {
718
+ const instancePath = $selectedInstancePath . get ( ) ;
719
+ if ( instancePath === undefined ) {
720
720
return ;
721
721
}
722
+ const [ { instance } ] = instancePath ;
722
723
const name = z . string ( ) . parse ( formData . get ( "name" ) ) ;
723
724
const newResource : Resource = {
724
725
id : resource ?. id ?? nanoid ( ) ,
@@ -731,18 +732,16 @@ export const SystemResourceForm = forwardRef<
731
732
const newVariable : DataSource = {
732
733
id : variable ?. id ?? nanoid ( ) ,
733
734
// preserve existing instance scope when edit
734
- scopeInstanceId : variable ?. scopeInstanceId ?? instanceId ,
735
+ scopeInstanceId : variable ?. scopeInstanceId ?? instance . id ,
735
736
name,
736
737
type : "resource" ,
737
738
resourceId : newResource . id ,
738
739
} ;
739
- serverSyncStore . createTransaction (
740
- [ $dataSources , $resources ] ,
741
- ( dataSources , resources ) => {
742
- dataSources . set ( newVariable . id , newVariable ) ;
743
- resources . set ( newResource . id , newResource ) ;
744
- }
745
- ) ;
740
+ updateWebstudioData ( ( data ) => {
741
+ data . dataSources . set ( newVariable . id , newVariable ) ;
742
+ data . resources . set ( newResource . id , newResource ) ;
743
+ restoreTreeVariablesMutable ( { instancePath, ...data } ) ;
744
+ } ) ;
746
745
} ,
747
746
} ) ) ;
748
747
@@ -825,10 +824,11 @@ export const GraphqlResourceForm = forwardRef<
825
824
826
825
useImperativeHandle ( ref , ( ) => ( {
827
826
save : ( formData ) => {
828
- const instanceId = $selectedInstance . get ( ) ?. id ;
829
- if ( instanceId === undefined ) {
827
+ const instancePath = $selectedInstancePath . get ( ) ;
828
+ if ( instancePath === undefined ) {
830
829
return ;
831
830
}
831
+ const [ { instance } ] = instancePath ;
832
832
const name = z . string ( ) . parse ( formData . get ( "name" ) ) ;
833
833
const body = generateObjectExpression (
834
834
new Map ( [
@@ -848,18 +848,16 @@ export const GraphqlResourceForm = forwardRef<
848
848
const newVariable : DataSource = {
849
849
id : variable ?. id ?? nanoid ( ) ,
850
850
// preserve existing instance scope when edit
851
- scopeInstanceId : variable ?. scopeInstanceId ?? instanceId ,
851
+ scopeInstanceId : variable ?. scopeInstanceId ?? instance . id ,
852
852
name,
853
853
type : "resource" ,
854
854
resourceId : newResource . id ,
855
855
} ;
856
- serverSyncStore . createTransaction (
857
- [ $dataSources , $resources ] ,
858
- ( dataSources , resources ) => {
859
- dataSources . set ( newVariable . id , newVariable ) ;
860
- resources . set ( newResource . id , newResource ) ;
861
- }
862
- ) ;
856
+ updateWebstudioData ( ( data ) => {
857
+ data . dataSources . set ( newVariable . id , newVariable ) ;
858
+ data . resources . set ( newResource . id , newResource ) ;
859
+ restoreTreeVariablesMutable ( { instancePath, ...data } ) ;
860
+ } ) ;
863
861
} ,
864
862
} ) ) ;
865
863
0 commit comments