Skip to content

Commit 0701c6d

Browse files
authored
chore(frontend): cleanup deprecated tool store logic (#613)
1 parent 287a4b8 commit 0701c6d

File tree

4 files changed

+15
-223
lines changed

4 files changed

+15
-223
lines changed

frontend/app/components/redesign/components/BuilderBackground.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import React from 'react'
22
import { cx } from 'class-variance-authority'
3-
import { useSnapshot } from 'valtio'
43
import { ToolsSecondaryButton } from '@/components/ToolsSecondaryButton'
5-
import { SLIDE_ANIMATION } from '@shared/types'
6-
import { toolState } from '~/stores/toolStore'
74

85
const DOT_PATTERN_SVG = `<svg width="12" height="12" viewBox="0 0 12 12" fill="none" xmlns="http://www.w3.org/2000/svg"><circle cx="6" cy="6" r="2" fill="white" fill-opacity="0.5" /></svg>`
96

@@ -19,17 +16,15 @@ interface BuilderBackgroundProps {
1916
className?: string
2017
children?: React.ReactNode
2118
onPreviewClick?: () => void
19+
isAnimationDisabled?: boolean
2220
}
2321

2422
export const BuilderBackground: React.FC<BuilderBackgroundProps> = ({
2523
className = '',
2624
children,
2725
onPreviewClick,
26+
isAnimationDisabled = false,
2827
}) => {
29-
const snap = useSnapshot(toolState)
30-
const isAnimationDisabled =
31-
snap.currentConfig.bannerSlideAnimation === SLIDE_ANIMATION.None
32-
3328
const createDotPattern = () => {
3429
return `data:image/svg+xml;base64,${btoa(DOT_PATTERN_SVG)}`
3530
}

frontend/app/components/redesign/components/dialogs/ScriptDialog.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,14 @@ function getScriptAttributes(snapshot: {
9898
walletAddress: string
9999
walletAddressId: string
100100
currentToolType: string
101-
activeVersion: string
101+
activeTab: string
102102
cdnUrl: string
103103
}): ScriptAttribute[] {
104104
const {
105105
walletAddress,
106106
walletAddressId,
107107
currentToolType: tool,
108-
activeVersion: profileId,
108+
activeTab: profileId,
109109
cdnUrl,
110110
} = snapshot
111111

frontend/app/routes/banner.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
MobileStepsIndicator,
1919
BuilderProfileTabs,
2020
} from '@/components'
21+
import { SLIDE_ANIMATION } from '@shared/types'
2122
import { BannerBuilder } from '~/components/banner/BannerBuilder'
2223
import {
2324
BannerPreview,
@@ -35,6 +36,7 @@ import {
3536
hydrateSnapshotsFromStorage,
3637
subscribeProfilesToStorage,
3738
subscribeProfilesToUpdates,
39+
useBannerProfile,
3840
} from '~/stores/banner-store'
3941
import {
4042
toolState,
@@ -85,6 +87,7 @@ export async function loader({ request, context }: LoaderFunctionArgs) {
8587
export default function Banner() {
8688
const snap = useSnapshot(toolState)
8789
const bannerSnap = useSnapshot(banner)
90+
const [profile] = useBannerProfile()
8891
const navigate = useNavigate()
8992
const uiActions = useUIActions()
9093
const { save, saveLastAction } = useSaveProfile()
@@ -141,6 +144,7 @@ export default function Banner() {
141144
}
142145
}
143146

147+
const isAnimationDisabled = profile.animation.type === SLIDE_ANIMATION.None
144148
return (
145149
<div className="bg-interface-bg-main w-full">
146150
<div className="flex flex-col items-center pt-[60px] md:pt-3xl">
@@ -260,7 +264,10 @@ export default function Banner() {
260264
id="preview"
261265
className="w-full mx-auto xl:mx-0 xl:sticky xl:top-md xl:self-start xl:flex-shrink-0 xl:w-[504px] h-fit"
262266
>
263-
<BuilderBackground onPreviewClick={handlePreviewClick}>
267+
<BuilderBackground
268+
onPreviewClick={handlePreviewClick}
269+
isAnimationDisabled={isAnimationDisabled}
270+
>
264271
<BannerPreview ref={bannerRef} cdnUrl={snap.cdnUrl} />
265272
</BuilderBackground>
266273
</div>

frontend/app/stores/toolStore.ts

Lines changed: 3 additions & 213 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
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'
42
import { API_URL, CDN_URL } from '@shared/defines'
53
import {
6-
type ElementConfigType,
74
type Tool,
85
type ProfileId,
96
type ToolProfiles,
@@ -13,7 +10,6 @@ import {
1310
PROFILE_A,
1411
} from '@shared/types'
1512
import type { StepStatus } from '~/components/redesign/components/StepsIndicator'
16-
import { APP_BASEPATH } from '~/lib/constants'
1713
import { actions as bannerActions } from '~/stores/banner-store'
1814
import { actions as offerwallActions } from '~/stores/offerwall-store'
1915
import { 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-
6728
export 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-
12657
export 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

379181
function 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

388185
function 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

Comments
 (0)