summaryrefslogtreecommitdiff
path: root/apps/web/lib/queries/use-unread-counts.ts
blob: 75deccbf6f18011879968bbc3d22f1e60563447a (plain) (blame)
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
"use client"

import { useQuery } from "@tanstack/react-query"
import { createSupabaseBrowserClient } from "@/lib/supabase/client"
import { queryKeys } from "./query-keys"

interface UnreadCountRow {
  feed_id: string
  unread_count: number
}

export function useUnreadCounts() {
  const supabaseClient = createSupabaseBrowserClient()

  return useQuery({
    queryKey: queryKeys.unreadCounts.all,
    queryFn: async () => {
      const { data, error } = await supabaseClient.rpc("get_unread_counts")

      if (error) throw error

      const countsByFeedIdentifier: Record<string, number> = {}

      for (const row of (data as UnreadCountRow[]) ?? []) {
        countsByFeedIdentifier[row.feed_id] = row.unread_count
      }

      return countsByFeedIdentifier
    },
    refetchInterval: 60_000,
  })
}