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 { platform } from "~/platform"
|
||||||
import { initAnalytics } from "./analytics"
|
import { initAnalytics } from "./analytics"
|
||||||
import { initCollections } from "./collections"
|
import { initCollections } from "./collections"
|
||||||
import { initEnvironments } from "./environments"
|
|
||||||
import { initHistory } from "./history"
|
import { initHistory } from "./history"
|
||||||
import { initSettings } from "./settings"
|
import { initSettings } from "./settings"
|
||||||
|
|
||||||
@@ -28,7 +27,7 @@ export function initializeFirebase() {
|
|||||||
initSettings()
|
initSettings()
|
||||||
initCollections()
|
initCollections()
|
||||||
initHistory()
|
initHistory()
|
||||||
initEnvironments()
|
platform.sync.environments.initEnvironmentsSync()
|
||||||
initAnalytics()
|
initAnalytics()
|
||||||
|
|
||||||
initialized = true
|
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 { AuthPlatformDef } from "./auth"
|
||||||
import { UIPlatformDef } from "./ui"
|
import { UIPlatformDef } from "./ui"
|
||||||
|
import { EnvironmentsPlatformDef } from "./environments"
|
||||||
|
|
||||||
export type PlatformDef = {
|
export type PlatformDef = {
|
||||||
ui?: UIPlatformDef
|
ui?: UIPlatformDef
|
||||||
auth: AuthPlatformDef
|
auth: AuthPlatformDef
|
||||||
|
sync: {
|
||||||
|
environments: EnvironmentsPlatformDef
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export let platform: PlatformDef
|
export let platform: PlatformDef
|
||||||
|
|||||||
@@ -6,14 +6,18 @@ import {
|
|||||||
onSnapshot,
|
onSnapshot,
|
||||||
setDoc,
|
setDoc,
|
||||||
} from "firebase/firestore"
|
} from "firebase/firestore"
|
||||||
import { platform } from "~/platform"
|
import { def as platformAuth } from "./firebase/auth"
|
||||||
import {
|
import {
|
||||||
environments$,
|
environments$,
|
||||||
globalEnv$,
|
globalEnv$,
|
||||||
replaceEnvironments,
|
replaceEnvironments,
|
||||||
setGlobalEnvVariables,
|
setGlobalEnvVariables,
|
||||||
} from "~/newstore/environments"
|
} from "@hoppscotch/common/newstore/environments"
|
||||||
import { getSettingSubject, settingsStore } from "~/newstore/settings"
|
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
|
* Used locally to prevent infinite loop when environment sync update
|
||||||
@@ -32,7 +36,7 @@ let loadedEnvironments = false
|
|||||||
let loadedGlobals = true
|
let loadedGlobals = true
|
||||||
|
|
||||||
async function writeEnvironments(environment: Environment[]) {
|
async function writeEnvironments(environment: Environment[]) {
|
||||||
const currentUser = platform.auth.getCurrentUser()
|
const currentUser = platformAuth.getCurrentUser()
|
||||||
|
|
||||||
if (currentUser === null)
|
if (currentUser === null)
|
||||||
throw new Error("Cannot write environments when signed out")
|
throw new Error("Cannot write environments when signed out")
|
||||||
@@ -57,7 +61,7 @@ async function writeEnvironments(environment: Environment[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function writeGlobalEnvironment(variables: Environment["variables"]) {
|
async function writeGlobalEnvironment(variables: Environment["variables"]) {
|
||||||
const currentUser = platform.auth.getCurrentUser()
|
const currentUser = platformAuth.getCurrentUser()
|
||||||
|
|
||||||
if (currentUser === null)
|
if (currentUser === null)
|
||||||
throw new Error("Cannot write global environment when signed out")
|
throw new Error("Cannot write global environment when signed out")
|
||||||
@@ -81,11 +85,11 @@ async function writeGlobalEnvironment(variables: Environment["variables"]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function initEnvironments() {
|
export function initEnvironmentsSync() {
|
||||||
const currentUser$ = platform.auth.getCurrentUserStream()
|
const currentUser$ = platformAuth.getCurrentUserStream()
|
||||||
|
|
||||||
const envListenSub = environments$.subscribe((envs) => {
|
const envListenSub = environments$.subscribe((envs) => {
|
||||||
const currentUser = platform.auth.getCurrentUser()
|
const currentUser = platformAuth.getCurrentUser()
|
||||||
|
|
||||||
if (
|
if (
|
||||||
currentUser &&
|
currentUser &&
|
||||||
@@ -97,7 +101,7 @@ export function initEnvironments() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
const globalListenSub = globalEnv$.subscribe((vars) => {
|
const globalListenSub = globalEnv$.subscribe((vars) => {
|
||||||
const currentUser = platform.auth.getCurrentUser()
|
const currentUser = platformAuth.getCurrentUser()
|
||||||
|
|
||||||
if (currentUser && settingsStore.value.syncEnvironments && loadedGlobals) {
|
if (currentUser && settingsStore.value.syncEnvironments && loadedGlobals) {
|
||||||
writeGlobalEnvironment(vars)
|
writeGlobalEnvironment(vars)
|
||||||
@@ -171,8 +175,12 @@ export function initEnvironments() {
|
|||||||
globalListenSub.unsubscribe()
|
globalListenSub.unsubscribe()
|
||||||
currentUserSub.unsubscribe()
|
currentUserSub.unsubscribe()
|
||||||
|
|
||||||
initEnvironments()
|
initEnvironmentsSync()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const def: EnvironmentsPlatformDef = {
|
||||||
|
initEnvironmentsSync,
|
||||||
|
}
|
||||||
@@ -1,6 +1,10 @@
|
|||||||
import { createHoppApp } from "@hoppscotch/common"
|
import { createHoppApp } from "@hoppscotch/common"
|
||||||
import { def as authDef } from "./firebase/auth"
|
import { def as authDef } from "./firebase/auth"
|
||||||
|
import { def as envDef } from "./environments"
|
||||||
|
|
||||||
createHoppApp("#app", {
|
createHoppApp("#app", {
|
||||||
auth: authDef,
|
auth: authDef,
|
||||||
|
sync: {
|
||||||
|
environments: envDef,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user