Skip to content

Commit

Permalink
fix: tabs: restoring tree state from session data in some failure cases
Browse files Browse the repository at this point in the history
This and 05aedbb, d21f816, 5979d7a, a2c6a59, c83df0b, 0d585f0 should
fix or at least decrease probability of problems related with broken tabs
structure on init (like #1507, #262, #267, and so on...)
  • Loading branch information
mbnuqw committed Apr 22, 2024
1 parent b0c1c38 commit ede4d93
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions src/services/tabs.fg.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,6 @@ function restoreTabsFromCache(
): Tab[] {
Logs.info('Tabs.restoreTabsFromCache')

let logWrongPanels: Record<string, null> | undefined
const firstPanelId = lastPanel.id
const idsMap: Record<ID, ID> = {}
const tabs: Tab[] = []
Expand Down Expand Up @@ -393,8 +392,6 @@ function restoreTabsFromCache(
// Normalize panelId
const panel = Sidebar.panelsById[tab.panelId]
if (!panel) {
if (!logWrongPanels) logWrongPanels = {}
logWrongPanels[tab.panelId] = null
tab.panelId = lastPanel.id
} else {
if (!tab.pinned) {
Expand All @@ -414,10 +411,6 @@ function restoreTabsFromCache(
tabs.push(tab)
}

if (logWrongPanels) {
Logs.warn('Tabs loading: Cannot find panels: ' + Object.keys(logWrongPanels).join(' '))
}

return tabs
}

Expand All @@ -428,7 +421,6 @@ function restoreTabsFromSessionData(
): Tab[] {
Logs.info('Tabs.restoreTabsFromSessionData')

let logWrongPanels: Record<string, null> | undefined
const firstPanelId = lastPanel.id
const idsMap: Record<ID, ID> = {}
const tabs: Tab[] = []
Expand Down Expand Up @@ -462,17 +454,7 @@ function restoreTabsFromSessionData(

// Normalize panelId
const panel = Sidebar.panelsById[tab.panelId]
if (!panel) {
if (!logWrongPanels) logWrongPanels = {}
logWrongPanels[tab.panelId] = null
tab.panelId = lastPanel.id
} else {
if (!tab.pinned) {
// Check order of panels
if (panel.index < lastPanel.index) tab.panelId = lastPanel.id
else lastPanel = panel
}
}
if (!panel) tab.panelId = lastPanel.id

// Use openerTabId as fallback for parentId
if (tab.parentId === -1 && tab.openerTabId !== undefined && Tabs.byId[tab.openerTabId]) {
Expand All @@ -484,8 +466,28 @@ function restoreTabsFromSessionData(
tabs.push(tab)
}

if (logWrongPanels) {
Logs.warn('Tabs loading: Cannot find panels: ' + Object.keys(logWrongPanels).join(' '))
let lastPanelIndex = -1
for (const panel of Sidebar.panels) {
if (!Utils.isTabsPanel(panel)) continue

let prevTabIndex = -1
for (let ti = 0; ti < tabs.length; ti++) {
const tab = tabs[ti]
if (tab.pinned) continue

if (tab.panelId !== panel.id) continue

if (tab.index <= lastPanelIndex || (prevTabIndex !== -1 && tab.index - 1 !== prevTabIndex)) {
let prevTab = tabs[ti - 1] as Tab | undefined
if (prevTab?.pinned) prevTab = undefined
tab.panelId = prevTab?.panelId ?? firstPanelId
continue
}

prevTabIndex = tab.index
}

lastPanelIndex = prevTabIndex
}

return tabs
Expand Down

0 comments on commit ede4d93

Please sign in to comment.