Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

Commit

Permalink
Quickfix: fix 'getState' error after insomnia update
Browse files Browse the repository at this point in the history
  • Loading branch information
Loïc Damet committed Aug 25, 2023
1 parent dbb2afc commit c6a7a3d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 38 deletions.
49 changes: 21 additions & 28 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 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
var getRouter = (): any => {

Check failure on line 6 in src/insomnia/state.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Unexpected var, use let or const instead
if (!router) {
const root = document.querySelector("#root") as Record<string, any>;

Check failure on line 8 in src/insomnia/state.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Strings must use singlequote

Check failure on line 8 in src/insomnia/state.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Extra semicolon
if (!root) {
return;

Check failure on line 10 in src/insomnia/state.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Extra semicolon
}
const parameter = Object.getOwnPropertyNames(root).findLast((x) => x.startsWith("__reactContainer"));

Check failure on line 12 in src/insomnia/state.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Strings must use singlequote

Check failure on line 12 in src/insomnia/state.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Extra semicolon
router = root[parameter as string]?.memoizedState?.element?.props?.router;

Check failure on line 13 in src/insomnia/state.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Extra semicolon
}
return router;

Check failure on line 15 in src/insomnia/state.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Extra semicolon
};

Check failure on line 16 in src/insomnia/state.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Extra semicolon

return store
}

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

Check failure on line 19 in src/insomnia/state.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Extra semicolon
return router?.state;
};

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

export const getActiveEnvironment = (): BaseDoc | null => {
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
export const getActiveEnvironmentId = (): string => {
const state = getState();
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 c6a7a3d

Please sign in to comment.