import React, { useState, useRef } from 'react'; import { api, getFileIcon } from '@nexus/shared'; const COLORS = { brand: "#1E40AF", gray50: "#F9FAFB", brandLight: "#EFF6FF", gray200: "#E5E7EB", gray400: "#9CA3AF", gray500: "#6B7280", gray600: "#4B5563", gray800: "#1F2937", gray900: "#111827", }; interface NewTicketModalProps { onClose: () => void; onCreate: (form: any) => void; } export const NewTicketModal: React.FC = ({ onClose, onCreate }) => { const [form, setForm] = useState({ title: "", category: "Software", priority: "P2", description: "" }); const [attachments, setAttachments] = useState([]); const [isUploading, setIsUploading] = useState(false); const [submitted, setSubmitted] = useState(false); const [loading, setLoading] = useState(false); const fileInputRef = useRef(null); const handleFileSelect = async (e: React.ChangeEvent) => { const files = e.target.files; if (!files) return; for (let i = 0; i < files.length; i++) { const file = files[i]; setIsUploading(true); const formData = new FormData(); formData.append("file", file); try { const resp = await api.post(`tickets/upload/`, formData, { headers: { 'Content-Type': 'multipart/form-data' } }); const backendBase = api.defaults.baseURL?.endsWith('/') ? api.defaults.baseURL : api.defaults.baseURL + '/'; const downloadUrl = `${backendBase}tickets/attachments/${resp.data.id}/download/`; setAttachments(prev => [...prev, { ...resp.data, downloadUrl }]); } catch (err) { console.error("Upload failed", err); alert(`Failed to upload ${file.name}`); } finally { setIsUploading(false); } } if (fileInputRef.current) fileInputRef.current.value = ""; }; const removeAttachment = (id: string) => { setAttachments(prev => prev.filter(a => a.id !== id)); }; const handleSubmit = async () => { if (!form.title || !form.description) return; setLoading(true); try { const payload = { ...form, attachment_ids: attachments.map(a => a.id) }; await onCreate(payload); setSubmitted(true); setTimeout(onClose, 2000); } catch (err) { console.error("Submission failed", err); } finally { setLoading(false); } }; return (
{submitted ? (
✓
Ticket submitted!
Our AI is triaging your request now...
) : ( <>
Create new ticket
setForm(f => ({ ...f, title: e.target.value }))} placeholder="Briefly describe your issue" style={{ width: "100%", padding: "9px 12px", border: `1px solid ${COLORS.gray200}`, borderRadius: 8, fontSize: 13, outline: "none", boxSizing: "border-box", color: COLORS.gray900, background: "#fff" }} />