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.3 KB
Raw Blame
import type { DraggableAttributes } from '@dnd-kit/core';
import type { SyntheticListenerMap } from '@dnd-kit/core/dist/hooks/utilities';
import { ChevronDown, ChevronRight, GripVertical } from '@signozhq/icons';
import { Button } from '@signozhq/ui/button';
import { Typography } from '@signozhq/ui/typography';
import cx from 'classnames';

import SectionActionsMenu from '../SectionActionsMenu/SectionActionsMenu';

import styles from './SectionHeader.module.scss';
import type { BrandedPermission } from 'lib/authz/hooks/useAuthZ/types';

export interface SectionDragHandle {
	attributes: DraggableAttributes;
	listeners: SyntheticListenerMap | undefined;
	setActivatorNodeRef: (element: HTMLElement | null) => void;
}

/** Editable-mode section actions — present together or not at all. */
export interface SectionHeaderActions {
	onRename: () => void;
	onAddPanel: () => void;
	onCloneSection: () => void;
	onDeleteSection: () => void;
}

interface SectionHeaderProps {
	sectionId: string;
	title: string;
	open: boolean;
	onToggle: () => void;
	repeatVariable?: string;
	/** Provided by SortableSection in sectioned mode; absent for untitled/free-flow. */
	dragHandle?: SectionDragHandle;
	/** Present for edit-permitted users; absent (no menu) in view mode. */
	actions?: SectionHeaderActions;
	/** Present when edits are unavailable — actions render disabled with its reason. */
	disabledChecks?: BrandedPermission[];
	disabledTooltip?: string;
}

function SectionDragHandleButton({
	dragHandle,
	sectionId,
}: {
	dragHandle: SectionDragHandle;
	sectionId: string;
}): JSX.Element {
	const { role: _role, ...sortableAttributes } = dragHandle.attributes;
	return (
		<span
			ref={dragHandle.setActivatorNodeRef}
			{...dragHandle.listeners}
			className={styles.dragHandle}
		>
			<Button
				type="button"
				variant="ghost"
				color="secondary"
				size="sm"
				icon
				aria-label="Drag to reorder section"
				testId={`dashboard-section-drag-${sectionId}`}
				{...sortableAttributes}
			>
				<GripVertical size={14} />
			</Button>
		</span>
	);
}

function SectionHeader({
	sectionId,
	title,
	open,
	onToggle,
	repeatVariable,
	dragHandle,
	actions,
	disabledChecks = [],
	disabledTooltip,
}: SectionHeaderProps): JSX.Element {
	return (
		<div className={cx(styles.header, { [styles.headerOpen]: open })}>
			{dragHandle ? (
				<SectionDragHandleButton dragHandle={dragHandle} sectionId={sectionId} />
			) : null}
			<div className={styles.toggle}>
				<Button
					size="md"
					type="button"
					variant="ghost"
					color="secondary"
					width="100%"
					onClick={onToggle}
					testId={`dashboard-section-toggle-${sectionId}`}
					prefix={open ? <ChevronDown size={14} /> : <ChevronRight size={14} />}
				>
					<Typography.Text className={styles.title}>{title}</Typography.Text>
					{repeatVariable ? (
						<Typography.Text className={styles.repeatBadge}>
							(repeats per ${repeatVariable})
						</Typography.Text>
					) : null}
				</Button>
			</div>
			{actions ? (
				<SectionActionsMenu
					sectionId={sectionId}
					disabledChecks={disabledChecks}
					disabledTooltip={disabledTooltip}
					onAddPanel={actions.onAddPanel}
					onRename={actions.onRename}
					onCloneSection={actions.onCloneSection}
					onDeleteSection={actions.onDeleteSection}
				/>
			) : null}
		</div>
	);
}

export default SectionHeader;