import { useState, useEffect } from 'react' import { useParams, useNavigate } from 'react-router-dom' import api from '@/services/api' import { useAuth } from '@/hooks/useAuth' import CopyableCode from '@/components/CopyableCode' interface SupportItem { id: number code: string | null support_code: string | null title: string description: string | null status: string priority: string project_id: number project_code: string | null milestone_id: number milestone_code: string | null reporter_id: number assignee_id: number | null taken_by: string | null created_at: string updated_at: string | null } const STATUS_OPTIONS = ['open', 'in_progress', 'resolved', 'closed'] const PRIORITY_OPTIONS = ['low', 'medium', 'high', 'critical'] export default function SupportDetailPage() { const { supportCode } = useParams() const navigate = useNavigate() const { user } = useAuth() const [support, setSupport] = useState(null) const [loading, setLoading] = useState(true) const [saving, setSaving] = useState(false) const [message, setMessage] = useState('') const [editMode, setEditMode] = useState(false) const [editForm, setEditForm] = useState({ title: '', description: '', priority: '' }) const [transitionStatus, setTransitionStatus] = useState('') const fetchSupport = async () => { try { const { data } = await api.get(`/supports/${supportCode}`) setSupport(data) setEditForm({ title: data.title, description: data.description || '', priority: data.priority, }) } catch (err: any) { setMessage(err.response?.data?.detail || 'Failed to load support ticket') } finally { setLoading(false) } } useEffect(() => { fetchSupport() }, [supportCode]) const handleTake = async () => { if (!support) return setSaving(true) setMessage('') try { const { data } = await api.post(`/supports/${supportCode}/take`) setSupport(data) setMessage('You have taken this support ticket') } catch (err: any) { setMessage(err.response?.data?.detail || 'Failed to take support ticket') } finally { setSaving(false) } } const handleTransition = async () => { if (!support || !transitionStatus) return setSaving(true) setMessage('') try { const { data } = await api.post(`/supports/${supportCode}/transition`, { status: transitionStatus, }) setSupport(data) setTransitionStatus('') setMessage(`Status changed to ${transitionStatus}`) } catch (err: any) { setMessage(err.response?.data?.detail || 'Failed to transition support ticket') } finally { setSaving(false) } } const handleSave = async () => { if (!support) return setSaving(true) setMessage('') try { const payload: Record = {} if (editForm.title.trim() !== support.title) payload.title = editForm.title.trim() if ((editForm.description || '') !== (support.description || '')) payload.description = editForm.description || null if (editForm.priority !== support.priority) payload.priority = editForm.priority if (Object.keys(payload).length === 0) { setEditMode(false) return } const { data } = await api.patch(`/supports/${supportCode}`, payload) setSupport(data) setEditMode(false) setMessage('Support ticket updated') } catch (err: any) { setMessage(err.response?.data?.detail || 'Failed to update support ticket') } finally { setSaving(false) } } const handleDelete = async () => { if (!support) return if (!confirm(`Delete support ticket ${support.support_code || support.id}? This cannot be undone.`)) return setSaving(true) try { await api.delete(`/supports/${supportCode}`) navigate(-1) } catch (err: any) { setMessage(err.response?.data?.detail || 'Failed to delete support ticket') setSaving(false) } } if (loading) return
Loading support ticket...
if (!support) return
{message || 'Support ticket not found'}
const canTake = user && (!support.assignee_id || support.assignee_id === user.id) && support.status !== 'closed' && support.status !== 'resolved' const isMine = user && support.assignee_id === user.id const availableTransitions = STATUS_OPTIONS.filter((s) => s !== support.status) return (

🎫 {support.support_code ? : `#${support.id}`}

{support.status} {support.priority} {support.project_code && Project: {support.project_code}} {support.milestone_code && Milestone: {support.milestone_code}}
{message && (
{message}
)}
{/* Main content */}
{editMode ? (