Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

chore: remove selectedRowWidgetId from provider (#10547)

Ashwin Bhatkal996c9a891f25
diff --git a/frontend/src/container/DashboardContainer/DashboardDescription/__tests__/DashboardDescription.test.tsx b/frontend/src/container/DashboardContainer/DashboardDescription/__tests__/DashboardDescription.test.tsx
index d71065f332..be80adc656 100644
--- a/frontend/src/container/DashboardContainer/DashboardDescription/__tests__/DashboardDescription.test.tsx
+++ b/frontend/src/container/DashboardContainer/DashboardDescription/__tests__/DashboardDescription.test.tsx
@@ -204,8 +204,6 @@ describe('Dashboard landing page actions header tests', () => {
updateLocalStorageDashboardVariables: jest.fn(),
dashboardQueryRangeCalled: false,
setDashboardQueryRangeCalled: jest.fn(),
- selectedRowWidgetId: null,
- setSelectedRowWidgetId: jest.fn(),
isDashboardFetching: false,
columnWidths: {},
setColumnWidths: jest.fn(),
diff --git a/frontend/src/container/DashboardContainer/DashboardDescription/index.tsx b/frontend/src/container/DashboardContainer/DashboardDescription/index.tsx
index e9a779ae09..2d3c60b260 100644
--- a/frontend/src/container/DashboardContainer/DashboardDescription/index.tsx
+++ b/frontend/src/container/DashboardContainer/DashboardDescription/index.tsx
@@ -78,7 +78,6 @@ function DashboardDescription(props: DashboardDescriptionProps): JSX.Element {
isDashboardLocked,
setSelectedDashboard,
handleToggleDashboardSlider,
- setSelectedRowWidgetId,
handleDashboardLockToggle,
} = useDashboard();
@@ -146,7 +145,6 @@ function DashboardDescription(props: DashboardDescriptionProps): JSX.Element {
const [addPanelPermission] = useComponentPermission(permissions, userRole);
const onEmptyWidgetHandler = useCallback(() => {
- setSelectedRowWidgetId(null);
handleToggleDashboardSlider(true);
logEvent('Dashboard Detail: Add new panel clicked', {
dashboardId: selectedDashboard?.id,
diff --git a/frontend/src/container/GridCardLayout/DashboardEmptyState/DashboardEmptyState.tsx b/frontend/src/container/GridCardLayout/DashboardEmptyState/DashboardEmptyState.tsx
index 4a7bf908c1..46415af904 100644
--- a/frontend/src/container/GridCardLayout/DashboardEmptyState/DashboardEmptyState.tsx
+++ b/frontend/src/container/GridCardLayout/DashboardEmptyState/DashboardEmptyState.tsx
@@ -19,7 +19,6 @@ export default function DashboardEmptyState(): JSX.Element {
selectedDashboard,
isDashboardLocked,
handleToggleDashboardSlider,
- setSelectedRowWidgetId,
} = useDashboard();
const variablesSettingsTabHandle = useRef<VariablesSettingsTab>(null);
@@ -42,7 +41,6 @@ export default function DashboardEmptyState(): JSX.Element {
const [addPanelPermission] = useComponentPermission(permissions, userRole);
const onEmptyWidgetHandler = useCallback(() => {
- setSelectedRowWidgetId(null);
handleToggleDashboardSlider(true);
logEvent('Dashboard Detail: Add new panel clicked', {
dashboardId: selectedDashboard?.id,
diff --git a/frontend/src/container/GridCardLayout/GridCardLayout.tsx b/frontend/src/container/GridCardLayout/GridCardLayout.tsx
index 50bc9bb63d..88cd9df67d 100644
--- a/frontend/src/container/GridCardLayout/GridCardLayout.tsx
+++ b/frontend/src/container/GridCardLayout/GridCardLayout.tsx
@@ -71,7 +71,6 @@ function GraphLayout(props: GraphLayoutProps): JSX.Element {
isDashboardLocked,
dashboardQueryRangeCalled,
setDashboardQueryRangeCalled,
- setSelectedRowWidgetId,
isDashboardFetching,
columnWidths,
} = useDashboard();
@@ -195,7 +194,6 @@ function GraphLayout(props: GraphLayoutProps): JSX.Element {
updateDashboardMutation.mutate(updatedDashboard, {
onSuccess: (updatedDashboard) => {
- setSelectedRowWidgetId(null);
if (updatedDashboard.data) {
if (updatedDashboard.data.data.layout) {
setLayouts(sortLayout(updatedDashboard.data.data.layout));
diff --git a/frontend/src/container/GridCardLayout/WidgetRow.tsx b/frontend/src/container/GridCardLayout/WidgetRow.tsx
index 9f7b5180d2..f6f9f350b5 100644
--- a/frontend/src/container/GridCardLayout/WidgetRow.tsx
+++ b/frontend/src/container/GridCardLayout/WidgetRow.tsx
@@ -5,6 +5,7 @@ import useComponentPermission from 'hooks/useComponentPermission';
import { EllipsisIcon, PenLine, Plus, X } from 'lucide-react';
import { useAppContext } from 'providers/App/App';
import { useDashboard } from 'providers/Dashboard/Dashboard';
+import { setSelectedRowWidgetId } from 'providers/Dashboard/helpers/selectedRowWidgetIdHelper';
import { ROLES, USER_ROLES } from 'types/roles';
import { ComponentTypes } from 'utils/permission';
@@ -37,7 +38,6 @@ export function WidgetRowHeader(props: WidgetRowHeaderProps): JSX.Element {
handleToggleDashboardSlider,
selectedDashboard,
isDashboardLocked,
- setSelectedRowWidgetId,
} = useDashboard();
const permissions: ComponentTypes[] = ['add_panel'];
@@ -81,7 +81,12 @@ export function WidgetRowHeader(props: WidgetRowHeaderProps): JSX.Element {
disabled={!editWidget && addPanelPermission && !isDashboardLocked}
icon={<Plus size={14} />}
onClick={(): void => {
- setSelectedRowWidgetId(id);
+ // TODO: @AshwinBhatkal Simplify this check in cleanup of https://github.com/SigNoz/engineering-pod/issues/3953
+ if (!selectedDashboard?.id) {
+ return;
+ }
+
+ setSelectedRowWidgetId(selectedDashboard.id, id);
handleToggleDashboardSlider(true);
}}
>
diff --git a/frontend/src/container/NewWidget/index.tsx b/frontend/src/container/NewWidget/index.tsx
index 2102438569..ae1c3f2036 100644
--- a/frontend/src/container/NewWidget/index.tsx
+++ b/frontend/src/container/NewWidget/index.tsx
@@ -34,6 +34,10 @@ import { cloneDeep, defaultTo, isEmpty, isUndefined } from 'lodash-es';
import { Check, X } from 'lucide-react';
import { DashboardWidgetPageParams } from 'pages/DashboardWidget';
import { useDashboard } from 'providers/Dashboard/Dashboard';
+import {
+ clearSelectedRowWidgetId,
+ getSelectedRowWidgetId,
+} from 'providers/Dashboard/helpers/selectedRowWidgetIdHelper';
import {
getNextWidgets,
getPreviousWidgets,
@@ -86,8 +90,6 @@ function NewWidget({
selectedDashboard,
setSelectedDashboard,
setToScrollWidgetId,
- selectedRowWidgetId,
- setSelectedRowWidgetId,
columnWidths,
} = useDashboard();
@@ -450,6 +452,8 @@ function NewWidget({
const widgetId = query.get('widgetId') || '';
let updatedLayout = selectedDashboard.data.layout || [];
+ const selectedRowWidgetId = getSelectedRowWidgetId(dashboardId);
+
if (isNewDashboard && isEmpty(selectedRowWidgetId)) {
const newLayoutItem = placeWidgetAtBottom(widgetId, updatedLayout);
updatedLayout = [...updatedLayout, newLayoutItem];
@@ -554,7 +558,6 @@ function NewWidget({
updateDashboardMutation.mutateAsync(dashboard, {
onSuccess: (updatedDashboard) => {
- setSelectedRowWidgetId(null);
setSelectedDashboard(updatedDashboard.data);
setToScrollWidgetId(selectedWidget?.id || '');
safeNavigate({
@@ -566,7 +569,6 @@ function NewWidget({
selectedDashboard,
query,
isNewDashboard,
- selectedRowWidgetId,
afterWidgets,
selectedWidget,
selectedTime.enum,
@@ -577,7 +579,6 @@ function NewWidget({
widgets,
setSelectedDashboard,
setToScrollWidgetId,
- setSelectedRowWidgetId,
safeNavigate,
dashboardId,
]);
@@ -681,6 +682,10 @@ function NewWidget({
* on mount here with the currentQuery in the begining itself
*/
setSupersetQuery(currentQuery);
+
+ return (): void => {
+ clearSelectedRowWidgetId(dashboardId);
+ };
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
diff --git a/frontend/src/providers/Dashboard/Dashboard.tsx b/frontend/src/providers/Dashboard/Dashboard.tsx
index 0ec27335d3..ae0d57383b 100644
--- a/frontend/src/providers/Dashboard/Dashboard.tsx
+++ b/frontend/src/providers/Dashboard/Dashboard.tsx
@@ -80,8 +80,6 @@ export const DashboardContext = createContext<IDashboardContext>({
updateLocalStorageDashboardVariables: () => {},
dashboardQueryRangeCalled: false,
setDashboardQueryRangeCalled: () => {},
- selectedRowWidgetId: '',
- setSelectedRowWidgetId: () => {},
isDashboardFetching: false,
columnWidths: {},
setColumnWidths: () => {},
@@ -101,10 +99,6 @@ export function DashboardProvider({
const [isDashboardLocked, setIsDashboardLocked] = useState<boolean>(false);
- const [selectedRowWidgetId, setSelectedRowWidgetId] = useState<string | null>(
- null,
- );
-
const [
dashboardQueryRangeCalled,
setDashboardQueryRangeCalled,
@@ -467,8 +461,6 @@ export function DashboardProvider({
updateLocalStorageDashboardVariables,
dashboardQueryRangeCalled,
setDashboardQueryRangeCalled,
- selectedRowWidgetId,
- setSelectedRowWidgetId,
isDashboardFetching,
columnWidths,
setColumnWidths,
@@ -487,8 +479,6 @@ export function DashboardProvider({
currentDashboard,
dashboardQueryRangeCalled,
setDashboardQueryRangeCalled,
- selectedRowWidgetId,
- setSelectedRowWidgetId,
isDashboardFetching,
columnWidths,
setColumnWidths,
diff --git a/frontend/src/providers/Dashboard/helpers/selectedRowWidgetIdHelper.ts b/frontend/src/providers/Dashboard/helpers/selectedRowWidgetIdHelper.ts
new file mode 100644
index 0000000000..3d82b897fa
--- /dev/null
+++ b/frontend/src/providers/Dashboard/helpers/selectedRowWidgetIdHelper.ts
@@ -0,0 +1,28 @@
+const PREFIX = 'dashboard_row_widget_';
+
+function getKey(dashboardId: string): string {
+ return `${PREFIX}${dashboardId}`;
+}
+
+export function setSelectedRowWidgetId(
+ dashboardId: string,
+ widgetId: string,
+): void {
+ const key = getKey(dashboardId);
+
+ // remove all other selected widget ids for the dashboard before setting the new one
+ // to ensure only one widget is selected at a time. Helps out in weird navigate and refresh scenarios
+ Object.keys(sessionStorage)
+ .filter((k) => k.startsWith(PREFIX) && k !== key)
+ .forEach((k) => sessionStorage.removeItem(k));
+
+ sessionStorage.setItem(key, widgetId);
+}
+
+export function getSelectedRowWidgetId(dashboardId: string): string | null {
+ return sessionStorage.getItem(getKey(dashboardId));
+}
+
+export function clearSelectedRowWidgetId(dashboardId: string): void {
+ sessionStorage.removeItem(getKey(dashboardId));
+}
diff --git a/frontend/src/providers/Dashboard/types.ts b/frontend/src/providers/Dashboard/types.ts
index 17b18f1c99..1b0d20a1e4 100644
--- a/frontend/src/providers/Dashboard/types.ts
+++ b/frontend/src/providers/Dashboard/types.ts
@@ -39,8 +39,6 @@ export interface IDashboardContext {
) => void;
dashboardQueryRangeCalled: boolean;
setDashboardQueryRangeCalled: (value: boolean) => void;
- selectedRowWidgetId: string | null;
- setSelectedRowWidgetId: React.Dispatch<React.SetStateAction<string | null>>;
isDashboardFetching: boolean;
columnWidths: WidgetColumnWidths;
setColumnWidths: React.Dispatch<React.SetStateAction<WidgetColumnWidths>>;