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.

2.7 KB
Raw Blame
import { useCallback } from 'react';
import { buildRoutePath } from 'lib/router/buildRoutePath';
import type { DashboardtypesPanelDTO } from 'api/generated/services/sigNoz.schemas';
import { QueryParams } from 'constants/query';
import ROUTES from 'constants/routes';
import { useQueryBuilder } from 'hooks/queryBuilder/useQueryBuilder';
import { useSafeNavigate } from 'hooks/useSafeNavigate';

import type { PanelEditorHandoffState } from '../PanelEditor/panelEditorHandoff';
import { getPanelBuilderQuery } from '../Panels/utils/getPanelBuilderQuery';
import { useDashboardStore } from '../store/useDashboardStore';
import { useTimeSearchParams } from 'hooks/useTimeSearchParams';
import logEvent from '@/api/common/logEvent';
import { DashboardDetailEvents } from '../../constants/events';

interface OpenPanelEditorOptions {
	handoffState?: PanelEditorHandoffState;
	/** Extra query merged into the editor URL (leading `?` optional). */
	search?: string;
	/** The panel being edited — its query rides in the URL as `compositeQuery`. */
	panel?: DashboardtypesPanelDTO;
}

/** Opens the V2 panel editor, carrying the active time window in the URL. */
export function useOpenPanelEditor(): (
	panelId: string,
	options?: OpenPanelEditorOptions,
) => void {
	const { safeNavigate } = useSafeNavigate();
	const timeSearch = useTimeSearchParams();
	const dashboardId = useDashboardStore((s) => s.dashboardId);
	const { resetQuery } = useQueryBuilder();

	return useCallback(
		(panelId: string, options?: OpenPanelEditorOptions): void => {
			void logEvent(DashboardDetailEvents.PanelAction, {
				action: 'edit',
				panelId,
				dashboardId,
			});
			const path = buildRoutePath(ROUTES.DASHBOARD_PANEL_EDITOR, {
				dashboardId,
				panelId,
			});
			const params = new URLSearchParams(options?.search);
			new URLSearchParams(timeSearch).forEach((value, key) => {
				params.set(key, value);
			});
			// `null` for a static kind — no query state to stage or persist.
			const query = options?.panel && getPanelBuilderQuery(options.panel);
			if (query) {
				// Single-encoded: `useGetCompositeQueryParam` decodes once on top of the decode
				// `URLSearchParams` already does.
				params.set(
					QueryParams.compositeQuery,
					encodeURIComponent(JSON.stringify(query)),
				);
				// The provider applies the URL in an effect, a tick after the builder's fields
				// have mounted and read the query they keep (PromQL inputs, add-on rows).
				resetQuery(query);
			}
			const search = params.toString();
			safeNavigate(
				search ? `${path}?${search}` : path,
				options?.handoffState ? { state: options.handoffState } : undefined,
			);
		},
		[safeNavigate, dashboardId, timeSearch, resetQuery],
	);
}