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.3 KB
Raw Blame
import { useMemo, useState } from 'react';
import { ChevronDown, DollarSign } from '@signozhq/icons';
import { Button } from '@signozhq/ui/button';
import { Dropdown, type DropdownActionItemType } from '@signozhq/ui/dropdown';

import { READ_ONLY_TOOLTIP } from './constants';
import type { EditorVariable } from './types';

import styles from './MarkdownEditor.module.scss';

interface InsertVariableMenuProps {
	variables: EditorVariable[];
	/** Receives the variable name; the caller decides the token syntax. */
	onSelect: (name: string) => void;
	disabled: boolean;
}

function toMenuItems(
	variables: EditorVariable[],
	onSelect: (name: string) => void,
): DropdownActionItemType[] {
	return variables.map((variable) => ({
		type: 'item',
		value: variable.name,
		label: (
			<span
				className={styles.variableRow}
				data-testid={`markdown-variable-${variable.name}`}
			>
				<span className={styles.variableName}>{`$${variable.name}`}</span>
				{variable.badge && (
					<span className={styles.variableBadge}>{variable.badge}</span>
				)}
			</span>
		),
		onClick: (): void => onSelect(variable.name),
	}));
}

/** Searchable variable picker; hidden entirely when there is nothing to insert. */
function InsertVariableMenu({
	variables,
	onSelect,
	disabled,
}: InsertVariableMenuProps): JSX.Element | null {
	const [search, setSearch] = useState('');

	const matches = useMemo(() => {
		const query = search.trim().toLowerCase();
		return query
			? variables.filter((variable) => variable.name.toLowerCase().includes(query))
			: variables;
	}, [variables, search]);

	const items = useMemo(
		() => toMenuItems(matches, onSelect),
		[matches, onSelect],
	);

	if (variables.length === 0) {
		return null;
	}

	return (
		<Dropdown
			items={items}
			nativeButton
			align="end"
			side="bottom"
			contentMaxWidth={320}
			disabled={disabled}
			disabledTooltip={READ_ONLY_TOOLTIP}
			searchInputProps={{
				placeholder: 'Search variables',
				onChange: setSearch,
			}}
		>
			<Button
				type="button"
				variant="outlined"
				color="secondary"
				size="sm"
				prefix={<DollarSign size={14} className={styles.insertVariableIcon} />}
				suffix={<ChevronDown size={14} />}
				testId="markdown-insert-variable"
			>
				Insert variable
			</Button>
		</Dropdown>
	);
}

export default InsertVariableMenu;