Skip to content

Commit cf39a5d

Browse files
committed
fix: resolve Firebase persistence and JSON parsing errors
- Fix JSON.parse() errors when reading legacy localStorage values - Added try-catch blocks in db.js local.get() to handle non-JSON strings - Gracefully falls back to raw string values for legacy data - Fix Firebase persistence initialization error - Reorganized getDb() to call enablePersistence() on the db instance - Improved error handling to gracefully continue without persistence if needed - Updated surveyService.js to use centralized window.db.getDb() - Prevents 'persistence can no longer be enabled' errors - Update @zenuml/core to v3.43.2
1 parent d599985 commit cf39a5d

File tree

4 files changed

+42
-23
lines changed

4 files changed

+42
-23
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
"@radix-ui/react-radio-group": "^1.1.3",
8383
"@radix-ui/react-select": "^2.0.0",
8484
"@radix-ui/react-tooltip": "^1.0.7",
85-
"@zenuml/core": "^3.43.0",
85+
"@zenuml/core": "^3.43.2",
8686
"clsx": "^2.0.0",
8787
"code-blast-codemirror": "chinchang/code-blast-codemirror#web-maker",
8888
"codemirror": "^5.65.16",

pnpm-lock.yaml

Lines changed: 9 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/db.js

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,26 @@ import { log } from './utils';
1515
get: (obj, cb) => {
1616
const retVal = {};
1717
if (typeof obj === 'string') {
18-
retVal[obj] = JSON.parse(window.localStorage.getItem(obj));
18+
try {
19+
retVal[obj] = JSON.parse(window.localStorage.getItem(obj));
20+
} catch (e) {
21+
// Handle non-JSON values (legacy data stored as plain strings)
22+
retVal[obj] = window.localStorage.getItem(obj);
23+
}
1924
setTimeout(() => cb(retVal), FAUX_DELAY);
2025
} else {
2126
Object.keys(obj).forEach((key) => {
2227
const val = window.localStorage.getItem(key);
23-
retVal[key] =
24-
val === undefined || val === null ? obj[key] : JSON.parse(val);
28+
if (val === undefined || val === null) {
29+
retVal[key] = obj[key];
30+
} else {
31+
try {
32+
retVal[key] = JSON.parse(val);
33+
} catch (e) {
34+
// Handle non-JSON values (legacy data stored as plain strings)
35+
retVal[key] = val;
36+
}
37+
}
2538
});
2639
setTimeout(() => cb(retVal), FAUX_DELAY);
2740
}
@@ -55,32 +68,34 @@ import { log } from './utils';
5568
if (db) {
5669
return resolve(db);
5770
}
58-
const store = firebase.firestore();
59-
return store
60-
.enablePersistence({ synchronizeTabs: true })
71+
// Initialize Cloud Firestore through firebase
72+
db = firebase.firestore();
73+
74+
// Try to enable persistence, but don't fail if it's already been called
75+
db.enablePersistence({ synchronizeTabs: true })
6176
.then(function () {
62-
// Initialize Cloud Firestore through firebase
63-
db = firebase.firestore();
64-
// const settings = {
65-
// timestampsInSnapshots: true
66-
// };
67-
// db.settings(settings);
68-
log('firebase db ready', db);
77+
log('firebase db ready with persistence', db);
6978
resolve(db);
7079
})
7180
.catch(function (err) {
72-
reject(err.code);
7381
if (err.code === 'failed-precondition') {
7482
// Multiple tabs open, persistence can only be enabled
7583
// in one tab at a a time.
7684
alert(
7785
"Opening ZenUML web app in multiple tabs isn't supported at present and it seems like you already have it opened in another tab. Please use in one tab.",
7886
);
7987
trackEvent('fn', 'multiTabError');
88+
reject(err.code);
8089
} else if (err.code === 'unimplemented') {
8190
// The current browser does not support all of the
8291
// features required to enable persistence
83-
// ...
92+
log('Persistence not supported, continuing without it');
93+
resolve(db);
94+
} else {
95+
// If persistence was already enabled (e.g., by another call),
96+
// just continue without it
97+
log('Could not enable persistence, continuing without it:', err.message);
98+
resolve(db);
8499
}
85100
});
86101
});

src/services/surveyService.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const COLLECTION_NAME = 'feature_surveys';
1313
*/
1414
export async function saveSurveyResponse(responseData) {
1515
try {
16-
const db = firebase.firestore();
16+
const db = await window.db.getDb();
1717
const user = firebase.auth().currentUser;
1818

1919
const surveyData = {
@@ -142,7 +142,7 @@ export async function syncFallbackSurveys() {
142142

143143
if (fallbackData.length === 0) return;
144144

145-
const db = firebase.firestore();
145+
const db = await window.db.getDb();
146146
const batch = db.batch();
147147

148148
fallbackData.forEach((surveyData) => {

0 commit comments

Comments
 (0)