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.0 KB
Raw Blame
import { useCallback } from 'react';
import { Button } from '@signozhq/ui/button';
import { Tooltip } from '@signozhq/ui/tooltip';
import logEvent from 'api/common/logEvent';
import Noz from 'components/Noz/Noz';
import { NOZ_TOOLTIP_TITLE } from 'components/Noz/Noz.constants';
import {
	AIAssistantEvents,
	AIAssistantOpenSource,
} from 'container/AIAssistant/events';
import { normalizePage } from 'container/AIAssistant/hooks/useAIAssistantAnalyticsContext';
import { selectPendingUserInputStreamCount } from 'container/AIAssistant/store/pendingInputSelectors';
import {
	openAIAssistant,
	useAIAssistantStore,
} from 'container/AIAssistant/store/useAIAssistantStore';
import { useIsAIAssistantEnabled } from 'hooks/useIsAIAssistantEnabled';
import { useAppLocation } from 'lib/router/useAppLocation';
import ROUTES from 'constants/routes';
import { Dot } from '@signozhq/icons';

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

/**
 * Opens the Noz drawer, matching the header entry it replaces. Cmd+K opens the
 * modal instead; this is deliberately the drawer.
 *
 * Carries the header's pending badge: when Noz is blocked on the user
 * (`awaiting_approval` / `awaiting_clarification`) a dot pulses. Without it,
 * hiding the header button would remove a notification rather than move it.
 */
function AskNoz(): JSX.Element | null {
	const { pathname } = useAppLocation();
	const isAIAssistantEnabled = useIsAIAssistantEnabled();
	const isDrawerOpen = useAIAssistantStore((state) => state.isDrawerOpen);
	const isModalOpen = useAIAssistantStore((state) => state.isModalOpen);
	const pendingUserInputCount = useAIAssistantStore(
		selectPendingUserInputStreamCount,
	);

	// Noz is already on screen in the modal, so the "needs you" dot would be noise.
	const showPendingBadge = pendingUserInputCount > 0 && !isModalOpen;

	// The drawer does not render on the Noz full page, so the button would be inert.
	const isAIAssistantPage = pathname.startsWith(ROUTES.AI_ASSISTANT_BASE);

	const handleOpen = useCallback((): void => {
		void logEvent(AIAssistantEvents.Opened, {
			source: AIAssistantOpenSource.BottomStrip,
			currentPage: normalizePage(pathname),
		});
		openAIAssistant();
	}, [pathname]);

	if (!isAIAssistantEnabled || isDrawerOpen || isAIAssistantPage) {
		return null;
	}

	return (
		<div className={styles.askNoz} data-testid="bottom-strip-ask-noz">
			{showPendingBadge && (
				<span className={styles.badge} aria-hidden>
					<span className={styles.pulseDot}>
						<Dot size={36} />
					</span>
				</span>
			)}
			<span className="noz-wave">
				<Tooltip title={NOZ_TOOLTIP_TITLE}>
					<Button
						variant="ghost"
						color="secondary"
						size="sm"
						prefix={<Noz size={16} />}
						onClick={handleOpen}
						aria-label={
							showPendingBadge
								? `Ask Noz, ${pendingUserInputCount} ${
										pendingUserInputCount === 1 ? 'action needs' : 'actions need'
									} your response`
								: 'Ask Noz'
						}
					>
						Ask Noz
					</Button>
				</Tooltip>
			</span>
		</div>
	);
}

export default AskNoz;