Skip to content

Commit

Permalink
wip: supabase text search
Browse files Browse the repository at this point in the history
  • Loading branch information
gaboesquivel committed Apr 22, 2024
1 parent 11acef4 commit 6b52ef7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
21 changes: 12 additions & 9 deletions apps/masterbots.ai/app/(browse)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,43 @@ 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) {
const thread = await getThread({ threadId: searchParams.threadId })
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 (
<div className="container">
<CategoryTabs categories={categories} />
<SearchInput />
{/* <div>Your query: {query}</div>
<div>Your query: {query}</div>
<ul>
{threads.map(t => (
<li key={t.threadId}>{t.messages[0]?.content || 'not found'}</li>
))}
</ul> */}
</ul>

{threads.length ? (
{/* {threads.length ? (
<ThreadList initialThreads={threads} />
) : (
<Card>no results</Card>
)}
)} */}
</div>
)
}
Expand Down
33 changes: 33 additions & 0 deletions apps/masterbots.ai/app/actions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use server'

import { createSupabaseServerClient } from '@/services/supabase'
import { Dub } from 'dub'

const dub = new Dub({
Expand Down Expand Up @@ -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
}

0 comments on commit 6b52ef7

Please sign in to comment.