-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathindex.tsx
More file actions
142 lines (117 loc) · 4.04 KB
/
index.tsx
File metadata and controls
142 lines (117 loc) · 4.04 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
'use client';
import { OramaSearchBox, OramaSearchButton } from '@orama/react-components';
import { useTranslations, useLocale } from 'next-intl';
import { useTheme } from 'next-themes';
import type { FC } from 'react';
import { useRouter } from '@/navigation.mjs';
import {
ORAMA_CLOUD_ENDPOINT,
ORAMA_CLOUD_API_KEY,
DEFAULT_ORAMA_QUERY_PARAMS,
DEFAULT_ORAMA_SUGGESTIONS,
BASE_URL,
} from '@/next.constants.mjs';
type ResultMapDescription = {
path: string;
pageSectionTitle: string;
};
type ResultMapPath = { path: string; siteSection: string };
import { themeConfig } from './utils';
const uppercaseFirst = (word: string) =>
word.charAt(0).toUpperCase() + word.slice(1);
const getFormattedPath = (path: string, title: string) =>
`${path
.replace(/#.+$/, '')
.split('/')
.map(element => element.replaceAll('-', ' '))
.map(element => uppercaseFirst(element))
.filter(Boolean)
.join(' > ')} — ${title}`;
const SearchButton: FC = () => {
const { resolvedTheme } = useTheme();
const t = useTranslations();
const locale = useLocale();
const colorScheme = resolvedTheme as 'light' | 'dark';
const router = useRouter();
const sourceMap = {
title: 'pageSectionTitle',
description: 'formattedPath',
path: 'path',
};
const resultMap = {
...sourceMap,
description: ({ path, pageSectionTitle }: ResultMapDescription) =>
getFormattedPath(path, pageSectionTitle),
path: ({ path, siteSection }: ResultMapPath) =>
siteSection.toLowerCase() === 'docs' ? `/${path}` : `/${locale}/${path}`,
section: 'siteSection',
};
return (
<>
<OramaSearchButton
style={{ flexGrow: 1 }}
colorScheme={colorScheme}
themeConfig={themeConfig}
aria-label={t('components.search.searchBox.placeholder')}
>
{t('components.search.searchBox.placeholder')}
</OramaSearchButton>
<OramaSearchBox
index={{ api_key: ORAMA_CLOUD_API_KEY, endpoint: ORAMA_CLOUD_ENDPOINT }}
colorScheme={colorScheme}
themeConfig={themeConfig}
sourceBaseUrl={BASE_URL}
sourcesMap={sourceMap}
resultMap={resultMap}
facetProperty="siteSection"
linksTarget="_self"
highlightTitle={{
caseSensitive: false,
HTMLTag: 'b',
CSSClass: 'font-bold',
}}
searchParams={DEFAULT_ORAMA_QUERY_PARAMS}
suggestions={DEFAULT_ORAMA_SUGGESTIONS}
chatMarkdownLinkHref={({ href }) => {
if (!href) {
return href;
}
const baseURLObject = new URL(BASE_URL);
const baseURLHostName = baseURLObject.hostname;
const searchBoxURLObject = new URL(href);
const searchBoxURLHostName = searchBoxURLObject.hostname;
const serachBoxURLPathName = searchBoxURLObject.pathname;
// We do not want to add the locale to the url for external links and docs links
if (
baseURLHostName !== searchBoxURLHostName ||
serachBoxURLPathName.startsWith('/docs/')
) {
return href;
}
const URLWithLocale = new URL(
`${locale}${searchBoxURLObject.pathname}`,
searchBoxURLObject.origin
);
return URLWithLocale.href;
}}
onAnswerSourceClick={event => {
event.preventDefault();
const baseURLObject = new URL(BASE_URL);
const { path } = event.detail.source;
const finalPath = path.startsWith('docs/')
? path
: `${locale}/${path}`;
const finalURL = new URL(finalPath, baseURLObject);
window.open(finalURL, '_blank');
}}
onSearchResultClick={event => {
event.preventDefault();
const fullURLObject = new URL(event.detail.result.path, BASE_URL);
// result.path already contains LOCALE. Locale is set to undefined here so router does not add it once again.
router.push(fullURLObject.href, { locale: undefined });
}}
/>
</>
);
};
export default SearchButton;