Skip to content

Commit de45273

Browse files
committed
Premium check in new modal file updated
1 parent 3d38d9f commit de45273

5 files changed

Lines changed: 52 additions & 17 deletions

File tree

screenshots/banner.png

-127 KB
Loading

src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3226,7 +3226,7 @@ const App = () => {
32263226
}}
32273227
onUpgradeClick={() => {
32283228
setShowEasyNotesSidebar(false);
3229-
setAboutOpen(true);
3229+
setLicenseOpen(true);
32303230
}}
32313231
/>
32323232
)

src/components/EasyNotesSidebar.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,12 @@ const EasyNotesSidebar: React.FC<EasyNotesSidebarProps> = ({
102102
// Load notes and provider status on component mount
103103
useEffect(() => {
104104
if (showEasyNotesSidebar) {
105-
console.log('[EasyNotesSidebar] Sidebar opened, checking for post-redirect authentication...');
105+
// Re-check license state when sidebar opens (in case it changed while
106+
// the sidebar was hidden, e.g. user activated license in LicenseModal)
107+
const currentLicenseState = LicenseManager.hasActiveLicense();
108+
console.log('[EasyNotesSidebar] Sidebar opened, license active:', currentLicenseState);
109+
setHasLicense(currentLicenseState);
110+
106111
checkPostRedirectAuth();
107112
loadNotesAndProviders();
108113
}

src/components/LicenseModal.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,16 @@ export function LicenseModal({ open, onClose, showToast }: LicenseModalProps) {
159159

160160
const handleSaveLicense = async () => {
161161
localStorage.setItem('easyeditor-user-name', name);
162+
console.log('[LicenseModal] Checking license for email:', email);
162163
await LicenseManager.setLicenseData(email, licenseKey);
163164
const valid = LicenseManager.hasActiveLicense();
165+
const licType = LicenseManager.getType();
166+
console.log('[LicenseModal] License check result: valid=', valid, ', type=', licType);
164167
setIsLicenseValid(valid);
165-
setType(LicenseManager.getType());
166-
if (!valid && showToast) {
168+
setType(licType);
169+
if (valid && showToast) {
170+
showToast(`License activated: ${licType || 'Premium'}`, 'success');
171+
} else if (!valid && showToast) {
167172
showToast(t('about.invalid_license') || 'Invalid license or email', 'error');
168173
}
169174
};

src/premium/LicenseManager.ts

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ class LicenseManager {
1212
private STORAGE_KEY_TYPE = 'easyeditor-user-type';
1313
private STORAGE_KEY_LICENSE_KEY = 'easyeditor-user-license-key';
1414

15-
private constructor() { }
15+
private constructor() {
16+
// Immediately restore cached license state so components get the right
17+
// value on their very first render (before the async API check completes).
18+
this.restoreFromCache();
19+
}
1620

1721
public static getInstance(): LicenseManager {
1822
if (!LicenseManager.instance) {
@@ -32,12 +36,22 @@ class LicenseManager {
3236
const cachedValid = localStorage.getItem(this.STORAGE_KEY_CACHED_LICENSE);
3337
const storedType = this.getStoredType();
3438

39+
const oldStatus = this.activeLicense;
40+
const oldType = this.type;
41+
3542
if (email && licenseKey && cachedValid === 'true') {
3643
this.activeLicense = true;
3744
this.type = storedType || '';
45+
console.log('[LicenseManager] Restored from cache: active=true, type=', this.type);
3846
} else {
3947
this.activeLicense = false;
4048
this.type = '';
49+
console.log('[LicenseManager] No cached license found (email:', !!email, ', key:', !!licenseKey, ', cached:', cachedValid, ')');
50+
}
51+
52+
// Notify listeners if state changed (so already-mounted components update)
53+
if (oldStatus !== this.activeLicense || oldType !== this.type) {
54+
this.notifyListeners();
4155
}
4256
}
4357

@@ -99,11 +113,14 @@ class LicenseManager {
99113
const email = this.getStoredEmail();
100114
const licenseKey = this.getStoredLicenseKey();
101115

116+
console.log('[LicenseManager] checkLicenseStatus: email=', !!email, ', key=', !!licenseKey);
117+
102118
if (!email || !licenseKey) {
103119
const oldStatus = this.activeLicense;
104120
this.activeLicense = false;
105121
this.type = '';
106122
localStorage.removeItem(this.STORAGE_KEY_CACHED_LICENSE);
123+
console.log('[LicenseManager] No email/key stored, license cleared');
107124
if (oldStatus !== this.activeLicense) {
108125
this.notifyListeners();
109126
}
@@ -135,6 +152,8 @@ class LicenseManager {
135152
const data = await response.json();
136153
const isLicenseActive = data.hasActiveLicense === true || data.hasActiveLicense === 'True';
137154

155+
console.log('[LicenseManager] API response: active=', isLicenseActive, ', email match=', data.email === email, ', key match=', data.linkedUserId === licenseKey);
156+
138157
// Match user's email and licenseKey against the returned linkedUserId
139158
if (isLicenseActive && data.email === email && data.linkedUserId === licenseKey) {
140159
this.activeLicense = true;
@@ -153,33 +172,39 @@ class LicenseManager {
153172
if (data.purchaseDate) {
154173
localStorage.setItem(this.STORAGE_KEY_DATE, data.purchaseDate.toString());
155174
}
175+
console.log('[LicenseManager] License validated: type=', this.type);
156176
} else {
157177
this.activeLicense = false;
158178
this.type = '';
159179
localStorage.removeItem(this.STORAGE_KEY_CACHED_LICENSE);
180+
console.log('[LicenseManager] License validation failed');
160181
}
161182
} else {
162183
this.activeLicense = false;
163184
this.type = '';
164185
localStorage.removeItem(this.STORAGE_KEY_CACHED_LICENSE);
186+
console.error('[LicenseManager] API returned non-ok status:', response.status);
165187
}
166188

167189
if (oldStatus !== this.activeLicense || oldType !== this.type) {
168190
this.notifyListeners();
169191
}
170192
} catch (error) {
171-
console.error('Error checking license status:', error);
172-
const oldStatus = this.activeLicense;
173-
this.activeLicense = false;
174-
localStorage.removeItem(this.STORAGE_KEY_CACHED_LICENSE);
175-
// We keep the plan locally if we fail to check? Better to clear it to be safe or keep it cached?
176-
// For safety/validity, if check fails, we assume no license.
177-
// But maybe we should keep the cached values if network error?
178-
// Current implementation clears it. I'll stick to that.
179-
this.type = '';
180-
181-
if (oldStatus !== this.activeLicense) {
182-
this.notifyListeners();
193+
console.error('[LicenseManager] Error checking license status:', error);
194+
// On network failure, preserve the cached license state rather than
195+
// invalidating a genuine license just because the user is offline.
196+
const cachedValid = localStorage.getItem(this.STORAGE_KEY_CACHED_LICENSE);
197+
if (cachedValid === 'true') {
198+
console.log('[LicenseManager] Network error but cached license exists — keeping cached state');
199+
// Don't clear the license; keep the cached state so offline users
200+
// can still use premium features until the next successful check.
201+
} else {
202+
const oldStatus = this.activeLicense;
203+
this.activeLicense = false;
204+
this.type = '';
205+
if (oldStatus !== this.activeLicense) {
206+
this.notifyListeners();
207+
}
183208
}
184209
}
185210
}

0 commit comments

Comments
 (0)