-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from NickRimmer/fix/navigation-new-implementation
Fix/navigation new implementation
- Loading branch information
Showing
23 changed files
with
274 additions
and
242 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
export * from './db' | ||
export * from './db.types' | ||
export * from './cleanup' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,11 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { getReactRefs, subscribeForRoutePushed } from './refs-react' | ||
import { subscibeForDbChangeEvents } from './refs-events' | ||
import { DocBaseModel } from '../types' | ||
import { notifyRequestSelected } from '../events/request-selected' | ||
import { notifyRequestUpdated } from '../events/request-updated' | ||
import { notifyRequestDeleted } from '../events/request-deleted' | ||
import { notifyRouteChanged } from '../events/route-changed' | ||
import { initConnection } from './refs-common' | ||
import { initRouter } from './refs-router' | ||
import { initEvents } from './refs-events' | ||
|
||
export const connect = (): boolean => { | ||
console.log('[plugin-navigator]', 'connecting to insomnia') | ||
initConnection() | ||
|
||
// get react references | ||
const reactRefs = getReactRefs() | ||
if (!reactRefs) { | ||
console.warn('[plugin-navigator]', 'react resources not found') | ||
return false | ||
} | ||
|
||
storeInternal = reactRefs.store | ||
routerInternal = reactRefs.router | ||
|
||
// connect to router | ||
subscribeForRoutePushed(routerInternal, (path: string) => notifyRouteChanged(path)) | ||
|
||
// connect to db changes | ||
subscibeForDbChangeEvents((doc, method) => { | ||
if (method === 'update' && doc.type === 'WorkspaceMeta') notifyRequestSelected(doc) | ||
if (method === 'update' && (doc.type === 'Request' || doc.type === 'GrpcRequest')) notifyRequestUpdated(doc) | ||
if (method === 'remove' && (doc.type === 'Request' || doc.type === 'GrpcRequest')) notifyRequestDeleted(doc) | ||
}) | ||
if (!initRouter()) return false | ||
initEvents() | ||
|
||
return true | ||
} | ||
|
||
// react store | ||
let storeInternal: any = {} | ||
export const getStore = (): any => storeInternal | ||
export const getState = (): any => storeInternal.getState().entities | ||
export const getAllRequests = (): Record<string, DocBaseModel> => { | ||
const result = {} | ||
|
||
Object.assign(result, getState().requests) | ||
Object.assign(result, getState().grpcRequests) | ||
|
||
return result | ||
} | ||
|
||
// react router | ||
let routerInternal: any = {} | ||
export const getRouter = (): any => routerInternal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { InsomniaDocBase, InsomniaRouter } from '../types' | ||
import { getRouter } from './refs-router' | ||
|
||
export const getData = (): InsomniaRouter['state']['loaderData'] => getRouter().state.loaderData | ||
export const getAllRequests = (): InsomniaDocBase[] => getData()[':workspaceId']['collection'].map(x => x.doc) | ||
export const getRequestOrUndefined = (requestId: string): InsomniaDocBase | undefined => getAllRequests().find(x => x._id === requestId) | ||
export const getActiveRequest = (): InsomniaDocBase | undefined => getData()['request/:requestId']['activeRequest'] | ||
export const getActiveWorkspace = (): InsomniaDocBase | undefined => getData()[':workspaceId'] && getData()[':workspaceId']['activeWorkspace'] | ||
export const getActiveProject = (): InsomniaDocBase | undefined => getData()[':workspaceId']['activeProject'] | ||
export const getActiveOrgId = (): string | undefined => getRouter().state.location.pathname.match(/^\/organization\/(org_[^/]+)/)?.[1] | ||
export const getAllWorkspaces = (): InsomniaDocBase[] => getData()['/project/:projectId']?.workspaces |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,70 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { DocBaseModel } from '../types' | ||
import { notifyRequestDeleted } from '../events/request-deleted' | ||
import { notifyRequestUpdated } from '../events/request-updated' | ||
import { InsomniaDocBase } from '../types' | ||
import { isCurrentConnectionStillActive } from './refs-common' | ||
import { subscribeForRouteChanged } from './refs-router' | ||
import { getRequestOrUndefined } from './refs-data' | ||
import { notifyRequestSelected } from '../events/request-selected' | ||
import { notifyDebugPageOpenChanged } from '../events/debug-page-opened' | ||
|
||
export const subscibeForEvents = (channelName: string, callback: (data: any) => void): void => { | ||
const unsubscribeMethod = (window as any).main.on(channelName, (_: never, data: any) => { | ||
if (!isCurrentConnectionStillActive()) { | ||
unsubscribeMethod() | ||
return | ||
} | ||
export const initEvents = (): void => { | ||
subscibeForDbChangeEvents((doc, method) => { | ||
if (method === 'update' && (doc.type === 'Request' || doc.type === 'GrpcRequest')) notifyRequestUpdated(doc) | ||
if (method === 'remove' && (doc.type === 'Request' || doc.type === 'GrpcRequest')) notifyRequestDeleted(doc) | ||
}) | ||
|
||
callback(data) | ||
subscribeForRouteChanged((path: string) => { | ||
if (isRouteDebugView(path)) { | ||
notifyDebugPageOpenChanged(true) | ||
const requestId = extractRequestId(path) | ||
if (!requestId) { | ||
console.warn('[plugin-navigator]', 'cannot extract request id from router path', path) | ||
return | ||
} | ||
|
||
const doc = getRequestOrUndefined(requestId) | ||
if (!doc) { | ||
console.warn('[plugin-navigator]', 'request not found', requestId) | ||
return | ||
} | ||
|
||
notifyRequestSelected(doc) | ||
} else { | ||
notifyDebugPageOpenChanged(false) | ||
} | ||
}) | ||
} | ||
|
||
export const subscibeForDbChangeEvents = (callback: (data: DocBaseModel, method: string) => void): void => { | ||
subscibeForEvents('db.changes', (changes: any[][]) => changes.forEach((data) => { | ||
// console.log('[plugin-navigator]', 'db.changes', data) | ||
|
||
const subscibeForDbChangeEvents = (callback: (data: InsomniaDocBase, method: string) => void): void => { | ||
subscribeForChannelEvents('db.changes', (changes: any[][]) => changes.forEach((data) => { | ||
if (data.length != 3) { | ||
console.warn('[plugin-navigator]', 'unexpected data recevied in db.changes channel', data) | ||
return | ||
} | ||
|
||
const method = data[0] | ||
const doc = data[1] as DocBaseModel | ||
const doc = data[1] as InsomniaDocBase | ||
|
||
if (!method || !doc) { | ||
console.warn('[plugin-navigator]', 'unexpected method or doc recevied in db.changes channel', data) | ||
return | ||
} | ||
|
||
// console.log('[plugin-navigator]', 'db.changes', method, doc) | ||
callback(doc, method) | ||
})) | ||
} | ||
|
||
const subscribeForChannelEvents = (channelName: string, callback: (data: any) => void): void => { | ||
const unsubscribeMethod = (window as any).main.on(channelName, (_: never, data: any) => { | ||
if (!isCurrentConnectionStillActive()) { | ||
unsubscribeMethod() | ||
return | ||
} | ||
|
||
callback(data) | ||
}) | ||
} | ||
|
||
const isRouteDebugView = (route: string) => /\/organization\/org_[^/]+\/project\/proj_[^/]+\/workspace\/wrk_[^/]+\/debug/.test(route) | ||
const extractRequestId = (route: string) => route.match(/\/((req_|greq_)[0-9a-z]+)$/)?.[1] |
27 changes: 13 additions & 14 deletions
27
...services/insomnia/connector/refs-react.ts → ...ervices/insomnia/connector/refs-router.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,35 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { InsomniaRouter, InsomniaRouterEvent } from '../types' | ||
import { isCurrentConnectionStillActive } from './refs-common' | ||
|
||
export type ReactRefs = { | ||
store: any; | ||
router: any; | ||
} | ||
let internalRouter: InsomniaRouter = {} as InsomniaRouter | ||
|
||
export const getReactRefs = (): ReactRefs | null => { | ||
export const initRouter = (): boolean => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const rootElement = document.querySelector('#root') as any | ||
if (!rootElement) { | ||
console.error('[plugin-navigator]', 'root element not found') | ||
return null | ||
return false | ||
} | ||
|
||
const containerElement = Object.getOwnPropertyNames(rootElement).findLast(x => x.startsWith('__reactContainer')) | ||
if (!containerElement) { | ||
console.warn('[plugin-navigator]', 'store container element not found') | ||
return null | ||
return false | ||
} | ||
|
||
return { | ||
store: rootElement[containerElement].memoizedState.element.props.store, | ||
router: rootElement[containerElement].memoizedState.element.props.children.props.router | ||
} | ||
internalRouter = rootElement[containerElement].memoizedState.element.props.router | ||
return true | ||
} | ||
|
||
export const subscribeForRoutePushed = (router: any, callback: (path: string) => void): void => { | ||
const routerUnsubscribeMethod = router.subscribe((data: any) => { | ||
export const getRouter = (): InsomniaRouter => internalRouter | ||
|
||
export const subscribeForRouteChanged = (callback: (path: string) => void): void => { | ||
const routerUnsubscribeMethod = internalRouter.subscribe((data: InsomniaRouterEvent) => { | ||
if (!isCurrentConnectionStillActive()) { | ||
routerUnsubscribeMethod() | ||
return | ||
} | ||
|
||
if (data.historyAction === 'PUSH') callback(data.location?.pathname) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
let lastValue: boolean | undefined = undefined | ||
type Listener = (opened: boolean) => void | ||
const listeners = new Set<Listener>() | ||
|
||
export const notifyDebugPageOpenChanged = (opened: boolean) => { | ||
if (lastValue === opened) return | ||
|
||
listeners.forEach((listener) => listener(opened)) | ||
lastValue = opened | ||
} | ||
|
||
export const onDebugPageOpenChanged = (listener: Listener): (() => void) => { | ||
listeners.add(listener) | ||
return () => listeners.delete(listener) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.