summaryrefslogtreecommitdiff
path: root/apps/web/lib/queries/use-all-highlights.ts
blob: 39988de812cf4c5df2927b4d97aea6158d225d0a (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
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 { 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
    },
  })
}