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
|
"use client"
import { useInfiniteQuery } from "@tanstack/react-query"
import { createSupabaseBrowserClient } from "@/lib/supabase/client"
import { queryKeys } from "./query-keys"
import type { TimelineEntry } from "@/lib/types/timeline"
const SAVED_PAGE_SIZE = 50
interface SavedEntryRow {
entry_id: string
read: boolean
saved: boolean
saved_at: string
entries: {
id: string
feed_id: string
title: string | null
url: string | null
author: string | null
summary: string | null
image_url: string | null
published_at: string | null
enclosure_url: string | null
enclosure_type: string | null
feeds: {
title: string | null
}
}
}
function mapSavedRowToTimelineEntry(row: SavedEntryRow): TimelineEntry {
return {
entryIdentifier: row.entries.id,
feedIdentifier: row.entries.feed_id,
feedTitle: row.entries.feeds?.title ?? "",
customTitle: null,
entryTitle: row.entries.title ?? "",
entryUrl: row.entries.url ?? "",
author: row.entries.author,
summary: row.entries.summary,
imageUrl: row.entries.image_url,
publishedAt: row.entries.published_at ?? "",
isRead: row.read,
isSaved: row.saved,
enclosureUrl: row.entries.enclosure_url,
enclosureType: row.entries.enclosure_type,
}
}
export function useSavedEntries() {
const supabaseClient = createSupabaseBrowserClient()
return useInfiniteQuery({
queryKey: queryKeys.savedEntries.all,
queryFn: async ({
pageParam,
}: {
pageParam: string | undefined
}) => {
let query = supabaseClient
.from("user_entry_states")
.select(
"entry_id, read, saved, saved_at, entries!inner(id, feed_id, title, url, author, summary, image_url, published_at, enclosure_url, enclosure_type, feeds!inner(title))"
)
.eq("saved", true)
.order("saved_at", { ascending: false })
.limit(SAVED_PAGE_SIZE)
if (pageParam) {
query = query.lt("saved_at", pageParam)
}
const { data, error } = await query
if (error) throw error
return ((data as unknown as SavedEntryRow[]) ?? []).map(
mapSavedRowToTimelineEntry
)
},
initialPageParam: undefined as string | undefined,
getNextPageParam: (lastPage: TimelineEntry[]) => {
if (lastPage.length < SAVED_PAGE_SIZE) return undefined
return lastPage[lastPage.length - 1].publishedAt
},
})
}
|