Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
show stored tabs on start
Browse files Browse the repository at this point in the history
  • Loading branch information
NickRimmer committed Jul 21, 2023
1 parent 90a715c commit f187370
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 44 deletions.
3 changes: 2 additions & 1 deletion src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { WindowApp, WindowMain } from './insomnia/insomnia.types'
declare global {
interface Window {
app: WindowApp,
main: WindowMain
main: WindowMain,
dev: any,
}
}

Expand Down
7 changes: 1 addition & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@ import * as ui from './ui'
import { cleanupWorkspacesAsync } from './services/db'

const initAsync = async () => {
// can be used during development, to be able to access insomnia instance from console for experiments
// (global as any).dev = {
// insomnia
// }

// initialize ui components
ui.render()
await ui.renderAsync()
await cleanupWorkspacesAsync()
console.log('[plugin-navigator]', 'initialized')
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/insomnia/connector/refs-react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const getReactRefs = (): ReactRefs | null => {

const containerElement = Object.getOwnPropertyNames(rootElement).findLast(x => x.startsWith('__reactContainer'))
if (!containerElement) {
console.error('[plugin-navigator]', 'store container element not found')
console.warn('[plugin-navigator]', 'store container element not found')
return null
}

Expand Down
13 changes: 8 additions & 5 deletions src/ui/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import { TabsPanel } from './tabs-panel'
import React from 'react'
import './index.styles.scss'

export const render = () => {
export const renderAsync = (): Promise<void> => new Promise((resolve, reject) => {
// find app header
const header: HTMLElement | null = document.querySelector('.app-header')
if (!header) throw new Error('Root element not found')
if (!header) {
reject(new Error('App header not found'))
return
}

// check if root element already exists
const existsRoot: HTMLElement | null = document.querySelector('#plugin-request-navigator-hub')
Expand All @@ -21,7 +24,7 @@ export const render = () => {

ReactDOM.render(
(<TabsPanel />),
root
root,
() => resolve()
)

}
})
68 changes: 37 additions & 31 deletions src/ui/tabs-panel/tabs-panel.hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useEffect, useRef, useState } from 'react'
import { TabData } from './tabs-panel.types'
import { onRouteChanged } from '../../services/insomnia/events/route-changed'
import { database, Workspace, WorkspaceTab } from '../../services/db'
import { getStore, getAllRequests } from '../../services/insomnia/connector'
import { getStore, getAllRequests, getRouter } from '../../services/insomnia/connector'
import { getRequestMethodName } from '../../services/helpers'

const debugViewRouteTemplate = /\/organization\/org_[^/]+\/project\/proj_[^/]+\/workspace\/wrk_[^/]+\/debug/gm
Expand All @@ -15,43 +15,49 @@ export const useTabsPanel = () => {
// reset tabs on route change
useEffect(() => {
onRouteChanged(route => {
if (debugViewRouteTemplate.test(route)) {
const store = getStore()
const state = store.getState()
const workspaceId = state.global.activeWorkspaceId
database.findOne<Workspace>({ workspaceId }, (err, storeWorkspace) => {
if (err) {
// if not able to load workspace tabs
console.error('[plugin-navigator]', 'error loading workspace tabs', err)
_setTabs([])
} else if (!storeWorkspace || storeWorkspace.tabs.length === 0) {
// if no saved tabs for workspace
_setTabs([])
} else {
// try to restore saved tabs
const allRequests = getAllRequests()
const loadedTabs = storeWorkspace
.tabs // all saved tabs for workspace
.map(x => ({ entity: x, doc: allRequests[x.requestId] })) // try to find request by id
.filter(x => x.doc) // filter out not found requests
.map<TabData>(x => { // map to tab data
const method = getRequestMethodName(x.doc)
return ({ requestId: x.doc._id, isActive: x.entity.isActive ?? false, method, title: x.doc.name })
}) || [] as TabData[]

// fix active tab
if (loadedTabs.length > 0 && loadedTabs.filter(x => x.isActive).length === 0) loadedTabs[0].isActive = true
setTabs(loadedTabs)
}
})
} else if (tabDataRef.current.length > 0) _setTabs([])
if (debugViewRouteTemplate.test(route)) requestChanged()
else if (tabDataRef.current.length > 0) _setTabs([])
})

if (debugViewRouteTemplate.test(getRouter().state.location.pathname))
requestChanged()
}, [])

useEffect(() => {
tabDataRef.current = tabs
}, [tabs])

const requestChanged = () => {
const store = getStore()
const state = store.getState()
const workspaceId = state.global.activeWorkspaceId
database.findOne<Workspace>({ workspaceId }, (err, storeWorkspace) => {
if (err) {
// if not able to load workspace tabs
console.error('[plugin-navigator]', 'error loading workspace tabs', err)
_setTabs([])
} else if (!storeWorkspace || storeWorkspace.tabs.length === 0) {
// if no saved tabs for workspace
_setTabs([])
} else {
// try to restore saved tabs
const allRequests = getAllRequests()
const loadedTabs = storeWorkspace
.tabs // all saved tabs for workspace
.map(x => ({ entity: x, doc: allRequests[x.requestId] })) // try to find request by id
.filter(x => x.doc) // filter out not found requests
.map<TabData>(x => { // map to tab data
const method = getRequestMethodName(x.doc)
return ({ requestId: x.doc._id, isActive: x.entity.isActive ?? false, method, title: x.doc.name })
}) || [] as TabData[]

// fix active tab
if (loadedTabs.length > 0 && loadedTabs.filter(x => x.isActive).length === 0) loadedTabs[0].isActive = true
setTabs(loadedTabs)
}
})
}

const setTabs = (tabs: TabData[]) => {
const store = getStore()
const state = store.getState()
Expand Down

0 comments on commit f187370

Please sign in to comment.