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.2 KB
Raw Blame
import { useState, type ChangeEvent, type KeyboardEvent } from 'react';
import { Input } from '@signozhq/ui/input';
import { Search, X } from '@signozhq/icons';
import { Tooltip } from '@signozhq/ui/tooltip';
import logEvent from 'api/common/logEvent';
import { DashboardDetailEvents } from 'pages/DashboardPage/constants/events';

import styles from './PanelHeaderSearch.module.scss';
import { Button } from '@signozhq/ui/button';

interface PanelHeaderSearchProps {
	/** Current filter term, owned by the panel shell. */
	value: string;
	/** Pushes the new term up; the renderer applies the filter. */
	onChange: (value: string) => void;
}

/**
 * Collapsible header search (V1 parity): an icon that expands into an input and
 * collapses once empty and blurred. Owns only its chrome, never the term.
 */
function PanelHeaderSearch({
	value,
	onChange,
}: PanelHeaderSearchProps): JSX.Element {
	const [expanded, setExpanded] = useState(false);

	const collapseIfEmpty = (): void => {
		if (!value) {
			setExpanded(false);
		}
	};

	const clear = (): void => {
		onChange('');
		setExpanded(false);
	};

	const handleSearchChange = (e: ChangeEvent<HTMLInputElement>): void => {
		onChange(e.target.value);
		void logEvent(DashboardDetailEvents.PanelSearched, {}, 'track', true);
	};

	if (!expanded) {
		return (
			<Tooltip title="Search">
				<Button
					type="button"
					variant="ghost"
					color="secondary"
					size="sm"
					icon
					onClick={(): void => setExpanded(true)}
					testId="panel-header-search-trigger"
					aria-label="Search"
				>
					<Search size={14} />
				</Button>
			</Tooltip>
		);
	}

	return (
		<Input
			autoFocus
			size={14}
			value={value}
			placeholder="Search…"
			containerClassName={styles.input}
			testId="panel-header-search-input"
			prefix={<Search size={14} />}
			suffix={
				<Button
					type="button"
					variant="ghost"
					color="secondary"
					size="sm"
					icon
					onClick={clear}
					testId="panel-header-search-clear"
					aria-label="Clear search"
				>
					<X size={14} />
				</Button>
			}
			onChange={handleSearchChange}
			onBlur={collapseIfEmpty}
			onKeyDown={(e: KeyboardEvent<HTMLInputElement>): void => {
				if (e.key === 'Escape') {
					clear();
				}
			}}
		/>
	);
}

export default PanelHeaderSearch;