Skip to content

Commit 36a5782

Browse files
committed
Rebuilt storage system to centralize logic and error tracking
1 parent 3093eab commit 36a5782

3 files changed

Lines changed: 266 additions & 57 deletions

File tree

src/App.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class ui extends Element {
3232
if (msg.type == 'start-direct-countdown') {
3333
this.handleDirectCountdown(msg.seconds, msg.commandName)
3434
}
35+
36+
if (msg.type == 'tracking-event') {
37+
Tracking.track(msg.event, msg.properties)
38+
}
3539
})
3640

3741
Router.setup({

src/Core.js

Lines changed: 67 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Tracking from 'src/utils/Tracking'
2+
import Storage from 'src/utils/Storage'
23
import { shouldShowCountdown, getCountdownSeconds, setCachedLicenseStatus } from 'src/payments/gate'
34
import { validateLicenseOnly } from 'src/payments/license'
45
import {
@@ -12,6 +13,18 @@ import {
1213

1314
const UI_HEIGHT = 540
1415

16+
// Initialize Storage with valid keys
17+
const STORAGE_KEYS = {
18+
UUID: 'UUID',
19+
PREFERENCES: 'preferences',
20+
AD_LAST_SHOWN_DATE: 'AD_LAST_SHOWN_DATE',
21+
AD_LAST_SHOWN_IMPRESSION: 'AD_LAST_SHOWN_IMPRESSION',
22+
LICENSE_V1: 'LICENSE_V1',
23+
SPACING: 'spacing' // legacy key
24+
}
25+
26+
Storage.init(STORAGE_KEYS)
27+
1528
const cmd = figma.command
1629
figma.showUI(__html__, { visible: false })
1730

@@ -121,11 +134,15 @@ function validateStoredLicense(license) {
121134
console.warn('[Core] License is invalid according to API:', result.error)
122135

123136
// Unlink invalid license
124-
return figma.clientStorage.setAsync('LICENSE_V1', null)
125-
.then(() => {
126-
setCachedLicenseStatus(null)
127-
// Notify user with clear message
128-
figma.notify(`License unlinked: ${result.error}. Please reactivate in the License tab.`)
137+
return Storage.set(Storage.getKey('LICENSE_V1'), null)
138+
.then((success) => {
139+
if (success) {
140+
setCachedLicenseStatus(null)
141+
// Notify user with clear message
142+
figma.notify(`License unlinked: ${result.error}. Please reactivate in the License tab.`)
143+
} else {
144+
figma.notify('Failed to unlink license. Please try again.')
145+
}
129146
})
130147
} else if (result.isNetworkError) {
131148
console.warn('[Core] Network error during license validation, keeping license active:', result.error)
@@ -145,20 +162,20 @@ function validateStoredLicense(license) {
145162
}
146163

147164
// Obtain UUID, preferences, and license then trigger init event
148-
Promise.all([
149-
figma.clientStorage.getAsync('UUID'),
150-
figma.clientStorage.getAsync('preferences'),
151-
figma.clientStorage.getAsync('AD_LAST_SHOWN_DATE'),
152-
figma.clientStorage.getAsync('AD_LAST_SHOWN_IMPRESSION'),
153-
figma.clientStorage.getAsync('spacing'), // legacy
154-
figma.clientStorage.getAsync('LICENSE_V1') // license data
155-
]).then(values => {
156-
let UUID = values[0]
157-
let preferences = values[1]
158-
let AD_LAST_SHOWN_DATE = values[2] || 572083200 // initial date, if no date was saved previously
159-
let AD_LAST_SHOWN_IMPRESSION = values[3] || 0 // initial impressions
160-
let spacing = values[4]
161-
let license = values[5] // license data
165+
Storage.getMultiple([
166+
Storage.getKey('UUID'),
167+
Storage.getKey('PREFERENCES'),
168+
Storage.getKey('AD_LAST_SHOWN_DATE'),
169+
Storage.getKey('AD_LAST_SHOWN_IMPRESSION'),
170+
Storage.getKey('SPACING'), // legacy
171+
Storage.getKey('LICENSE_V1') // license data
172+
]).then(storageData => {
173+
let UUID = storageData[Storage.getKey('UUID')]
174+
let preferences = storageData[Storage.getKey('PREFERENCES')]
175+
let AD_LAST_SHOWN_DATE = storageData[Storage.getKey('AD_LAST_SHOWN_DATE')] || 572083200 // initial date, if no date was saved previously
176+
let AD_LAST_SHOWN_IMPRESSION = storageData[Storage.getKey('AD_LAST_SHOWN_IMPRESSION')] || 0 // initial impressions
177+
let spacing = storageData[Storage.getKey('SPACING')]
178+
let license = storageData[Storage.getKey('LICENSE_V1')] // license data
162179

163180
let SPACING = { x: 100, y: 200 }
164181
let START_NAME = '000'
@@ -178,7 +195,7 @@ Promise.all([
178195

179196
if (!UUID) {
180197
UUID = Tracking.createUUID()
181-
figma.clientStorage.setAsync('UUID', UUID)
198+
Storage.set(Storage.getKey('UUID'), UUID)
182199
}
183200

184201
// Cache license status for gate decisions
@@ -279,51 +296,44 @@ Promise.all([
279296
} else
280297
if (msg.type === 'preferences') {
281298
preferences = msg.preferences
282-
figma.clientStorage.setAsync('preferences', preferences)
283-
figma.notify('Preferences saved')
299+
Storage.set(Storage.getKey('PREFERENCES'), preferences).then((success) => {
300+
if (success) {
301+
figma.notify('Preferences saved')
302+
} else {
303+
figma.notify('Failed to save preferences. Please try again.')
304+
}
305+
})
284306
} else
285307
if (msg.type === 'displayImpression') {
286308
figma.ui.resize(320, 540+124)
287-
figma.clientStorage.setAsync('AD_LAST_SHOWN_DATE', Date.now())
288-
figma.clientStorage.setAsync('AD_LAST_SHOWN_IMPRESSION', parseInt(AD_LAST_SHOWN_IMPRESSION)+1)
309+
Storage.setMultiple({
310+
[Storage.getKey('AD_LAST_SHOWN_DATE')]: Date.now(),
311+
[Storage.getKey('AD_LAST_SHOWN_IMPRESSION')]: parseInt(AD_LAST_SHOWN_IMPRESSION)+1
312+
})
289313
}
290314

291315
if (msg.type === 'resetImpression') {
292-
figma.clientStorage.setAsync('AD_LAST_SHOWN_IMPRESSION', 0)
316+
Storage.set(Storage.getKey('AD_LAST_SHOWN_IMPRESSION'), 0)
293317
} else
294318
if (msg.type === 'get-license') {
295319
// Return stored license data to UI
296-
figma.clientStorage.getAsync('LICENSE_V1')
320+
Storage.get(Storage.getKey('LICENSE_V1'))
297321
.then(license => {
298322
figma.ui.postMessage({
299323
type: 'license-data',
300324
license: license || null
301325
})
302326
})
303-
.catch(error => {
304-
console.error('[Core] Failed to retrieve license:', error)
305-
figma.ui.postMessage({
306-
type: 'license-data',
307-
license: null
308-
})
309-
})
310327
} else
311328
if (msg.type === 'get-license-for-gate') {
312329
// Return license data specifically for gate cache updates
313-
figma.clientStorage.getAsync('LICENSE_V1')
330+
Storage.get(Storage.getKey('LICENSE_V1'))
314331
.then(license => {
315332
figma.ui.postMessage({
316333
type: 'license-data-for-gate',
317334
license: license || null
318335
})
319336
})
320-
.catch(error => {
321-
console.error('[Core] Failed to retrieve license for gate:', error)
322-
figma.ui.postMessage({
323-
type: 'license-data-for-gate',
324-
license: null
325-
})
326-
})
327337
} else
328338
if (msg.type === 'activate-license') {
329339
// Store license data from UI
@@ -338,26 +348,26 @@ Promise.all([
338348
uses: msg.uses || 1 // Include usage count from activation
339349
}
340350

341-
figma.clientStorage.setAsync('LICENSE_V1', licenseData)
342-
.then(() => {
343-
setCachedLicenseStatus(licenseData) // Update cache
344-
figma.notify('You now have Super Tidy Pro')
345-
})
346-
.catch(error => {
347-
console.error('[Core] Failed to store license data:', error)
348-
figma.notify('Failed to save license. Please try again.')
351+
Storage.set(Storage.getKey('LICENSE_V1'), licenseData)
352+
.then((success) => {
353+
if (success) {
354+
setCachedLicenseStatus(licenseData) // Update cache
355+
figma.notify('You now have Super Tidy Pro')
356+
} else {
357+
figma.notify('Failed to save license. Please try again.')
358+
}
349359
})
350360
} else
351361
if (msg.type === 'remove-license') {
352362
// Remove stored license
353-
figma.clientStorage.setAsync('LICENSE_V1', null)
354-
.then(() => {
355-
setCachedLicenseStatus(null) // Update cache
356-
figma.notify('License unlinked from this device')
357-
})
358-
.catch(error => {
359-
console.error('[Core] Failed to remove license:', error)
360-
figma.notify('Failed to unlink license. Please try again.')
363+
Storage.remove(Storage.getKey('LICENSE_V1'))
364+
.then((success) => {
365+
if (success) {
366+
setCachedLicenseStatus(null) // Update cache
367+
figma.notify('License unlinked from this device')
368+
} else {
369+
figma.notify('Failed to unlink license. Please try again.')
370+
}
361371
})
362372
}
363373
}

0 commit comments

Comments
 (0)