Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions apps/community/app/(board)/search/[token]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ import { isAppError } from '@meith/core'
import { requireSlot } from '@meith/theme-kit'

import { getActor } from '@/server/context'
import { openSearch, SEARCH_PAGE } from '@/server/search-page'
import { getContainer } from '@/server/container'
import {
openSearch,
readRefinement,
SEARCH_COUNT_CAP,
SEARCH_PAGE,
} from '@/server/search-page'
import { currentSessionKey } from '@/server/session-key'
import { currentTheme } from '@/server/theme'
import { filterView, viewerRef } from '@/server/plugin-view'
import { getViewerPreferences } from '@/server/viewer-preferences'
import { buildSearchResultsView } from '@/view/search-results'
import { CURSOR_FIELDS } from '@/view/search-controls'
import { buildSearchResultsView, type SearchForumRef } from '@/view/search-results'

export const metadata: Metadata = { title: 'Search results' }

Expand All @@ -30,8 +37,8 @@ export default async function SearchResultsPage({
return text === undefined || text === '' ? undefined : text
}

const rank = Number(one('rank'))
const postId = Number(one('after'))
const rank = Number(one(CURSOR_FIELDS.rank))
const postId = Number(one(CURSOR_FIELDS.after))
const after =
Number.isFinite(rank) && Number.isSafeInteger(postId) && postId > 0
? { rank, postId }
Expand All @@ -47,29 +54,40 @@ export default async function SearchResultsPage({
sessionKey: await currentSessionKey(),
token,
after,
refine: readRefinement(query),
now,
})
} catch (err) {
if (isAppError(err)) notFound()
throw err
}

const { search, results } = view
const { search, results, summary, filters, effective, refine } = view
const { timezone } = await getViewerPreferences()

const model = buildSearchResultsView({
token,
terms: search.terms,
createdAt: search.createdAt,
hits: results.hits,
nextHref:
results.nextCursor === null
? null
: `/search/${token}?rank=${results.nextCursor.rank}&after=${results.nextCursor.postId}`,
nextCursor: results.nextCursor,
pageSize: SEARCH_PAGE,
now,
timeZone: timezone,
filters,
effective,
refine,
summary,
forums: await namedForums(),
countCap: SEARCH_COUNT_CAP,
})

const SearchResults = requireSlot(await currentTheme(), 'SearchResults')

return <SearchResults {...await filterView('view.search-results', model, viewerRef(actor))} />
}

async function namedForums(): Promise<readonly SearchForumRef[]> {
const { forums } = getContainer()
return (await forums.listAll()).map((forum) => ({ id: forum.id, title: forum.title }))
}
Loading