Skip to content

Commit

Permalink
chore: add new orama search widget
Browse files Browse the repository at this point in the history
  • Loading branch information
rjborba committed Sep 5, 2024
1 parent c7bab7f commit 1687b4b
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 42 deletions.
4 changes: 3 additions & 1 deletion app/components/ClientOnlySearchButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { Suspense } from 'react'
import { ImSpinner2 } from 'react-icons/im'

const LazySearchButton = React.lazy(() =>
import('@orama/searchbox').then((mod) => ({ default: mod.SearchButton }))
import('@orama/react-components').then((mod) => ({
default: mod.OramaSearchButton,
}))
)

let defaultMounted = false
Expand Down
36 changes: 23 additions & 13 deletions app/components/DocsLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import {
useNavigate,
useParams,
} from '@tanstack/react-router'
import type { AnyOrama, SearchParamsFullText, AnyDocument } from '@orama/orama'
import { SearchBox } from '@orama/searchbox'
import { OramaSearchBox } from '@orama/react-components'
import { Carbon } from '~/components/Carbon'
import { Select } from '~/components/Select'
import { useLocalStorage } from '~/utils/useLocalStorage'
Expand Down Expand Up @@ -336,6 +335,8 @@ export function DocsLayout({
[menuConfig]
)

const [isSearchboxOpen, setIsSearchboxOpen] = React.useState(false)

const docsMatch = matches.find((d) => d.pathname.includes('/docs'))

const relativePathname = lastMatch.pathname.replace(
Expand Down Expand Up @@ -444,15 +445,6 @@ export function DocsLayout({
)
})

const oramaSearchParams: SearchParamsFullText<AnyOrama, AnyDocument> = {
threshold: 0,
where: {
category: {
eq: capitalize(libraryId),
},
},
}

const logo = (
<DocsLogo
name={name}
Expand Down Expand Up @@ -499,7 +491,12 @@ export function DocsLayout({
onSelect={versionConfig.onSelect}
/>
</div>
<ClientOnlySearchButton {...searchButtonParams} />
<ClientOnlySearchButton
{...searchButtonParams}
onClick={() => setIsSearchboxOpen(true)}
>
Search
</ClientOnlySearchButton>
{menuItems}
</div>
</details>
Expand Down Expand Up @@ -546,7 +543,20 @@ export function DocsLayout({
className={`min-h-screen flex flex-col lg:flex-row w-full transition-all duration-300`}
>
<div className="fixed z-50">
<SearchBox {...searchBoxParams} searchParams={oramaSearchParams} />
<OramaSearchBox
{...searchBoxParams}
searchParams={{
threshold: 0,
where: {
category: {
eq: capitalize(libraryId!) as string,
},
} as any,
}}
facetProperty={undefined}
open={isSearchboxOpen}
onSearchboxClosed={() => setIsSearchboxOpen(false)}
/>
</div>
{smallMenu}
{largeMenu}
Expand Down
40 changes: 15 additions & 25 deletions app/components/Orama.tsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,28 @@
import type {
RegisterSearchButtonProps,
RegisterSearchBoxProps,
} from '@orama/searchbox'
import { OramaClient } from '@oramacloud/client'
import '@orama/searchbox/dist/index.css'

const oramaInstance = new OramaClient({
// The search endpoint for the Orama index
endpoint: 'https://cloud.orama.run/v1/indexes/tanstack-dev-ur0ppd',
// The public API key for performing search. This is commit-safe.
api_key: 'xqfn8QcuImADRGPIlhWTo9cT5UNiqPDj',
})

export const searchBoxParams: RegisterSearchBoxProps = {
oramaInstance,
colorScheme: 'system',
backdrop: true,
export const searchBoxParams = {
index: {
api_key: 'xqfn8QcuImADRGPIlhWTo9cT5UNiqPDj',
endpoint: 'https://cloud.orama.run/v1/indexes/tanstack-dev-ur0ppd',
},
colorScheme: 'system' as 'light' | 'dark' | 'system',
facetProperty: 'category',
resultsMap: {
resultMap: {
description: 'content',
section: 'category',
},

themeConfig: {
light: {},
dark: {
'--backdrop-bg-color': '#0d103591',
colors: {
light: {},
dark: {
'--backdrop-background-color-primary': 'rgba(0, 0, 0, 0.7)',
},
},
},
searchMode: 'hybrid',
searchParams: {
threshold: 0,
},
}

export const searchButtonParams: RegisterSearchButtonProps = {
export const searchButtonParams = {
colorScheme: 'system',
fullWidth: true,
}
17 changes: 14 additions & 3 deletions app/routes/_libraries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { CgClose, CgMenuLeft, CgMusicSpeaker } from 'react-icons/cg'
import { MdLibraryBooks, MdSupport } from 'react-icons/md'
import { twMerge } from 'tailwind-merge'
import { SearchBox } from '@orama/searchbox'
import { OramaSearchBox } from '@orama/react-components'
import { sortBy } from '~/utils/utils'
import logoColor100w from '~/images/logo-color-100w.png'
import {
Expand Down Expand Up @@ -46,6 +46,8 @@ function LibrariesLayout() {

const [mounted, setMounted] = React.useState(false)

const [onSearchboxOpen, setOnSearchboxOpen] = React.useState(false)

React.useEffect(() => {
setMounted(true)
}, [])
Expand Down Expand Up @@ -255,7 +257,12 @@ function LibrariesLayout() {
{logo}
</div>
<div className="p-2">
<ClientOnlySearchButton {...searchButtonParams} />
<ClientOnlySearchButton
{...searchButtonParams}
onClick={() => setOnSearchboxOpen(true)}
>
Search
</ClientOnlySearchButton>
</div>
<div className="flex-1 flex flex-col gap-4 whitespace-nowrap overflow-y-auto text-base pb-[300px]">
<div className="space-y-1 text-sm p-2 border-b border-gray-500/10 dark:border-gray-500/20">
Expand All @@ -278,7 +285,11 @@ function LibrariesLayout() {
</div>
{mounted ? (
<div className="fixed z-50">
<SearchBox {...searchBoxParams} />
<OramaSearchBox
{...searchBoxParams}
open={onSearchboxOpen}
onSearchboxClosed={() => setOnSearchboxOpen(false)}
/>
</div>
) : null}
</div>
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@octokit/graphql": "^7.0.2",
"@octokit/rest": "^20.0.2",
"@orama/searchbox": "1.0.0-rc42",
"@orama/react-components": "^0.0.22",
"@oramacloud/client": "^1.2.2",
"@remix-run/node": "^2.8.1",
"@sentry/react": "^8.2.1",
Expand Down
Loading

0 comments on commit 1687b4b

Please sign in to comment.