From 8edad7ded7e1008460ec3de6171a4c1f95c18e8c Mon Sep 17 00:00:00 2001 From: 0xc0Der <59133155+0xc0Der@users.noreply.github.com> Date: Wed, 3 Nov 2021 19:45:52 +0200 Subject: [PATCH] persist environment selection. (#1925) * feat: persist environment selection * refactor: minor change to selectedEnvIndex calculation Co-authored-by: Liyas Thomas Co-authored-by: Andrew Bastin --- .../newstore/localpersistence.ts | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/packages/hoppscotch-app/newstore/localpersistence.ts b/packages/hoppscotch-app/newstore/localpersistence.ts index d7a2879d2..804a62f39 100644 --- a/packages/hoppscotch-app/newstore/localpersistence.ts +++ b/packages/hoppscotch-app/newstore/localpersistence.ts @@ -3,6 +3,8 @@ import clone from "lodash/clone" import assign from "lodash/assign" import isEmpty from "lodash/isEmpty" +import * as O from "fp-ts/Option" +import { pipe } from "fp-ts/function" import { settingsStore, bulkApplySettings, @@ -34,6 +36,8 @@ import { addGlobalEnvVariable, setGlobalEnvVariables, globalEnv$, + selectedEnvIndex$, + setCurrentEnvironment, } from "./environments" import { restRequest$, setRESTRequest } from "./RESTSession" import { translateToNewRequest } from "~/helpers/types/HoppRESTRequest" @@ -185,6 +189,29 @@ function setupEnvironmentsPersistence() { }) } +function setupSelectedEnvPersistence() { + const selectedEnvIndex = + pipe( + // Value from local storage can be nullable + O.fromNullable( + window.localStorage.getItem("selectedEnvIndex") + ), + O.map(parseInt), // If not null, parse to integer + O.chain( + O.fromPredicate( + Number.isInteger // Check if the number is proper int (not NaN) + ) + ), + O.getOrElse(() => -1) // If all the above conditions pass, we are good, else set default value (-1) + ) + + setCurrentEnvironment(selectedEnvIndex) + + selectedEnvIndex$.subscribe((index) => { + window.localStorage.setItem("selectedEnvIndex", index.toString()) + }) +} + function setupGlobalEnvsPersistence() { const globals: Environment["variables"] = JSON.parse( window.localStorage.getItem("globalEnv") || "[]" @@ -221,6 +248,7 @@ export function setupLocalPersistence() { setupCollectionsPersistence() setupGlobalEnvsPersistence() setupEnvironmentsPersistence() + setupSelectedEnvPersistence() } /**