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.
TypeScript apmapplication-monitoringdistributed-tracinggogood-first-issuejaegerloglogsmetricsmonitoringnextjsobservabilityopen-sourceopentelemetryprometheusreactreactjsself-hostedtracingtypescript
import { useCallback, useMemo, useRef } from 'react';
import { buildRoutePath } from 'lib/router/buildRoutePath';
import { Redirect } from 'lib/router/Redirect';
import { useAppLocation } from 'lib/router/useAppLocation';
import { useAppParams } from 'lib/router/useAppParams';
import { Typography } from '@signozhq/ui/typography';
import Spinner from 'components/Spinner';
import ROUTES from 'constants/routes';
import { useGetCompositeQueryParam } from 'hooks/queryBuilder/useGetCompositeQueryParam';
import { useSafeNavigate } from 'hooks/useSafeNavigate';
import { withAuthZPage } from 'lib/authz/components/withAuthZ/withAuthZPage';
import {
buildDashboardDeletePermission,
buildDashboardReadPermission,
buildDashboardUpdatePermission,
} from 'lib/authz/hooks/useAuthZ/permissions/dashboard.permissions';
import { useDashboardFetch } from '../DashboardContainer/hooks/useDashboardFetch';
import { useResolvedVariables } from '../DashboardContainer/hooks/useResolvedVariables';
import PanelEditorContainer from '../DashboardContainer/PanelEditor';
import type { PanelEditorHandoffState } from '../DashboardContainer/PanelEditor/panelEditorHandoff';
import {
parseNewPanelKind,
parseNewPanelTarget,
} from '../DashboardContainer/PanelEditor/newPanelRoute';
import { useSyncVariablesForSuggestions } from '../DashboardContainer/hooks/useSyncVariablesForSuggestions';
import { useTimeSearchParams } from 'hooks/useTimeSearchParams';
import { createDefaultPanel } from '../DashboardContainer/patchOps';
import { useDashboardStore } from '../DashboardContainer/store/useDashboardStore';
import { useSeedVariableSelection } from '../DashboardContainer/VariablesBar/hooks/useSeedVariableSelection';
import { buildNewPanelSeed } from './newPanelSeed';
import styles from './PanelEditorPage.module.scss';
/**
* Full-page route for editing a V2 dashboard panel. Resolves the panel from the
* fetched dashboard spec and wires up navigate-back callbacks.
*/
function PanelEditorPage(): JSX.Element {
const { dashboardId, panelId } = useAppParams<'dashboardId' | 'panelId'>();
const { search, state } = useAppLocation();
const { safeNavigate } = useSafeNavigate();
const timeSearch = useTimeSearchParams();
// Edits handed off from the View modal's drilldown — open the editor on these
// instead of the saved panel. Lost on refresh/new-tab, which falls back to saved.
const handoffSpec = (state as PanelEditorHandoffState | null)?.editSpec;
const { dashboard, isLoading, isError, error, refetch } = useDashboardFetch(
dashboardId || '',
);
// On a refresh/direct URL this route is the only mount, so seed the edit
// context the way DashboardContainer does — during render, so the subtree's
// first render already sees the id (useDashboardFetchRequired throws without it).
const setEditContext = useDashboardStore((s) => s.setEditContext);
if (dashboard?.id) {
setEditContext({ dashboardId: dashboard.id, refetch });
}
// No variables bar on this route: seed the selection and publish the resolved
// payload so the preview and context links get variable values after a refresh.
useSeedVariableSelection(dashboard);
useResolvedVariables(dashboard);
// Feed variables to the query builder autocomplete inside the editor.
useSyncVariablesForSuggestions(dashboard);
// An explorer "Add to Dashboard" export rides the query in `compositeQuery` (V1
// parity). Captured once at mount: the editor rewrites `compositeQuery` in the URL
// as the user edits, and re-reading it would churn the draft (its reset target and
// dirty baseline live in the initially-loaded panel).
const exportCompositeQuery = useGetCompositeQueryParam();
const exportCompositeQueryRef = useRef(exportCompositeQuery);
// A `panel/new?panelKind=…` route means "create": seed a default panel of that
// kind rather than looking one up (seeded from the exported query when present).
// Persisted (with a real id) only on save.
const newKind = parseNewPanelKind(panelId || '', search);
const existingPanel = panelId ? dashboard?.spec.panels[panelId] : undefined;
const panel = useMemo(() => {
if (newKind) {
// A `compositeQuery` at mount means the explorer routed an export here.
const isExplorerExport = !!exportCompositeQueryRef.current;
const { kind, pluginSpec, queries } = buildNewPanelSeed(
newKind,
exportCompositeQueryRef.current,
isExplorerExport,
);
return createDefaultPanel(kind, pluginSpec, queries);
}
if (!existingPanel) {
return undefined;
}
// Open on the modal's drilldown edits when handed off; else the saved panel.
return handoffSpec ? { ...existingPanel, spec: handoffSpec } : existingPanel;
}, [newKind, existingPanel, handoffSpec]);
// Target section for a newly-created panel (set by the "Add panel" trigger).
const target = parseNewPanelTarget(search);
const backToDashboard = useCallback((): void => {
// Drop editor-only URL state (variables come from the persisted store), but carry
// time so a custom range picked in the editor isn't reset to the dashboard default.
const path = buildRoutePath(ROUTES.DASHBOARD, {
dashboardId: dashboardId || '',
});
safeNavigate(timeSearch ? `${path}?${timeSearch}` : path);
}, [safeNavigate, dashboardId, timeSearch]);
if (isLoading) {
return <Spinner tip="Loading dashboard..." />;
}
if (isError || !dashboard) {
return (
<div className={styles.errorState}>
<Typography.Title>Failed to load dashboard</Typography.Title>
<Typography.Text>{(error as Error)?.message}</Typography.Text>
</div>
);
}
// No panel (stale/deleted id, or unknown new-panel kind) — send the user back.
if (!panel) {
return (
<Redirect
to={`${buildRoutePath(ROUTES.DASHBOARD, { dashboardId: dashboardId || '' })}${search}`}
/>
);
}
return (
<PanelEditorContainer
dashboardId={dashboardId || ''}
panelId={panelId || ''}
panel={panel}
savedPanel={existingPanel}
isNew={!!newKind}
target={target}
onClose={backToDashboard}
onSaved={backToDashboard}
/>
);
}
// Typed explicitly: the route lazy-loads this, and Loadable needs indexable props.
export default withAuthZPage<Record<string, unknown>>(PanelEditorPage, {
checks: (_props, router) => [
buildDashboardReadPermission(router.params.dashboardId ?? ''),
],
// Same batch as `checks`, so the controls below resolve from cache.
preloadChecks: (_props, router) => [
buildDashboardUpdatePermission(router.params.dashboardId ?? ''),
buildDashboardDeletePermission(router.params.dashboardId ?? ''),
],
fallbackOnLoading: <Spinner tip="Loading dashboard..." />,
});