Skip to content

Commit

Permalink
Added feature to sync all config initially
Browse files Browse the repository at this point in the history
  • Loading branch information
dharmesh-hemaram committed Aug 29, 2024
1 parent bc07c1b commit e163391
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 20 deletions.
1 change: 1 addition & 0 deletions apps/acf-extension/src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
23 changes: 14 additions & 9 deletions apps/acf-extension/src/background/sync-config.ts
Original file line number Diff line number Diff line change
@@ -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<Configuration>): Array<Configuration> {
return configs.filter((config: Configuration) => config.url && config.updated && !config.download);
filterConfig(configs: Array<Configuration>, updated: boolean): Array<Configuration> {
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() {
Expand All @@ -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<Configuration> = this.filterConfig(storageResult[LOCAL_STORAGE_KEY.CONFIGS] || []);
const configs: Array<Configuration> = 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`);
Expand All @@ -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);
}
});
});
8 changes: 7 additions & 1 deletion apps/acf-options-page/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -22,6 +22,12 @@ function App() {
dispatch(firebaseIsLoginAPI());
}, [dispatch]);

useEffect(() => {
if (user) {
dispatch(profileGetAPI());
}
}, [user, dispatch]);

const onCloseAlert = () => {
setShow(false);
localStorage.setItem('login', 'true');
Expand Down
6 changes: 1 addition & 5 deletions apps/acf-options-page/src/app/header_porfile.tsx
Original file line number Diff line number Diff line change
@@ -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({
Expand Down
2 changes: 1 addition & 1 deletion libs/shared/firebase-database/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <reference types="chrome"/>

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';
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e163391

Please sign in to comment.