-
Notifications
You must be signed in to change notification settings - Fork 192
Expand file tree
/
Copy pathComponent.tsx
More file actions
61 lines (49 loc) · 1.46 KB
/
Component.tsx
File metadata and controls
61 lines (49 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { DocSearch } from '@docsearch/react'
import { usePathname } from 'next/navigation'
import React from 'react'
import classes from './index.module.scss'
function getVersionedUrl(url: string, path: string): string {
if (!url) return url
const isV2 = path.includes('/docs/v2/')
if (isV2 && !url.includes('/docs/v2/')) {
return url.replace('/docs/', '/docs/v2/')
}
if (!isV2 && url.includes('/docs/v2/')) {
return url.replace('/docs/v2/', '/docs/')
}
return url
}
function Hit({ children, hit, path }: { children: React.ReactNode; hit: any; path: string }) {
const blog = hit?.url?.includes('/blog/') || false
const url = getVersionedUrl(hit?.url, path)
return (
<a className={blog ? classes.blogResult : ''} href={url}>
{children}
</a>
)
}
function Component() {
const path = usePathname()
const isV2 = path.includes('/docs/v2/')
return (
<div className={classes.searchWrapper}>
<DocSearch
apiKey={process.env.NEXT_PUBLIC_ALGOLIA_DOCSEARCH_KEY || ''}
appId="9MJY7K9GOW"
hitComponent={({ children, hit }) => Hit({ children, hit, path })}
indexName="payloadcms"
searchParameters={
isV2
? { facetFilters: ['version:v2'] }
: { facetFilters: ['version:v3'] }
}
/>
{isV2 && (
<div className={classes.versionBadge}>
{isV2 ? 'Searching in v2 docs' : 'Searching in v3 docs'}
</div>
)}
</div>
)
}
export default Component