feat: remember last selected team id (#2210)

Co-authored-by: Liyas Thomas <liyascthomas@gmail.com>
Co-authored-by: Andrew Bastin <andrewbastin.k@gmail.com>
This commit is contained in:
Anwarul Islam
2022-04-02 15:47:37 +06:00
committed by GitHub
parent 183d5f3545
commit 8cafef48fb
4 changed files with 98 additions and 4 deletions

View File

@@ -53,6 +53,7 @@ import { WSRequest$, setWSRequest } from "./WebSocketSession"
import { SIORequest$, setSIORequest } from "./SocketIOSession"
import { SSERequest$, setSSERequest } from "./SSESession"
import { MQTTRequest$, setMQTTRequest } from "./MQTTSession"
import { bulkApplyLocalState, localStateStore } from "./localstate"
function checkAndMigrateOldSettings() {
const vuexData = JSON.parse(window.localStorage.getItem("vuex") || "{}")
@@ -114,6 +115,18 @@ function checkAndMigrateOldSettings() {
}
}
function setupLocalStatePersistence() {
const localStateData = JSON.parse(
window.localStorage.getItem("localState") ?? "{}"
)
if (localStateData) bulkApplyLocalState(localStateData)
localStateStore.subject$.subscribe((state) => {
window.localStorage.setItem("localState", JSON.stringify(state))
})
}
function setupSettingsPersistence() {
const settingsData = JSON.parse(
window.localStorage.getItem("settings") || "{}"
@@ -313,6 +326,7 @@ function setupRequestPersistence() {
export function setupLocalPersistence() {
checkAndMigrateOldSettings()
setupLocalStatePersistence()
setupSettingsPersistence()
setupRequestPersistence()
setupHistoryPersistence()

View File

@@ -0,0 +1,67 @@
import { Ref } from "@nuxtjs/composition-api"
import { distinctUntilChanged, pluck } from "rxjs"
import DispatchingStore, { defineDispatchers } from "./DispatchingStore"
import { useStream } from "~/helpers/utils/composables"
type LocalState = {
REMEMBERED_TEAM_ID: string | undefined
}
const defaultLocalState: LocalState = {
REMEMBERED_TEAM_ID: undefined,
}
const dispatchers = defineDispatchers({
bulkApplyState(_currentState: LocalState, payload: Partial<LocalState>) {
return payload
},
applyState<K extends keyof LocalState>(
_currentState: LocalState,
{ key, value }: { key: K; value: LocalState[K] }
) {
const result: Partial<LocalState> = {
[key]: value,
}
return result
},
})
export const localStateStore = new DispatchingStore(
defaultLocalState,
dispatchers
)
export const localState$ = localStateStore.subject$.asObservable()
export function bulkApplyLocalState(obj: Partial<LocalState>) {
localStateStore.dispatch({
dispatcher: "bulkApplyState",
payload: obj,
})
}
export function applyLocalState<K extends keyof LocalState>(
key: K,
value: LocalState[K]
) {
localStateStore.dispatch({
dispatcher: "applyState",
payload: { key, value },
})
}
export function useLocalState<K extends keyof LocalState>(
key: K
): Ref<LocalState[K]> {
return useStream(
localStateStore.subject$.pipe(pluck(key), distinctUntilChanged()),
localStateStore.value[key],
(value: LocalState[K]) => {
localStateStore.dispatch({
dispatcher: "applyState",
payload: { key, value },
})
}
)
}