@@ -5,25 +5,7 @@ import { getAddOnStatus } from './add-ons'
5
5
6
6
import type { Mode , SerializedOptions } from '@tanstack/cta-engine'
7
7
8
- import type {
9
- AddOnInfo ,
10
- DryRunOutput ,
11
- ProjectFiles ,
12
- StarterInfo ,
13
- } from '@/types.js'
14
-
15
- export const isInitialized = atom ( false )
16
-
17
- export const projectFiles = atom < ProjectFiles > ( {
18
- originalOutput : {
19
- files : { } ,
20
- commands : [ ] ,
21
- } ,
22
- } )
23
-
24
- export const projectLocalFiles = atom < Record < string , string > > ( { } )
25
-
26
- export const applicationMode = atom < 'add' | 'setup' > ( 'add' )
8
+ import type { AddOnInfo , ProjectFiles , StarterInfo } from '@/types.js'
27
9
28
10
// Options
29
11
@@ -43,10 +25,6 @@ export const projectStarter = atom<StarterInfo | undefined>(undefined)
43
25
44
26
// Addons
45
27
46
- export const codeRouterAddOns = atom < Array < AddOnInfo > > ( [ ] )
47
-
48
- export const fileRouterAddOns = atom < Array < AddOnInfo > > ( [ ] )
49
-
50
28
export const customAddOns = atom < Array < AddOnInfo > > ( [ ] )
51
29
52
30
export const availableAddOns = atom < Array < AddOnInfo > > ( ( get ) => {
@@ -102,21 +80,35 @@ export const selectedAddOns = atom<Array<AddOnInfo>>((get) =>
102
80
)
103
81
104
82
export const dryRunAtom = atomWithQuery ( ( get ) => ( {
105
- queryKey : [ 'dry-run' , get ( projectOptions ) , get ( selectedAddOns ) ] ,
83
+ queryKey : [
84
+ 'dry-run' ,
85
+ get ( applicationMode ) ,
86
+ JSON . stringify ( get ( projectOptions ) ) ,
87
+ JSON . stringify ( get ( selectedAddOns ) . map ( ( addOn ) => addOn . id ) ) ,
88
+ get ( projectStarter ) ?. url ,
89
+ ] ,
106
90
queryFn : async ( ) => {
107
- if ( get ( applicationMode ) === 'setup' ) {
108
- const options = {
109
- ...get ( projectOptions ) ,
110
- starter : get ( projectStarter ) ?. url || undefined ,
91
+ if ( get ( applicationMode ) === 'none' ) {
92
+ return {
93
+ files : { } ,
94
+ commands : [ ] ,
95
+ deletedFiles : [ ] ,
111
96
}
112
- options . chosenAddOns = get ( selectedAddOns ) . map ( ( addOn ) => addOn . id )
97
+ }
98
+
99
+ const addOns = get ( selectedAddOns ) . map ( ( addOn ) => addOn . id )
100
+ if ( get ( applicationMode ) === 'setup' ) {
113
101
const outputReq = await fetch ( '/api/dry-run-create-app' , {
114
102
method : 'POST' ,
115
103
headers : {
116
104
'Content-Type' : 'application/json' ,
117
105
} ,
118
106
body : JSON . stringify ( {
119
- options,
107
+ options : {
108
+ ...get ( projectOptions ) ,
109
+ chosenAddOns : addOns ,
110
+ starter : get ( projectStarter ) ?. url ,
111
+ } ,
120
112
} ) ,
121
113
} )
122
114
return outputReq . json ( )
@@ -127,12 +119,12 @@ export const dryRunAtom = atomWithQuery((get) => ({
127
119
'Content-Type' : 'application/json' ,
128
120
} ,
129
121
body : JSON . stringify ( {
130
- addOns : get ( selectedAddOns ) . map ( ( addOn ) => addOn . id ) ,
122
+ addOns,
131
123
} ) ,
132
124
} )
133
125
return outputReq . json ( )
134
126
} ,
135
- initialData : {
127
+ initialData : get ( initialPayloadAtom ) . data . output || {
136
128
files : { } ,
137
129
commands : [ ] ,
138
130
deletedFiles : [ ] ,
@@ -185,31 +177,48 @@ export const removeStarter = atom(null, (get, set) => {
185
177
set ( projectStarter , undefined )
186
178
} )
187
179
188
- export const loadInitialSetup = atom ( null , async ( get , set ) => {
189
- console . log ( 'write' )
190
-
191
- const payloadReq = await fetch ( '/api/initial-payload' )
192
- const {
193
- addOns,
194
- localFiles,
195
- options,
196
- output,
197
- applicationMode : appMode ,
198
- } = await payloadReq . json ( )
199
-
200
- set ( applicationMode , appMode )
201
- set ( codeRouterAddOns , addOns [ 'code-router' ] )
202
- set ( fileRouterAddOns , addOns [ 'file-router' ] )
203
- set ( projectFiles , {
204
- originalOutput : output ,
205
- } )
206
- set ( projectOptions , options )
207
- set ( originalSelectedAddOns , options . chosenAddOns )
208
- set ( projectLocalFiles , localFiles )
209
-
210
- set ( isInitialized , true )
211
- } )
180
+ export const initialPayloadAtom = atomWithQuery ( ( ) => ( {
181
+ queryKey : [ 'initial-payload' ] ,
182
+ queryFn : async ( ) => {
183
+ const payloadReq = await fetch ( '/api/initial-payload' )
184
+ const data = await payloadReq . json ( )
185
+ getDefaultStore ( ) . set ( projectOptions , data . options )
186
+ getDefaultStore ( ) . set ( originalSelectedAddOns , data . options . chosenAddOns )
187
+ return data
188
+ } ,
189
+ initialData : {
190
+ addOns : {
191
+ 'code-router' : [ ] as Array < AddOnInfo > ,
192
+ 'file-router' : [ ] as Array < AddOnInfo > ,
193
+ } ,
194
+ localFiles : { } as Record < string , string > ,
195
+ options : { } as SerializedOptions ,
196
+ output : {
197
+ files : { } ,
198
+ commands : [ ] ,
199
+ } as ProjectFiles ,
200
+ applicationMode : 'none' as 'add' | 'setup' | 'none' ,
201
+ } ,
202
+ } ) )
203
+
204
+ export const codeRouterAddOns = atom < Array < AddOnInfo > > (
205
+ ( get ) => get ( initialPayloadAtom ) . data . addOns [ 'code-router' ] || [ ] ,
206
+ )
207
+
208
+ export const fileRouterAddOns = atom < Array < AddOnInfo > > (
209
+ ( get ) => get ( initialPayloadAtom ) . data . addOns [ 'file-router' ] || [ ] ,
210
+ )
211
+
212
+ export const applicationMode = atom < 'add' | 'setup' | 'none' > (
213
+ ( get ) => get ( initialPayloadAtom ) . data . applicationMode || 'none' ,
214
+ )
215
+
216
+ export const projectLocalFiles = atom < Record < string , string > > (
217
+ ( get ) => get ( initialPayloadAtom ) . data . localFiles || { } ,
218
+ )
219
+
220
+ export const projectFiles = atom < ProjectFiles > (
221
+ ( get ) => get ( initialPayloadAtom ) . data . output ,
222
+ )
212
223
213
- if ( typeof window !== 'undefined' ) {
214
- getDefaultStore ( ) . set ( loadInitialSetup )
215
- }
224
+ export const isInitialized = atom ( ( get ) => get ( initialPayloadAtom ) . isFetched )
0 commit comments