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.

4.0 KB
Raw Blame
import type { Control, UseFormRegister } from 'react-hook-form';
import { Controller } from 'react-hook-form';
import { Button } from '@signozhq/ui/button';
import { Input } from '@signozhq/ui/input';
import { ToggleGroup } from '@signozhq/ui/toggle-group';
import { DatePicker } from 'antd';
import AuthZButton from 'lib/authz/components/AuthZButton/AuthZButton';
import { AuthZGuardContent } from 'lib/authz/components/AuthZGuard/AuthZGuardContent';
import {
	APIKeyCreatePermission,
	buildSAAttachPermission,
} from 'lib/authz/hooks/useAuthZ/permissions/service-account.permissions';
import { popupContainer } from 'utils/selectPopupContainer';

import { disabledDate } from '../utils';
import type { FormValues } from './types';
import { ExpiryMode, FORM_ID } from './types';

export interface KeyFormPhaseProps {
	register: UseFormRegister<FormValues>;
	control: Control<FormValues>;
	expiryMode: ExpiryMode;
	isSubmitting: boolean;
	isValid: boolean;
	onSubmit: () => void;
	onClose: () => void;
	accountId?: string;
}

function KeyFormPhase({
	register,
	control,
	expiryMode,
	isSubmitting,
	isValid,
	onSubmit,
	onClose,
	accountId,
}: KeyFormPhaseProps): JSX.Element {
	const checks = accountId
		? [APIKeyCreatePermission, buildSAAttachPermission(accountId)]
		: [];

	return (
		<>
			<form id={FORM_ID} className="add-key-modal__form" onSubmit={onSubmit}>
				<AuthZGuardContent checks={checks}>
					<>
						<div className="add-key-modal__field">
							<label className="add-key-modal__label" htmlFor="key-name">
								Name <span style={{ color: 'var(--destructive)' }}>*</span>
							</label>
							<Input
								id="key-name"
								placeholder="Enter key name e.g.: Service Owner"
								className="add-key-modal__input"
								testId="add-key-name-input"
								{...register('keyName', {
									required: true,
									validate: (v) => !!v.trim(),
								})}
							/>
						</div>

						<div className="add-key-modal__field">
							<span className="add-key-modal__label">Expiration</span>
							<Controller
								name="expiryMode"
								control={control}
								render={({ field }): JSX.Element => (
									<ToggleGroup
										variant="outlined"
										color="secondary"
										type="single"
										value={field.value}
										onChange={(val: string): void => {
											if (val) {
												field.onChange(val);
											}
										}}
										size="sm"
										width="60%"
										items={[
											{ value: ExpiryMode.NONE, label: 'No Expiration' },
											{ value: ExpiryMode.DATE, label: 'Set Expiration Date' },
										]}
									/>
								)}
							/>
						</div>

						{expiryMode === ExpiryMode.DATE && (
							<div className="add-key-modal__field">
								<label className="add-key-modal__label" htmlFor="expiry-date">
									Expiration Date
								</label>
								<div className="add-key-modal__datepicker">
									<Controller
										name="expiryDate"
										control={control}
										render={({ field }): JSX.Element => (
											<DatePicker
												id="expiry-date"
												value={field.value}
												onChange={field.onChange}
												popupClassName="add-key-modal-datepicker-popup"
												getPopupContainer={popupContainer}
												disabledDate={disabledDate}
											/>
										)}
									/>
								</div>
							</div>
						)}
					</>
				</AuthZGuardContent>
			</form>

			<div className="add-key-modal__footer">
				<div className="add-key-modal__footer-right">
					<Button
						size="md"
						variant="solid"
						color="secondary"
						onClick={onClose}
						testId="add-key-cancel-btn"
					>
						Cancel
					</Button>
					<AuthZButton
						size="md"
						checks={checks}
						authZEnabled={!!accountId}
						type="submit"
						form={FORM_ID}
						variant="solid"
						color="primary"
						loading={isSubmitting}
						disabled={!isValid}
						testId="add-key-submit-btn"
					>
						Create Key
					</AuthZButton>
				</div>
			</div>
		</>
	);
}

export default KeyFormPhase;