Accepts psaTicketId and psaConnectionId, fetches context on mount when both IDs are present, and exposes refresh() for manual re-fetch. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
TypeScript
import { useState, useEffect, useCallback } from 'react'
|
|
import { psaContextApi, type TicketContext } from '@/api/psaContext'
|
|
|
|
interface UseTicketContextResult {
|
|
context: TicketContext | null
|
|
loading: boolean
|
|
error: string | null
|
|
refresh: () => void
|
|
}
|
|
|
|
export function useTicketContext(
|
|
psaTicketId: string | null | undefined,
|
|
psaConnectionId: string | null | undefined
|
|
): UseTicketContextResult {
|
|
const [context, setContext] = useState<TicketContext | null>(null)
|
|
const [loading, setLoading] = useState(false)
|
|
const [error, setError] = useState<string | null>(null)
|
|
|
|
const fetchContext = useCallback(async () => {
|
|
if (!psaTicketId || !psaConnectionId) return
|
|
|
|
setLoading(true)
|
|
setError(null)
|
|
try {
|
|
const data = await psaContextApi.getTicketContext(psaTicketId)
|
|
setContext(data)
|
|
} catch (err) {
|
|
const message = err instanceof Error ? err.message : 'Failed to load ticket context'
|
|
setError(message)
|
|
} finally {
|
|
setLoading(false)
|
|
}
|
|
}, [psaTicketId, psaConnectionId])
|
|
|
|
useEffect(() => {
|
|
fetchContext()
|
|
}, [fetchContext])
|
|
|
|
return { context, loading, error, refresh: fetchContext }
|
|
}
|