import { useState, type ReactNode } from 'react'; import { REGEXP_ONLY_DIGITS } from 'input-otp'; import { Button } from '@/components/base/buttons/button'; import { Dialog, Modal, ModalOverlay } from '@/components/application/modals/modal'; import { PinInput } from '@/components/base/pin-input/pin-input'; import { FeaturedIcon } from '@/components/foundations/featured-icon/featured-icon'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faShieldKeyhole } from '@fortawesome/pro-duotone-svg-icons'; import type { FC } from 'react'; import { notify } from '@/lib/toast'; const ShieldIcon: FC<{ className?: string }> = ({ className }) => ( ); const API_URL = import.meta.env.VITE_API_URL ?? 'http://localhost:4100'; type MaintAction = { endpoint: string; label: string; description: string; needsPreStep?: boolean; clientSideHandler?: (payload: any) => Promise<{ status: string; message: string }>; }; type MaintOtpModalProps = { isOpen: boolean; onOpenChange: (open: boolean) => void; action: MaintAction | null; preStepContent?: ReactNode; preStepPayload?: Record; preStepReady?: boolean; }; export const MaintOtpModal = ({ isOpen, onOpenChange, action, preStepContent, preStepPayload, preStepReady = true }: MaintOtpModalProps) => { const [otp, setOtp] = useState(''); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const handleSubmit = async () => { if (!action || otp.length < 6) return; setLoading(true); setError(null); try { if (action.clientSideHandler) { // Client-side action — OTP verified by calling a dummy maint endpoint first const otpRes = await fetch(`${API_URL}/api/maint/force-ready`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'x-maint-otp': otp }, }); if (!otpRes.ok) { setError('Invalid maintenance code'); setLoading(false); return; } const result = await action.clientSideHandler(preStepPayload); notify.success(action.label, result.message ?? 'Completed'); onOpenChange(false); setOtp(''); } else { // Standard sidecar endpoint const res = await fetch(`${API_URL}/api/maint/${action.endpoint}`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'x-maint-otp': otp, }, ...(preStepPayload ? { body: JSON.stringify(preStepPayload) } : {}), }); const data = await res.json(); if (res.ok) { console.log(`[MAINT] ${action.label}:`, data); notify.success(action.label, data.message ?? 'Completed successfully'); onOpenChange(false); setOtp(''); } else { setError(data.message ?? 'Failed'); } } } catch { setError('Request failed'); } finally { setLoading(false); } }; const handleOtpChange = (value: string) => { setOtp(value); setError(null); }; const handleClose = () => { onOpenChange(false); setOtp(''); setError(null); }; if (!action) return null; const showOtp = !action.needsPreStep || preStepReady; return ( {() => (
{/* Header */}

{action.label}

{action.description}

{/* Pre-step content (e.g., campaign selection) */} {action.needsPreStep && preStepContent && (
{preStepContent}
)} {/* Pin Input — shown when pre-step is ready (or no pre-step needed) */} {showOtp && (
Enter maintenance code {error && (

{error}

)}
)} {/* Footer */}
)}
); };