1- import { proxy , subscribe , useSnapshot } from 'valtio'
2- import { proxySet } from 'valtio/utils'
3- import { getDefaultData } from '@shared/default-data'
1+ import { proxy , subscribe } from 'valtio'
42import { API_URL , CDN_URL } from '@shared/defines'
53import {
6- type ElementConfigType ,
74 type Tool ,
85 type ProfileId ,
96 type ToolProfiles ,
@@ -13,7 +10,6 @@ import {
1310 PROFILE_A ,
1411} from '@shared/types'
1512import type { StepStatus } from '~/components/redesign/components/StepsIndicator'
16- import { APP_BASEPATH } from '~/lib/constants'
1713import { actions as bannerActions } from '~/stores/banner-store'
1814import { actions as offerwallActions } from '~/stores/offerwall-store'
1915import { actions as widgetActions } from '~/stores/widget-store'
@@ -29,63 +25,10 @@ const EXCLUDED_FROM_STORAGE = new Set<keyof typeof toolState>([
2925 'cdnUrl' ,
3026] )
3127
32- const STABLE_KEYS = [ 'version1' , 'version2' , 'version3' ] as const
33- const DEFAULT_VERSION_NAMES = [
34- 'Default preset 1' ,
35- 'Default preset 2' ,
36- 'Default preset 3' ,
37- ] as const
38-
39- export type StableKey = ( typeof STABLE_KEYS ) [ number ]
40-
41- interface SaveConfigResponse {
42- grantRequired ?: string
43- intent ?: string
44- error ?: string
45- [ key : string ] : unknown
46- }
47-
48- const createDefaultConfig = ( versionName : string ) : ElementConfigType => ( {
49- ...getDefaultData ( ) ,
50- versionName,
51- } )
52-
53- /** @deprecated */
54- export const createDefaultConfigs = ( ) : Record <
55- StableKey ,
56- ElementConfigType
57- > => {
58- return STABLE_KEYS . reduce (
59- ( configs , key , index ) => {
60- configs [ key ] = createDefaultConfig ( DEFAULT_VERSION_NAMES [ index ] )
61- return configs
62- } ,
63- { } as Record < StableKey , ElementConfigType > ,
64- )
65- }
66-
6728export const toolState = proxy ( {
68- configurations : createDefaultConfigs ( ) ,
69- /*
70- * savedConfigurations: baseline configs.
71- * tracks the configurations that are saved persistently,
72- * used to compare against local modifications.
73- */
74- savedConfigurations : createDefaultConfigs ( ) ,
75- /*
76- * dirtyProfiles: tracks the configurations that are modified locally.
77- */
78- dirtyProfiles : proxySet < StableKey > ( ) ,
79- /** @deprecated */
80- activeVersion : 'version1' as StableKey ,
8129 activeTab : PROFILE_A as ProfileId ,
8230 currentToolType : 'unknown' as Tool ,
8331
84- /** always returns the active configuration */
85- get currentConfig ( ) {
86- return this . configurations [ this . activeVersion ]
87- } ,
88-
8932 // UI state
9033 lastSaveAction : 'save-success' as 'save-success' | 'script' ,
9134
@@ -111,25 +54,7 @@ export const toolState = proxy({
11154 buildStep : 'unfilled' as StepStatus ,
11255} )
11356
114- subscribe ( toolState , ( ) => {
115- updateChangesTracking ( toolState . activeVersion )
116- } )
117-
118- export function useCurrentConfig ( options ?: {
119- sync : boolean
120- } ) : [ ElementConfigType , ElementConfigType ] {
121- // https://github.com/pmndrs/valtio/issues/132
122- const snapshot = useSnapshot ( toolState , options ) . currentConfig
123- return [ snapshot , toolState . currentConfig ]
124- }
125-
12657export const toolActions = {
127- get versionOptions ( ) {
128- return STABLE_KEYS . map ( ( key ) => ( {
129- stableKey : key ,
130- versionName : toolState . configurations [ key ] . versionName ,
131- } ) )
132- } ,
13358 setActiveTab ( profileId : ProfileId ) {
13459 toolState . activeTab = profileId
13560 } ,
@@ -174,31 +99,6 @@ export const toolActions = {
17499 actions . resetProfiles ( )
175100 }
176101 } ,
177- /** legacy backwards compatibility */
178- setConfigs : (
179- fullConfigObject : Record < StableKey , Partial < ElementConfigType > > | null ,
180- ) => {
181- const newFullConfig : Record < StableKey , ElementConfigType > =
182- createDefaultConfigs ( )
183-
184- STABLE_KEYS . forEach ( ( profileId ) => {
185- if ( ! fullConfigObject || ! fullConfigObject [ profileId ] ) {
186- return
187- }
188-
189- newFullConfig [ profileId ] = {
190- ...newFullConfig [ profileId ] ,
191- ...fullConfigObject [ profileId ] ,
192- }
193-
194- toolState . configurations [ profileId ] = { ...newFullConfig [ profileId ] }
195-
196- toolState . savedConfigurations [ profileId ] = { ...newFullConfig [ profileId ] }
197- } )
198-
199- toolState . dirtyProfiles . clear ( )
200- } ,
201-
202102 setCurrentToolType : ( toolType : Tool ) => {
203103 toolState . currentToolType = toolType
204104 } ,
@@ -238,108 +138,10 @@ export const toolActions = {
238138 setHasRemoteConfigs : ( hasRemoteConfigs : boolean ) => {
239139 toolState . hasRemoteConfigs = hasRemoteConfigs
240140 } ,
241-
242- /**
243- * Checks if any local changes have been made to the configurations.
244- */
245- hasCustomEdits : ( ) : boolean => toolState . dirtyProfiles . size > 0 ,
246- saveConfig : async ( ) => {
247- if ( ! toolState . walletAddress ) {
248- throw new Error ( 'Wallet address is missing' )
249- }
250-
251- toolState . isSubmitting = true
252- try {
253- const configToSave = {
254- ...toolState . currentConfig ,
255- walletAddress : toolState . walletAddress ,
256- }
257-
258- const formData = new FormData ( )
259-
260- Object . entries ( configToSave ) . forEach ( ( [ key , value ] ) => {
261- if ( value !== undefined && value !== null && key !== 'walletAddress' ) {
262- formData . append ( key , String ( value ) )
263- }
264- } )
265-
266- formData . append ( 'walletAddress' , toolState . walletAddress )
267- formData . append ( 'version' , toolState . activeVersion )
268-
269- const updatedFullConfig = {
270- ...toolState . configurations ,
271- [ toolState . activeVersion ] : configToSave ,
272- }
273-
274- formData . append ( 'fullconfig' , JSON . stringify ( updatedFullConfig ) )
275- formData . append ( 'intent' , 'update' )
276-
277- const baseUrl = location . origin + APP_BASEPATH
278- const url = new URL ( `${ baseUrl } /api/config/${ toolState . currentToolType } ` )
279- const response = await fetch ( url , {
280- method : 'PUT' ,
281- body : formData ,
282- } )
283- if ( ! response . ok ) {
284- const details = await response . json ( )
285- throw new Error ( `Save request failed with status: ${ response . status } ` , {
286- cause : { details } ,
287- } )
288- }
289-
290- const data = ( await response . json ( ) ) as SaveConfigResponse
291- if ( data ?. grantRequired ) {
292- return { success : false , data }
293- }
294-
295- STABLE_KEYS . forEach ( ( profileId ) => {
296- toolState . savedConfigurations [ profileId ] = {
297- ...toolState . configurations [ profileId ] ,
298- }
299- } )
300- toolState . dirtyProfiles . clear ( )
301-
302- return { success : true , data }
303- } catch ( error ) {
304- console . error ( 'Save error:' , error )
305- throw error
306- } finally {
307- toolState . isSubmitting = false
308- }
309- } ,
310-
311141 setGrantResponse : ( grantResponse : string , isGrantAccepted : boolean ) => {
312142 toolState . grantResponse = grantResponse
313143 toolState . isGrantAccepted = isGrantAccepted
314144 } ,
315-
316- handleTabSelect : ( profileId : StableKey ) => {
317- toolState . activeVersion = profileId
318- } ,
319-
320- handleVersionNameChange : ( newName : string ) => {
321- toolState . currentConfig . versionName = newName
322- } ,
323- }
324-
325- function isConfigModified ( profileId : StableKey ) : boolean {
326- const currentConfig = toolState . configurations [ profileId ]
327- const savedConfig = toolState . savedConfigurations [ profileId ]
328-
329- if ( ! currentConfig || ! savedConfig ) {
330- return false
331- }
332-
333- return JSON . stringify ( currentConfig ) !== JSON . stringify ( savedConfig )
334- }
335-
336- function updateChangesTracking ( profileId : StableKey ) {
337- const isModified = isConfigModified ( profileId )
338- if ( isModified ) {
339- toolState . dirtyProfiles . add ( profileId )
340- } else {
341- toolState . dirtyProfiles . delete ( profileId )
342- }
343145}
344146
345147/** Load from localStorage on init, remove storage if invalid */
@@ -377,21 +179,9 @@ export function persistState() {
377179}
378180
379181function createStorageState ( state : typeof toolState ) {
380- const omitted = omit ( state , EXCLUDED_FROM_STORAGE )
381-
382- return {
383- ...omitted ,
384- dirtyProfiles : Array . from ( state . dirtyProfiles ) ,
385- }
182+ return omit ( state , EXCLUDED_FROM_STORAGE )
386183}
387184
388185function parsedStorageData ( parsed : Record < string , unknown > ) {
389- const omitted = omit ( parsed , EXCLUDED_FROM_STORAGE )
390-
391- return {
392- ...omitted ,
393- dirtyProfiles : proxySet < StableKey > (
394- Array . isArray ( parsed . dirtyProfiles ) ? parsed . dirtyProfiles : [ ] ,
395- ) ,
396- }
186+ return omit ( parsed , EXCLUDED_FROM_STORAGE )
397187}
0 commit comments