All files / web/src/hooks usePostcards.ts

0% Statements 0/53
0% Branches 0/1
0% Functions 0/1
0% Lines 0/53

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54                                                                                                           
'use client'

import { useQuery } from '@tanstack/react-query'
import { postcardKeys } from '@/lib/queryKeys'

interface PostcardListItem {
  id: string
  callerNumber: number
  status: string
  imageUrl: string | null
  thumbnailUrl: string | null
  isRead: boolean
  createdAt: string
  manifest: {
    callerNumber: number
    callerPersonality: string
    childName: string
    childEmoji: string
    sessionSummary: string
    moments: Array<{
      rank: number
      caption: string
      category: string
    }>
  }
}

export function usePostcards(playerId?: string) {
  return useQuery({
    queryKey: postcardKeys.list(playerId),
    queryFn: async (): Promise<PostcardListItem[]> => {
      const params = playerId ? `?playerId=${playerId}` : ''
      const res = await fetch(`/api/postcards${params}`)
      if (!res.ok) throw new Error('Failed to fetch postcards')
      const data = await res.json()
      return data.postcards
    },
  })
}

export function useUnreadPostcardCount(playerId?: string) {
  return useQuery({
    queryKey: postcardKeys.unreadCount(playerId),
    queryFn: async (): Promise<number> => {
      const params = playerId ? `?playerId=${playerId}` : ''
      const res = await fetch(`/api/postcards/unread-count${params}`)
      if (!res.ok) throw new Error('Failed to fetch unread count')
      const data = await res.json()
      return data.count
    },
    refetchInterval: 30_000, // Poll every 30s for new postcards
  })
}