summaryrefslogtreecommitdiff
path: root/apps/web/lib/queries/use-subscriptions.ts
diff options
context:
space:
mode:
Diffstat (limited to 'apps/web/lib/queries/use-subscriptions.ts')
-rw-r--r--apps/web/lib/queries/use-subscriptions.ts78
1 files changed, 78 insertions, 0 deletions
diff --git a/apps/web/lib/queries/use-subscriptions.ts b/apps/web/lib/queries/use-subscriptions.ts
new file mode 100644
index 0000000..ebf099d
--- /dev/null
+++ b/apps/web/lib/queries/use-subscriptions.ts
@@ -0,0 +1,78 @@
+"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
+ feeds: {
+ title: string | null
+ url: string
+ 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
+}
+
+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, feeds(title, url, consecutive_failures, last_fetch_error, last_fetched_at, fetch_interval_seconds, feed_type)")
+ .order("position", { ascending: true }),
+ supabaseClient
+ .from("folders")
+ .select("id, name, position")
+ .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,
+ 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,
+ }))
+
+ const folders: Folder[] = (
+ (foldersResult.data as unknown as FolderRow[]) ?? []
+ ).map((row) => ({
+ folderIdentifier: row.id,
+ name: row.name,
+ position: row.position,
+ }))
+
+ return { subscriptions, folders }
+ },
+ })
+}