@@ -5,25 +5,7 @@ import { getAddOnStatus } from './add-ons'
55
66import type { Mode , SerializedOptions } from '@tanstack/cta-engine'
77
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'
279
2810// Options
2911
@@ -43,10 +25,6 @@ export const projectStarter = atom<StarterInfo | undefined>(undefined)
4325
4426// Addons
4527
46- export const codeRouterAddOns = atom < Array < AddOnInfo > > ( [ ] )
47-
48- export const fileRouterAddOns = atom < Array < AddOnInfo > > ( [ ] )
49-
5028export const customAddOns = atom < Array < AddOnInfo > > ( [ ] )
5129
5230export const availableAddOns = atom < Array < AddOnInfo > > ( ( get ) => {
@@ -102,21 +80,35 @@ export const selectedAddOns = atom<Array<AddOnInfo>>((get) =>
10280)
10381
10482export 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+ ] ,
10690 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 : [ ] ,
11196 }
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' ) {
113101 const outputReq = await fetch ( '/api/dry-run-create-app' , {
114102 method : 'POST' ,
115103 headers : {
116104 'Content-Type' : 'application/json' ,
117105 } ,
118106 body : JSON . stringify ( {
119- options,
107+ options : {
108+ ...get ( projectOptions ) ,
109+ chosenAddOns : addOns ,
110+ starter : get ( projectStarter ) ?. url ,
111+ } ,
120112 } ) ,
121113 } )
122114 return outputReq . json ( )
@@ -127,12 +119,12 @@ export const dryRunAtom = atomWithQuery((get) => ({
127119 'Content-Type' : 'application/json' ,
128120 } ,
129121 body : JSON . stringify ( {
130- addOns : get ( selectedAddOns ) . map ( ( addOn ) => addOn . id ) ,
122+ addOns,
131123 } ) ,
132124 } )
133125 return outputReq . json ( )
134126 } ,
135- initialData : {
127+ initialData : get ( initialPayloadAtom ) . data . output || {
136128 files : { } ,
137129 commands : [ ] ,
138130 deletedFiles : [ ] ,
@@ -185,31 +177,48 @@ export const removeStarter = atom(null, (get, set) => {
185177 set ( projectStarter , undefined )
186178} )
187179
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+ )
212223
213- if ( typeof window !== 'undefined' ) {
214- getDefaultStore ( ) . set ( loadInitialSetup )
215- }
224+ export const isInitialized = atom ( ( get ) => get ( initialPayloadAtom ) . isFetched )
0 commit comments