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.4 KB
Raw Blame
import { useCallback, useState } from 'react';

import logEvent from 'api/common/logEvent';
import type { DashboardtypesLayoutDTO } from 'api/generated/services/sigNoz.schemas';
import { DashboardDetailEvents } from 'pages/DashboardPage/constants/events';
import { useErrorModal } from 'providers/ErrorModalProvider';
import APIError from 'types/api/error';

import { useOptimisticPatch } from '../../../hooks/useOptimisticPatch';
import {
	addSectionOp,
	newGridLayout,
	reorderLayoutsOp,
} from '../../../patchOps';
import { useDashboardStore } from '../../../store/useDashboardStore';
import { useScrollIntoViewStore } from '../../../store/useScrollIntoViewStore';
import { getSectionStableId } from '../../../utils';

interface Params {
	layouts: DashboardtypesLayoutDTO[] | undefined | null;
}

interface Result {
	addSection: (title: string) => Promise<boolean>;
	isSaving: boolean;
}

/**
 * Appends an empty titled section. When the dashboard has no layouts yet, the
 * layouts array is created via a `replace` (an `add` to a missing/empty array
 * pointer is unreliable); otherwise a new Grid is appended.
 */
export function useAddSection({ layouts }: Params): Result {
	const dashboardId = useDashboardStore((s) => s.dashboardId);
	const { patchAsync } = useOptimisticPatch();
	const [isSaving, setIsSaving] = useState(false);
	const { showErrorModal } = useErrorModal();
	const setScrollTargetId = useScrollIntoViewStore((s) => s.setScrollTargetId);

	const addSection = useCallback(
		async (title: string): Promise<boolean> => {
			const trimmed = title.trim();
			if (!dashboardId || !trimmed) {
				return false;
			}
			const isFirstSection = !layouts || layouts.length === 0;
			const op = isFirstSection
				? reorderLayoutsOp([newGridLayout(trimmed)])
				: addSectionOp(trimmed);
			try {
				setIsSaving(true);
				await patchAsync([op]);
				void logEvent(DashboardDetailEvents.SectionAction, {
					action: 'add',
					dashboardId,
				});
				// The new empty section is appended, so its layout index is the prior count;
				// key it the way `getSectionStableId` does so it reveals itself on render.
				const newIndex = isFirstSection ? 0 : layouts.length;
				setScrollTargetId(getSectionStableId([], newIndex));
				return true;
			} catch (error) {
				showErrorModal(error as APIError);
				return false;
			} finally {
				setIsSaving(false);
			}
		},
		[layouts, dashboardId, patchAsync, showErrorModal, setScrollTargetId],
	);

	return { addSection, isSaving };
}