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.

5.3 KB
Raw Blame
import { type ChangeEvent, useState } from 'react';
// eslint-disable-next-line signoz/no-antd-components -- no @signozhq/ui multiline TextArea yet
import { Input as AntInput } from 'antd';
import { Button } from '@signozhq/ui/button';
import { Input } from '@signozhq/ui/input';
import { Typography } from '@signozhq/ui/typography';
import { toast } from '@signozhq/ui/sonner';
import { AxiosError } from 'axios';
import { buildRoutePath } from 'lib/router/buildRoutePath';
import logEvent from 'api/common/logEvent';
import { createDashboardV2 } from 'api/generated/services/dashboard';
import ROUTES from 'constants/routes';
import { useSafeNavigate } from 'hooks/useSafeNavigate';
import { useErrorModal } from 'providers/ErrorModalProvider';
import { DashboardListEvents } from 'pages/DashboardsListPage/constants/events';
import APIError from 'types/api/error';
import TagKeyValueInput from 'components/TagKeyValueInput/TagKeyValueInput';

import { keyValueStringsToTags } from '../../utils/helpers';

import DashboardImagePicker from '../../../DashboardPage/DashboardContainer/DashboardSettings/Overview/DashboardImagePicker/DashboardImagePicker';
import { DEFAULT_DASHBOARD_ICON_PATH } from 'pages/DashboardPage/DashboardContainer/dashboardIcons';
import { DASHBOARD_NAME_MAX_LENGTH } from '../../../DashboardPage/DashboardContainer/constants';
import styles from './NewDashboardModal.module.scss';

const DEFAULT_NAME = 'Sample Dashboard';

interface Props {
	onClose: () => void;
}

function BlankDashboardPanel({ onClose }: Props): JSX.Element {
	const { safeNavigate } = useSafeNavigate();
	const { showErrorModal } = useErrorModal();

	const [name, setName] = useState(DEFAULT_NAME);
	const [description, setDescription] = useState('');
	const [image, setImage] = useState<string>(DEFAULT_DASHBOARD_ICON_PATH);
	const [tags, setTags] = useState<string[]>([]);
	const [submitting, setSubmitting] = useState(false);

	const canSubmit = name.trim().length > 0 && !submitting;

	const handleCreate = async (): Promise<void> => {
		if (!canSubmit) {
			return;
		}
		try {
			setSubmitting(true);
			logEvent('Dashboard List: Create dashboard clicked', {});
			const postableTags = keyValueStringsToTags(tags);
			const created = await createDashboardV2({
				schemaVersion: 'v6',
				generateName: true,
				image,
				tags: postableTags.length ? postableTags : null,
				spec: {
					display: {
						name: name.trim(),
						description: description.trim() || undefined,
					},
					layouts: [],
					panels: {},
					variables: [],
				},
			});
			void logEvent(DashboardListEvents.DashboardCreated, {
				method: 'blank',
				hasDescription: Boolean(description.trim()),
				tagCount: postableTags.length,
				hasImage: Boolean(image),
			});
			onClose();
			safeNavigate(
				buildRoutePath(ROUTES.DASHBOARD, { dashboardId: created.data.id }),
			);
		} catch (e) {
			showErrorModal(e as APIError);
			toast.error((e as AxiosError).toString() || 'Failed to create dashboard');
			setSubmitting(false);
		}
	};

	return (
		<div className={styles.panel}>
			<div className={styles.form}>
				<div className={styles.field}>
					<Typography.Text className={styles.label}>
						Title <Typography.Text className={styles.required}>*</Typography.Text>
					</Typography.Text>
					<div className={styles.titleRow}>
						<DashboardImagePicker
							image={image}
							onChange={setImage}
							triggerClassName={styles.imageTrigger}
						/>
						<Input
							className={styles.titleInput}
							value={name}
							autoFocus
							maxLength={DASHBOARD_NAME_MAX_LENGTH}
							placeholder="e.g. Sample Dashboard"
							testId="create-dashboard-name"
							onChange={(e: ChangeEvent<HTMLInputElement>): void =>
								setName(e.target.value)
							}
							onKeyDown={(e): void => {
								if (e.key === 'Enter') {
									void handleCreate();
								}
							}}
						/>
					</div>
				</div>

				<div className={styles.field}>
					<Typography.Text className={styles.label}>Description</Typography.Text>
					{/* eslint-disable-next-line signoz/no-antd-components -- no @signozhq TextArea yet */}
					<AntInput.TextArea
						value={description}
						rows={3}
						placeholder="What is this dashboard for?"
						data-testid="create-dashboard-description"
						onChange={(e): void => setDescription(e.target.value)}
					/>
				</div>

				<div className={styles.field}>
					<Typography.Text className={styles.label}>Tags</Typography.Text>
					<TagKeyValueInput
						tags={tags}
						onTagsChange={setTags}
						placeholder="team:jarvis (press Enter)"
						testId="create-dashboard-tags"
					/>
					<Typography.Text className={styles.hint}>
						Use key:value (e.g. team:jarvis) and press Enter to add.
					</Typography.Text>
				</div>
			</div>

			<div className={styles.footer}>
				<Button
					variant="ghost"
					color="secondary"
					size="md"
					onClick={onClose}
					testId="create-dashboard-cancel"
				>
					Cancel
				</Button>
				<Button
					disabledTooltip="Enter a dashboard name first"
					variant="solid"
					color="primary"
					size="md"
					disabled={name.trim().length === 0}
					loading={submitting}
					testId="create-dashboard-submit"
					onClick={(): void => {
						void handleCreate();
					}}
				>
					Create dashboard
				</Button>
			</div>
		</div>
	);
}

export default BlankDashboardPanel;