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
|
"use client"
import { useQuery } from "@tanstack/react-query"
import { createSupabaseBrowserClient } from "@/lib/supabase/client"
import { queryKeys } from "./query-keys"
import type { Folder, Subscription } from "@/lib/types/subscription"
interface SubscriptionRow {
id: string
feed_id: string
folder_id: string | null
custom_title: string | null
position: number
hidden_from_timeline: boolean
feeds: {
title: string | null
url: string
visibility: "public" | "authenticated"
consecutive_failures: number
last_fetch_error: string | null
last_fetched_at: string | null
fetch_interval_seconds: number
feed_type: string | null
}
}
interface FolderRow {
id: string
name: string
position: number
icon_url: string | null
}
export function useSubscriptions() {
const supabaseClient = createSupabaseBrowserClient()
return useQuery({
queryKey: queryKeys.subscriptions.all,
queryFn: async () => {
const [subscriptionsResult, foldersResult] = await Promise.all([
supabaseClient
.from("subscriptions")
.select("id, feed_id, folder_id, custom_title, position, hidden_from_timeline, feeds(title, url, visibility, consecutive_failures, last_fetch_error, last_fetched_at, fetch_interval_seconds, feed_type)")
.order("position", { ascending: true }),
supabaseClient
.from("folders")
.select("id, name, position, icon_url")
.order("position", { ascending: true }),
])
if (subscriptionsResult.error) throw subscriptionsResult.error
if (foldersResult.error) throw foldersResult.error
const subscriptions: Subscription[] = (
(subscriptionsResult.data as unknown as SubscriptionRow[]) ?? []
).map((row) => ({
subscriptionIdentifier: row.id,
feedIdentifier: row.feed_id,
folderIdentifier: row.folder_id,
customTitle: row.custom_title,
position: row.position,
feedTitle: row.feeds?.title ?? "",
feedUrl: row.feeds?.url ?? "",
consecutiveFailures: row.feeds?.consecutive_failures ?? 0,
lastFetchError: row.feeds?.last_fetch_error ?? null,
lastFetchedAt: row.feeds?.last_fetched_at ?? null,
fetchIntervalSeconds: row.feeds?.fetch_interval_seconds ?? 3600,
feedType: row.feeds?.feed_type ?? null,
feedVisibility: row.feeds?.visibility ?? "public",
hiddenFromTimeline: row.hidden_from_timeline,
}))
const folders: Folder[] = (
(foldersResult.data as unknown as FolderRow[]) ?? []
).map((row) => ({
folderIdentifier: row.id,
name: row.name,
position: row.position,
iconUrl: row.icon_url,
}))
return { subscriptions, folders }
},
})
}
|