diff --git a/apps/acf-extension/src/background/index.ts b/apps/acf-extension/src/background/index.ts index b3fd41fc..46db107b 100644 --- a/apps/acf-extension/src/background/index.ts +++ b/apps/acf-extension/src/background/index.ts @@ -19,6 +19,7 @@ import AcfBackup from './acf-backup'; import registerContextMenus from './context-menu'; import { auth } from './firebase'; import { googleAnalytics } from './google-analytics'; +import './sync-config'; import { TabsMessenger } from './tab'; import { Update } from './update'; diff --git a/apps/acf-extension/src/background/sync-config.ts b/apps/acf-extension/src/background/sync-config.ts index 8566d07d..32636e7e 100644 --- a/apps/acf-extension/src/background/sync-config.ts +++ b/apps/acf-extension/src/background/sync-config.ts @@ -1,18 +1,20 @@ import { Configuration, LOCAL_STORAGE_KEY } from '@dhruv-techapps/acf-common'; -import { FirebaseDatabaseBackground } from '@dhruv-techapps/firebase-database'; +import { FirebaseDatabaseBackground, SYNC_ALL_CONFIG_ALARM, SYNC_CONFIG_ALARM } from '@dhruv-techapps/firebase-database'; import { Auth } from '@dhruv-techapps/firebase-oauth'; import { FirebaseStorageBackground } from '@dhruv-techapps/firebase-storage'; import { EDGE_OAUTH_CLIENT_ID } from '../common/environments'; import { auth } from './firebase'; import { googleAnalytics } from './google-analytics'; -const SYNC_CONFIG_ALARM = 'sync-config'; - export class SyncConfig { constructor(private auth: Auth) {} - filterConfig(configs: Array): Array { - return configs.filter((config: Configuration) => config.url && config.updated && !config.download); + filterConfig(configs: Array, updated: boolean): Array { + if (updated) { + return configs.filter((config: Configuration) => config.url && config.updated && !config.download); + } else { + return configs.filter((config: Configuration) => config.url && !config.download); + } } async reset() { @@ -25,22 +27,23 @@ export class SyncConfig { await chrome.storage.local.set({ [LOCAL_STORAGE_KEY.CONFIGS]: updatedConfigs }); } - async syncConfig() { + async syncConfig(updated: boolean) { if (!this.auth.currentUser) { return; } try { const { uid } = this.auth.currentUser; const storageResult = await chrome.storage.local.get(LOCAL_STORAGE_KEY.CONFIGS); - const configs: Array = this.filterConfig(storageResult[LOCAL_STORAGE_KEY.CONFIGS] || []); + const configs: Array = this.filterConfig(storageResult[LOCAL_STORAGE_KEY.CONFIGS] || [], updated); if (configs.length === 0) { return; } for (const config of configs) { try { + // Update Database const db = { url: config.url, name: config.name, userId: uid }; await new FirebaseDatabaseBackground(this.auth, EDGE_OAUTH_CLIENT_ID).setConfig(db, config.id); - + // Update Storage config.download = true; const blob = new Blob([JSON.stringify(config)], { type: 'application/json;charset=utf-8;' }); await new FirebaseStorageBackground(this.auth).uploadFile(blob, `users/${uid}/${config.id}.json`); @@ -66,7 +69,9 @@ export class SyncConfig { auth.authStateReady().then(() => { chrome.alarms.onAlarm.addListener(({ name }) => { if (name === SYNC_CONFIG_ALARM) { - new SyncConfig(auth).syncConfig(); + new SyncConfig(auth).syncConfig(true); + } else if (name === SYNC_ALL_CONFIG_ALARM) { + new SyncConfig(auth).syncConfig(false); } }); }); diff --git a/apps/acf-options-page/src/App.tsx b/apps/acf-options-page/src/App.tsx index d5daddd9..a11cf30f 100644 --- a/apps/acf-options-page/src/App.tsx +++ b/apps/acf-options-page/src/App.tsx @@ -10,7 +10,7 @@ import { LoginModal } from './modal/login.modal'; import { SubscribeModal } from './modal/subscribe.modal'; import { getManifest } from './store/app.api'; import { appSelector } from './store/app.slice'; -import { firebaseIsLoginAPI, firebaseSelector } from './store/firebase'; +import { firebaseIsLoginAPI, firebaseSelector, profileGetAPI } from './store/firebase'; function App() { const { loading, error } = useAppSelector(appSelector); @@ -22,6 +22,12 @@ function App() { dispatch(firebaseIsLoginAPI()); }, [dispatch]); + useEffect(() => { + if (user) { + dispatch(profileGetAPI()); + } + }, [user, dispatch]); + const onCloseAlert = () => { setShow(false); localStorage.setItem('login', 'true'); diff --git a/apps/acf-options-page/src/app/header_porfile.tsx b/apps/acf-options-page/src/app/header_porfile.tsx index 1210d7d4..f14dea89 100644 --- a/apps/acf-options-page/src/app/header_porfile.tsx +++ b/apps/acf-options-page/src/app/header_porfile.tsx @@ -1,17 +1,13 @@ -import { useEffect } from 'react'; import { NavDropdown } from 'react-bootstrap'; import { useConfirmationModalContext } from '../_providers/confirm.provider'; import { useAppDispatch, useAppSelector } from '../hooks'; -import { firebaseDatabaseSelector, profileGetAPI, profileSetAPI } from '../store/firebase'; +import { firebaseDatabaseSelector, profileSetAPI } from '../store/firebase'; import { LockFill, UnLockFill } from '../util'; export const HeaderProfile = () => { const dispatch = useAppDispatch(); const modalContext = useConfirmationModalContext(); const { profile } = useAppSelector(firebaseDatabaseSelector); - useEffect(() => { - dispatch(profileGetAPI()); - }, [dispatch]); const onSwitchProfile = async () => { const result = await modalContext.showConfirmation({ diff --git a/libs/shared/firebase-database/src/index.ts b/libs/shared/firebase-database/src/index.ts index f949e3bc..fd4eeec5 100644 --- a/libs/shared/firebase-database/src/index.ts +++ b/libs/shared/firebase-database/src/index.ts @@ -1,5 +1,5 @@ /// export * from './lib/firebase-database-background'; -export { RUNTIME_MESSAGE_FIREBASE_DATABASE } from './lib/firebase-database.constant'; +export { RUNTIME_MESSAGE_FIREBASE_DATABASE, SYNC_ALL_CONFIG_ALARM, SYNC_CONFIG_ALARM } from './lib/firebase-database.constant'; export * from './lib/firebase-database.service'; diff --git a/libs/shared/firebase-database/src/lib/firebase-database-background.ts b/libs/shared/firebase-database/src/lib/firebase-database-background.ts index 070a0a0b..67719cc4 100644 --- a/libs/shared/firebase-database/src/lib/firebase-database-background.ts +++ b/libs/shared/firebase-database/src/lib/firebase-database-background.ts @@ -1,7 +1,7 @@ import { Auth, FirebaseOauth2Background } from '@dhruv-techapps/firebase-oauth'; import { Database, child, get, getDatabase, ref, remove, set } from 'firebase/database'; import XMLHttpRequest from 'xhr-shim'; -import { DBConfigRequest, SYNC_CONFIG_ALARM } from './firebase-database.constant'; +import { DBConfigRequest, SYNC_ALL_CONFIG_ALARM, SYNC_CONFIG_ALARM } from './firebase-database.constant'; self['XMLHttpRequest'] = XMLHttpRequest; export class FirebaseDatabaseBackground extends FirebaseOauth2Background { @@ -38,11 +38,11 @@ export class FirebaseDatabaseBackground extends FirebaseOauth2Background { const result = set(dbRef, profile); if (profile) { const alarmInfo: chrome.alarms.AlarmCreateInfo = { when: Date.now() + 500 }; - await chrome.alarms.clear(SYNC_CONFIG_ALARM); - alarmInfo.periodInMinutes = 1440 * 7; - chrome.alarms.create(SYNC_CONFIG_ALARM, alarmInfo); + await chrome.alarms.create(SYNC_ALL_CONFIG_ALARM, { delayInMinutes: 1 }); + await chrome.alarms.create(SYNC_CONFIG_ALARM, { ...alarmInfo, periodInMinutes: 1440 * 7 }); } else { chrome.alarms.clear(SYNC_CONFIG_ALARM); + chrome.alarms.clear(SYNC_ALL_CONFIG_ALARM); } return result; } diff --git a/libs/shared/firebase-database/src/lib/firebase-database.constant.ts b/libs/shared/firebase-database/src/lib/firebase-database.constant.ts index 5b7ccafe..404fd323 100644 --- a/libs/shared/firebase-database/src/lib/firebase-database.constant.ts +++ b/libs/shared/firebase-database/src/lib/firebase-database.constant.ts @@ -2,6 +2,7 @@ export const NOTIFICATIONS_TITLE = 'Firebase Database'; export const NOTIFICATIONS_ID = 'firebase-database'; export const RUNTIME_MESSAGE_FIREBASE_DATABASE = 'firebase-database'; export const SYNC_CONFIG_ALARM = 'syncConfigAlarm'; +export const SYNC_ALL_CONFIG_ALARM = 'syncAllConfigAlarm'; export type DBConfigRequest = { url: string;