summaryrefslogtreecommitdiff
path: root/apps/web/lib/queries/use-entry-search.ts
diff options
context:
space:
mode:
Diffstat (limited to 'apps/web/lib/queries/use-entry-search.ts')
-rw-r--r--apps/web/lib/queries/use-entry-search.ts58
1 files changed, 58 insertions, 0 deletions
diff --git a/apps/web/lib/queries/use-entry-search.ts b/apps/web/lib/queries/use-entry-search.ts
new file mode 100644
index 0000000..9e05ac8
--- /dev/null
+++ b/apps/web/lib/queries/use-entry-search.ts
@@ -0,0 +1,58 @@
+"use client"
+
+import { useQuery } from "@tanstack/react-query"
+import { createSupabaseBrowserClient } from "@/lib/supabase/client"
+import { queryKeys } from "./query-keys"
+import type { TimelineEntry } from "@/lib/types/timeline"
+
+interface SearchResultRow {
+ entry_id: string
+ feed_id: string
+ feed_title: string
+ custom_title: string | null
+ entry_title: string
+ entry_url: string
+ author: string | null
+ summary: string | null
+ image_url: string | null
+ published_at: string
+ is_read: boolean
+ is_saved: boolean
+}
+
+export function useEntrySearch(searchQuery: string) {
+ const supabaseClient = createSupabaseBrowserClient()
+ const trimmedQuery = searchQuery.trim()
+
+ return useQuery({
+ queryKey: queryKeys.entrySearch.query(trimmedQuery),
+ queryFn: async () => {
+ const { data, error } = await supabaseClient.rpc("search_entries", {
+ p_query: trimmedQuery,
+ p_result_limit: 30,
+ })
+
+ if (error) throw error
+
+ return ((data as SearchResultRow[]) ?? []).map(
+ (row): TimelineEntry => ({
+ entryIdentifier: row.entry_id,
+ feedIdentifier: row.feed_id,
+ feedTitle: row.feed_title,
+ customTitle: row.custom_title,
+ entryTitle: row.entry_title,
+ entryUrl: row.entry_url,
+ author: row.author,
+ summary: row.summary,
+ imageUrl: row.image_url,
+ publishedAt: row.published_at,
+ isRead: row.is_read,
+ isSaved: row.is_saved,
+ enclosureUrl: null,
+ enclosureType: null,
+ })
+ )
+ },
+ enabled: trimmedQuery.length >= 2,
+ })
+}