fix: harden ticket client data guards

This commit is contained in:
Esdras Renan 2025-11-01 01:48:15 -03:00
parent 5c5207ceb8
commit a3d431efa8
9 changed files with 129 additions and 111 deletions

View file

@ -74,15 +74,16 @@ function TicketRow({ ticket, entering }: { ticket: Ticket; entering: boolean })
export function RecentTicketsPanel() {
const { convexUserId } = useAuth()
const ticketsRaw = useQuery(
api.tickets.list,
convexUserId ? { tenantId: DEFAULT_TENANT_ID, viewerId: convexUserId as Id<"users">, limit: 12 } : "skip"
)
const ticketsArgs = convexUserId
? { tenantId: DEFAULT_TENANT_ID, viewerId: convexUserId as Id<"users">, limit: 12 }
: undefined
const ticketsResult = useQuery(convexUserId ? api.tickets.list : "skip", ticketsArgs)
const [enteringId, setEnteringId] = useState<string | null>(null)
const previousIdsRef = useRef<string[]>([])
const tickets = useMemo(() => {
const all = mapTicketsFromServerList((ticketsRaw ?? []) as unknown[]).filter((t) => t.status !== "RESOLVED")
if (!Array.isArray(ticketsResult)) return []
const all = mapTicketsFromServerList(ticketsResult as unknown[]).filter((t) => t.status !== "RESOLVED")
// Unassigned first (no assignee), oldest first among unassigned; then the rest by updatedAt desc
const unassigned = all
.filter((t) => !t.assignee)
@ -91,10 +92,10 @@ export function RecentTicketsPanel() {
.filter((t) => !!t.assignee)
.sort((a, b) => b.updatedAt.getTime() - a.updatedAt.getTime())
return [...unassigned, ...assigned].slice(0, 6)
}, [ticketsRaw])
}, [ticketsResult])
useEffect(() => {
if (ticketsRaw === undefined) {
if (!Array.isArray(ticketsResult)) {
previousIdsRef.current = []
return
}
@ -113,7 +114,7 @@ export function RecentTicketsPanel() {
setEnteringId(topId)
}
previousIdsRef.current = ids
}, [tickets, ticketsRaw])
}, [tickets, ticketsResult])
useEffect(() => {
if (!enteringId) return
@ -121,7 +122,7 @@ export function RecentTicketsPanel() {
return () => window.clearTimeout(timer)
}, [enteringId])
if (ticketsRaw === undefined) {
if (convexUserId && !Array.isArray(ticketsResult)) {
return (
<Card className="rounded-2xl border border-slate-200 bg-white shadow-sm">
<CardHeader className="pb-2">