mirror of
https://github.com/chinchang/web-maker.git
synced 2025-05-15 06:45:29 +02:00
post lastSeenVersion to remote db
This commit is contained in:
parent
b4aaec81eb
commit
bce45f9da2
93
src/db.js
93
src/db.js
@ -4,6 +4,33 @@
|
||||
var db;
|
||||
var dbPromise;
|
||||
|
||||
var local = {
|
||||
get: (obj, cb) => {
|
||||
const retVal = {};
|
||||
if (typeof obj === 'string') {
|
||||
retVal[obj] = JSON.parse(window.localStorage.getItem(obj));
|
||||
setTimeout(() => cb(retVal), FAUX_DELAY);
|
||||
} else {
|
||||
Object.keys(obj).forEach(key => {
|
||||
const val = window.localStorage.getItem(key);
|
||||
retVal[key] =
|
||||
val === undefined || val === null ? obj[key] : JSON.parse(val);
|
||||
});
|
||||
setTimeout(() => cb(retVal), FAUX_DELAY);
|
||||
}
|
||||
},
|
||||
set: (obj, cb) => {
|
||||
Object.keys(obj).forEach(key => {
|
||||
window.localStorage.setItem(key, JSON.stringify(obj[key]));
|
||||
});
|
||||
setTimeout(() => {
|
||||
if (cb) {
|
||||
return cb();
|
||||
}
|
||||
}, FAUX_DELAY);
|
||||
}
|
||||
};
|
||||
|
||||
async function getDb() {
|
||||
if (dbPromise) {
|
||||
return dbPromise;
|
||||
@ -38,6 +65,44 @@
|
||||
return dbPromise;
|
||||
}
|
||||
|
||||
async function getUserLastSeenVersion() {
|
||||
const d = deferred();
|
||||
if (window.IS_EXTENSION) {
|
||||
chrome.storage.sync.get(
|
||||
{
|
||||
lastSeenVersion: ''
|
||||
},
|
||||
function syncGetCallback(result) {
|
||||
d.resolve(result.lastSeenVersion);
|
||||
}
|
||||
);
|
||||
}
|
||||
local.get('lastSeenVersion', result => {
|
||||
d.resolve(result.lastSeenVersion);
|
||||
});
|
||||
// Might consider getting actual value from remote db.
|
||||
// Not critical right now.
|
||||
}
|
||||
|
||||
async function setUserLastSeenVersion(user, version) {
|
||||
if (window.IS_EXTENSION) {
|
||||
chrome.storage.sync.set(
|
||||
{
|
||||
lastSeenVersion: version
|
||||
},
|
||||
function() {}
|
||||
);
|
||||
return;
|
||||
}
|
||||
// Settings the lastSeenVersion in localStorage also because next time we need
|
||||
// to fetch it irrespective of the user being logged in or out
|
||||
local.set({ lastSeenVersion: version });
|
||||
if (user) {
|
||||
const remoteDb = await getDb();
|
||||
remoteDb.doc(`users/${user.uid}`).update({ lastSeenVersion: version });
|
||||
}
|
||||
}
|
||||
|
||||
async function getUser(userId) {
|
||||
const remoteDb = await getDb();
|
||||
return remoteDb.doc(`users/${userId}`).get().then(doc => {
|
||||
@ -46,35 +111,11 @@
|
||||
});
|
||||
}
|
||||
|
||||
var local = {
|
||||
get: (obj, cb) => {
|
||||
const retVal = {};
|
||||
if (typeof obj === 'string') {
|
||||
retVal[obj] = JSON.parse(window.localStorage.getItem(obj));
|
||||
setTimeout(() => cb(retVal), FAUX_DELAY);
|
||||
} else {
|
||||
Object.keys(obj).forEach(key => {
|
||||
const val = window.localStorage.getItem(key);
|
||||
retVal[key] =
|
||||
val === undefined || val === null ? obj[key] : JSON.parse(val);
|
||||
});
|
||||
setTimeout(() => cb(retVal), FAUX_DELAY);
|
||||
}
|
||||
},
|
||||
set: (obj, cb) => {
|
||||
Object.keys(obj).forEach(key => {
|
||||
window.localStorage.setItem(key, JSON.stringify(obj[key]));
|
||||
});
|
||||
setTimeout(() => {
|
||||
if (cb) {
|
||||
return cb();
|
||||
}
|
||||
}, FAUX_DELAY);
|
||||
}
|
||||
};
|
||||
window.db = {
|
||||
getDb,
|
||||
getUser,
|
||||
getUserLastSeenVersion,
|
||||
setUserLastSeenVersion,
|
||||
local: chrome && chrome.storage ? chrome.storage.local : local,
|
||||
sync: chrome && chrome.storage ? chrome.storage.sync : local
|
||||
};
|
||||
|
@ -2073,12 +2073,7 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
||||
) {
|
||||
hasSeenNotifications = true;
|
||||
notificationsBtn.classList.remove('has-new');
|
||||
db.sync.set(
|
||||
{
|
||||
lastSeenVersion: version
|
||||
},
|
||||
function() {}
|
||||
);
|
||||
window.db.setUserLastSeenVersion(window.user.uid, version);
|
||||
}
|
||||
trackEvent('ui', 'notificationButtonClick', version);
|
||||
return false;
|
||||
@ -2385,6 +2380,7 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
||||
if (result.preserveLastCode && lastCode) {
|
||||
unsavedEditCount = 0;
|
||||
if (lastCode.id) {
|
||||
// Ignore for remote db
|
||||
db.local.get(lastCode.id, function(itemResult) {
|
||||
utils.log('Load item ', lastCode.id);
|
||||
currentItem = itemResult[lastCode.id];
|
||||
@ -2405,37 +2401,27 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
||||
});
|
||||
|
||||
// Check for new version notifications
|
||||
db.sync.get(
|
||||
{
|
||||
lastSeenVersion: ''
|
||||
},
|
||||
function syncGetCallback(result) {
|
||||
// Check if new user
|
||||
if (!result.lastSeenVersion) {
|
||||
onboardModal.classList.add('is-modal-visible');
|
||||
if (document.cookie.indexOf('onboarded') === -1) {
|
||||
trackEvent('ui', 'onboardModalSeen', version);
|
||||
document.cookie = 'onboarded=1';
|
||||
}
|
||||
db.sync.set(
|
||||
{
|
||||
lastSeenVersion: version
|
||||
},
|
||||
function() {}
|
||||
);
|
||||
// set some initial preferences on closing the onboard modal
|
||||
// Old onboarding.
|
||||
// utils.once(document, 'overlaysClosed', function() {});
|
||||
}
|
||||
if (
|
||||
!result.lastSeenVersion ||
|
||||
utils.semverCompare(result.lastSeenVersion, version) === -1
|
||||
) {
|
||||
notificationsBtn.classList.add('has-new');
|
||||
hasSeenNotifications = false;
|
||||
db.getUserLastSeenVersion().then(lastSeenVersion => {
|
||||
// Check if new user
|
||||
if (!lastSeenVersion) {
|
||||
onboardModal.classList.add('is-modal-visible');
|
||||
if (document.cookie.indexOf('onboarded') === -1) {
|
||||
trackEvent('ui', 'onboardModalSeen', version);
|
||||
document.cookie = 'onboarded=1';
|
||||
}
|
||||
window.db.setUserLastSeenVersion(window.user, version);
|
||||
// set some initial preferences on closing the onboard modal
|
||||
// Old onboarding.
|
||||
// utils.once(document, 'overlaysClosed', function() {});
|
||||
}
|
||||
);
|
||||
if (
|
||||
!lastSeenVersion ||
|
||||
utils.semverCompare(lastSeenVersion, version) === -1
|
||||
) {
|
||||
notificationsBtn.classList.add('has-new');
|
||||
hasSeenNotifications = false;
|
||||
}
|
||||
});
|
||||
|
||||
scope.acssSettingsCm = CodeMirror.fromTextArea(acssSettingsTextarea, {
|
||||
mode: 'application/ld+json'
|
||||
|
Loading…
x
Reference in New Issue
Block a user