chore: move history firebase things to hoppscotch-web (#2954)

This commit is contained in:
Akash K
2023-03-30 00:09:08 +05:30
committed by GitHub
parent a66a2f5645
commit cc802b1e9f
5 changed files with 28 additions and 14 deletions

View File

@@ -1,7 +1,6 @@
import { initializeApp } from "firebase/app" import { initializeApp } from "firebase/app"
import { platform } from "~/platform" import { platform } from "~/platform"
import { initAnalytics } from "./analytics" import { initAnalytics } from "./analytics"
import { initHistory } from "./history"
const firebaseConfig = { const firebaseConfig = {
apiKey: import.meta.env.VITE_API_KEY, apiKey: import.meta.env.VITE_API_KEY,
@@ -24,7 +23,7 @@ export function initializeFirebase() {
platform.auth.performAuthInit() platform.auth.performAuthInit()
platform.sync.settings.initSettingsSync() platform.sync.settings.initSettingsSync()
platform.sync.collections.initCollectionsSync() platform.sync.collections.initCollectionsSync()
initHistory() platform.sync.history.initHistorySync()
platform.sync.environments.initEnvironmentsSync() platform.sync.environments.initEnvironmentsSync()
initAnalytics() initAnalytics()

View File

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

View File

@@ -3,6 +3,7 @@ import { UIPlatformDef } from "./ui"
import { EnvironmentsPlatformDef } from "./environments" import { EnvironmentsPlatformDef } from "./environments"
import { CollectionsPlatformDef } from "./collections" import { CollectionsPlatformDef } from "./collections"
import { SettingsPlatformDef } from "./settings" import { SettingsPlatformDef } from "./settings"
import { HistoryPlatformDef } from "./history"
export type PlatformDef = { export type PlatformDef = {
ui?: UIPlatformDef ui?: UIPlatformDef
@@ -11,6 +12,7 @@ export type PlatformDef = {
environments: EnvironmentsPlatformDef environments: EnvironmentsPlatformDef
collections: CollectionsPlatformDef collections: CollectionsPlatformDef
settings: SettingsPlatformDef settings: SettingsPlatformDef
history: HistoryPlatformDef
} }
} }

View File

@@ -12,8 +12,11 @@ import {
updateDoc, updateDoc,
} from "firebase/firestore" } from "firebase/firestore"
import { FormDataKeyValue } from "@hoppscotch/data" import { FormDataKeyValue } from "@hoppscotch/data"
import { platform } from "~/platform" import { def as platformAuth } from "./firebase/auth"
import { getSettingSubject, settingsStore } from "~/newstore/settings" import {
getSettingSubject,
settingsStore,
} from "@hoppscotch/common/newstore/settings"
import { import {
GQLHistoryEntry, GQLHistoryEntry,
graphqlHistoryStore, graphqlHistoryStore,
@@ -24,7 +27,8 @@ import {
setRESTHistoryEntries, setRESTHistoryEntries,
translateToNewGQLHistory, translateToNewGQLHistory,
translateToNewRESTHistory, translateToNewRESTHistory,
} from "~/newstore/history" } from "@hoppscotch/common/newstore/history"
import { HistoryPlatformDef } from "@hoppscotch/common/platform/history"
type HistoryFBCollections = "history" | "graphqlHistory" type HistoryFBCollections = "history" | "graphqlHistory"
@@ -76,7 +80,7 @@ async function writeHistory(
? purgeFormDataFromRequest(entry as RESTHistoryEntry) ? purgeFormDataFromRequest(entry as RESTHistoryEntry)
: entry : entry
const currentUser = platform.auth.getCurrentUser() const currentUser = platformAuth.getCurrentUser()
if (currentUser === null) if (currentUser === null)
throw new Error("User not logged in to sync history") throw new Error("User not logged in to sync history")
@@ -98,7 +102,7 @@ async function deleteHistory(
entry: (RESTHistoryEntry | GQLHistoryEntry) & { id: string }, entry: (RESTHistoryEntry | GQLHistoryEntry) & { id: string },
col: HistoryFBCollections col: HistoryFBCollections
) { ) {
const currentUser = platform.auth.getCurrentUser() const currentUser = platformAuth.getCurrentUser()
if (currentUser === null) if (currentUser === null)
throw new Error("User not logged in to delete history") throw new Error("User not logged in to delete history")
@@ -114,7 +118,7 @@ async function deleteHistory(
} }
async function clearHistory(col: HistoryFBCollections) { async function clearHistory(col: HistoryFBCollections) {
const currentUser = platform.auth.getCurrentUser() const currentUser = platformAuth.getCurrentUser()
if (currentUser === null) if (currentUser === null)
throw new Error("User not logged in to clear history") throw new Error("User not logged in to clear history")
@@ -130,7 +134,7 @@ async function toggleStar(
entry: (RESTHistoryEntry | GQLHistoryEntry) & { id: string }, entry: (RESTHistoryEntry | GQLHistoryEntry) & { id: string },
col: HistoryFBCollections col: HistoryFBCollections
) { ) {
const currentUser = platform.auth.getCurrentUser() const currentUser = platformAuth.getCurrentUser()
if (currentUser === null) throw new Error("User not logged in to toggle star") if (currentUser === null) throw new Error("User not logged in to toggle star")
@@ -145,11 +149,11 @@ async function toggleStar(
} }
} }
export function initHistory() { export function initHistorySync() {
const currentUser$ = platform.auth.getCurrentUserStream() const currentUser$ = platformAuth.getCurrentUserStream()
const restHistorySub = restHistoryStore.dispatches$.subscribe((dispatch) => { const restHistorySub = restHistoryStore.dispatches$.subscribe((dispatch) => {
const currentUser = platform.auth.getCurrentUser() const currentUser = platformAuth.getCurrentUser()
if (loadedRESTHistory && currentUser && settingsStore.value.syncHistory) { if (loadedRESTHistory && currentUser && settingsStore.value.syncHistory) {
if (dispatch.dispatcher === "addEntry") { if (dispatch.dispatcher === "addEntry") {
@@ -166,7 +170,7 @@ export function initHistory() {
const gqlHistorySub = graphqlHistoryStore.dispatches$.subscribe( const gqlHistorySub = graphqlHistoryStore.dispatches$.subscribe(
(dispatch) => { (dispatch) => {
const currentUser = platform.auth.getCurrentUser() const currentUser = platformAuth.getCurrentUser()
if ( if (
loadedGraphqlHistory && loadedGraphqlHistory &&
@@ -262,7 +266,11 @@ export function initHistory() {
gqlHistorySub.unsubscribe() gqlHistorySub.unsubscribe()
currentUserSub.unsubscribe() currentUserSub.unsubscribe()
initHistory() initHistorySync()
} }
}) })
} }
export const def: HistoryPlatformDef = {
initHistorySync,
}

View File

@@ -3,6 +3,7 @@ import { def as authDef } from "./firebase/auth"
import { def as envDef } from "./environments" import { def as envDef } from "./environments"
import { def as collectionsDef } from "./collections" import { def as collectionsDef } from "./collections"
import { def as settingsDef } from "./settings" import { def as settingsDef } from "./settings"
import { def as historyDef } from "./history"
createHoppApp("#app", { createHoppApp("#app", {
auth: authDef, auth: authDef,
@@ -10,5 +11,6 @@ createHoppApp("#app", {
environments: envDef, environments: envDef,
collections: collectionsDef, collections: collectionsDef,
settings: settingsDef, settings: settingsDef,
history: historyDef,
}, },
}) })