"use client" import { useInfiniteQuery } from "@tanstack/react-query" import { createSupabaseBrowserClient } from "@/lib/supabase/client" import { queryKeys } from "./query-keys" import type { HighlightWithEntryContext } from "@/lib/types/highlight" const HIGHLIGHTS_PAGE_SIZE = 50 interface HighlightWithContextRow { id: string entry_id: string highlighted_text: string note: string | null text_offset: number text_length: number text_prefix: string text_suffix: string color: string created_at: string entries: { id: string title: string | null feeds: { title: string | null } } } function mapRowToHighlightWithContext( row: HighlightWithContextRow ): HighlightWithEntryContext { return { identifier: row.id, entryIdentifier: row.entry_id, highlightedText: row.highlighted_text, note: row.note, textOffset: row.text_offset, textLength: row.text_length, textPrefix: row.text_prefix, textSuffix: row.text_suffix, color: row.color, createdAt: row.created_at, entryTitle: row.entries?.title ?? null, feedTitle: row.entries?.feeds?.title ?? null, } } export function useAllHighlights() { const supabaseClient = createSupabaseBrowserClient() return useInfiniteQuery({ queryKey: queryKeys.highlights.all, queryFn: async ({ pageParam, }: { pageParam: string | undefined }) => { let query = supabaseClient .from("user_highlights") .select( "id, entry_id, highlighted_text, note, text_offset, text_length, text_prefix, text_suffix, color, created_at, entries!inner(id, title, feeds!inner(title))" ) .order("created_at", { ascending: false }) .limit(HIGHLIGHTS_PAGE_SIZE) if (pageParam) { query = query.lt("created_at", pageParam) } const { data, error } = await query if (error) throw error return ( (data as unknown as HighlightWithContextRow[]) ?? [] ).map(mapRowToHighlightWithContext) }, initialPageParam: undefined as string | undefined, getNextPageParam: (lastPage: HighlightWithEntryContext[]) => { if (lastPage.length < HIGHLIGHTS_PAGE_SIZE) return undefined return lastPage[lastPage.length - 1].createdAt }, }) }