Skip to content

Commit

Permalink
Quickfix: fix 'getState' error after insomnia update (#8)
Browse files Browse the repository at this point in the history
* Quickfix: fix 'getState' error after insomnia update

* lint fixes

---------

Co-authored-by: Loïc Damet <loic.damet.e@thalesdigital.io>
Co-authored-by: Nick Rimmer <NickRimmer@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 26, 2023
1 parent dbb2afc commit 3487970
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 33 deletions.
39 changes: 16 additions & 23 deletions src/insomnia/state.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,30 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import { BaseDoc } from './insomnia.types'

let store: any = null
let router: any = null

export const getStore = (): any => {
if (!store) {
const getRouter = (): any => {
if (!router) {
const root = document.querySelector('#root') as Record<string, any>
const parameter = Object.getOwnPropertyNames(root).findLast(x => x.startsWith('__reactContainer')) as string
store = root[parameter].memoizedState.element.props.store
if (!root) {
return
}
const parameter = Object.getOwnPropertyNames(root).findLast((x) => x.startsWith('__reactContainer'))
router = root[parameter as string]?.memoizedState?.element?.props?.router
}

return store
return router
}

export const getState = (): any => {
const store = getStore()
return store.getState()
export const getState = () => {
const router = getRouter()
return router?.state
}

export const getActiveWorkspace = (): BaseDoc => {
export const getActiveWorkspaceId = (): string => {
const state = getState()
const workspaceId = state.global.activeWorkspaceId
const workspace = state.entities.workspaces[workspaceId]
return workspace
return state?.loaderData[':workspaceId'].activeWorkspace._id
}

export const getActiveEnvironment = (): BaseDoc | null => {
export const getActiveEnvironmentId = (): string => {
const state = getState()
const activeWorkspaceId = getActiveWorkspace()._id
const activeWorkspaceMeta = (Object.values(state.entities.workspaceMetas) as any[]).find(x => x.parentId === activeWorkspaceId) as BaseDoc
const activeEnvironmentId = activeWorkspaceMeta.activeEnvironmentId
const activeEnvironment = state.entities.environments[activeEnvironmentId] ?? null as (BaseDoc | null)

return activeEnvironment
return state?.loaderData[':workspaceId'].activeEnvironment._id
}
20 changes: 10 additions & 10 deletions src/services/main.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { BaseDoc, subscribe, getActiveEnvironment, getActiveWorkspace } from '../insomnia'
import { BaseDoc, subscribe, getActiveEnvironmentId, getActiveWorkspaceId } from '../insomnia'
import { database } from '../services/db'
import { StoredToken } from './main.types'

const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))

const onTokenUpdate = (token: BaseDoc) => {
const activeWorkspace = getActiveWorkspace()
const activeEnvironment = getActiveEnvironment()
const activeWorkspaceId = getActiveWorkspaceId()
const activeEnvironmentId = getActiveEnvironmentId()
// console.log('[oa2-mate] Token updated', { requestId: token.parentId, accessToken: token.accessToken })

const tokenToStore: StoredToken = {
accessToken: token.accessToken,
environmentId: activeEnvironment?._id ?? null,
workspaceId: activeWorkspace._id,
environmentId: activeEnvironmentId ?? null,
workspaceId: activeWorkspaceId,
createdAt: Date.now(),
}

Expand Down Expand Up @@ -61,14 +61,14 @@ export const init = async () => {
}

export const getCurrentTokenAsync = async (): Promise<StoredToken | null> => {
const activeWorkspace = getActiveWorkspace()
const activeEnvironment = getActiveEnvironment()
return await getTokenAsync(activeWorkspace._id, activeEnvironment?._id ?? null)
const activeWorkspaceId = getActiveWorkspaceId()
const activeEnvironmentId = getActiveEnvironmentId()
return await getTokenAsync(activeWorkspaceId, activeEnvironmentId ?? null)
}

export const getLatestTokenAsync = async (): Promise<StoredToken | null> => {
const activeWorkspace = getActiveWorkspace()
const tokens = await getTokensAsync(activeWorkspace._id)
const activeWorkspaceId = getActiveWorkspaceId()
const tokens = await getTokensAsync(activeWorkspaceId)
if (tokens.length === 0) return null
return tokens.reduce((prev, curr) => prev.createdAt > curr.createdAt ? prev : curr, tokens[0] ?? null)
}

0 comments on commit 3487970

Please sign in to comment.