chore: move environments firebase things to hoppscotch-web (#2939)
This commit is contained in:
@@ -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
|
||||
|
||||
3
packages/hoppscotch-common/src/platform/environments.ts
Normal file
3
packages/hoppscotch-common/src/platform/environments.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export type EnvironmentsPlatformDef = {
|
||||
initEnvironmentsSync: () => void
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
@@ -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,
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user