From 3fa40525385ff10acd84bbff4a27f25164813733 Mon Sep 17 00:00:00 2001 From: Akash K <57758277+amk-dev@users.noreply.github.com> Date: Thu, 2 Mar 2023 18:50:13 +0530 Subject: [PATCH] chore: move environments firebase things to hoppscotch-web (#2939) --- .../hoppscotch-common/src/helpers/fb/index.ts | 3 +- .../src/platform/environments.ts | 3 ++ .../hoppscotch-common/src/platform/index.ts | 4 +++ .../fb => hoppscotch-web/src}/environments.ts | 28 ++++++++++++------- packages/hoppscotch-web/src/main.ts | 4 +++ 5 files changed, 30 insertions(+), 12 deletions(-) create mode 100644 packages/hoppscotch-common/src/platform/environments.ts rename packages/{hoppscotch-common/src/helpers/fb => hoppscotch-web/src}/environments.ts (86%) diff --git a/packages/hoppscotch-common/src/helpers/fb/index.ts b/packages/hoppscotch-common/src/helpers/fb/index.ts index 7463c3b8a..1e8af88d7 100644 --- a/packages/hoppscotch-common/src/helpers/fb/index.ts +++ b/packages/hoppscotch-common/src/helpers/fb/index.ts @@ -2,7 +2,6 @@ import { initializeApp } from "firebase/app" import { platform } from "~/platform" import { initAnalytics } from "./analytics" import { initCollections } from "./collections" -import { initEnvironments } from "./environments" import { initHistory } from "./history" import { initSettings } from "./settings" @@ -28,7 +27,7 @@ export function initializeFirebase() { initSettings() initCollections() initHistory() - initEnvironments() + platform.sync.environments.initEnvironmentsSync() initAnalytics() initialized = true diff --git a/packages/hoppscotch-common/src/platform/environments.ts b/packages/hoppscotch-common/src/platform/environments.ts new file mode 100644 index 000000000..6ba4f0f2c --- /dev/null +++ b/packages/hoppscotch-common/src/platform/environments.ts @@ -0,0 +1,3 @@ +export type EnvironmentsPlatformDef = { + initEnvironmentsSync: () => void +} diff --git a/packages/hoppscotch-common/src/platform/index.ts b/packages/hoppscotch-common/src/platform/index.ts index 94e9a2734..5a0203dc7 100644 --- a/packages/hoppscotch-common/src/platform/index.ts +++ b/packages/hoppscotch-common/src/platform/index.ts @@ -1,9 +1,13 @@ import { AuthPlatformDef } from "./auth" import { UIPlatformDef } from "./ui" +import { EnvironmentsPlatformDef } from "./environments" export type PlatformDef = { ui?: UIPlatformDef auth: AuthPlatformDef + sync: { + environments: EnvironmentsPlatformDef + } } export let platform: PlatformDef diff --git a/packages/hoppscotch-common/src/helpers/fb/environments.ts b/packages/hoppscotch-web/src/environments.ts similarity index 86% rename from packages/hoppscotch-common/src/helpers/fb/environments.ts rename to packages/hoppscotch-web/src/environments.ts index be34b184a..823dd7b88 100644 --- a/packages/hoppscotch-common/src/helpers/fb/environments.ts +++ b/packages/hoppscotch-web/src/environments.ts @@ -6,14 +6,18 @@ import { onSnapshot, setDoc, } from "firebase/firestore" -import { platform } from "~/platform" +import { def as platformAuth } from "./firebase/auth" import { environments$, globalEnv$, replaceEnvironments, setGlobalEnvVariables, -} from "~/newstore/environments" -import { getSettingSubject, settingsStore } from "~/newstore/settings" +} from "@hoppscotch/common/newstore/environments" +import { + getSettingSubject, + settingsStore, +} from "@hoppscotch/common/newstore/settings" +import { EnvironmentsPlatformDef } from "@hoppscotch/common/platform/environments" /** * Used locally to prevent infinite loop when environment sync update @@ -32,7 +36,7 @@ let loadedEnvironments = false let loadedGlobals = true async function writeEnvironments(environment: Environment[]) { - const currentUser = platform.auth.getCurrentUser() + const currentUser = platformAuth.getCurrentUser() if (currentUser === null) throw new Error("Cannot write environments when signed out") @@ -57,7 +61,7 @@ async function writeEnvironments(environment: Environment[]) { } async function writeGlobalEnvironment(variables: Environment["variables"]) { - const currentUser = platform.auth.getCurrentUser() + const currentUser = platformAuth.getCurrentUser() if (currentUser === null) throw new Error("Cannot write global environment when signed out") @@ -81,11 +85,11 @@ async function writeGlobalEnvironment(variables: Environment["variables"]) { } } -export function initEnvironments() { - const currentUser$ = platform.auth.getCurrentUserStream() +export function initEnvironmentsSync() { + const currentUser$ = platformAuth.getCurrentUserStream() const envListenSub = environments$.subscribe((envs) => { - const currentUser = platform.auth.getCurrentUser() + const currentUser = platformAuth.getCurrentUser() if ( currentUser && @@ -97,7 +101,7 @@ export function initEnvironments() { }) const globalListenSub = globalEnv$.subscribe((vars) => { - const currentUser = platform.auth.getCurrentUser() + const currentUser = platformAuth.getCurrentUser() if (currentUser && settingsStore.value.syncEnvironments && loadedGlobals) { writeGlobalEnvironment(vars) @@ -171,8 +175,12 @@ export function initEnvironments() { globalListenSub.unsubscribe() currentUserSub.unsubscribe() - initEnvironments() + initEnvironmentsSync() } } ) } + +export const def: EnvironmentsPlatformDef = { + initEnvironmentsSync, +} diff --git a/packages/hoppscotch-web/src/main.ts b/packages/hoppscotch-web/src/main.ts index 9114700af..5945e25ac 100644 --- a/packages/hoppscotch-web/src/main.ts +++ b/packages/hoppscotch-web/src/main.ts @@ -1,6 +1,10 @@ import { createHoppApp } from "@hoppscotch/common" import { def as authDef } from "./firebase/auth" +import { def as envDef } from "./environments" createHoppApp("#app", { auth: authDef, + sync: { + environments: envDef, + }, })