Skip to content

Commit

Permalink
feat(diag): Save and restore current diagnostics tab when panel leave…
Browse files Browse the repository at this point in the history
…s and enters focus. (#238)
  • Loading branch information
chmeyer-ms authored Oct 8, 2024
1 parent d1ff75b commit a4ef3de
Showing 1 changed file with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions webview-ui/src/diagnostics_panel/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { VSCodePanelTab, VSCodePanelView, VSCodePanels } from '@vscode/webview-ui-toolkit/react';
import './App.css';
import { StatGroupSelectionBox } from './controls/StatGroupSelectionBox';
import { useCallback, useState } from 'react';
import { useCallback, useEffect, useState } from 'react';
import { StatisticType, YAxisType, createStatResolver } from './StatisticResolver';
import MinecraftStatisticLineChart from './controls/MinecraftStatisticLineChart';
import MinecraftStatisticStackedLineChart from './controls/MinecraftStatisticStackedLineChart';
Expand All @@ -12,6 +12,16 @@ import { MultipleStatisticProvider, SimpleStatisticProvider, StatisticUpdatedMes

import * as statPrefabs from './StatisticPrefabs';

const vscode = acquireVsCodeApi();

interface TabState {
tabId: string;
}

interface VSCodePanelsChangeEvent extends Event {
target: EventTarget & { activeid: string };
}

// Filter out events with a value of zero that haven't been previously subscribed to
function constructSubscribedSignalFilter() {
const nonFilteredValues: string[] = [];
Expand All @@ -31,22 +41,48 @@ function constructSubscribedSignalFilter() {
function App() {
// State
const [selectedPlugin, setSelectedPlugin] = useState<string>('no_plugin_selected');
const [selectedClient, setSelectedClient] = useState<string>('no_client_selected');
const [currentTab, setCurrentTab] = useState<string>();

// Load initial state from vscode
useEffect(() => {
const tabState = vscode.getState() as TabState;
if (tabState && tabState.tabId) {
setCurrentTab(tabState.tabId);
}
}, []);

// Save current tab state whenever it changes
useEffect(() => {
if (currentTab) {
const tabState: TabState = { tabId: currentTab };
vscode.setState(tabState);
}
}, [currentTab]);

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

const [selectedClient, setSelectedClient] = useState<string>('no_client_selected');

const handleClientSelection = useCallback((clientSelectionId: string) => {
console.log(`Selected Client: ${clientSelectionId}`);
setSelectedClient(() => clientSelectionId);
}, []);

const handlePanelChange = useCallback((event: VSCodePanelsChangeEvent): void => {
const newTabId = event.target.activeid;
if (newTabId) {
setCurrentTab(newTabId);
}
}, []);

return (
<main>
<VSCodePanels>
<VSCodePanels
activeid={currentTab}
onChange={event => handlePanelChange(event as VSCodePanelsChangeEvent)}
>
<VSCodePanelTab id="tab-1">World</VSCodePanelTab>
<VSCodePanelTab id="tab-2">Memory</VSCodePanelTab>
<VSCodePanelTab id="tab-3">Server Timing</VSCodePanelTab>
Expand Down

0 comments on commit a4ef3de

Please sign in to comment.