Skip to content

Commit a4ef3de

Browse files
authored
feat(diag): Save and restore current diagnostics tab when panel leaves and enters focus. (#238)
1 parent d1ff75b commit a4ef3de

File tree

1 file changed

+40
-4
lines changed
  • webview-ui/src/diagnostics_panel

1 file changed

+40
-4
lines changed

webview-ui/src/diagnostics_panel/App.tsx

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { VSCodePanelTab, VSCodePanelView, VSCodePanels } from '@vscode/webview-ui-toolkit/react';
44
import './App.css';
55
import { StatGroupSelectionBox } from './controls/StatGroupSelectionBox';
6-
import { useCallback, useState } from 'react';
6+
import { useCallback, useEffect, useState } from 'react';
77
import { StatisticType, YAxisType, createStatResolver } from './StatisticResolver';
88
import MinecraftStatisticLineChart from './controls/MinecraftStatisticLineChart';
99
import MinecraftStatisticStackedLineChart from './controls/MinecraftStatisticStackedLineChart';
@@ -12,6 +12,16 @@ import { MultipleStatisticProvider, SimpleStatisticProvider, StatisticUpdatedMes
1212

1313
import * as statPrefabs from './StatisticPrefabs';
1414

15+
const vscode = acquireVsCodeApi();
16+
17+
interface TabState {
18+
tabId: string;
19+
}
20+
21+
interface VSCodePanelsChangeEvent extends Event {
22+
target: EventTarget & { activeid: string };
23+
}
24+
1525
// Filter out events with a value of zero that haven't been previously subscribed to
1626
function constructSubscribedSignalFilter() {
1727
const nonFilteredValues: string[] = [];
@@ -31,22 +41,48 @@ function constructSubscribedSignalFilter() {
3141
function App() {
3242
// State
3343
const [selectedPlugin, setSelectedPlugin] = useState<string>('no_plugin_selected');
44+
const [selectedClient, setSelectedClient] = useState<string>('no_client_selected');
45+
const [currentTab, setCurrentTab] = useState<string>();
46+
47+
// Load initial state from vscode
48+
useEffect(() => {
49+
const tabState = vscode.getState() as TabState;
50+
if (tabState && tabState.tabId) {
51+
setCurrentTab(tabState.tabId);
52+
}
53+
}, []);
54+
55+
// Save current tab state whenever it changes
56+
useEffect(() => {
57+
if (currentTab) {
58+
const tabState: TabState = { tabId: currentTab };
59+
vscode.setState(tabState);
60+
}
61+
}, [currentTab]);
3462

3563
const handlePluginSelection = useCallback((pluginSelectionId: string) => {
3664
console.log(`Selected Plugin: ${pluginSelectionId}`);
3765
setSelectedPlugin(() => pluginSelectionId);
3866
}, []);
3967

40-
const [selectedClient, setSelectedClient] = useState<string>('no_client_selected');
41-
4268
const handleClientSelection = useCallback((clientSelectionId: string) => {
4369
console.log(`Selected Client: ${clientSelectionId}`);
4470
setSelectedClient(() => clientSelectionId);
4571
}, []);
4672

73+
const handlePanelChange = useCallback((event: VSCodePanelsChangeEvent): void => {
74+
const newTabId = event.target.activeid;
75+
if (newTabId) {
76+
setCurrentTab(newTabId);
77+
}
78+
}, []);
79+
4780
return (
4881
<main>
49-
<VSCodePanels>
82+
<VSCodePanels
83+
activeid={currentTab}
84+
onChange={event => handlePanelChange(event as VSCodePanelsChangeEvent)}
85+
>
5086
<VSCodePanelTab id="tab-1">World</VSCodePanelTab>
5187
<VSCodePanelTab id="tab-2">Memory</VSCodePanelTab>
5288
<VSCodePanelTab id="tab-3">Server Timing</VSCodePanelTab>

0 commit comments

Comments
 (0)