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.

1.7 KB
Raw Blame
import { useCallback, useMemo } from 'react';
import type { DropdownSubmenuItemType } from '@signozhq/ui/dropdown';
import type { DashboardtypesPanelDTO } from 'api/generated/services/sigNoz.schemas';
import {
	DownloadFormat,
	type PanelActionCapabilities,
} from 'pages/DashboardPage/DashboardContainer/Panels/types/panelDefinition';
import type { PanelQueryData } from 'pages/DashboardPage/DashboardContainer/queryV5/types';

import { buildDownloadMenuItem } from '../utils/buildDownloadMenuItem';
import { useDownloadPanelCsv } from './useDownloadPanelCsv';
import { useDownloadPanelImage } from './useDownloadPanelImage';
import { usePanelTitle } from './usePanelTitle';

interface UseDownloadPanelMenuItemArgs {
	panelId: string;
	panel: DashboardtypesPanelDTO;
	data: PanelQueryData;
	actions: PanelActionCapabilities;
}

/**
 * Resolves the panel's "Download" submenu item: CSV from the query response,
 * PNG/SVG from the rendered node. Null when the kind supports no format.
 */
export function useDownloadPanelMenuItem({
	panelId,
	panel,
	data,
	actions,
}: UseDownloadPanelMenuItemArgs): DropdownSubmenuItemType | null {
	const panelName = usePanelTitle(panel);
	const downloadPanelCsv = useDownloadPanelCsv({
		panel,
		data,
		canDownloadCsv: actions.download[DownloadFormat.CSV],
	});
	const { downloadPanelImage } = useDownloadPanelImage();

	const onDownload = useCallback(
		(format: DownloadFormat): void => {
			if (format === DownloadFormat.CSV) {
				downloadPanelCsv();
				return;
			}
			void downloadPanelImage(panelId, panelName, format);
		},
		[downloadPanelCsv, downloadPanelImage, panelId, panelName],
	);

	return useMemo(
		() => buildDownloadMenuItem({ supported: actions.download, onDownload }),
		[actions.download, onDownload],
	);
}