diff --git a/packages/hoppscotch-selfhost-web/src/api/mutations/UpdateUserSession.graphql b/packages/hoppscotch-selfhost-web/src/api/mutations/UpdateUserSession.graphql new file mode 100644 index 000000000..5ce87e5b6 --- /dev/null +++ b/packages/hoppscotch-selfhost-web/src/api/mutations/UpdateUserSession.graphql @@ -0,0 +1,11 @@ +mutation UpdateUserSession( + $currentSession: String! + $sessionType: SessionType! +) { + updateUserSessions( + currentSession: $currentSession + sessionType: $sessionType + ) { + currentRESTSession + } +} diff --git a/packages/hoppscotch-selfhost-web/src/api/queries/GetCurrentRESTSession.graphql b/packages/hoppscotch-selfhost-web/src/api/queries/GetCurrentRESTSession.graphql new file mode 100644 index 000000000..fee09e491 --- /dev/null +++ b/packages/hoppscotch-selfhost-web/src/api/queries/GetCurrentRESTSession.graphql @@ -0,0 +1,5 @@ +query GetCurrentRESTSession { + me { + currentRESTSession + } +} diff --git a/packages/hoppscotch-selfhost-web/src/main.ts b/packages/hoppscotch-selfhost-web/src/main.ts index 354d17ab6..64d5273d3 100644 --- a/packages/hoppscotch-selfhost-web/src/main.ts +++ b/packages/hoppscotch-selfhost-web/src/main.ts @@ -4,6 +4,7 @@ import { def as environmentsDef } from "./platform/environments/environments.pla import { def as collectionsDef } from "./platform/collections/collections.platform" import { def as settingsDef } from "./platform/settings/settings.platform" import { def as historyDef } from "./platform/history/history.platform" +import { def as tabStateDef } from "./platform/tabState/tabState.platform" createHoppApp("#app", { auth: authDef, @@ -12,5 +13,6 @@ createHoppApp("#app", { collections: collectionsDef, settings: settingsDef, history: historyDef, + tabState: tabStateDef, }, }) diff --git a/packages/hoppscotch-selfhost-web/src/platform/tabState/tabState.api.ts b/packages/hoppscotch-selfhost-web/src/platform/tabState/tabState.api.ts new file mode 100644 index 000000000..a351b072b --- /dev/null +++ b/packages/hoppscotch-selfhost-web/src/platform/tabState/tabState.api.ts @@ -0,0 +1,35 @@ +import { + runMutation, + runGQLQuery, +} from "@hoppscotch/common/helpers/backend/GQLClient" +import { + GetCurrentRestSessionDocument, + GetCurrentRestSessionQuery, + GetCurrentRestSessionQueryVariables, + SessionType, + UpdateUserSessionDocument, + UpdateUserSessionMutation, + UpdateUserSessionMutationVariables, +} from "../../api/generated/graphql" + +export const updateUserSession = ( + currentSession: string, + sessionType: SessionType +) => + runMutation< + UpdateUserSessionMutation, + UpdateUserSessionMutationVariables, + "" + >(UpdateUserSessionDocument, { + sessionType, + currentSession, + })() + +export const getCurrentRestSession = () => + runGQLQuery< + GetCurrentRestSessionQuery, + GetCurrentRestSessionQueryVariables, + "" + >({ + query: GetCurrentRestSessionDocument, + }) diff --git a/packages/hoppscotch-selfhost-web/src/platform/tabState/tabState.platform.ts b/packages/hoppscotch-selfhost-web/src/platform/tabState/tabState.platform.ts new file mode 100644 index 000000000..dd2bb333e --- /dev/null +++ b/packages/hoppscotch-selfhost-web/src/platform/tabState/tabState.platform.ts @@ -0,0 +1,38 @@ +import { PersistableRESTTabState } from "@hoppscotch/common/helpers/rest/tab" +import { HoppUser } from "@hoppscotch/common/platform/auth" +import { TabStatePlatformDef } from "@hoppscotch/common/platform/tab" +import { def as platformAuth } from "@platform/auth" +import { getCurrentRestSession, updateUserSession } from "./tabState.api" +import { SessionType } from "../../api/generated/graphql" +import * as E from "fp-ts/Either" + +async function writeCurrentTabState( + _: HoppUser, + persistableTabState: PersistableRESTTabState +) { + await updateUserSession(JSON.stringify(persistableTabState), SessionType.Rest) +} + +async function loadTabStateFromSync(): Promise { + const currentUser = platformAuth.getCurrentUser() + + if (!currentUser) + throw new Error("Cannot load request from sync without login") + + const res = await getCurrentRestSession() + + if (E.isRight(res)) { + const currentRESTSession = res.right.me.currentRESTSession + + return currentRESTSession ? JSON.parse(currentRESTSession) : null + } else { + console.log(res) + } + + return null +} + +export const def: TabStatePlatformDef = { + loadTabStateFromSync, + writeCurrentTabState, +}