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.

3.6 KB
Raw Blame
import { useCallback } from 'react';
import { useMutation, useQueryClient } from 'react-query';
import {
	getGetDashboardV2QueryKey,
	// eslint-disable-next-line no-restricted-imports -- this hook is the one sanctioned caller of patchDashboardV2; everything else goes through patchAsync.
	patchDashboardV2,
} from 'api/generated/services/dashboard';
import type {
	DashboardtypesJSONPatchOperationDTO,
	GetDashboardV2200,
} from 'api/generated/services/sigNoz.schemas';
import APIError from 'types/api/error';

import { applyJsonPatch } from '../optimistic/applyJsonPatch';
import { useDashboardStore } from '../store/useDashboardStore';
import { useTranslation } from 'react-i18next';

/** Cached dashboard snapshot, kept for rollback on error. */
interface OptimisticPatchContext {
	previous?: GetDashboardV2200;
}

export interface UseOptimisticPatch {
	patchAsync: (ops: DashboardtypesJSONPatchOperationDTO[]) => Promise<unknown>;
	isPatching: boolean;
	error: Error | null;
}

/**
 * Central optimistic mutation for V2 dashboard spec edits: writes the ops to the
 * cached dashboard immediately, rolls back on error, and reconciles from the PATCH
 * response (which returns the updated dashboard) rather than a follow-up GET — an
 * immediate refetch can read stale, pre-mutation data and flicker the UI back.
 * `dashboardId` defaults to the edit-context store; the panel editor passes its own.
 */
export function useOptimisticPatch(
	dashboardIdOverride?: string,
): UseOptimisticPatch {
	const storeDashboardId = useDashboardStore((s) => s.dashboardId);
	const dashboardId = dashboardIdOverride ?? storeDashboardId;
	const { t } = useTranslation('dashboard');
	const queryClient = useQueryClient();
	const queryKey = getGetDashboardV2QueryKey({ id: dashboardId });

	const mutation = useMutation<
		Awaited<ReturnType<typeof patchDashboardV2>>,
		APIError,
		DashboardtypesJSONPatchOperationDTO[],
		OptimisticPatchContext
	>((ops) => patchDashboardV2({ id: dashboardId }, ops), {
		onMutate: async (ops) => {
			await queryClient.cancelQueries(queryKey);
			const previous = queryClient.getQueryData<GetDashboardV2200>(queryKey);
			if (previous?.data) {
				// Ops are rooted at the DTO's `/spec`, so patch `.data`, keep the envelope.
				queryClient.setQueryData<GetDashboardV2200>(queryKey, {
					...previous,
					data: applyJsonPatch(previous.data, ops),
				});
			}
			return { previous };
		},
		onError: (_error, _ops, context) => {
			if (context?.previous) {
				queryClient.setQueryData(queryKey, context.previous);
			}
		},
		onSuccess: (response) => {
			queryClient.setQueryData<GetDashboardV2200>(queryKey, (prev) =>
				prev ? { ...prev, data: response.data } : (response as GetDashboardV2200),
			);
		},
	});

	// Defense-in-depth: block edits when the store is warm for this dashboard and it
	// isn't editable. Skipped when the store isn't seeded for this id (panel editor
	// via direct URL), where that surface gates its own save.
	const { mutateAsync } = mutation;
	const patchAsync = useCallback(
		(ops: DashboardtypesJSONPatchOperationDTO[]): Promise<unknown> => {
			// Defense-in-depth against a lock, read straight from the cache this hook
			// already owns. Permission is enforced by the UI and, definitively, by the
			// backend — checking it here would drag authz into every mutation.
			const cached = queryClient.getQueryData<GetDashboardV2200>(queryKey);
			if (cached?.data?.locked) {
				return Promise.reject(new Error(t('dashboard_locked')));
			}
			return mutateAsync(ops);
		},
		[queryClient, queryKey, mutateAsync],
	);

	return {
		patchAsync,
		isPatching: mutation.isLoading,
		error: mutation.error ?? null,
	};
}