summaryrefslogtreecommitdiff
path: root/apps/web/lib/queries/use-custom-feeds.ts
blob: f2918b56124e64283ba0ecc2501e6e0d58679fa7 (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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"use client"

import { useQuery } from "@tanstack/react-query"
import { createSupabaseBrowserClient } from "@/lib/supabase/client"
import { queryKeys } from "./query-keys"
import type { CustomFeed } from "@/lib/types/custom-feed"

interface CustomFeedRow {
  id: string
  name: string
  query: string
  match_mode: string
  source_folder_id: string | null
  position: number
  icon_url: string | null
}

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

  return useQuery({
    queryKey: queryKeys.customFeeds.all,
    queryFn: async () => {
      const {
        data: { user },
      } = await supabaseClient.auth.getUser()

      if (!user) throw new Error("not authenticated")

      const { data, error } = await supabaseClient
        .from("custom_feeds")
        .select("id, name, query, match_mode, source_folder_id, position, icon_url")
        .eq("user_id", user.id)
        .order("position")

      if (error) throw error

      return ((data as CustomFeedRow[]) ?? []).map(
        (row): CustomFeed => ({
          identifier: row.id,
          name: row.name,
          query: row.query,
          matchMode: row.match_mode as "and" | "or",
          sourceFolderIdentifier: row.source_folder_id,
          position: row.position,
          iconUrl: row.icon_url,
        })
      )
    },
  })
}