chore: merge hoppscotch/hoppscotch/staging into hoppscotch/self-hosted/main

This commit is contained in:
Andrew Bastin
2023-03-13 17:09:53 +05:30
7 changed files with 44 additions and 69 deletions

View File

@@ -1,184 +0,0 @@
import {
collection,
doc,
getFirestore,
onSnapshot,
setDoc,
} from "firebase/firestore"
import {
translateToNewRESTCollection,
translateToNewGQLCollection,
} from "@hoppscotch/data"
import { platform } from "~/platform"
import {
restCollections$,
graphqlCollections$,
setRESTCollections,
setGraphqlCollections,
} from "~/newstore/collections"
import { getSettingSubject, settingsStore } from "~/newstore/settings"
type CollectionFlags = "collectionsGraphql" | "collections"
/**
* Whether the collections are loaded. If this is set to true
* Updates to the collections store are written into firebase.
*
* If you have want to update the store and not fire the store update
* subscription, set this variable to false, do the update and then
* set it to true
*/
let loadedRESTCollections = false
/**
* Whether the collections are loaded. If this is set to true
* Updates to the collections store are written into firebase.
*
* If you have want to update the store and not fire the store update
* subscription, set this variable to false, do the update and then
* set it to true
*/
let loadedGraphqlCollections = false
export async function writeCollections(
collection: any[],
flag: CollectionFlags
) {
const currentUser = platform.auth.getCurrentUser()
if (currentUser === null)
throw new Error("User not logged in to write collections")
const cl = {
updatedOn: new Date(),
author: currentUser.uid,
author_name: currentUser.displayName,
author_image: currentUser.photoURL,
collection,
}
try {
await setDoc(
doc(getFirestore(), "users", currentUser.uid, flag, "sync"),
cl
)
} catch (e) {
console.error("error updating", cl, e)
throw e
}
}
export function initCollections() {
const currentUser$ = platform.auth.getCurrentUserStream()
const restCollSub = restCollections$.subscribe((collections) => {
const currentUser = platform.auth.getCurrentUser()
if (
loadedRESTCollections &&
currentUser &&
settingsStore.value.syncCollections
) {
writeCollections(collections, "collections")
}
})
const gqlCollSub = graphqlCollections$.subscribe((collections) => {
const currentUser = platform.auth.getCurrentUser()
if (
loadedGraphqlCollections &&
currentUser &&
settingsStore.value.syncCollections
) {
writeCollections(collections, "collectionsGraphql")
}
})
let restSnapshotStop: (() => void) | null = null
let graphqlSnapshotStop: (() => void) | null = null
const currentUserSub = currentUser$.subscribe((user) => {
if (!user) {
if (restSnapshotStop) {
restSnapshotStop()
restSnapshotStop = null
}
if (graphqlSnapshotStop) {
graphqlSnapshotStop()
graphqlSnapshotStop = null
}
} else {
restSnapshotStop = onSnapshot(
collection(getFirestore(), "users", user.uid, "collections"),
(collectionsRef) => {
const collections: any[] = []
collectionsRef.forEach((doc) => {
const collection = doc.data()
collection.id = doc.id
collections.push(collection)
})
// Prevent infinite ping-pong of updates
loadedRESTCollections = false
// TODO: Wth is with collections[0]
if (collections.length > 0 && settingsStore.value.syncCollections) {
setRESTCollections(
(collections[0].collection ?? []).map(
translateToNewRESTCollection
)
)
}
loadedRESTCollections = true
}
)
graphqlSnapshotStop = onSnapshot(
collection(getFirestore(), "users", user.uid, "collectionsGraphql"),
(collectionsRef) => {
const collections: any[] = []
collectionsRef.forEach((doc) => {
const collection = doc.data()
collection.id = doc.id
collections.push(collection)
})
// Prevent infinite ping-pong of updates
loadedGraphqlCollections = false
// TODO: Wth is with collections[0]
if (collections.length > 0 && settingsStore.value.syncCollections) {
setGraphqlCollections(
(collections[0].collection ?? []).map(translateToNewGQLCollection)
)
}
loadedGraphqlCollections = true
}
)
}
})
let oldSyncStatus = settingsStore.value.syncCollections
const syncStop = getSettingSubject("syncCollections").subscribe(
(newStatus) => {
if (oldSyncStatus === true && newStatus === false) {
restSnapshotStop?.()
graphqlSnapshotStop?.()
oldSyncStatus = newStatus
} else if (oldSyncStatus === false && newStatus === true) {
syncStop.unsubscribe()
restCollSub.unsubscribe()
gqlCollSub.unsubscribe()
currentUserSub.unsubscribe()
initCollections()
}
}
)
}

View File

@@ -1,7 +1,6 @@
import { initializeApp } from "firebase/app"
import { platform } from "~/platform"
import { initAnalytics } from "./analytics"
import { initCollections } from "./collections"
import { initHistory } from "./history"
import { initSettings } from "./settings"
@@ -25,7 +24,7 @@ export function initializeFirebase() {
platform.auth.performAuthInit()
initSettings()
initCollections()
platform.sync.collections.initCollectionsSync()
initHistory()
platform.sync.environments.initEnvironmentsSync()
initAnalytics()

View File

@@ -0,0 +1,3 @@
export type CollectionsPlatformDef = {
initCollectionsSync: () => void
}

View File

@@ -1,12 +1,14 @@
import { AuthPlatformDef } from "./auth"
import { UIPlatformDef } from "./ui"
import { EnvironmentsPlatformDef } from "./environments"
import { CollectionsPlatformDef } from "./collections"
export type PlatformDef = {
ui?: UIPlatformDef
auth: AuthPlatformDef
sync: {
environments: EnvironmentsPlatformDef
collections: CollectionsPlatformDef
}
}