diff --git a/apps/masterbots.ai/app/(browse)/page.tsx b/apps/masterbots.ai/app/(browse)/page.tsx index 930ed396..3e6e305d 100644 --- a/apps/masterbots.ai/app/(browse)/page.tsx +++ b/apps/masterbots.ai/app/(browse)/page.tsx @@ -6,6 +6,7 @@ import { Card } from '@/components/ui/card' import { decodeQuery } from '@/lib/url' import { permanentRedirect } from 'next/navigation' import { getThreadLink } from '@/lib/threads' +import { getThreads } from '../actions' export default async function HomePage({ searchParams }: HomePageProps) { if (searchParams.threadId) { @@ -13,33 +14,35 @@ export default async function HomePage({ searchParams }: HomePageProps) { permanentRedirect(getThreadLink({ thread })) } + // TODO: move this static generation phase const categories = await getCategories() + const query = searchParams.query ? decodeQuery(searchParams.query) : null const limit = searchParams.limit ? parseInt(searchParams.limit) : 20 const page = searchParams.page ? parseInt(searchParams.page) : 1 + console.log('hey hey hey', { query, limit, page }) + const formData = new FormData() - const threads = await getBrowseThreads({ - limit, - offset: (page - 1) * limit, - query - }) + // Append the limit parameter to the FormData + formData.append('query', query) + const threads = await getThreads(formData) return (
- {/*
Your query: {query}
+
Your query: {query}
    {threads.map(t => (
  • {t.messages[0]?.content || 'not found'}
  • ))} -
*/} + - {threads.length ? ( + {/* {threads.length ? ( ) : ( no results - )} + )} */}
) } diff --git a/apps/masterbots.ai/app/actions.ts b/apps/masterbots.ai/app/actions.ts index 50e215c8..2a04a816 100644 --- a/apps/masterbots.ai/app/actions.ts +++ b/apps/masterbots.ai/app/actions.ts @@ -1,5 +1,6 @@ 'use server' +import { createSupabaseServerClient } from '@/services/supabase' import { Dub } from 'dub' const dub = new Dub({ @@ -33,3 +34,35 @@ export async function shorten(_prevState: any, formData: any) { } } } + +/* + The base query is set up to fetch all threads and all associated messages, ordered by creation time. + When a search query is provided, a modified select statement is used which applies a filter to exclude messages + with a role of 'system' only during the search. This way, the search will ignore 'system' messages but the + final output for threads that match other criteria will include all messages. + Conditional Query Construction: The filter and text search are only applied if there's a non-empty searchQuery. + This ensures that all messages, including those with a 'system' role, are returned if there is no search criterion. +*/ +export async function getThreads(formData: FormData) { + const supabase = await createSupabaseServerClient() + const query = formData.get('query') + + let supaQuery = supabase.from('thread').select( + ` + *, + message ( + * order(created_at desc) + ) + ` + ) + + if (query) { + supaQuery = supaQuery + .filter('message.role', 'neq', 'system') + .textSearch('message.content', query.toString()) + } + + const { data, error } = await supaQuery + + return error ? null : data +}