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.

refactor: move dashboard provider from redux to zustand (#10628)

Ashwin Bhatkalc360e4498d72
diff --git a/frontend/src/components/CeleryTask/useNavigateToExplorer.ts b/frontend/src/components/CeleryTask/useNavigateToExplorer.ts
index 43cee03ef3..84da964b53 100644
--- a/frontend/src/components/CeleryTask/useNavigateToExplorer.ts
+++ b/frontend/src/components/CeleryTask/useNavigateToExplorer.ts
@@ -7,7 +7,7 @@ import ROUTES from 'constants/routes';
import useUpdatedQuery from 'container/GridCardLayout/useResolveQuery';
import { useQueryBuilder } from 'hooks/queryBuilder/useQueryBuilder';
import { useNotifications } from 'hooks/useNotifications';
-import { useDashboard } from 'providers/Dashboard/Dashboard';
+import { useDashboardStore } from 'providers/Dashboard/store/useDashboardStore';
import { AppState } from 'store/reducers';
import { Query, TagFilterItem } from 'types/api/queryBuilder/queryBuilderData';
import { DataSource, MetricAggregateOperator } from 'types/common/queryBuilder';
@@ -79,7 +79,7 @@ export function useNavigateToExplorer(): (
);
const { getUpdatedQuery } = useUpdatedQuery();
- const { selectedDashboard } = useDashboard();
+ const { selectedDashboard } = useDashboardStore();
const { notifications } = useNotifications();
return useCallback(
diff --git a/frontend/src/components/QueryBuilderV2/QueryV2/__tests__/QuerySearch.test.tsx b/frontend/src/components/QueryBuilderV2/QueryV2/__tests__/QuerySearch.test.tsx
index 433560e087..05f6b313a7 100644
--- a/frontend/src/components/QueryBuilderV2/QueryV2/__tests__/QuerySearch.test.tsx
+++ b/frontend/src/components/QueryBuilderV2/QueryV2/__tests__/QuerySearch.test.tsx
@@ -86,8 +86,8 @@ jest.mock('hooks/useDarkMode', () => ({
useIsDarkMode: (): boolean => false,
}));
-jest.mock('providers/Dashboard/Dashboard', () => ({
- useDashboard: (): { selectedDashboard: undefined } => ({
+jest.mock('providers/Dashboard/store/useDashboardStore', () => ({
+ useDashboardStore: (): { selectedDashboard: undefined } => ({
selectedDashboard: undefined,
}),
}));
diff --git a/frontend/src/container/DashboardContainer/DashboardDescription/__tests__/DashboardDescription.test.tsx b/frontend/src/container/DashboardContainer/DashboardDescription/__tests__/DashboardDescription.test.tsx
index 0d757f45ee..f8bc00806e 100644
--- a/frontend/src/container/DashboardContainer/DashboardDescription/__tests__/DashboardDescription.test.tsx
+++ b/frontend/src/container/DashboardContainer/DashboardDescription/__tests__/DashboardDescription.test.tsx
@@ -1,4 +1,6 @@
+import { ReactNode } from 'react';
import { MemoryRouter, useLocation } from 'react-router-dom';
+import { useDashboardBootstrap } from 'hooks/dashboard/useDashboardBootstrap';
import {
getDashboardById,
getNonIntegrationDashboardById,
@@ -6,10 +8,9 @@ import {
import { server } from 'mocks-server/server';
import { rest } from 'msw';
import {
- DashboardContext,
- DashboardProvider,
-} from 'providers/Dashboard/Dashboard';
-import { IDashboardContext } from 'providers/Dashboard/types';
+ resetDashboard,
+ useDashboardStore,
+} from 'providers/Dashboard/store/useDashboardStore';
import {
fireEvent,
render,
@@ -21,6 +22,18 @@ import { Dashboard } from 'types/api/dashboard/getAll';
import DashboardDescription from '..';
+function DashboardBootstrapWrapper({
+ dashboardId,
+ children,
+}: {
+ dashboardId: string;
+ children: ReactNode;
+}): JSX.Element {
+ useDashboardBootstrap(dashboardId);
+ // eslint-disable-next-line react/jsx-no-useless-fragment
+ return <>{children}</>;
+}
+
interface MockSafeNavigateReturn {
safeNavigate: jest.MockedFunction<(url: string) => void>;
}
@@ -54,6 +67,7 @@ describe('Dashboard landing page actions header tests', () => {
beforeEach(() => {
mockSafeNavigate.mockClear();
sessionStorage.clear();
+ resetDashboard();
});
it('unlock dashboard should be disabled for integrations created dashboards', async () => {
@@ -64,7 +78,7 @@ describe('Dashboard landing page actions header tests', () => {
(useLocation as jest.Mock).mockReturnValue(mockLocation);
const { getByTestId } = render(
<MemoryRouter initialEntries={[DASHBOARD_PATH]}>
- <DashboardProvider dashboardId="4">
+ <DashboardBootstrapWrapper dashboardId="4">
<DashboardDescription
handle={{
active: false,
@@ -73,7 +87,7 @@ describe('Dashboard landing page actions header tests', () => {
node: { current: null },
}}
/>
- </DashboardProvider>
+ </DashboardBootstrapWrapper>
</MemoryRouter>,
);
@@ -105,7 +119,7 @@ describe('Dashboard landing page actions header tests', () => {
);
const { getByTestId } = render(
<MemoryRouter initialEntries={[DASHBOARD_PATH]}>
- <DashboardProvider dashboardId="4">
+ <DashboardBootstrapWrapper dashboardId="4">
<DashboardDescription
handle={{
active: false,
@@ -114,7 +128,7 @@ describe('Dashboard landing page actions header tests', () => {
node: { current: null },
}}
/>
- </DashboardProvider>
+ </DashboardBootstrapWrapper>
</MemoryRouter>,
);
@@ -144,7 +158,7 @@ describe('Dashboard landing page actions header tests', () => {
const { getByText } = render(
<MemoryRouter initialEntries={[DASHBOARD_PATH]}>
- <DashboardProvider dashboardId="4">
+ <DashboardBootstrapWrapper dashboardId="4">
<DashboardDescription
handle={{
active: false,
@@ -153,7 +167,7 @@ describe('Dashboard landing page actions header tests', () => {
node: { current: null },
}}
/>
- </DashboardProvider>
+ </DashboardBootstrapWrapper>
</MemoryRouter>,
);
@@ -181,37 +195,26 @@ describe('Dashboard landing page actions header tests', () => {
(useLocation as jest.Mock).mockReturnValue(mockLocation);
- const mockContextValue: IDashboardContext = {
- isDashboardLocked: false,
- handleDashboardLockToggle: jest.fn(),
- dashboardResponse: {} as IDashboardContext['dashboardResponse'],
+ useDashboardStore.setState({
selectedDashboard: (getDashboardById.data as unknown) as Dashboard,
layouts: [],
panelMap: {},
setPanelMap: jest.fn(),
setLayouts: jest.fn(),
setSelectedDashboard: jest.fn(),
- updatedTimeRef: { current: null },
- updateLocalStorageDashboardVariables: jest.fn(),
- dashboardQueryRangeCalled: false,
- setDashboardQueryRangeCalled: jest.fn(),
- isDashboardFetching: false,
columnWidths: {},
- setColumnWidths: jest.fn(),
- };
+ });
const { getByText } = render(
<MemoryRouter initialEntries={[DASHBOARD_PATH]}>
- <DashboardContext.Provider value={mockContextValue}>
- <DashboardDescription
- handle={{
- active: false,
- enter: (): Promise<void> => Promise.resolve(),
- exit: (): Promise<void> => Promise.resolve(),
- node: { current: null },
- }}
- />
- </DashboardContext.Provider>
+ <DashboardDescription
+ handle={{
+ active: false,
+ enter: (): Promise<void> => Promise.resolve(),
+ exit: (): Promise<void> => Promise.resolve(),
+ node: { current: null },
+ }}
+ />
</MemoryRouter>,
);
diff --git a/frontend/src/container/DashboardContainer/DashboardDescription/index.tsx b/frontend/src/container/DashboardContainer/DashboardDescription/index.tsx
index 822a130154..d1cbe5330e 100644
--- a/frontend/src/container/DashboardContainer/DashboardDescription/index.tsx
+++ b/frontend/src/container/DashboardContainer/DashboardDescription/index.tsx
@@ -21,6 +21,7 @@ import { DeleteButton } from 'container/ListOfDashboard/TableComponents/DeleteBu
import DateTimeSelectionV2 from 'container/TopNav/DateTimeSelectionV2';
import { useDashboardVariables } from 'hooks/dashboard/useDashboardVariables';
import { useGetPublicDashboardMeta } from 'hooks/dashboard/useGetPublicDashboardMeta';
+import { useLockDashboard } from 'hooks/dashboard/useLockDashboard';
import { useUpdateDashboard } from 'hooks/dashboard/useUpdateDashboard';
import useComponentPermission from 'hooks/useComponentPermission';
import { useGetTenantLicense } from 'hooks/useGetTenantLicense';
@@ -39,8 +40,11 @@ import {
X,
} from 'lucide-react';
import { useAppContext } from 'providers/App/App';
-import { useDashboard } from 'providers/Dashboard/Dashboard';
import { usePanelTypeSelectionModalStore } from 'providers/Dashboard/helpers/panelTypeSelectionModalHelper';
+import {
+ selectIsDashboardLocked,
+ useDashboardStore,
+} from 'providers/Dashboard/store/useDashboardStore';
import { sortLayout } from 'providers/Dashboard/util';
import { DashboardData } from 'types/api/dashboard/getAll';
import { Props } from 'types/api/dashboard/update';
@@ -79,10 +83,11 @@ function DashboardDescription(props: DashboardDescriptionProps): JSX.Element {
setPanelMap,
layouts,
setLayouts,
- isDashboardLocked,
setSelectedDashboard,
- handleDashboardLockToggle,
- } = useDashboard();
+ } = useDashboardStore();
+
+ const isDashboardLocked = useDashboardStore(selectIsDashboardLocked);
+ const handleDashboardLockToggle = useLockDashboard();
const variablesSettingsTabHandle = useRef<VariablesSettingsTab>(null);
const [isSettingsDrawerOpen, setIsSettingsDrawerOpen] = useState<boolean>(
diff --git a/frontend/src/container/DashboardContainer/DashboardSettings/DashboardVariableSettings/VariableItem/VariableItem.tsx b/frontend/src/container/DashboardContainer/DashboardSettings/DashboardVariableSettings/VariableItem/VariableItem.tsx
index 660e403efe..cbb15c6e27 100644
--- a/frontend/src/container/DashboardContainer/DashboardSettings/DashboardVariableSettings/VariableItem/VariableItem.tsx
+++ b/frontend/src/container/DashboardContainer/DashboardSettings/DashboardVariableSettings/VariableItem/VariableItem.tsx
@@ -30,7 +30,7 @@ import {
Pyramid,
X,
} from 'lucide-react';
-import { useDashboard } from 'providers/Dashboard/Dashboard';
+import { useDashboardStore } from 'providers/Dashboard/store/useDashboardStore';
import { AppState } from 'store/reducers';
import {
IDashboardVariable,
@@ -239,7 +239,7 @@ function VariableItem({
const [selectedWidgets, setSelectedWidgets] = useState<string[]>([]);
- const { selectedDashboard } = useDashboard();
+ const { selectedDashboard } = useDashboardStore();
const widgetsByDynamicVariableId = useWidgetsByDynamicVariableId();
useEffect(() => {
diff --git a/frontend/src/container/DashboardContainer/DashboardSettings/DashboardVariableSettings/VariableItem/WidgetSelector.tsx b/frontend/src/container/DashboardContainer/DashboardSettings/DashboardVariableSettings/VariableItem/WidgetSelector.tsx
index 2f1718245c..985c286dc3 100644
--- a/frontend/src/container/DashboardContainer/DashboardSettings/DashboardVariableSettings/VariableItem/WidgetSelector.tsx
+++ b/frontend/src/container/DashboardContainer/DashboardSettings/DashboardVariableSettings/VariableItem/WidgetSelector.tsx
@@ -2,7 +2,7 @@ import React from 'react';
import { CustomMultiSelect } from 'components/NewSelect';
import { PANEL_GROUP_TYPES } from 'constants/queryBuilder';
import { generateGridTitle } from 'container/GridPanelSwitch/utils';
-import { useDashboard } from 'providers/Dashboard/Dashboard';
+import { useDashboardStore } from 'providers/Dashboard/store/useDashboardStore';
import { WidgetRow, Widgets } from 'types/api/dashboard/getAll';
export function WidgetSelector({
@@ -12,7 +12,7 @@ export function WidgetSelector({
selectedWidgets: string[];
setSelectedWidgets: (widgets: string[]) => void;
}): JSX.Element {
- const { selectedDashboard } = useDashboard();
+ const { selectedDashboard } = useDashboardStore();
// Get layout IDs for cross-referencing
const layoutIds = new Set(
diff --git a/frontend/src/container/DashboardContainer/DashboardSettings/DashboardVariableSettings/index.tsx b/frontend/src/container/DashboardContainer/DashboardSettings/DashboardVariableSettings/index.tsx
index 2d413d5154..bd0dc14bb0 100644
--- a/frontend/src/container/DashboardContainer/DashboardSettings/DashboardVariableSettings/index.tsx
+++ b/frontend/src/container/DashboardContainer/DashboardSettings/DashboardVariableSettings/index.tsx
@@ -19,8 +19,8 @@ import { useDashboardVariables } from 'hooks/dashboard/useDashboardVariables';
import { useUpdateDashboard } from 'hooks/dashboard/useUpdateDashboard';
import { useNotifications } from 'hooks/useNotifications';
import { PenLine, Trash2 } from 'lucide-react';
-import { useDashboard } from 'providers/Dashboard/Dashboard';
import { IDashboardVariables } from 'providers/Dashboard/store/dashboardVariables/dashboardVariablesStoreTypes';
+import { useDashboardStore } from 'providers/Dashboard/store/useDashboardStore';
import { IDashboardVariable } from 'types/api/dashboard/getAll';
import { TVariableMode } from './types';
@@ -87,7 +87,7 @@ function VariablesSettings({
const { t } = useTranslation(['dashboard']);
- const { selectedDashboard, setSelectedDashboard } = useDashboard();
+ const { selectedDashboard, setSelectedDashboard } = useDashboardStore();
const { dashboardVariables } = useDashboardVariables();
const { notifications } = useNotifications();
diff --git a/frontend/src/container/DashboardContainer/DashboardSettings/General/index.tsx b/frontend/src/container/DashboardContainer/DashboardSettings/General/index.tsx
index 461f3102e9..1ab5dac116 100644
--- a/frontend/src/container/DashboardContainer/DashboardSettings/General/index.tsx
+++ b/frontend/src/container/DashboardContainer/DashboardSettings/General/index.tsx
@@ -5,7 +5,7 @@ import AddTags from 'container/DashboardContainer/DashboardSettings/General/AddT
import { useUpdateDashboard } from 'hooks/dashboard/useUpdateDashboard';
import { isEqual } from 'lodash-es';
import { Check, X } from 'lucide-react';
-import { useDashboard } from 'providers/Dashboard/Dashboard';
+import { useDashboardStore } from 'providers/Dashboard/store/useDashboardStore';
import { Button } from './styles';
import { Base64Icons } from './utils';
@@ -15,7 +15,7 @@ import './GeneralSettings.styles.scss';
const { Option } = Select;
function GeneralDashboardSettings(): JSX.Element {
- const { selectedDashboard, setSelectedDashboard } = useDashboard();
+ const { selectedDashboard, setSelectedDashboard } = useDashboardStore();
const updateDashboardMutation = useUpdateDashboard();
diff --git a/frontend/src/container/DashboardContainer/DashboardSettings/PublicDashboard/__tests__/PublicDashboard.test.tsx b/frontend/src/container/DashboardContainer/DashboardSettings/PublicDashboard/__tests__/PublicDashboard.test.tsx
index 85c71cee23..1ebbfd889f 100644
--- a/frontend/src/container/DashboardContainer/DashboardSettings/PublicDashboard/__tests__/PublicDashboard.test.tsx
+++ b/frontend/src/container/DashboardContainer/DashboardSettings/PublicDashboard/__tests__/PublicDashboard.test.tsx
@@ -7,14 +7,14 @@ import {
unpublishedPublicDashboardMeta,
} from 'mocks-server/__mockdata__/publicDashboard';
import { rest, server } from 'mocks-server/server';
-import { useDashboard } from 'providers/Dashboard/Dashboard';
+import { useDashboardStore } from 'providers/Dashboard/store/useDashboardStore';
import { render, screen, userEvent, waitFor } from 'tests/test-utils';
import { USER_ROLES } from 'types/roles';
import PublicDashboardSetting from '../index';
// Mock dependencies
-jest.mock('providers/Dashboard/Dashboard');
+jest.mock('providers/Dashboard/store/useDashboardStore');
jest.mock('react-use', () => ({
...jest.requireActual('react-use'),
useCopyToClipboard: jest.fn(),
@@ -26,7 +26,7 @@ jest.mock('@signozhq/sonner', () => ({
},
}));
-const mockUseDashboard = jest.mocked(useDashboard);
+const mockUseDashboard = jest.mocked(useDashboardStore);
const mockUseCopyToClipboard = jest.mocked(useCopyToClipboard);
const mockToast = jest.mocked(toast);
@@ -67,10 +67,10 @@ beforeEach(() => {
// Mock window.open
window.open = jest.fn();
- // Mock useDashboard
+ // Mock useDashboardStore
mockUseDashboard.mockReturnValue(({
selectedDashboard: mockSelectedDashboard,
- } as unknown) as ReturnType<typeof useDashboard>);
+ } as unknown) as ReturnType<typeof useDashboardStore>);
// Mock useCopyToClipboard
mockUseCopyToClipboard.mockReturnValue(([
diff --git a/frontend/src/container/DashboardContainer/DashboardSettings/PublicDashboard/index.tsx b/frontend/src/container/DashboardContainer/DashboardSettings/PublicDashboard/index.tsx
index fcda97237d..629a4fc44d 100644
--- a/frontend/src/container/DashboardContainer/DashboardSettings/PublicDashboard/index.tsx
+++ b/frontend/src/container/DashboardContainer/DashboardSettings/PublicDashboard/index.tsx
@@ -11,7 +11,7 @@ import { useGetPublicDashboardMeta } from 'hooks/dashboard/useGetPublicDashboard
import { useGetTenantLicense } from 'hooks/useGetTenantLicense';
import { Copy, ExternalLink, Globe, Info, Loader2, Trash } from 'lucide-react';
import { useAppContext } from 'providers/App/App';
-import { useDashboard } from 'providers/Dashboard/Dashboard';
+import { useDashboardStore } from 'providers/Dashboard/store/useDashboardStore';
import { PublicDashboardMetaProps } from 'types/api/dashboard/public/getMeta';
import APIError from 'types/api/error';
import { USER_ROLES } from 'types/roles';
@@ -59,7 +59,7 @@ function PublicDashboardSetting(): JSX.Element {
const [defaultTimeRange, setDefaultTimeRange] = useState('30m');
const [, setCopyPublicDashboardURL] = useCopyToClipboard();
- const { selectedDashboard } = useDashboard();
+ const { selectedDashboard } = useDashboardStore();
const { isCloudUser, isEnterpriseSelfHostedUser } = useGetTenantLicense();
diff --git a/frontend/src/container/DashboardContainer/DashboardVariablesSelection/DashboardVariableSelection.tsx b/frontend/src/container/DashboardContainer/DashboardVariablesSelection/DashboardVariableSelection.tsx
index 2c00b23261..60e5b7bdf0 100644
--- a/frontend/src/container/DashboardContainer/DashboardVariablesSelection/DashboardVariableSelection.tsx
+++ b/frontend/src/container/DashboardContainer/DashboardVariablesSelection/DashboardVariableSelection.tsx
@@ -3,13 +3,14 @@ import { memo, useCallback, useEffect, useMemo } from 'react';
import { useSelector } from 'react-redux';
import { Row } from 'antd';
import { ALL_SELECTED_VALUE } from 'components/NewSelect/utils';
+import { updateLocalStorageDashboardVariable } from 'hooks/dashboard/useDashboardFromLocalStorage';
import {
useDashboardVariables,
useDashboardVariablesSelector,
} from 'hooks/dashboard/useDashboardVariables';
import useVariablesFromUrl from 'hooks/dashboard/useVariablesFromUrl';
-import { useDashboard } from 'providers/Dashboard/Dashboard';
import { updateDashboardVariablesStore } from 'providers/Dashboard/store/dashboardVariables/dashboardVariablesStore';
+import { useDashboardStore } from 'providers/Dashboard/store/useDashboardStore';
import {
enqueueDescendantsOfVariable,
enqueueFetchOfAllVariables,
@@ -18,23 +19,23 @@ import {
import { AppState } from 'store/reducers';
import { IDashboardVariable } from 'types/api/dashboard/getAll';
import { GlobalReducer } from 'types/reducer/globalTime';
+import { useShallow } from 'zustand/react/shallow';
import VariableItem from './VariableItem';
import './DashboardVariableSelection.styles.scss';
function DashboardVariableSelection(): JSX.Element | null {
- const {
- setSelectedDashboard,
- updateLocalStorageDashboardVariables,
- } = useDashboard();
+ const { dashboardId, setSelectedDashboard } = useDashboardStore(
+ useShallow((s) => ({
+ dashboardId: s.selectedDashboard?.id ?? '',
+ setSelectedDashboard: s.setSelectedDashboard,
+ })),
+ );
const { updateUrlVariable } = useVariablesFromUrl();
const { dashboardVariables } = useDashboardVariables();
- const dashboardId = useDashboardVariablesSelector(
- (state) => state.dashboardId,
- );
const sortedVariablesArray = useDashboardVariablesSelector(
(state) => state.sortedVariablesArray,
);
@@ -82,7 +83,13 @@ function DashboardVariableSelection(): JSX.Element | null {
// This makes localStorage much lighter by avoiding storing all individual values
const variable = dashboardVariables[id] || dashboardVariables[name];
const isDynamic = variable.type === 'DYNAMIC';
- updateLocalStorageDashboardVariables(name, value, allSelected, isDynamic);
+ updateLocalStorageDashboardVariable(
+ dashboardId,
+ name,
+ value,
+ allSelected,
+ isDynamic,
+ );
if (allSelected) {
updateUrlVariable(name || id, ALL_SELECTED_VALUE);
@@ -150,13 +157,7 @@ function DashboardVariableSelection(): JSX.Element | null {
// Safe to call synchronously now that the store already has the updated value.
enqueueDescendantsOfVariable(name);
},
- [
- dashboardId,
- dashboardVariables,
- updateLocalStorageDashboardVariables,
- updateUrlVariable,
- setSelectedDashboard,
- ],
+ [dashboardId, dashboardVariables, updateUrlVariable, setSelectedDashboard],
);
return (
diff --git a/frontend/src/container/DashboardContainer/DashboardVariablesSelection/__test__/DashboardVariableSelection.test.tsx b/frontend/src/container/DashboardContainer/DashboardVariablesSelection/__test__/DashboardVariableSelection.test.tsx
index 8d83623a6f..9a46728569 100644
--- a/frontend/src/container/DashboardContainer/DashboardVariablesSelection/__test__/DashboardVariableSelection.test.tsx
+++ b/frontend/src/container/DashboardContainer/DashboardVariablesSelection/__test__/DashboardVariableSelection.test.tsx
@@ -32,11 +32,22 @@ const mockVariableItemCallbacks: {
// Mock providers/Dashboard/Dashboard
const mockSetSelectedDashboard = jest.fn();
const mockUpdateLocalStorageDashboardVariables = jest.fn();
-jest.mock('providers/Dashboard/Dashboard', () => ({
- useDashboard: (): Record<string, unknown> => ({
- setSelectedDashboard: mockSetSelectedDashboard,
- updateLocalStorageDashboardVariables: mockUpdateLocalStorageDashboardVariables,
- }),
+interface MockDashboardStoreState {
+ selectedDashboard?: { id: string };
+ setSelectedDashboard: typeof mockSetSelectedDashboard;
+ updateLocalStorageDashboardVariables: typeof mockUpdateLocalStorageDashboardVariables;
+}
+jest.mock('providers/Dashboard/store/useDashboardStore', () => ({
+ useDashboardStore: (
+ selector?: (s: Record<string, unknown>) => MockDashboardStoreState,
+ ): MockDashboardStoreState => {
+ const state = {
+ selectedDashboard: { id: 'dash-1' },
+ setSelectedDashboard: mockSetSelectedDashboard,
+ updateLocalStorageDashboardVariables: mockUpdateLocalStorageDashboardVariables,
+ };
+ return selector ? selector(state) : state;
+ },
}));
// Mock hooks/dashboard/useVariablesFromUrl
diff --git a/frontend/src/container/DashboardContainer/DashboardVariablesSelection/useDashboardVariableUpdate.ts b/frontend/src/container/DashboardContainer/DashboardVariablesSelection/useDashboardVariableUpdate.ts
index 3f88de0cbd..ecc8f1a74d 100644
--- a/frontend/src/container/DashboardContainer/DashboardVariablesSelection/useDashboardVariableUpdate.ts
+++ b/frontend/src/container/DashboardContainer/DashboardVariablesSelection/useDashboardVariableUpdate.ts
@@ -1,11 +1,13 @@
/* eslint-disable sonarjs/cognitive-complexity */
import { useCallback } from 'react';
import { useAddDynamicVariableToPanels } from 'hooks/dashboard/useAddDynamicVariableToPanels';
+import { updateLocalStorageDashboardVariable } from 'hooks/dashboard/useDashboardFromLocalStorage';
import { useUpdateDashboard } from 'hooks/dashboard/useUpdateDashboard';
-import { useDashboard } from 'providers/Dashboard/Dashboard';
import { IDashboardVariables } from 'providers/Dashboard/store/dashboardVariables/dashboardVariablesStoreTypes';
+import { useDashboardStore } from 'providers/Dashboard/store/useDashboardStore';
import { IDashboardVariable } from 'types/api/dashboard/getAll';
import { v4 as uuidv4 } from 'uuid';
+import { useShallow } from 'zustand/react/shallow';
import { convertVariablesToDbFormat } from './util';
@@ -37,11 +39,16 @@ interface UseDashboardVariableUpdateReturn {
export const useDashboardVariableUpdate = (): UseDashboardVariableUpdateReturn => {
const {
+ dashboardId,
selectedDashboard,
setSelectedDashboard,
- updateLocalStorageDashboardVariables,
- } = useDashboard();
-
+ } = useDashboardStore(
+ useShallow((s) => ({
+ dashboardId: s.selectedDashboard?.id ?? '',
+ selectedDashboard: s.selectedDashboard,
+ setSelectedDashboard: s.setSelectedDashboard,
+ })),
+ );
const addDynamicVariableToPanels = useAddDynamicVariableToPanels();
const updateMutation = useUpdateDashboard();
@@ -59,7 +66,13 @@ export const useDashboardVariableUpdate = (): UseDashboardVariableUpdateReturn =
// This makes localStorage much lighter and more efficient.
// currently all the variables are dynamic
const isDynamic = true;
- updateLocalStorageDashboardVariables(name, value, allSelected, isDynamic);
+ updateLocalStorageDashboardVariable(
+ dashboardId,
+ name,
+ value,
+ allSelected,
+ isDynamic,
+ );
if (selectedDashboard) {
setSelectedDashboard((prev) => {
@@ -97,11 +110,7 @@ export const useDashboardVariableUpdate = (): UseDashboardVariableUpdateReturn =
}
}
},
- [
- selectedDashboard,
- setSelectedDashboard,
- updateLocalStorageDashboardVariables,
- ],
+ [dashboardId, selectedDashboard, setSelectedDashboard],
);
const updateVariables = useCallback(
diff --git a/frontend/src/container/DashboardContainer/__test__/IntegrationTests.test.tsx b/frontend/src/container/DashboardContainer/__test__/IntegrationTests.test.tsx
index 2bce1d3223..68175ebba2 100644
--- a/frontend/src/container/DashboardContainer/__test__/IntegrationTests.test.tsx
+++ b/frontend/src/container/DashboardContainer/__test__/IntegrationTests.test.tsx
@@ -49,8 +49,8 @@ const mockDashboard = {
// Mock the dashboard provider with stable functions to prevent infinite loops
const mockSetSelectedDashboard = jest.fn();
const mockUpdateLocalStorageDashboardVariables = jest.fn();
-jest.mock('providers/Dashboard/Dashboard', () => ({
- useDashboard: (): any => ({
+jest.mock('providers/Dashboard/store/useDashboardStore', () => ({
+ useDashboardStore: (): any => ({
selectedDashboard: mockDashboard,
setSelectedDashboard: mockSetSelectedDashboard,
updateLocalStorageDashboardVariables: mockUpdateLocalStorageDashboardVariables,
diff --git a/frontend/src/container/DashboardContainer/__test__/PanelManagement.test.tsx b/frontend/src/container/DashboardContainer/__test__/PanelManagement.test.tsx
index 4b1505ea5f..f7cad8caf2 100644
--- a/frontend/src/container/DashboardContainer/__test__/PanelManagement.test.tsx
+++ b/frontend/src/container/DashboardContainer/__test__/PanelManagement.test.tsx
@@ -56,8 +56,8 @@ const mockDashboard = {
},
};
// Mock dependencies
-jest.mock('providers/Dashboard/Dashboard', () => ({
- useDashboard: (): any => ({
+jest.mock('providers/Dashboard/store/useDashboardStore', () => ({
+ useDashboardStore: (): any => ({
selectedDashboard: mockDashboard,
}),
}));
@@ -152,8 +152,8 @@ describe('Panel Management Tests', () => {
};
// Temporarily mock the dashboard
- jest.doMock('providers/Dashboard/Dashboard', () => ({
- useDashboard: (): any => ({
+ jest.doMock('providers/Dashboard/store/useDashboardStore', () => ({
+ useDashboardStore: (): any => ({
selectedDashboard: modifiedDashboard,
}),
}));
diff --git a/frontend/src/container/DashboardContainer/components/DashboardHeader/DashboardBreadcrumbs.tsx b/frontend/src/container/DashboardContainer/components/DashboardHeader/DashboardBreadcrumbs.tsx
index ca1c376d1c..abe484bd34 100644
--- a/frontend/src/container/DashboardContainer/components/DashboardHeader/DashboardBreadcrumbs.tsx
+++ b/frontend/src/container/DashboardContainer/components/DashboardHeader/DashboardBreadcrumbs.tsx
@@ -4,7 +4,7 @@ import ROUTES from 'constants/routes';
import { DASHBOARDS_LIST_QUERY_PARAMS_STORAGE_KEY } from 'hooks/dashboard/useDashboardsListQueryParams';
import { useSafeNavigate } from 'hooks/useSafeNavigate';
import { LayoutGrid } from 'lucide-react';
-import { useDashboard } from 'providers/Dashboard/Dashboard';
+import { useDashboardStore } from 'providers/Dashboard/store/useDashboardStore';
import { DashboardData } from 'types/api/dashboard/getAll';
import { Base64Icons } from '../../DashboardSettings/General/utils';
@@ -13,7 +13,7 @@ import './DashboardBreadcrumbs.styles.scss';
function DashboardBreadcrumbs(): JSX.Element {
const { safeNavigate } = useSafeNavigate();
- const { selectedDashboard } = useDashboard();
+ const { selectedDashboard } = useDashboardStore();
const updatedAtRef = useRef(selectedDashboard?.updatedAt);
const selectedData = selectedDashboard
diff --git a/frontend/src/container/DashboardContainer/visualization/components/ChartManager/ChartManager.tsx b/frontend/src/container/DashboardContainer/visualization/components/ChartManager/ChartManager.tsx
index 313104959b..e30807ded8 100644
--- a/frontend/src/container/DashboardContainer/visualization/components/ChartManager/ChartManager.tsx
+++ b/frontend/src/container/DashboardContainer/visualization/components/ChartManager/ChartManager.tsx
@@ -6,7 +6,10 @@ import { useNotifications } from 'hooks/useNotifications';
import { UPlotConfigBuilder } from 'lib/uPlotV2/config/UPlotConfigBuilder';
import { usePlotContext } from 'lib/uPlotV2/context/PlotContext';
import useLegendsSync from 'lib/uPlotV2/hooks/useLegendsSync';
-import { useDashboard } from 'providers/Dashboard/Dashboard';
+import {
+ selectIsDashboardLocked,
+ useDashboardStore,
+} from 'providers/Dashboard/store/useDashboardStore';
import { getChartManagerColumns } from './getChartMangerColumns';
import { ExtendedChartDataset, getDefaultTableDataSet } from './utils';
@@ -50,7 +53,7 @@ export default function ChartManager({
onToggleSeriesVisibility,
syncSeriesVisibilityToLocalStorage,
} = usePlotContext();
- const { isDashboardLocked } = useDashboard();
+ const isDashboardLocked = useDashboardStore(selectIsDashboardLocked);
const [tableDataSet, setTableDataSet] = useState<ExtendedChartDataset[]>(() =>
getDefaultTableDataSet(
diff --git a/frontend/src/container/DashboardContainer/visualization/components/ChartManager/__tests__/ChartManager.test.tsx b/frontend/src/container/DashboardContainer/visualization/components/ChartManager/__tests__/ChartManager.test.tsx
index 92b963f802..ff0c4ae833 100644
--- a/frontend/src/container/DashboardContainer/visualization/components/ChartManager/__tests__/ChartManager.test.tsx
+++ b/frontend/src/container/DashboardContainer/visualization/components/ChartManager/__tests__/ChartManager.test.tsx
@@ -32,10 +32,18 @@ jest.mock('lib/uPlotV2/hooks/useLegendsSync', () => ({
}),
}));
-jest.mock('providers/Dashboard/Dashboard', () => ({
- useDashboard: (): { isDashboardLocked: boolean } => ({
- isDashboardLocked: false,
- }),
+jest.mock('providers/Dashboard/store/useDashboardStore', () => ({
+ useDashboardStore: (
+ selector?: (s: {
+ selectedDashboard: { locked: boolean } | undefined;
+ }) => { selectedDashboard: { locked: boolean } },
+ ): { selectedDashboard: { locked: boolean } } => {
+ const mockState = { selectedDashboard: { locked: false } };
+ return selector ? selector(mockState) : mockState;
+ },
+ selectIsDashboardLocked: (s: {
+ selectedDashboard: { locked: boolean } | undefined;
+ }): boolean => s.selectedDashboard?.locked ?? false,
}));
jest.mock('hooks/useNotifications', () => ({
diff --git a/frontend/src/container/GridCardLayout/DashboardEmptyState/DashboardEmptyState.tsx b/frontend/src/container/GridCardLayout/DashboardEmptyState/DashboardEmptyState.tsx
index 6894625708..b75ac8e5c0 100644
--- a/frontend/src/container/GridCardLayout/DashboardEmptyState/DashboardEmptyState.tsx
+++ b/frontend/src/container/GridCardLayout/DashboardEmptyState/DashboardEmptyState.tsx
@@ -8,8 +8,11 @@ import { VariablesSettingsTab } from 'container/DashboardContainer/DashboardDesc
import DashboardSettings from 'container/DashboardContainer/DashboardSettings';
import useComponentPermission from 'hooks/useComponentPermission';
import { useAppContext } from 'providers/App/App';
-import { useDashboard } from 'providers/Dashboard/Dashboard';
import { usePanelTypeSelectionModalStore } from 'providers/Dashboard/helpers/panelTypeSelectionModalHelper';
+import {
+ selectIsDashboardLocked,
+ useDashboardStore,
+} from 'providers/Dashboard/store/useDashboardStore';
import { ROLES, USER_ROLES } from 'types/roles';
import { ComponentTypes } from 'utils/permission';
@@ -20,7 +23,8 @@ export default function DashboardEmptyState(): JSX.Element {
(s) => s.setIsPanelTypeSelectionModalOpen,
);
- const { selectedDashboard, isDashboardLocked } = useDashboard();
+ const { selectedDashboard } = useDashboardStore();
+ const isDashboardLocked = useDashboardStore(selectIsDashboardLocked);
const variablesSettingsTabHandle = useRef<VariablesSettingsTab>(null);
const [isSettingsDrawerOpen, setIsSettingsDrawerOpen] = useState<boolean>(
diff --git a/frontend/src/container/GridCardLayout/GridCard/FullView/GraphManager.tsx b/frontend/src/container/GridCardLayout/GridCard/FullView/GraphManager.tsx
index 77004b38e8..807c76d126 100644
--- a/frontend/src/container/GridCardLayout/GridCard/FullView/GraphManager.tsx
+++ b/frontend/src/container/GridCardLayout/GridCard/FullView/GraphManager.tsx
@@ -3,7 +3,10 @@ import { Button, Input } from 'antd';
import type { CheckboxChangeEvent } from 'antd/es/checkbox';
import { ResizeTable } from 'components/ResizeTable';
import { useNotifications } from 'hooks/useNotifications';
-import { useDashboard } from 'providers/Dashboard/Dashboard';
+import {
+ selectIsDashboardLocked,
+ useDashboardStore,
+} from 'providers/Dashboard/store/useDashboardStore';
import { getGraphManagerTableColumns } from './TableRender/GraphManagerColumns';
import { ExtendedChartDataset, GraphManagerProps } from './types';
@@ -34,7 +37,7 @@ function GraphManager({
}, [data, options]);
const { notifications } = useNotifications();
- const { isDashboardLocked } = useDashboard();
+ const isDashboardLocked = useDashboardStore(selectIsDashboardLocked);
const checkBoxOnChangeHandler = useCallback(
(e: CheckboxChangeEvent, index: number): void => {
diff --git a/frontend/src/container/GridCardLayout/GridCard/FullView/index.tsx b/frontend/src/container/GridCardLayout/GridCard/FullView/index.tsx
index f1de540599..ea441b8bdd 100644
--- a/frontend/src/container/GridCardLayout/GridCard/FullView/index.tsx
+++ b/frontend/src/container/GridCardLayout/GridCard/FullView/index.tsx
@@ -39,7 +39,10 @@ import { getDashboardVariables } from 'lib/dashboardVariables/getDashboardVariab
import GetMinMax from 'lib/getMinMax';
import { isEmpty } from 'lodash-es';
import { useAppContext } from 'providers/App/App';
-import { useDashboard } from 'providers/Dashboard/Dashboard';
+import {
+ selectIsDashboardLocked,
+ useDashboardStore,
+} from 'providers/Dashboard/store/useDashboardStore';
import { AppState } from 'store/reducers';
import { Warning } from 'types/api';
import { GlobalReducer } from 'types/reducer/globalTime';
@@ -81,11 +84,8 @@ function FullView({
setCurrentGraphRef(fullViewRef);
}, [setCurrentGraphRef]);
- const {
- selectedDashboard,
- isDashboardLocked,
- setColumnWidths,
- } = useDashboard();
+ const { selectedDashboard, setColumnWidths } = useDashboardStore();
+ const isDashboardLocked = useDashboardStore(selectIsDashboardLocked);
const onColumnWidthsChange = useCallback(
(widths: Record<string, number>) => {
diff --git a/frontend/src/container/GridCardLayout/GridCard/WidgetGraphComponent.test.tsx b/frontend/src/container/GridCardLayout/GridCard/WidgetGraphComponent.test.tsx
index 49dcfe36f1..4457999e7a 100644
--- a/frontend/src/container/GridCardLayout/GridCard/WidgetGraphComponent.test.tsx
+++ b/frontend/src/container/GridCardLayout/GridCard/WidgetGraphComponent.test.tsx
@@ -161,8 +161,8 @@ const mockProps: WidgetGraphComponentProps = {
};
// Mock useDashabord hook
-jest.mock('providers/Dashboard/Dashboard', () => ({
- useDashboard: (): any => ({
+jest.mock('providers/Dashboard/store/useDashboardStore', () => ({
+ useDashboardStore: (): any => ({
selectedDashboard: {
data: {
variables: [],
diff --git a/frontend/src/container/GridCardLayout/GridCard/WidgetGraphComponent.tsx b/frontend/src/container/GridCardLayout/GridCard/WidgetGraphComponent.tsx
index bd24254b33..623e2fe6f8 100644
--- a/frontend/src/container/GridCardLayout/GridCard/WidgetGraphComponent.tsx
+++ b/frontend/src/container/GridCardLayout/GridCard/WidgetGraphComponent.tsx
@@ -28,7 +28,7 @@ import {
getCustomTimeRangeWindowSweepInMS,
getStartAndEndTimesInMilliseconds,
} from 'pages/MessagingQueues/MessagingQueuesUtils';
-import { useDashboard } from 'providers/Dashboard/Dashboard';
+import { useDashboardStore } from 'providers/Dashboard/store/useDashboardStore';
import { Widgets } from 'types/api/dashboard/getAll';
import { Props } from 'types/api/dashboard/update';
import { EQueryType } from 'types/common/dashboard';
@@ -106,7 +106,7 @@ function WidgetGraphComponent({
selectedDashboard,
setSelectedDashboard,
setColumnWidths,
- } = useDashboard();
+ } = useDashboardStore();
const onColumnWidthsChange = useCallback(
(widths: Record<string, number>) => {
diff --git a/frontend/src/container/GridCardLayout/GridCard/index.tsx b/frontend/src/container/GridCardLayout/GridCard/index.tsx
index 0c5d40d090..37d0c5fd8d 100644
--- a/frontend/src/container/GridCardLayout/GridCard/index.tsx
+++ b/frontend/src/container/GridCardLayout/GridCard/index.tsx
@@ -1,6 +1,7 @@
import { memo, useEffect, useMemo, useRef, useState } from 'react';
// eslint-disable-next-line no-restricted-imports
import { useDispatch, useSelector } from 'react-redux';
+import * as Sentry from '@sentry/react';
import logEvent from 'api/common/logEvent';
import { DEFAULT_ENTITY_VERSION, ENTITY_VERSION_V5 } from 'constants/app';
import { QueryParams } from 'constants/query';
@@ -17,7 +18,6 @@ import { getVariableReferencesInQuery } from 'lib/dashboardVariables/variableRef
import getTimeString from 'lib/getTimeString';
import { isEqual } from 'lodash-es';
import isEmpty from 'lodash-es/isEmpty';
-import { useDashboard } from 'providers/Dashboard/Dashboard';
import { UpdateTimeInterval } from 'store/actions';
import { AppState } from 'store/reducers';
import APIError from 'types/api/error';
@@ -68,7 +68,19 @@ function GridCardGraph({
const [isInternalServerError, setIsInternalServerError] = useState<boolean>(
false,
);
- const { setDashboardQueryRangeCalled } = useDashboard();
+ const queryRangeCalledRef = useRef(false);
+
+ useEffect(() => {
+ const timeoutId = setTimeout(() => {
+ if (!queryRangeCalledRef.current) {
+ Sentry.captureEvent({
+ message: `Dashboard query range not called within expected timeframe for widget ${widget?.id}`,
+ level: 'warning',
+ });
+ }
+ }, 120000);
+ return (): void => clearTimeout(timeoutId);
+ }, [widget?.id]);
const {
minTime,
@@ -260,14 +272,14 @@ function GridCardGraph({
});
}
}
- setDashboardQueryRangeCalled(true);
+ queryRangeCalledRef.current = true;
},
onSettled: (data) => {
dataAvailable?.(
isDataAvailableByPanelType(data?.payload?.data, widget?.panelTypes),
);
getGraphData?.(data?.payload?.data);
- setDashboardQueryRangeCalled(true);
+ queryRangeCalledRef.current = true;
},
},
);
diff --git a/frontend/src/container/GridCardLayout/GridCardLayout.tsx b/frontend/src/container/GridCardLayout/GridCardLayout.tsx
index 88cd9df67d..9cc55db4f7 100644
--- a/frontend/src/container/GridCardLayout/GridCardLayout.tsx
+++ b/frontend/src/container/GridCardLayout/GridCardLayout.tsx
@@ -1,10 +1,10 @@
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { FullScreen, FullScreenHandle } from 'react-full-screen';
import { ItemCallback, Layout } from 'react-grid-layout';
+import { useIsFetching } from 'react-query';
// eslint-disable-next-line no-restricted-imports
import { useDispatch } from 'react-redux';
import { useLocation } from 'react-router-dom';
-import * as Sentry from '@sentry/react';
import { Color } from '@signozhq/design-tokens';
import { Button, Form, Input, Modal, Typography } from 'antd';
import logEvent from 'api/common/logEvent';
@@ -12,6 +12,7 @@ import cx from 'classnames';
import { ENTITY_VERSION_V5 } from 'constants/app';
import { QueryParams } from 'constants/query';
import { PANEL_GROUP_TYPES, PANEL_TYPES } from 'constants/queryBuilder';
+import { REACT_QUERY_KEY } from 'constants/reactQueryKeys';
import { themeColors } from 'constants/theme';
import { DEFAULT_ROW_NAME } from 'container/DashboardContainer/DashboardDescription/utils';
import { useDashboardVariables } from 'hooks/dashboard/useDashboardVariables';
@@ -31,7 +32,10 @@ import {
X,
} from 'lucide-react';
import { useAppContext } from 'providers/App/App';
-import { useDashboard } from 'providers/Dashboard/Dashboard';
+import {
+ selectIsDashboardLocked,
+ useDashboardStore,
+} from 'providers/Dashboard/store/useDashboardStore';
import { sortLayout } from 'providers/Dashboard/util';
import { UpdateTimeInterval } from 'store/actions';
import { Widgets } from 'types/api/dashboard/getAll';
@@ -61,6 +65,9 @@ interface GraphLayoutProps {
function GraphLayout(props: GraphLayoutProps): JSX.Element {
const { handle, enableDrillDown = false } = props;
const { safeNavigate } = useSafeNavigate();
+ const isDashboardFetching =
+ useIsFetching([REACT_QUERY_KEY.DASHBOARD_BY_ID]) > 0;
+
const {
selectedDashboard,
layouts,
@@ -68,12 +75,9 @@ function GraphLayout(props: GraphLayoutProps): JSX.Element {
panelMap,
setPanelMap,
setSelectedDashboard,
- isDashboardLocked,
- dashboardQueryRangeCalled,
- setDashboardQueryRangeCalled,
- isDashboardFetching,
columnWidths,
- } = useDashboard();
+ } = useDashboardStore();
+ const isDashboardLocked = useDashboardStore(selectIsDashboardLocked);
const { data } = selectedDashboard || {};
const { pathname } = useLocation();
const dispatch = useDispatch();
@@ -137,25 +141,6 @@ function GraphLayout(props: GraphLayoutProps): JSX.Element {
setDashboardLayout(sortLayout(layouts));
}, [layouts]);
- useEffect(() => {
- setDashboardQueryRangeCalled(false);
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, []);
-
- useEffect(() => {
- const timeoutId = setTimeout(() => {
- // Send Sentry event if query_range is not called within expected timeframe (2 mins) when there are widgets
- if (!dashboardQueryRangeCalled && data?.widgets?.length) {
- Sentry.captureEvent({
- message: `Dashboard query range not called within expected timeframe even when there are ${data?.widgets?.length} widgets`,
- level: 'warning',
- });
- }
- }, 120000);
-
- return (): void => clearTimeout(timeoutId);
- }, [dashboardQueryRangeCalled, data?.widgets?.length]);
-
const logEventCalledRef = useRef(false);
useEffect(() => {
if (!logEventCalledRef.current && !isUndefined(data)) {
diff --git a/frontend/src/container/GridCardLayout/WidgetRow.tsx b/frontend/src/container/GridCardLayout/WidgetRow.tsx
index 5d7da07658..a864cdf242 100644
--- a/frontend/src/container/GridCardLayout/WidgetRow.tsx
+++ b/frontend/src/container/GridCardLayout/WidgetRow.tsx
@@ -4,9 +4,12 @@ import { Button, Popover } from 'antd';
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 { usePanelTypeSelectionModalStore } from 'providers/Dashboard/helpers/panelTypeSelectionModalHelper';
import { setSelectedRowWidgetId } from 'providers/Dashboard/helpers/selectedRowWidgetIdHelper';
+import {
+ selectIsDashboardLocked,
+ useDashboardStore,
+} from 'providers/Dashboard/store/useDashboardStore';
import { ROLES, USER_ROLES } from 'types/roles';
import { ComponentTypes } from 'utils/permission';
@@ -39,7 +42,8 @@ export function WidgetRowHeader(props: WidgetRowHeaderProps): JSX.Element {
(s) => s.setIsPanelTypeSelectionModalOpen,
);
- const { selectedDashboard, isDashboardLocked } = useDashboard();
+ const { selectedDashboard } = useDashboardStore();
+ const isDashboardLocked = useDashboardStore(selectIsDashboardLocked);
const permissions: ComponentTypes[] = ['add_panel'];
const { user } = useAppContext();
diff --git a/frontend/src/container/GridCardLayout/useNavigateToExplorerPages.ts b/frontend/src/container/GridCardLayout/useNavigateToExplorerPages.ts
index b6214cf53f..b71ada3b12 100644
--- a/frontend/src/container/GridCardLayout/useNavigateToExplorerPages.ts
+++ b/frontend/src/container/GridCardLayout/useNavigateToExplorerPages.ts
@@ -1,6 +1,6 @@
import { useCallback } from 'react';
import { useNotifications } from 'hooks/useNotifications';
-import { useDashboard } from 'providers/Dashboard/Dashboard';
+import { useDashboardStore } from 'providers/Dashboard/store/useDashboardStore';
import { Widgets } from 'types/api/dashboard/getAll';
import { BaseAutocompleteData } from 'types/api/queryBuilder/queryAutocompleteResponse';
import {
@@ -121,7 +121,7 @@ function useNavigateToExplorerPages(): (
) => Promise<{
[queryName: string]: { filters: TagFilterItem[]; dataSource?: string };
}> {
- const { selectedDashboard } = useDashboard();
+ const { selectedDashboard } = useDashboardStore();
const { notifications } = useNotifications();
return useCallback(
diff --git a/frontend/src/container/LogExplorerQuerySection/LogExplorerQuerySection.test.tsx b/frontend/src/container/LogExplorerQuerySection/LogExplorerQuerySection.test.tsx
index 91aa3fe2ae..dc7c021e9a 100644
--- a/frontend/src/container/LogExplorerQuerySection/LogExplorerQuerySection.test.tsx
+++ b/frontend/src/container/LogExplorerQuerySection/LogExplorerQuerySection.test.tsx
@@ -92,8 +92,8 @@ jest.mock('hooks/useDarkMode', () => ({
useIsDarkMode: (): boolean => false,
}));
-jest.mock('providers/Dashboard/Dashboard', () => ({
- useDashboard: (): { selectedDashboard: undefined } => ({
+jest.mock('providers/Dashboard/store/useDashboardStore', () => ({
+ useDashboardStore: (): { selectedDashboard: undefined } => ({
selectedDashboard: undefined,
}),
}));
diff --git a/frontend/src/container/NewWidget/__test__/NewWidget.test.tsx b/frontend/src/container/NewWidget/__test__/NewWidget.test.tsx
index 9451d63880..67f2aecdd1 100644
--- a/frontend/src/container/NewWidget/__test__/NewWidget.test.tsx
+++ b/frontend/src/container/NewWidget/__test__/NewWidget.test.tsx
@@ -6,11 +6,24 @@
// - Handling multiple rows correctly
// - Handling widgets with different heights
+import { ReactNode } from 'react';
import { I18nextProvider } from 'react-i18next';
import { useSearchParams } from 'react-router-dom-v5-compat';
import { screen } from '@testing-library/react';
import { PANEL_TYPES } from 'constants/queryBuilder';
-import { DashboardProvider } from 'providers/Dashboard/Dashboard';
+import { useDashboardBootstrap } from 'hooks/dashboard/useDashboardBootstrap';
+
+function DashboardBootstrapWrapper({
+ dashboardId,
+ children,
+}: {
+ dashboardId: string;
+ children: ReactNode;
+}): JSX.Element {
+ useDashboardBootstrap(dashboardId);
+ // eslint-disable-next-line react/jsx-no-useless-fragment
+ return <>{children}</>;
+}
import { PreferenceContextProvider } from 'providers/preferences/context/PreferenceContextProvider';
import i18n from 'ReactI18';
import {
@@ -309,7 +322,7 @@ describe('Stacking bar in new panel', () => {
const { container, getByText } = render(
<I18nextProvider i18n={i18n}>
- <DashboardProvider dashboardId="">
+ <DashboardBootstrapWrapper dashboardId="">
<PreferenceContextProvider>
<NewWidget
dashboardId=""
@@ -317,7 +330,7 @@ describe('Stacking bar in new panel', () => {
selectedGraph={PANEL_TYPES.BAR}
/>
</PreferenceContextProvider>
- </DashboardProvider>
+ </DashboardBootstrapWrapper>
</I18nextProvider>,
);
@@ -362,13 +375,13 @@ describe('when switching to BAR panel type', () => {
});
const { getByTestId, getByText, container } = render(
- <DashboardProvider dashboardId="">
+ <DashboardBootstrapWrapper dashboardId="">
<NewWidget
dashboardId=""
selectedDashboard={undefined}
selectedGraph={PANEL_TYPES.BAR}
/>
- </DashboardProvider>,
+ </DashboardBootstrapWrapper>,
);
expect(getByTestId('panel-change-select')).toHaveAttribute(
diff --git a/frontend/src/container/NewWidget/types.ts b/frontend/src/container/NewWidget/types.ts
index 7a94525b2f..3df55d0f91 100644
--- a/frontend/src/container/NewWidget/types.ts
+++ b/frontend/src/container/NewWidget/types.ts
@@ -2,16 +2,15 @@ import { Dispatch, SetStateAction } from 'react';
import { UseQueryResult } from 'react-query';
import { PANEL_TYPES } from 'constants/queryBuilder';
import { GetQueryResultsProps } from 'lib/dashboard/getQueryResults';
-import { IDashboardContext } from 'providers/Dashboard/types';
import { SuccessResponse, Warning } from 'types/api';
-import { Widgets } from 'types/api/dashboard/getAll';
+import { Dashboard, Widgets } from 'types/api/dashboard/getAll';
import { MetricRangePayloadProps } from 'types/api/metrics/getQueryRange';
import { timePreferance } from './RightContainer/timeItems';
export interface NewWidgetProps {
dashboardId: string;
- selectedDashboard: IDashboardContext['selectedDashboard'];
+ selectedDashboard: Dashboard | undefined;
selectedGraph: PANEL_TYPES;
enableDrillDown?: boolean;
}
@@ -35,7 +34,7 @@ export interface WidgetGraphProps {
>
>;
enableDrillDown?: boolean;
- selectedDashboard: IDashboardContext['selectedDashboard'];
+ selectedDashboard: Dashboard | undefined;
isNewPanel?: boolean;
}
diff --git a/frontend/src/container/QueryTable/Drilldown/useBaseAggregateOptions.tsx b/frontend/src/container/QueryTable/Drilldown/useBaseAggregateOptions.tsx
index aea611d414..c350038fa1 100644
--- a/frontend/src/container/QueryTable/Drilldown/useBaseAggregateOptions.tsx
+++ b/frontend/src/container/QueryTable/Drilldown/useBaseAggregateOptions.tsx
@@ -11,7 +11,7 @@ import useContextVariables from 'hooks/dashboard/useContextVariables';
import { useSafeNavigate } from 'hooks/useSafeNavigate';
import createQueryParams from 'lib/createQueryParams';
import ContextMenu from 'periscope/components/ContextMenu';
-import { useDashboard } from 'providers/Dashboard/Dashboard';
+import { useDashboardStore } from 'providers/Dashboard/store/useDashboardStore';
import { ContextLinksData } from 'types/api/dashboard/getAll';
import { Query } from 'types/api/queryBuilder/queryBuilderData';
@@ -66,7 +66,7 @@ const useBaseAggregateOptions = ({
getUpdatedQuery,
isLoading: isResolveQueryLoading,
} = useUpdatedQuery();
- const { selectedDashboard } = useDashboard();
+ const { selectedDashboard } = useDashboardStore();
useEffect(() => {
if (!aggregateData) {
diff --git a/frontend/src/container/QueryTable/__test__/QueryTable.test.tsx b/frontend/src/container/QueryTable/__test__/QueryTable.test.tsx
index 55864e2d2b..9034528b5f 100644
--- a/frontend/src/container/QueryTable/__test__/QueryTable.test.tsx
+++ b/frontend/src/container/QueryTable/__test__/QueryTable.test.tsx
@@ -12,8 +12,8 @@ jest.mock('react-router-dom', () => ({
}));
// Mock useDashabord hook
-jest.mock('providers/Dashboard/Dashboard', () => ({
- useDashboard: (): any => ({
+jest.mock('providers/Dashboard/store/useDashboardStore', () => ({
+ useDashboardStore: (): any => ({
selectedDashboard: {
data: {
variables: [],
diff --git a/frontend/src/hooks/dashboard/__test__/useTransformDashboardVariables.test.tsx b/frontend/src/hooks/dashboard/__test__/useTransformDashboardVariables.test.tsx
index b5dbcb2d78..4777158613 100644
--- a/frontend/src/hooks/dashboard/__test__/useTransformDashboardVariables.test.tsx
+++ b/frontend/src/hooks/dashboard/__test__/useTransformDashboardVariables.test.tsx
@@ -1,5 +1,5 @@
import { renderHook } from '@testing-library/react';
-import { useDashboardVariablesFromLocalStorage } from 'hooks/dashboard/useDashboardFromLocalStorage';
+import { getLocalStorageDashboardVariables } from 'hooks/dashboard/useDashboardFromLocalStorage';
import { useTransformDashboardVariables } from 'hooks/dashboard/useTransformDashboardVariables';
import useVariablesFromUrl from 'hooks/dashboard/useVariablesFromUrl';
import { Dashboard, IDashboardVariable } from 'types/api/dashboard/getAll';
@@ -7,8 +7,8 @@ import { Dashboard, IDashboardVariable } from 'types/api/dashboard/getAll';
jest.mock('hooks/dashboard/useDashboardFromLocalStorage');
jest.mock('hooks/dashboard/useVariablesFromUrl');
-const mockUseDashboardVariablesFromLocalStorage = useDashboardVariablesFromLocalStorage as jest.MockedFunction<
- typeof useDashboardVariablesFromLocalStorage
+const mockGetLocalStorageDashboardVariables = getLocalStorageDashboardVariables as jest.MockedFunction<
+ typeof getLocalStorageDashboardVariables
>;
const mockUseVariablesFromUrl = useVariablesFromUrl as jest.MockedFunction<
typeof useVariablesFromUrl
@@ -46,10 +46,7 @@ const setupHook = (
currentDashboard: Record<string, any> = {},
urlVariables: Record<string, any> = {},
): ReturnType<typeof useTransformDashboardVariables> => {
- mockUseDashboardVariablesFromLocalStorage.mockReturnValue({
- currentDashboard,
- updateLocalStorageDashboardVariables: jest.fn(),
- });
+ mockGetLocalStorageDashboardVariables.mockReturnValue(currentDashboard as any);
mockUseVariablesFromUrl.mockReturnValue({
getUrlVariables: () => urlVariables,
setUrlVariables: jest.fn(),
diff --git a/frontend/src/hooks/dashboard/useDashboardBootstrap.ts b/frontend/src/hooks/dashboard/useDashboardBootstrap.ts
new file mode 100644
index 0000000000..003906a115
--- /dev/null
+++ b/frontend/src/hooks/dashboard/useDashboardBootstrap.ts
@@ -0,0 +1,164 @@
+import { useEffect, useRef } from 'react';
+import { useTranslation } from 'react-i18next';
+// eslint-disable-next-line no-restricted-imports
+import { useDispatch, useSelector } from 'react-redux';
+import { Modal } from 'antd';
+import dayjs from 'dayjs';
+import { useTransformDashboardVariables } from 'hooks/dashboard/useTransformDashboardVariables';
+import useTabVisibility from 'hooks/useTabFocus';
+import { getUpdatedLayout } from 'lib/dashboard/getUpdatedLayout';
+import { getMinMaxForSelectedTime } from 'lib/getMinMax';
+import { defaultTo } from 'lodash-es';
+import { initializeDefaultVariables } from 'providers/Dashboard/initializeDefaultVariables';
+import { useDashboardStore } from 'providers/Dashboard/store/useDashboardStore';
+import { sortLayout } from 'providers/Dashboard/util';
+// eslint-disable-next-line no-restricted-imports
+import { Dispatch } from 'redux';
+import { AppState } from 'store/reducers';
+import AppActions from 'types/actions';
+import { UPDATE_TIME_INTERVAL } from 'types/actions/globalTime';
+import { Dashboard } from 'types/api/dashboard/getAll';
+import { GlobalReducer } from 'types/reducer/globalTime';
+
+import { useDashboardQuery } from './useDashboardQuery';
+import { useDashboardVariablesSync } from './useDashboardVariablesSync';
+
+interface UseDashboardBootstrapOptions {
+ /** Pass `onModal.confirm` from `Modal.useModal()` to get theme-aware modals. Falls back to static `Modal.confirm`. */
+ confirm?: typeof Modal.confirm;
+}
+
+export interface UseDashboardBootstrapReturn {
+ isLoading: boolean;
+ isError: boolean;
+ isFetching: boolean;
+ error: unknown;
+}
+
+export function useDashboardBootstrap(
+ dashboardId: string,
+ options: UseDashboardBootstrapOptions = {},
+): UseDashboardBootstrapReturn {
+ const confirm = options.confirm ?? Modal.confirm;
+
+ const { t } = useTranslation(['dashboard']);
+ const dispatch = useDispatch<Dispatch<AppActions>>();
+ const globalTime = useSelector<AppState, GlobalReducer>(
+ (state) => state.globalTime,
+ );
+
+ const {
+ setSelectedDashboard,
+ setLayouts,
+ setPanelMap,
+ resetDashboardStore,
+ } = useDashboardStore();
+
+ const dashboardRef = useRef<Dashboard>();
+ const modalRef = useRef<ReturnType<typeof Modal.confirm>>();
+
+ const isVisible = useTabVisibility();
+
+ const {
+ getUrlVariables,
+ updateUrlVariable,
+ transformDashboardVariables,
+ } = useTransformDashboardVariables(dashboardId);
+
+ // Keep the external variables store in sync with selectedDashboard
+ useDashboardVariablesSync(dashboardId);
+
+ const dashboardQuery = useDashboardQuery(dashboardId);
+
+ // Handle new dashboard data: initialize on first load, detect changes on subsequent fetches.
+ // React Query's structural sharing means this effect only fires when data actually changes.
+ useEffect(() => {
+ if (!dashboardQuery.data?.data) {
+ return;
+ }
+
+ const updatedDashboardData = transformDashboardVariables(
+ dashboardQuery.data.data,
+ );
+ const updatedDate = dayjs(updatedDashboardData?.updatedAt);
+
+ // First load: initialize store and URL variables, then return
+ if (!dashboardRef.current) {
+ const variables = updatedDashboardData?.data?.variables;
+ if (variables) {
+ initializeDefaultVariables(variables, getUrlVariables, updateUrlVariable);
+ }
+ setSelectedDashboard(updatedDashboardData);
+ dashboardRef.current = updatedDashboardData;
+ setLayouts(sortLayout(getUpdatedLayout(updatedDashboardData?.data.layout)));
+ setPanelMap(defaultTo(updatedDashboardData?.data?.panelMap, {}));
+ return;
+ }
+
+ // Subsequent fetches: skip if updatedAt hasn't advanced
+ if (!updatedDate.isAfter(dayjs(dashboardRef.current.updatedAt))) {
+ return;
+ }
+
+ // Data has changed: prompt user if tab is visible
+ if (isVisible && dashboardRef.current.id === updatedDashboardData?.id) {
+ const modal = confirm({
+ centered: true,
+ title: t('dashboard_has_been_updated'),
+ content: t('do_you_want_to_refresh_the_dashboard'),
+ onOk() {
+ setSelectedDashboard(updatedDashboardData);
+
+ const { maxTime, minTime } = getMinMaxForSelectedTime(
+ globalTime.selectedTime,
+ globalTime.minTime,
+ globalTime.maxTime,
+ );
+ dispatch({
+ type: UPDATE_TIME_INTERVAL,
+ payload: { maxTime, minTime, selectedTime: globalTime.selectedTime },
+ });
+
+ dashboardRef.current = updatedDashboardData;
+ setLayouts(
+ sortLayout(getUpdatedLayout(updatedDashboardData?.data.layout)),
+ );
+ setPanelMap(defaultTo(updatedDashboardData?.data.panelMap, {}));
+ },
+ });
+ modalRef.current = modal;
+ }
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [dashboardQuery.data]);
+
+ // Refetch when tab becomes visible (after initial load)
+ useEffect(() => {
+ if (isVisible && dashboardRef.current && !!dashboardId) {
+ dashboardQuery.refetch();
+ }
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [isVisible]);
+
+ // Dismiss stale modal when tab is hidden
+ useEffect(() => {
+ if (!isVisible && modalRef.current) {
+ modalRef.current.destroy();
+ }
+ }, [isVisible]);
+
+ // Reset store on unmount so stale state doesn't bleed across dashboards
+ useEffect(
+ () => (): void => {
+ resetDashboardStore();
+ },
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ [],
+ );
+
+ return {
+ isLoading: dashboardQuery.isLoading,
+ isError: dashboardQuery.isError,
+ isFetching: dashboardQuery.isFetching,
+ error: dashboardQuery.error,
+ };
+}
diff --git a/frontend/src/hooks/dashboard/useDashboardFromLocalStorage.ts b/frontend/src/hooks/dashboard/useDashboardFromLocalStorage.ts
new file mode 100644
index 0000000000..bfc35a3cf0
--- /dev/null
+++ b/frontend/src/hooks/dashboard/useDashboardFromLocalStorage.ts
@@ -0,0 +1,68 @@
+import getLocalStorageKey from 'api/browser/localstorage/get';
+import setLocalStorageKey from 'api/browser/localstorage/set';
+import { LOCALSTORAGE } from 'constants/localStorage';
+import { IDashboardVariable } from 'types/api/dashboard/getAll';
+
+export interface LocalStoreDashboardVariables {
+ [id: string]: {
+ selectedValue: IDashboardVariable['selectedValue'];
+ allSelected: boolean;
+ };
+}
+
+interface DashboardLocalStorageVariables {
+ [id: string]: LocalStoreDashboardVariables;
+}
+
+function readAll(): DashboardLocalStorageVariables {
+ const raw = getLocalStorageKey(LOCALSTORAGE.DASHBOARD_VARIABLES);
+ if (!raw) {
+ return {};
+ }
+ try {
+ return JSON.parse(raw);
+ } catch {
+ console.error('Failed to parse dashboard variables from local storage');
+ return {};
+ }
+}
+
+function writeAll(data: DashboardLocalStorageVariables): void {
+ try {
+ setLocalStorageKey(LOCALSTORAGE.DASHBOARD_VARIABLES, JSON.stringify(data));
+ } catch {
+ console.error('Failed to set dashboard variables in local storage');
+ }
+}
+
+/** Read the saved variable selections for a dashboard from localStorage. */
+export function getLocalStorageDashboardVariables(
+ dashboardId: string,
+): LocalStoreDashboardVariables {
+ return readAll()[dashboardId] ?? {};
+}
+
+/**
+ * Write one variable's selection for a dashboard to localStorage.
+ * All call sites write to the same store with no React state coordination.
+ */
+export function updateLocalStorageDashboardVariable(
+ dashboardId: string,
+ id: string,
+ selectedValue: IDashboardVariable['selectedValue'],
+ allSelected: boolean,
+ isDynamic?: boolean,
+): void {
+ const all = readAll();
+ all[dashboardId] = {
+ ...(all[dashboardId] ?? {}),
+ [id]:
+ isDynamic && allSelected
+ ? {
+ selectedValue: (undefined as unknown) as IDashboardVariable['selectedValue'],
+ allSelected: true,
+ }
+ : { selectedValue, allSelected },
+ };
+ writeAll(all);
+}
diff --git a/frontend/src/hooks/dashboard/useDashboardFromLocalStorage.tsx b/frontend/src/hooks/dashboard/useDashboardFromLocalStorage.tsx
deleted file mode 100644
index 9fd4d76ebb..0000000000
--- a/frontend/src/hooks/dashboard/useDashboardFromLocalStorage.tsx
+++ /dev/null
@@ -1,110 +0,0 @@
-import { useEffect, useState } from 'react';
-import getLocalStorageKey from 'api/browser/localstorage/get';
-import setLocalStorageKey from 'api/browser/localstorage/set';
-import { LOCALSTORAGE } from 'constants/localStorage';
-import { defaultTo } from 'lodash-es';
-import { IDashboardVariable } from 'types/api/dashboard/getAll';
-
-interface LocalStoreDashboardVariables {
- [id: string]: {
- selectedValue: IDashboardVariable['selectedValue'];
- allSelected: boolean;
- };
-}
-interface DashboardLocalStorageVariables {
- [id: string]: LocalStoreDashboardVariables;
-}
-
-export interface UseDashboardVariablesFromLocalStorageReturn {
- currentDashboard: LocalStoreDashboardVariables;
- updateLocalStorageDashboardVariables: (
- id: string,
- selectedValue: IDashboardVariable['selectedValue'],
- allSelected: boolean,
- isDynamic?: boolean,
- ) => void;
-}
-
-export const useDashboardVariablesFromLocalStorage = (
- dashboardId: string,
-): UseDashboardVariablesFromLocalStorageReturn => {
- const [
- allDashboards,
- setAllDashboards,
- ] = useState<DashboardLocalStorageVariables>({});
-
- const [
- currentDashboard,
- setCurrentDashboard,
- ] = useState<LocalStoreDashboardVariables>({});
-
- useEffect(() => {
- const localStoreDashboardVariablesString = getLocalStorageKey(
- LOCALSTORAGE.DASHBOARD_VARIABLES,
- );
- let localStoreDashboardVariables: DashboardLocalStorageVariables = {};
- if (localStoreDashboardVariablesString === null) {
- try {
- const serialzedData = JSON.stringify({
- [dashboardId]: {},
- });
-
- setLocalStorageKey(LOCALSTORAGE.DASHBOARD_VARIABLES, serialzedData);
- } catch {
- console.error('Failed to seralise the data');
- }
- } else {
- try {
- localStoreDashboardVariables = JSON.parse(
- localStoreDashboardVariablesString,
- );
- } catch {
- console.error('Failed to parse dashboards from local storage');
- localStoreDashboardVariables = {};
- } finally {
- setAllDashboards(localStoreDashboardVariables);
- }
- }
- setCurrentDashboard(defaultTo(localStoreDashboardVariables[dashboardId], {}));
- }, [dashboardId]);
-
- useEffect(() => {
- try {
- const serializedData = JSON.stringify(allDashboards);
- setLocalStorageKey(LOCALSTORAGE.DASHBOARD_VARIABLES, serializedData);
- } catch {
- console.error('Failed to set dashboards in local storage');
- }
- }, [allDashboards]);
-
- useEffect(() => {
- setAllDashboards((prev) => ({
- ...prev,
- [dashboardId]: { ...currentDashboard },
- }));
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [currentDashboard]);
-
- const updateLocalStorageDashboardVariables = (
- id: string,
- selectedValue: IDashboardVariable['selectedValue'],
- allSelected: boolean,
- isDynamic?: boolean,
- ): void => {
- setCurrentDashboard((prev) => ({
- ...prev,
- [id]:
- isDynamic && allSelected
- ? {
- selectedValue: (undefined as unknown) as IDashboardVariable['selectedValue'],
- allSelected: true,
- }
- : { selectedValue, allSelected },
- }));
- };
-
- return {
- currentDashboard,
- updateLocalStorageDashboardVariables,
- };
-};
diff --git a/frontend/src/hooks/dashboard/useDashboardQuery.ts b/frontend/src/hooks/dashboard/useDashboardQuery.ts
new file mode 100644
index 0000000000..e8805276d9
--- /dev/null
+++ b/frontend/src/hooks/dashboard/useDashboardQuery.ts
@@ -0,0 +1,49 @@
+import { useQuery, UseQueryResult } from 'react-query';
+// eslint-disable-next-line no-restricted-imports
+import { useSelector } from 'react-redux';
+import getDashboard from 'api/v1/dashboards/id/get';
+import {
+ DASHBOARD_CACHE_TIME,
+ DASHBOARD_CACHE_TIME_ON_REFRESH_ENABLED,
+} from 'constants/queryCacheTime';
+import { REACT_QUERY_KEY } from 'constants/reactQueryKeys';
+import { useAppContext } from 'providers/App/App';
+import { useErrorModal } from 'providers/ErrorModalProvider';
+import { AppState } from 'store/reducers';
+import { SuccessResponseV2 } from 'types/api';
+import { Dashboard } from 'types/api/dashboard/getAll';
+import APIError from 'types/api/error';
+import { GlobalReducer } from 'types/reducer/globalTime';
+
+/**
+ * Fetches a dashboard by ID. Handles auth gating, cache time based on
+ * auto-refresh setting, and surfaces API errors via the error modal.
+ */
+export function useDashboardQuery(
+ dashboardId: string,
+): UseQueryResult<SuccessResponseV2<Dashboard>> {
+ const { isLoggedIn } = useAppContext();
+ const { showErrorModal } = useErrorModal();
+ const globalTime = useSelector<AppState, GlobalReducer>(
+ (state) => state.globalTime,
+ );
+
+ return useQuery(
+ [
+ REACT_QUERY_KEY.DASHBOARD_BY_ID,
+ dashboardId,
+ globalTime.isAutoRefreshDisabled,
+ ],
+ {
+ enabled: !!dashboardId && isLoggedIn,
+ queryFn: () => getDashboard({ id: dashboardId }),
+ refetchOnWindowFocus: false,
+ cacheTime: globalTime.isAutoRefreshDisabled
+ ? DASHBOARD_CACHE_TIME
+ : DASHBOARD_CACHE_TIME_ON_REFRESH_ENABLED,
+ onError: (error) => {
+ showErrorModal(error as APIError);
+ },
+ },
+ );
+}
diff --git a/frontend/src/hooks/dashboard/useDashboardVariablesSync.ts b/frontend/src/hooks/dashboard/useDashboardVariablesSync.ts
new file mode 100644
index 0000000000..95ab497502
--- /dev/null
+++ b/frontend/src/hooks/dashboard/useDashboardVariablesSync.ts
@@ -0,0 +1,33 @@
+import { useEffect } from 'react';
+import isEqual from 'lodash-es/isEqual';
+import {
+ setDashboardVariablesStore,
+ updateDashboardVariablesStore,
+} from 'providers/Dashboard/store/dashboardVariables/dashboardVariablesStore';
+import {
+ DashboardStore,
+ useDashboardStore,
+} from 'providers/Dashboard/store/useDashboardStore';
+
+import { useDashboardVariablesSelector } from './useDashboardVariables';
+
+/**
+ * Keeps the external variables store in sync with the zustand dashboard store.
+ * When selectedDashboard changes, propagates variable updates to the variables store.
+ */
+export function useDashboardVariablesSync(dashboardId: string): void {
+ const dashboardVariables = useDashboardVariablesSelector((s) => s.variables);
+ const savedDashboardId = useDashboardVariablesSelector((s) => s.dashboardId);
+ const selectedDashboard = useDashboardStore(
+ (s: DashboardStore) => s.selectedDashboard,
+ );
+
+ useEffect(() => {
+ const updatedVariables = selectedDashboard?.data.variables || {};
+ if (savedDashboardId !== dashboardId) {
+ setDashboardVariablesStore({ dashboardId, variables: updatedVariables });
+ } else if (!isEqual(dashboardVariables, updatedVariables)) {
+ updateDashboardVariablesStore({ dashboardId, variables: updatedVariables });
+ }
+ }, [selectedDashboard]); // eslint-disable-line react-hooks/exhaustive-deps
+}
diff --git a/frontend/src/hooks/dashboard/useLockDashboard.ts b/frontend/src/hooks/dashboard/useLockDashboard.ts
new file mode 100644
index 0000000000..74b01f05d1
--- /dev/null
+++ b/frontend/src/hooks/dashboard/useLockDashboard.ts
@@ -0,0 +1,42 @@
+import { useMutation } from 'react-query';
+import locked from 'api/v1/dashboards/id/lock';
+import {
+ getSelectedDashboard,
+ useDashboardStore,
+} from 'providers/Dashboard/store/useDashboardStore';
+import { useErrorModal } from 'providers/ErrorModalProvider';
+import APIError from 'types/api/error';
+
+/**
+ * Hook for toggling dashboard locked state.
+ * Calls the lock API and syncs the result into the Zustand store.
+ */
+export function useLockDashboard(): (value: boolean) => Promise<void> {
+ const { showErrorModal } = useErrorModal();
+ const { setSelectedDashboard } = useDashboardStore();
+
+ const { mutate: lockDashboard } = useMutation(locked, {
+ onSuccess: (_, props) => {
+ setSelectedDashboard((prev) =>
+ prev ? { ...prev, locked: props.lock } : prev,
+ );
+ },
+ onError: (error) => {
+ showErrorModal(error as APIError);
+ },
+ });
+
+ return async (value: boolean): Promise<void> => {
+ const selectedDashboard = getSelectedDashboard();
+ if (selectedDashboard) {
+ try {
+ await lockDashboard({
+ id: selectedDashboard.id,
+ lock: value,
+ });
+ } catch (error) {
+ showErrorModal(error as APIError);
+ }
+ }
+ };
+}
diff --git a/frontend/src/hooks/dashboard/useTransformDashboardVariables.ts b/frontend/src/hooks/dashboard/useTransformDashboardVariables.ts
index 9cf21e9039..6b609b2692 100644
--- a/frontend/src/hooks/dashboard/useTransformDashboardVariables.ts
+++ b/frontend/src/hooks/dashboard/useTransformDashboardVariables.ts
@@ -1,8 +1,5 @@
import { ALL_SELECTED_VALUE } from 'components/NewSelect/utils';
-import {
- useDashboardVariablesFromLocalStorage,
- UseDashboardVariablesFromLocalStorageReturn,
-} from 'hooks/dashboard/useDashboardFromLocalStorage';
+import { getLocalStorageDashboardVariables } from 'hooks/dashboard/useDashboardFromLocalStorage';
import useVariablesFromUrl, {
UseVariablesFromUrlReturn,
} from 'hooks/dashboard/useVariablesFromUrl';
@@ -13,14 +10,10 @@ import { v4 as generateUUID } from 'uuid';
export function useTransformDashboardVariables(
dashboardId: string,
-): Pick<UseVariablesFromUrlReturn, 'getUrlVariables' | 'updateUrlVariable'> &
- UseDashboardVariablesFromLocalStorageReturn & {
- transformDashboardVariables: (data: Dashboard) => Dashboard;
- } {
- const {
- currentDashboard,
- updateLocalStorageDashboardVariables,
- } = useDashboardVariablesFromLocalStorage(dashboardId);
+): Pick<UseVariablesFromUrlReturn, 'getUrlVariables' | 'updateUrlVariable'> & {
+ transformDashboardVariables: (data: Dashboard) => Dashboard;
+ currentDashboard: ReturnType<typeof getLocalStorageDashboardVariables>;
+} {
const { getUrlVariables, updateUrlVariable } = useVariablesFromUrl();
const mergeDBWithLocalStorage = (
@@ -80,7 +73,7 @@ export function useTransformDashboardVariables(
if (data && data.data && data.data.variables) {
const clonedDashboardData = mergeDBWithLocalStorage(
JSON.parse(JSON.stringify(data)),
- currentDashboard,
+ getLocalStorageDashboardVariables(dashboardId),
);
const { variables } = clonedDashboardData.data;
const existingOrders: Set<number> = new Set();
@@ -122,7 +115,6 @@ export function useTransformDashboardVariables(
transformDashboardVariables,
getUrlVariables,
updateUrlVariable,
- currentDashboard,
- updateLocalStorageDashboardVariables,
+ currentDashboard: getLocalStorageDashboardVariables(dashboardId),
};
}
diff --git a/frontend/src/hooks/dashboard/useUpdateDashboard.tsx b/frontend/src/hooks/dashboard/useUpdateDashboard.tsx
index 3f3175159a..750bf8b950 100644
--- a/frontend/src/hooks/dashboard/useUpdateDashboard.tsx
+++ b/frontend/src/hooks/dashboard/useUpdateDashboard.tsx
@@ -1,7 +1,5 @@
import { useMutation, UseMutationResult } from 'react-query';
import update from 'api/v1/dashboards/id/update';
-import dayjs from 'dayjs';
-import { useDashboard } from 'providers/Dashboard/Dashboard';
import { useErrorModal } from 'providers/ErrorModalProvider';
import { SuccessResponseV2 } from 'types/api';
import { Dashboard } from 'types/api/dashboard/getAll';
@@ -9,14 +7,8 @@ import { Props } from 'types/api/dashboard/update';
import APIError from 'types/api/error';
export const useUpdateDashboard = (): UseUpdateDashboard => {
- const { updatedTimeRef } = useDashboard();
const { showErrorModal } = useErrorModal();
return useMutation(update, {
- onSuccess: (data) => {
- if (data.data) {
- updatedTimeRef.current = dayjs(data.data.updatedAt);
- }
- },
onError: (error) => {
showErrorModal(error);
},
diff --git a/frontend/src/hooks/dashboard/useWidgetsByDynamicVariableId.ts b/frontend/src/hooks/dashboard/useWidgetsByDynamicVariableId.ts
index cd30dd306d..93325165f5 100644
--- a/frontend/src/hooks/dashboard/useWidgetsByDynamicVariableId.ts
+++ b/frontend/src/hooks/dashboard/useWidgetsByDynamicVariableId.ts
@@ -1,7 +1,7 @@
import { useMemo } from 'react';
import { PANEL_GROUP_TYPES } from 'constants/queryBuilder';
import { createDynamicVariableToWidgetsMap } from 'hooks/dashboard/utils';
-import { useDashboard } from 'providers/Dashboard/Dashboard';
+import { useDashboardStore } from 'providers/Dashboard/store/useDashboardStore';
import { Widgets } from 'types/api/dashboard/getAll';
import { useDashboardVariablesByType } from './useDashboardVariablesByType';
@@ -12,7 +12,7 @@ import { useDashboardVariablesByType } from './useDashboardVariablesByType';
*/
export function useWidgetsByDynamicVariableId(): Record<string, string[]> {
const dynamicVariables = useDashboardVariablesByType('DYNAMIC', 'values');
- const { selectedDashboard } = useDashboard();
+ const { selectedDashboard } = useDashboardStore();
return useMemo(() => {
const widgets =
diff --git a/frontend/src/hooks/queryBuilder/useCreateAlerts.tsx b/frontend/src/hooks/queryBuilder/useCreateAlerts.tsx
index db978f38f7..2bfd0c3072 100644
--- a/frontend/src/hooks/queryBuilder/useCreateAlerts.tsx
+++ b/frontend/src/hooks/queryBuilder/useCreateAlerts.tsx
@@ -17,7 +17,7 @@ import { useNotifications } from 'hooks/useNotifications';
import { getDashboardVariables } from 'lib/dashboardVariables/getDashboardVariables';
import { mapQueryDataFromApi } from 'lib/newQueryBuilder/queryBuilderMappers/mapQueryDataFromApi';
import { isEmpty } from 'lodash-es';
-import { useDashboard } from 'providers/Dashboard/Dashboard';
+import { useDashboardStore } from 'providers/Dashboard/store/useDashboardStore';
import { AppState } from 'store/reducers';
import { Widgets } from 'types/api/dashboard/getAll';
import { GlobalReducer } from 'types/reducer/globalTime';
@@ -33,7 +33,7 @@ const useCreateAlerts = (widget?: Widgets, caller?: string): VoidFunction => {
const { notifications } = useNotifications();
- const { selectedDashboard } = useDashboard();
+ const { selectedDashboard } = useDashboardStore();
const { dashboardVariables } = useDashboardVariables();
const dashboardDynamicVariables = useDashboardVariablesByType(
diff --git a/frontend/src/pages/DashboardPage/DashboardPage.tsx b/frontend/src/pages/DashboardPage/DashboardPage.tsx
deleted file mode 100644
index 1d40bdeba6..0000000000
--- a/frontend/src/pages/DashboardPage/DashboardPage.tsx
+++ /dev/null
@@ -1,40 +0,0 @@
-import { useEffect } from 'react';
-import { Typography } from 'antd';
-import { AxiosError } from 'axios';
-import NotFound from 'components/NotFound';
-import Spinner from 'components/Spinner';
-import DashboardContainer from 'container/DashboardContainer';
-import { useDashboard } from 'providers/Dashboard/Dashboard';
-import { ErrorType } from 'types/common';
-
-function DashboardPage(): JSX.Element {
- const { dashboardResponse } = useDashboard();
-
- const { isFetching, isError, isLoading } = dashboardResponse;
-
- const errorMessage = isError
- ? (dashboardResponse?.error as AxiosError<{ errorType: string }>)?.response
- ?.data?.errorType
- : 'Something went wrong';
-
- useEffect(() => {
- const dashboardTitle = dashboardResponse.data?.data.data.title;
- document.title = dashboardTitle || document.title;
- }, [dashboardResponse.data?.data.data.title, isFetching]);
-
- if (isError && !isFetching && errorMessage === ErrorType.NotFound) {
- return <NotFound />;
- }
-
- if (isError && errorMessage) {
- return <Typography>{errorMessage}</Typography>;
- }
-
- if (isLoading) {
- return <Spinner tip="Loading.." />;
- }
-
- return <DashboardContainer />;
-}
-
-export default DashboardPage;
diff --git a/frontend/src/pages/DashboardPage/index.tsx b/frontend/src/pages/DashboardPage/index.tsx
index bc707f4f4c..eec467ab0f 100644
--- a/frontend/src/pages/DashboardPage/index.tsx
+++ b/frontend/src/pages/DashboardPage/index.tsx
@@ -1,16 +1,56 @@
+import { useEffect } from 'react';
import { useParams } from 'react-router-dom';
-import { DashboardProvider } from 'providers/Dashboard/Dashboard';
+import { Modal, Typography } from 'antd';
+import { AxiosError } from 'axios';
+import NotFound from 'components/NotFound';
+import Spinner from 'components/Spinner';
+import DashboardContainer from 'container/DashboardContainer';
+import { useDashboardBootstrap } from 'hooks/dashboard/useDashboardBootstrap';
+import { useDashboardStore } from 'providers/Dashboard/store/useDashboardStore';
+import { ErrorType } from 'types/common';
-import DashboardPage from './DashboardPage';
-
-function DashboardPageWithProvider(): JSX.Element {
+function DashboardPage(): JSX.Element {
const { dashboardId } = useParams<{ dashboardId: string }>();
+ const [onModal, Content] = Modal.useModal();
+
+ const {
+ isLoading,
+ isError,
+ isFetching,
+ error,
+ } = useDashboardBootstrap(dashboardId, { confirm: onModal.confirm });
+
+ const dashboardTitle = useDashboardStore(
+ (s) => s.selectedDashboard?.data.title,
+ );
+
+ useEffect(() => {
+ document.title = dashboardTitle || document.title;
+ }, [dashboardTitle]);
+
+ const errorMessage = isError
+ ? (error as AxiosError<{ errorType: string }>)?.response?.data?.errorType
+ : 'Something went wrong';
+
+ if (isError && !isFetching && errorMessage === ErrorType.NotFound) {
+ return <NotFound />;
+ }
+
+ if (isError && errorMessage) {
+ return <Typography>{errorMessage}</Typography>;
+ }
+
+ if (isLoading) {
+ return <Spinner tip="Loading.." />;
+ }
+
return (
- <DashboardProvider dashboardId={dashboardId}>
- <DashboardPage />
- </DashboardProvider>
+ <>
+ {Content}
+ <DashboardContainer />
+ </>
);
}
-export default DashboardPageWithProvider;
+export default DashboardPage;
diff --git a/frontend/src/providers/Dashboard/Dashboard.tsx b/frontend/src/providers/Dashboard/Dashboard.tsx
deleted file mode 100644
index 33930dd654..0000000000
--- a/frontend/src/providers/Dashboard/Dashboard.tsx
+++ /dev/null
@@ -1,365 +0,0 @@
-import {
- // eslint-disable-next-line no-restricted-imports
- createContext,
- PropsWithChildren,
- // eslint-disable-next-line no-restricted-imports
- useContext,
- useEffect,
- useMemo,
- useRef,
- useState,
-} from 'react';
-import { Layout } from 'react-grid-layout';
-import { useTranslation } from 'react-i18next';
-import { useMutation, useQuery, UseQueryResult } from 'react-query';
-// eslint-disable-next-line no-restricted-imports
-import { useDispatch, useSelector } from 'react-redux';
-import { Modal } from 'antd';
-import getDashboard from 'api/v1/dashboards/id/get';
-import locked from 'api/v1/dashboards/id/lock';
-import { REACT_QUERY_KEY } from 'constants/reactQueryKeys';
-import dayjs, { Dayjs } from 'dayjs';
-import { useTransformDashboardVariables } from 'hooks/dashboard/useTransformDashboardVariables';
-import useTabVisibility from 'hooks/useTabFocus';
-import { getUpdatedLayout } from 'lib/dashboard/getUpdatedLayout';
-import { getMinMaxForSelectedTime } from 'lib/getMinMax';
-import { defaultTo } from 'lodash-es';
-import isEqual from 'lodash-es/isEqual';
-import isUndefined from 'lodash-es/isUndefined';
-import omitBy from 'lodash-es/omitBy';
-import { useAppContext } from 'providers/App/App';
-import { initializeDefaultVariables } from 'providers/Dashboard/initializeDefaultVariables';
-import { useErrorModal } from 'providers/ErrorModalProvider';
-// eslint-disable-next-line no-restricted-imports
-import { Dispatch } from 'redux';
-import { AppState } from 'store/reducers';
-import AppActions from 'types/actions';
-import { UPDATE_TIME_INTERVAL } from 'types/actions/globalTime';
-import { SuccessResponseV2 } from 'types/api';
-import { Dashboard } from 'types/api/dashboard/getAll';
-import APIError from 'types/api/error';
-import { GlobalReducer } from 'types/reducer/globalTime';
-
-import {
- DASHBOARD_CACHE_TIME,
- DASHBOARD_CACHE_TIME_ON_REFRESH_ENABLED,
-} from '../../constants/queryCacheTime';
-import { useDashboardVariablesSelector } from '../../hooks/dashboard/useDashboardVariables';
-import {
- setDashboardVariablesStore,
- updateDashboardVariablesStore,
-} from './store/dashboardVariables/dashboardVariablesStore';
-import { IDashboardContext, WidgetColumnWidths } from './types';
-import { sortLayout } from './util';
-
-export const DashboardContext = createContext<IDashboardContext>({
- isDashboardLocked: false,
- handleDashboardLockToggle: () => {},
- dashboardResponse: {} as UseQueryResult<
- SuccessResponseV2<Dashboard>,
- APIError
- >,
- selectedDashboard: {} as Dashboard,
- layouts: [],
- panelMap: {},
- setPanelMap: () => {},
-
- setLayouts: () => {},
- setSelectedDashboard: () => {},
- updatedTimeRef: {} as React.MutableRefObject<Dayjs | null>,
- updateLocalStorageDashboardVariables: () => {},
- dashboardQueryRangeCalled: false,
- setDashboardQueryRangeCalled: () => {},
- isDashboardFetching: false,
- columnWidths: {},
- setColumnWidths: () => {},
-});
-
-// eslint-disable-next-line sonarjs/cognitive-complexity
-export function DashboardProvider({
- children,
- dashboardId,
-}: PropsWithChildren<{ dashboardId: string }>): JSX.Element {
- const [isDashboardLocked, setIsDashboardLocked] = useState<boolean>(false);
-
- const [
- dashboardQueryRangeCalled,
- setDashboardQueryRangeCalled,
- ] = useState<boolean>(false);
-
- const { showErrorModal } = useErrorModal();
-
- const dispatch = useDispatch<Dispatch<AppActions>>();
-
- const globalTime = useSelector<AppState, GlobalReducer>(
- (state) => state.globalTime,
- );
-
- const [onModal, Content] = Modal.useModal();
-
- const [layouts, setLayouts] = useState<Layout[]>([]);
-
- const [panelMap, setPanelMap] = useState<
- Record<string, { widgets: Layout[]; collapsed: boolean }>
- >({});
-
- const { isLoggedIn } = useAppContext();
-
- const [selectedDashboard, setSelectedDashboard] = useState<Dashboard>();
- const dashboardVariables = useDashboardVariablesSelector((s) => s.variables);
- const savedDashboardId = useDashboardVariablesSelector((s) => s.dashboardId);
-
- useEffect(() => {
- const existingVariables = dashboardVariables;
- const updatedVariables = selectedDashboard?.data.variables || {};
-
- if (savedDashboardId !== dashboardId) {
- setDashboardVariablesStore({
- dashboardId,
- variables: updatedVariables,
- });
- } else if (!isEqual(existingVariables, updatedVariables)) {
- updateDashboardVariablesStore({
- dashboardId,
- variables: updatedVariables,
- });
- }
- }, [selectedDashboard]);
-
- const {
- currentDashboard,
- updateLocalStorageDashboardVariables,
- getUrlVariables,
- updateUrlVariable,
- transformDashboardVariables,
- } = useTransformDashboardVariables(dashboardId);
-
- const updatedTimeRef = useRef<Dayjs | null>(null); // Using ref to store the updated time
- const modalRef = useRef<any>(null);
-
- const isVisible = useTabVisibility();
-
- const { t } = useTranslation(['dashboard']);
- const dashboardRef = useRef<Dashboard>();
-
- const [isDashboardFetching, setIsDashboardFetching] = useState<boolean>(false);
-
- const dashboardResponse = useQuery(
- [
- REACT_QUERY_KEY.DASHBOARD_BY_ID,
- dashboardId,
- globalTime.isAutoRefreshDisabled,
- ],
- {
- enabled: !!dashboardId && isLoggedIn,
- queryFn: async () => {
- setIsDashboardFetching(true);
- try {
- return await getDashboard({
- id: dashboardId,
- });
- } catch (error) {
- showErrorModal(error as APIError);
- return;
- } finally {
- setIsDashboardFetching(false);
- }
- },
- refetchOnWindowFocus: false,
- cacheTime: globalTime.isAutoRefreshDisabled
- ? DASHBOARD_CACHE_TIME
- : DASHBOARD_CACHE_TIME_ON_REFRESH_ENABLED,
- onError: (error) => {
- showErrorModal(error as APIError);
- },
-
- onSuccess: (data: SuccessResponseV2<Dashboard>) => {
- const updatedDashboardData = transformDashboardVariables(data?.data);
-
- // initialize URL variables after dashboard state is set to avoid race conditions
- const variables = updatedDashboardData?.data?.variables;
- if (variables) {
- initializeDefaultVariables(variables, getUrlVariables, updateUrlVariable);
- }
-
- const updatedDate = dayjs(updatedDashboardData?.updatedAt);
-
- setIsDashboardLocked(updatedDashboardData?.locked || false);
-
- // on first render
- if (updatedTimeRef.current === null) {
- setSelectedDashboard(updatedDashboardData);
-
- updatedTimeRef.current = updatedDate;
-
- dashboardRef.current = updatedDashboardData;
-
- setLayouts(
- sortLayout(getUpdatedLayout(updatedDashboardData?.data.layout)),
- );
-
- setPanelMap(defaultTo(updatedDashboardData?.data?.panelMap, {}));
- }
-
- if (
- updatedTimeRef.current !== null &&
- updatedDate.isAfter(updatedTimeRef.current) &&
- isVisible &&
- dashboardRef.current?.id === updatedDashboardData?.id
- ) {
- // show modal when state is out of sync
- const modal = onModal.confirm({
- centered: true,
- title: t('dashboard_has_been_updated'),
- content: t('do_you_want_to_refresh_the_dashboard'),
- onOk() {
- setSelectedDashboard(updatedDashboardData);
-
- const { maxTime, minTime } = getMinMaxForSelectedTime(
- globalTime.selectedTime,
- globalTime.minTime,
- globalTime.maxTime,
- );
-
- dispatch({
- type: UPDATE_TIME_INTERVAL,
- payload: {
- maxTime,
- minTime,
- selectedTime: globalTime.selectedTime,
- },
- });
-
- dashboardRef.current = updatedDashboardData;
-
- updatedTimeRef.current = dayjs(updatedDashboardData?.updatedAt);
-
- setLayouts(
- sortLayout(getUpdatedLayout(updatedDashboardData?.data.layout)),
- );
-
- setPanelMap(defaultTo(updatedDashboardData?.data.panelMap, {}));
- },
- });
-
- modalRef.current = modal;
- } else {
- // normal flow
- updatedTimeRef.current = dayjs(updatedDashboardData?.updatedAt);
-
- dashboardRef.current = updatedDashboardData;
-
- if (!isEqual(selectedDashboard, updatedDashboardData)) {
- setSelectedDashboard(updatedDashboardData);
- }
-
- if (
- !isEqual(
- [omitBy(layouts, (value): boolean => isUndefined(value))[0]],
- updatedDashboardData?.data.layout,
- )
- ) {
- setLayouts(
- sortLayout(getUpdatedLayout(updatedDashboardData?.data.layout)),
- );
-
- setPanelMap(defaultTo(updatedDashboardData?.data.panelMap, {}));
- }
- }
- },
- },
- );
-
- useEffect(() => {
- // make the call on tab visibility only if the user is on dashboard / widget page
- if (isVisible && updatedTimeRef.current && !!dashboardId) {
- dashboardResponse.refetch();
- }
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [isVisible]);
-
- useEffect(() => {
- if (!isVisible && modalRef.current) {
- modalRef.current.destroy();
- }
- }, [isVisible]);
-
- const { mutate: lockDashboard } = useMutation(locked, {
- onSuccess: (_, props) => {
- setIsDashboardLocked(props.lock);
- },
- onError: (error) => {
- showErrorModal(error as APIError);
- },
- });
-
- const handleDashboardLockToggle = async (value: boolean): Promise<void> => {
- if (selectedDashboard) {
- try {
- await lockDashboard({
- id: selectedDashboard.id,
- lock: value,
- });
- } catch (error) {
- showErrorModal(error as APIError);
- }
- }
- };
-
- const [columnWidths, setColumnWidths] = useState<WidgetColumnWidths>({});
-
- const value: IDashboardContext = useMemo(
- () => ({
- isDashboardLocked,
- handleDashboardLockToggle,
- dashboardResponse,
- selectedDashboard,
- dashboardId,
- layouts,
- panelMap,
- setLayouts,
- setPanelMap,
- setSelectedDashboard,
- updatedTimeRef,
- updateLocalStorageDashboardVariables,
- dashboardQueryRangeCalled,
- setDashboardQueryRangeCalled,
- isDashboardFetching,
- columnWidths,
- setColumnWidths,
- }),
- // eslint-disable-next-line react-hooks/exhaustive-deps
- [
- isDashboardLocked,
- dashboardResponse,
- selectedDashboard,
- dashboardId,
- layouts,
- panelMap,
- updateLocalStorageDashboardVariables,
- currentDashboard,
- dashboardQueryRangeCalled,
- setDashboardQueryRangeCalled,
- isDashboardFetching,
- columnWidths,
- setColumnWidths,
- ],
- );
-
- return (
- <DashboardContext.Provider value={value}>
- {Content}
- {children}
- </DashboardContext.Provider>
- );
-}
-
-export const useDashboard = (): IDashboardContext => {
- const context = useContext(DashboardContext);
-
- if (!context) {
- throw new Error('Should be used inside the context');
- }
-
- return context;
-};
diff --git a/frontend/src/providers/Dashboard/__tests__/Dashboard.test.tsx b/frontend/src/providers/Dashboard/__tests__/Dashboard.test.tsx
index d86aaed92b..f47bf601ae 100644
--- a/frontend/src/providers/Dashboard/__tests__/Dashboard.test.tsx
+++ b/frontend/src/providers/Dashboard/__tests__/Dashboard.test.tsx
@@ -1,3 +1,4 @@
+import { ReactNode } from 'react';
import { QueryClient, QueryClientProvider } from 'react-query';
// eslint-disable-next-line no-restricted-imports
import { useSelector } from 'react-redux';
@@ -6,7 +7,20 @@ import { render, RenderResult, screen, waitFor } from '@testing-library/react';
import getDashboard from 'api/v1/dashboards/id/get';
import { DASHBOARD_CACHE_TIME_ON_REFRESH_ENABLED } from 'constants/queryCacheTime';
import { REACT_QUERY_KEY } from 'constants/reactQueryKeys';
-import { DashboardProvider, useDashboard } from 'providers/Dashboard/Dashboard';
+import { useDashboardBootstrap } from 'hooks/dashboard/useDashboardBootstrap';
+
+function DashboardBootstrapWrapper({
+ dashboardId,
+ children,
+}: {
+ dashboardId: string;
+ children: ReactNode;
+}): JSX.Element {
+ useDashboardBootstrap(dashboardId);
+ // eslint-disable-next-line react/jsx-no-useless-fragment
+ return <>{children}</>;
+}
+import { useDashboardStore } from 'providers/Dashboard/store/useDashboardStore';
import { IDashboardVariable } from 'types/api/dashboard/getAll';
import { useDashboardVariables } from '../../../hooks/dashboard/useDashboardVariables';
@@ -55,17 +69,12 @@ jest.mock('react-redux', () => ({
jest.mock('uuid', () => ({ v4: jest.fn(() => 'mock-uuid') }));
function TestComponent(): JSX.Element {
- const { dashboardResponse, selectedDashboard } = useDashboard();
+ const { selectedDashboard } = useDashboardStore();
const { dashboardVariables } = useDashboardVariables();
return (
<div>
<div data-testid="dashboard-id">{selectedDashboard?.id}</div>
- <div data-testid="query-status">{dashboardResponse.status}</div>
- <div data-testid="is-loading">{dashboardResponse.isLoading.toString()}</div>
- <div data-testid="is-fetching">
- {dashboardResponse.isFetching.toString()}
- </div>
<div data-testid="dashboard-variables">
{dashboardVariables ? JSON.stringify(dashboardVariables) : 'null'}
</div>
@@ -89,7 +98,7 @@ function createTestQueryClient(): QueryClient {
}
// Helper to render with dashboard provider
-function renderWithDashboardProvider(
+function renderWithDashboardBootstrap(
dashboardId = 'test-dashboard-id',
): RenderResult {
const queryClient = createTestQueryClient();
@@ -98,9 +107,9 @@ function renderWithDashboardProvider(
return render(
<QueryClientProvider client={queryClient}>
<MemoryRouter initialEntries={[initialRoute]}>
- <DashboardProvider dashboardId={dashboardId}>
+ <DashboardBootstrapWrapper dashboardId={dashboardId}>
<TestComponent />
- </DashboardProvider>
+ </DashboardBootstrapWrapper>
</MemoryRouter>
</QueryClientProvider>,
);
@@ -172,7 +181,7 @@ describe('Dashboard Provider - Query Key with Route Params', () => {
describe('Query Key Behavior', () => {
it('should include route params in query key when on dashboard page', async () => {
const dashboardId = 'test-dashboard-id';
- renderWithDashboardProvider(dashboardId);
+ renderWithDashboardBootstrap(dashboardId);
await waitFor(() => {
expect(mockGetDashboard).toHaveBeenCalledWith({ id: dashboardId });
@@ -187,7 +196,7 @@ describe('Dashboard Provider - Query Key with Route Params', () => {
const newDashboardId = 'new-dashboard-id';
// First render with initial dashboard ID
- const { rerender } = renderWithDashboardProvider(initialDashboardId);
+ const { rerender } = renderWithDashboardBootstrap(initialDashboardId);
await waitFor(() => {
expect(mockGetDashboard).toHaveBeenCalledWith({ id: initialDashboardId });
@@ -197,9 +206,9 @@ describe('Dashboard Provider - Query Key with Route Params', () => {
rerender(
<QueryClientProvider client={createTestQueryClient()}>
<MemoryRouter initialEntries={[`/dashboard/${newDashboardId}`]}>
- <DashboardProvider dashboardId={newDashboardId}>
+ <DashboardBootstrapWrapper dashboardId={newDashboardId}>
<TestComponent />
- </DashboardProvider>
+ </DashboardBootstrapWrapper>
</MemoryRouter>
</QueryClientProvider>,
);
@@ -213,7 +222,7 @@ describe('Dashboard Provider - Query Key with Route Params', () => {
});
it('should not fetch when no dashboardId is provided', () => {
- renderWithDashboardProvider('');
+ renderWithDashboardBootstrap('');
// Should not call the API
expect(mockGetDashboard).not.toHaveBeenCalled();
@@ -229,9 +238,9 @@ describe('Dashboard Provider - Query Key with Route Params', () => {
const { rerender } = render(
<QueryClientProvider client={queryClient}>
<MemoryRouter initialEntries={[`/dashboard/${dashboardId1}`]}>
- <DashboardProvider dashboardId={dashboardId1}>
+ <DashboardBootstrapWrapper dashboardId={dashboardId1}>
<TestComponent />
- </DashboardProvider>
+ </DashboardBootstrapWrapper>
</MemoryRouter>
</QueryClientProvider>,
);
@@ -243,9 +252,9 @@ describe('Dashboard Provider - Query Key with Route Params', () => {
rerender(
<QueryClientProvider client={queryClient}>
<MemoryRouter initialEntries={[`/dashboard/${dashboardId2}`]}>
- <DashboardProvider dashboardId={dashboardId2}>
+ <DashboardBootstrapWrapper dashboardId={dashboardId2}>
<TestComponent />
- </DashboardProvider>
+ </DashboardBootstrapWrapper>
</MemoryRouter>
</QueryClientProvider>,
);
@@ -286,9 +295,9 @@ describe('Dashboard Provider - Query Key with Route Params', () => {
render(
<QueryClientProvider client={queryClient}>
<MemoryRouter initialEntries={[`/dashboard/${dashboardId}`]}>
- <DashboardProvider dashboardId={dashboardId}>
+ <DashboardBootstrapWrapper dashboardId={dashboardId}>
<TestComponent />
- </DashboardProvider>
+ </DashboardBootstrapWrapper>
</MemoryRouter>
</QueryClientProvider>,
);
@@ -365,7 +374,7 @@ describe('Dashboard Provider - URL Variables Integration', () => {
// Empty URL variables - tests initialization flow
mockGetUrlVariables.mockReturnValue({});
- renderWithDashboardProvider(DASHBOARD_ID);
+ renderWithDashboardBootstrap(DASHBOARD_ID);
await waitFor(() => {
expect(mockGetDashboard).toHaveBeenCalledWith({ id: DASHBOARD_ID });
@@ -421,7 +430,7 @@ describe('Dashboard Provider - URL Variables Integration', () => {
.mockReturnValueOnce('development')
.mockReturnValueOnce(['db', 'cache']);
- renderWithDashboardProvider(DASHBOARD_ID);
+ renderWithDashboardBootstrap(DASHBOARD_ID);
await waitFor(() => {
expect(mockGetDashboard).toHaveBeenCalledWith({ id: DASHBOARD_ID });
@@ -481,7 +490,7 @@ describe('Dashboard Provider - URL Variables Integration', () => {
mockGetUrlVariables.mockReturnValue(urlVariables);
- renderWithDashboardProvider(DASHBOARD_ID);
+ renderWithDashboardBootstrap(DASHBOARD_ID);
await waitFor(() => {
expect(mockGetDashboard).toHaveBeenCalledWith({ id: DASHBOARD_ID });
@@ -517,7 +526,7 @@ describe('Dashboard Provider - URL Variables Integration', () => {
.mockReturnValueOnce('development')
.mockReturnValueOnce(['api']);
- renderWithDashboardProvider(DASHBOARD_ID);
+ renderWithDashboardBootstrap(DASHBOARD_ID);
await waitFor(() => {
// Verify normalization was called with the specific values and variable configs
@@ -584,7 +593,7 @@ describe('Dashboard Provider - Textbox Variable Backward Compatibility', () => {
} as any);
/* eslint-enable @typescript-eslint/no-explicit-any */
- renderWithDashboardProvider(DASHBOARD_ID);
+ renderWithDashboardBootstrap(DASHBOARD_ID);
await waitFor(() => {
expect(mockGetDashboard).toHaveBeenCalledWith({ id: DASHBOARD_ID });
@@ -626,7 +635,7 @@ describe('Dashboard Provider - Textbox Variable Backward Compatibility', () => {
} as any);
/* eslint-enable @typescript-eslint/no-explicit-any */
- renderWithDashboardProvider(DASHBOARD_ID);
+ renderWithDashboardBootstrap(DASHBOARD_ID);
await waitFor(() => {
expect(mockGetDashboard).toHaveBeenCalledWith({ id: DASHBOARD_ID });
@@ -669,7 +678,7 @@ describe('Dashboard Provider - Textbox Variable Backward Compatibility', () => {
} as any);
/* eslint-enable @typescript-eslint/no-explicit-any */
- renderWithDashboardProvider(DASHBOARD_ID);
+ renderWithDashboardBootstrap(DASHBOARD_ID);
await waitFor(() => {
expect(mockGetDashboard).toHaveBeenCalledWith({ id: DASHBOARD_ID });
@@ -711,7 +720,7 @@ describe('Dashboard Provider - Textbox Variable Backward Compatibility', () => {
} as any);
/* eslint-enable @typescript-eslint/no-explicit-any */
- renderWithDashboardProvider(DASHBOARD_ID);
+ renderWithDashboardBootstrap(DASHBOARD_ID);
await waitFor(() => {
expect(mockGetDashboard).toHaveBeenCalledWith({ id: DASHBOARD_ID });
diff --git a/frontend/src/providers/Dashboard/store/slices/dashboardLayoutSlice.ts b/frontend/src/providers/Dashboard/store/slices/dashboardLayoutSlice.ts
new file mode 100644
index 0000000000..ea7338cbea
--- /dev/null
+++ b/frontend/src/providers/Dashboard/store/slices/dashboardLayoutSlice.ts
@@ -0,0 +1,51 @@
+import type { Layout } from 'react-grid-layout';
+import type { StateCreator } from 'zustand';
+
+import type { DashboardStore } from '../useDashboardStore';
+
+export interface DashboardLayoutSlice {
+ //
+ layouts: Layout[];
+ setLayouts: (updater: Layout[] | ((prev: Layout[]) => Layout[])) => void;
+ //
+ panelMap: Record<string, { widgets: Layout[]; collapsed: boolean }>;
+ setPanelMap: (
+ updater:
+ | Record<string, { widgets: Layout[]; collapsed: boolean }>
+ | ((
+ prev: Record<string, { widgets: Layout[]; collapsed: boolean }>,
+ ) => Record<string, { widgets: Layout[]; collapsed: boolean }>),
+ ) => void;
+ // resetDashboardLayout: () => void;
+}
+
+export const initialDashboardLayoutState = {
+ layouts: [] as Layout[],
+ panelMap: {} as Record<string, { widgets: Layout[]; collapsed: boolean }>,
+};
+
+export const createDashboardLayoutSlice: StateCreator<
+ DashboardStore,
+ [['zustand/immer', never]],
+ [],
+ DashboardLayoutSlice
+> = (set) => ({
+ ...initialDashboardLayoutState,
+
+ setLayouts: (updater): void =>
+ set((state) => {
+ state.layouts =
+ typeof updater === 'function' ? updater(state.layouts) : updater;
+ }),
+
+ setPanelMap: (updater): void =>
+ set((state) => {
+ state.panelMap =
+ typeof updater === 'function' ? updater(state.panelMap) : updater;
+ }),
+
+ // resetDashboardLayout: () =>
+ // set((state) => {
+ // Object.assign(state, initialDashboardLayoutState);
+ // }),
+});
diff --git a/frontend/src/providers/Dashboard/store/slices/dashboardUISlice.ts b/frontend/src/providers/Dashboard/store/slices/dashboardUISlice.ts
new file mode 100644
index 0000000000..e1517c4435
--- /dev/null
+++ b/frontend/src/providers/Dashboard/store/slices/dashboardUISlice.ts
@@ -0,0 +1,57 @@
+import type { Dashboard } from 'types/api/dashboard/getAll';
+import type { StateCreator } from 'zustand';
+
+import type { DashboardStore } from '../useDashboardStore';
+
+export type WidgetColumnWidths = {
+ [widgetId: string]: Record<string, number>;
+};
+
+export interface DashboardUISlice {
+ //
+ selectedDashboard: Dashboard | undefined;
+ setSelectedDashboard: (
+ updater:
+ | Dashboard
+ | undefined
+ | ((prev: Dashboard | undefined) => Dashboard | undefined),
+ ) => void;
+ //
+ columnWidths: WidgetColumnWidths;
+ setColumnWidths: (
+ updater:
+ | WidgetColumnWidths
+ | ((prev: WidgetColumnWidths) => WidgetColumnWidths),
+ ) => void;
+}
+
+export const initialDashboardUIState = {
+ selectedDashboard: undefined as Dashboard | undefined,
+ columnWidths: {} as WidgetColumnWidths,
+};
+
+export const createDashboardUISlice: StateCreator<
+ DashboardStore,
+ [['zustand/immer', never]],
+ [],
+ DashboardUISlice
+> = (set) => ({
+ ...initialDashboardUIState,
+
+ setSelectedDashboard: (updater): void =>
+ set((state: DashboardUISlice): void => {
+ state.selectedDashboard =
+ typeof updater === 'function' ? updater(state.selectedDashboard) : updater;
+ }),
+
+ setColumnWidths: (updater): void =>
+ set((state: DashboardUISlice): void => {
+ state.columnWidths =
+ typeof updater === 'function' ? updater(state.columnWidths) : updater;
+ }),
+
+ resetDashboardUI: (): void =>
+ set((state: DashboardUISlice): void => {
+ Object.assign(state, initialDashboardUIState);
+ }),
+});
diff --git a/frontend/src/providers/Dashboard/store/useDashboardStore.ts b/frontend/src/providers/Dashboard/store/useDashboardStore.ts
new file mode 100644
index 0000000000..d3f3443ed5
--- /dev/null
+++ b/frontend/src/providers/Dashboard/store/useDashboardStore.ts
@@ -0,0 +1,50 @@
+import type { Layout } from 'react-grid-layout';
+import type { Dashboard } from 'types/api/dashboard/getAll';
+import { create } from 'zustand';
+import { immer } from 'zustand/middleware/immer';
+
+import {
+ createDashboardLayoutSlice,
+ DashboardLayoutSlice,
+ initialDashboardLayoutState,
+} from './slices/dashboardLayoutSlice';
+import {
+ createDashboardUISlice,
+ DashboardUISlice,
+ initialDashboardUIState,
+} from './slices/dashboardUISlice';
+
+export type DashboardStore = DashboardUISlice &
+ DashboardLayoutSlice & {
+ resetDashboardStore: () => void;
+ };
+
+/**
+ * 'select*' is a redux naming convention that can be carried over to zustand.
+ * It is used to select a piece of state from the store.
+ * In this case, we are selecting the locked state of the selected dashboard.
+ * */
+export const selectIsDashboardLocked = (s: DashboardStore): boolean =>
+ s.selectedDashboard?.locked ?? false;
+
+export const useDashboardStore = create<DashboardStore>()(
+ immer((set, get, api) => ({
+ ...createDashboardUISlice(set, get, api),
+ ...createDashboardLayoutSlice(set, get, api),
+
+ resetDashboardStore: (): void =>
+ set((state: DashboardStore) => {
+ Object.assign(state, initialDashboardUIState, initialDashboardLayoutState);
+ }),
+ })),
+);
+
+// Standalone imperative accessors — use these instead of calling useDashboardStore.getState() at call sites.
+export const getSelectedDashboard = (): Dashboard | undefined =>
+ useDashboardStore.getState().selectedDashboard;
+
+export const getDashboardLayouts = (): Layout[] =>
+ useDashboardStore.getState().layouts;
+
+export const resetDashboard = (): void =>
+ useDashboardStore.getState().resetDashboardStore();
diff --git a/frontend/src/providers/Dashboard/types.ts b/frontend/src/providers/Dashboard/types.ts
deleted file mode 100644
index 5dd1f3e67b..0000000000
--- a/frontend/src/providers/Dashboard/types.ts
+++ /dev/null
@@ -1,41 +0,0 @@
-import { Layout } from 'react-grid-layout';
-import { UseQueryResult } from 'react-query';
-import dayjs from 'dayjs';
-import { SuccessResponseV2 } from 'types/api';
-import { Dashboard } from 'types/api/dashboard/getAll';
-
-export type WidgetColumnWidths = {
- [widgetId: string]: Record<string, number>;
-};
-
-export interface IDashboardContext {
- isDashboardLocked: boolean;
- handleDashboardLockToggle: (value: boolean) => void;
- dashboardResponse: UseQueryResult<SuccessResponseV2<Dashboard>, unknown>;
- selectedDashboard: Dashboard | undefined;
- layouts: Layout[];
- panelMap: Record<string, { widgets: Layout[]; collapsed: boolean }>;
- setPanelMap: React.Dispatch<React.SetStateAction<Record<string, any>>>;
- setLayouts: React.Dispatch<React.SetStateAction<Layout[]>>;
- setSelectedDashboard: React.Dispatch<
- React.SetStateAction<Dashboard | undefined>
- >;
- updatedTimeRef: React.MutableRefObject<dayjs.Dayjs | null>;
- updateLocalStorageDashboardVariables: (
- id: string,
- selectedValue:
- | string
- | number
- | boolean
- | (string | number | boolean)[]
- | null
- | undefined,
- allSelected: boolean,
- isDynamic?: boolean,
- ) => void;
- dashboardQueryRangeCalled: boolean;
- setDashboardQueryRangeCalled: (value: boolean) => void;
- isDashboardFetching: boolean;
- columnWidths: WidgetColumnWidths;
- setColumnWidths: React.Dispatch<React.SetStateAction<WidgetColumnWidths>>;
-}