All files / web/src/hooks useNotificationSubscription.ts

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

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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198                                                                                                                                                                                                                                                                                                                                                                                                           
'use client'

import { useCallback, useState } from 'react'
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { api } from '@/lib/queryClient'
import { notificationSubscriptionKeys } from '@/lib/queryKeys'
import {
  registerServiceWorker,
  subscribeToPush,
  pushSubscriptionToJson,
} from '@/lib/notifications/register-sw'
import type { WebPushSubscriptionJson } from '@/db/schema'

interface Subscription {
  id: string
  playerId: string
  userId: string | null
  email: string | null
  pushSubscription: WebPushSubscriptionJson | null
  channels: { webPush?: boolean; email?: boolean; inApp?: boolean }
  status: string
}

interface SubscribeOptions {
  email?: string
  shareToken?: string
  enablePush?: boolean
}

function getStorageKey(playerId: string) {
  return `notification-sub:${playerId}`
}

function readLocalSub(playerId: string): Subscription | null {
  try {
    const raw = localStorage.getItem(getStorageKey(playerId))
    if (!raw) return null
    return JSON.parse(raw) as Subscription
  } catch {
    return null
  }
}

export function useNotificationSubscription(
  playerId: string | undefined,
  userId: string | undefined
) {
  const queryClient = useQueryClient()

  // Local state for anonymous subscriptions (can't query the API without auth)
  // Initialize from localStorage so anonymous subs survive page refresh
  const [localSubscription, setLocalSubscription] = useState<Subscription | null>(() => {
    if (userId || !playerId) return null
    if (typeof window === 'undefined') return null
    return readLocalSub(playerId)
  })

  // Fetch existing subscriptions (authenticated only)
  const subscriptionsQuery = useQuery({
    queryKey: notificationSubscriptionKeys.list(playerId ?? ''),
    queryFn: async (): Promise<Subscription[]> => {
      const res = await api(`notifications/subscriptions?playerId=${playerId}`)
      if (!res.ok) return []
      const data = await res.json()
      return data.subscriptions ?? []
    },
    enabled: !!playerId && !!userId,
    staleTime: 60 * 1000,
  })

  const serverSubscriptions = subscriptionsQuery.data ?? []
  const subscriptions = localSubscription ? [localSubscription] : serverSubscriptions
  const isSubscribed = subscriptions.length > 0

  // Check browser push support
  const pushSupported =
    typeof window !== 'undefined' &&
    'serviceWorker' in navigator &&
    'PushManager' in window &&
    'Notification' in window

  const subscribeMutation = useMutation({
    mutationFn: async (options: SubscribeOptions) => {
      let pushSub: WebPushSubscriptionJson | undefined

      if (options.enablePush && pushSupported) {
        const permission = await Notification.requestPermission()
        if (permission === 'granted') {
          const registration = await registerServiceWorker()
          if (registration) {
            const keyRes = await api('notifications/vapid-public-key')
            const { vapidPublicKey } = await keyRes.json()
            if (!vapidPublicKey) {
              console.warn(
                '[useNotificationSubscription] VAPID public key not configured on server; skipping push subscription'
              )
            } else {
              const browserSub = await subscribeToPush(registration, vapidPublicKey)
              pushSub = pushSubscriptionToJson(browserSub)
            }
          }
        }
      }

      const body: Record<string, unknown> = {
        playerId,
        channels: {
          webPush: !!pushSub,
          email: !!options.email,
          inApp: !!userId,
        },
      }

      if (pushSub) body.pushSubscription = pushSub
      if (options.email) body.email = options.email
      if (options.shareToken) body.shareToken = options.shareToken

      const res = await api('notifications/subscriptions', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(body),
      })

      if (!res.ok) {
        const err = await res.json().catch(() => ({ error: 'Failed to subscribe' }))
        throw new Error(err.error || 'Failed to subscribe')
      }

      return res.json()
    },
    onSuccess: (data) => {
      if (userId && playerId) {
        // Authenticated: invalidate the server query
        queryClient.invalidateQueries({
          queryKey: notificationSubscriptionKeys.list(playerId),
        })
      } else if (data?.subscription) {
        // Anonymous: track locally since we can't query the API
        setLocalSubscription(data.subscription)
        // Persist to localStorage so it survives page refresh
        if (playerId) {
          try {
            localStorage.setItem(getStorageKey(playerId), JSON.stringify(data.subscription))
          } catch {
            // localStorage may be unavailable
          }
        }
      }
    },
  })

  const unsubscribeMutation = useMutation({
    mutationFn: async (subscriptionId: string) => {
      const res = await api(`notifications/subscriptions/${subscriptionId}`, {
        method: 'DELETE',
      })
      if (!res.ok) throw new Error('Failed to unsubscribe')
      return res.json()
    },
    onSuccess: () => {
      setLocalSubscription(null)
      // Clear localStorage for anonymous subscriptions
      if (playerId) {
        try {
          localStorage.removeItem(getStorageKey(playerId))
        } catch {
          // localStorage may be unavailable
        }
        queryClient.invalidateQueries({
          queryKey: notificationSubscriptionKeys.list(playerId),
        })
      }
    },
  })

  const subscribe = useCallback(
    (options: SubscribeOptions = {}) => subscribeMutation.mutate(options),
    [subscribeMutation]
  )

  const unsubscribe = useCallback(
    (subscriptionId: string) => unsubscribeMutation.mutate(subscriptionId),
    [unsubscribeMutation]
  )

  return {
    subscriptions,
    isSubscribed,
    isLoading: subscriptionsQuery.isLoading,
    pushSupported,
    subscribe,
    unsubscribe,
    subscribePending: subscribeMutation.isPending,
    unsubscribePending: unsubscribeMutation.isPending,
    subscribeError: subscribeMutation.error,
  }
}