Skip to content

Commit 08034fb

Browse files
committed
fix: Add base field to search worker api
1 parent d484f03 commit 08034fb

File tree

14 files changed

+127
-122
lines changed

14 files changed

+127
-122
lines changed

src/components/App/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export function App(props: DocInnerProps): ReactElement {
134134
return (
135135
<div className="App">
136136
<ThemeProvider theme={theme} direction={direction}>
137-
<LangProvider value={lang}>
137+
<LangProvider value={fixedLang}>
138138
<RouterProvider value={router}>
139139
<SearchProvider value={search}>
140140
<InterfaceProvider interface={viewerInterface || {}}>

src/components/Router/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import type {Router} from '@diplodoc/components';
33
import {createContext, useContext} from 'react';
44

55
export interface RouterConfig extends Router {
6-
depth: number;
6+
base: string;
77
}
88

99
export const RouterContext = createContext<RouterConfig>({
1010
pathname: '/',
11-
depth: 0,
11+
base: './',
1212
});
1313

1414
RouterContext.displayName = 'RouterContext';
File renamed without changes.

src/components/Search/Search.tsx renamed to src/components/Search/Page.tsx

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import block from 'bem-cn-lite';
77
import {useRouter} from '../Router';
88

99
import {useProvider} from './useProvider';
10-
import './Search.scss';
10+
import './Page.scss';
1111

1212
const b = block('Search');
1313

@@ -20,25 +20,35 @@ function getUrlParams() {
2020
}
2121

2222
function setUrlParams(query: string, page: number) {
23-
const params = new URLSearchParams();
24-
if (query) params.set('query', query);
25-
if (page > 1) params.set('page', String(page));
26-
window.history.pushState({}, '', `?${params.toString()}`);
23+
const url = new URL(window.location.href);
24+
25+
if (query) {
26+
url.searchParams.set('query', query);
27+
}
28+
29+
if (page > 1) {
30+
url.searchParams.set('page', String(page));
31+
}
32+
33+
window.history.pushState({}, '', url);
2734
}
2835

2936
function formatResults(searchResults: SearchResultData[]): FormattedSearchResultData {
30-
if (!Array.isArray(searchResults)) return [];
37+
if (!Array.isArray(searchResults)) {
38+
return [];
39+
}
40+
3141
return searchResults.map((result) => ({
32-
title: result?.title || result?.hierarchy?.lvl0 || result?.hierarchy?.lvl1 || '',
33-
url: result?.url || result?.link || '#',
34-
description: result?.description || result?.content || result?.text || '',
35-
section: result?.section || result?.hierarchy?.lvl1 || '',
42+
title: result?.title || '',
43+
url: result?.link || '#',
44+
description: result?.description || '',
45+
section: result?.section || '',
3646
}));
3747
}
3848

3949
const ITEMS_PER_PAGE = 10;
4050

41-
export const Search: React.FC = () => {
51+
export const Page: React.FC = () => {
4252
const provider = useProvider();
4353
const router = useRouter();
4454

src/components/Search/index.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/components/Search/index.tsx

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import type {ReactElement} from 'react';
2+
import type {SearchConfig, WorkerApi, WorkerConfig} from './types';
3+
import type {DocInnerProps} from '../App';
4+
5+
import React, {createContext, useContext, useEffect} from 'react';
6+
import {ThemeProvider} from '@gravity-ui/uikit';
7+
import {ConsentPopup, Lang, SUPPORTED_LANGS, configure} from '@diplodoc/components';
8+
9+
import {RouterProvider} from '../Router';
10+
import {LangProvider} from '../../hooks/useLang';
11+
import {getDirection, updateRootClassName, updateThemeClassName} from '../../utils';
12+
import {useSettings} from '../App/useSettings';
13+
import {useMobile} from '../App/useMobile';
14+
15+
import {Page} from './Page';
16+
17+
export type {SearchConfig, WorkerConfig, WorkerApi};
18+
19+
export const SearchContext = createContext<SearchConfig | null | undefined>(null);
20+
21+
SearchContext.displayName = 'SearchContext';
22+
23+
export const SearchProvider = SearchContext.Provider;
24+
25+
export function Search(props: DocInnerProps): ReactElement {
26+
const {lang, search, router, analytics} = props;
27+
const settings = useSettings();
28+
const mobileView = useMobile();
29+
const fixedLang = SUPPORTED_LANGS.includes(lang) ? lang : Lang.En;
30+
const direction = getDirection(lang);
31+
32+
configure({
33+
lang: fixedLang,
34+
localeCode: fixedLang,
35+
});
36+
37+
const {theme, wideFormat} = settings;
38+
39+
useEffect(() => {
40+
updateRootClassName({mobileView, wideFormat});
41+
updateThemeClassName({theme});
42+
}, [theme, mobileView, wideFormat]);
43+
44+
return (
45+
<ThemeProvider theme={theme} direction={direction}>
46+
<LangProvider value={fixedLang}>
47+
<RouterProvider value={router}>
48+
<SearchProvider value={search}>
49+
<Page />
50+
{analytics && (
51+
<ConsentPopup
52+
router={router}
53+
gtmId={analytics?.gtm?.id || ''}
54+
consentMode={analytics?.gtm?.mode}
55+
/>
56+
)}
57+
</SearchProvider>
58+
</RouterProvider>
59+
</LangProvider>
60+
</ThemeProvider>
61+
);
62+
}
63+
64+
export function useSearch() {
65+
return useContext(SearchContext);
66+
}

src/components/Search/provider/index.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import type {ISearchProvider, ISearchResult} from '@diplodoc/components';
22
import type {SearchConfig, SearchProviderExtended, WorkerConfig} from '../types';
33

4-
import {buildSearchLink} from './utils/searchLink';
5-
64
export class DefaultSearchProvider implements ISearchProvider, SearchProviderExtended {
75
private worker!: Promise<Worker>;
86

@@ -15,7 +13,6 @@ export class DefaultSearchProvider implements ISearchProvider, SearchProviderExt
1513
init = () => {
1614
this.worker = initWorker({
1715
...this.config,
18-
base: this.base,
1916
mark: 'Suggest__Item__Marker',
2017
});
2118
};
@@ -36,11 +33,22 @@ export class DefaultSearchProvider implements ISearchProvider, SearchProviderExt
3633
}) as Promise<{items: ISearchResult[]; total: number}>;
3734
}
3835

39-
link = (query: string, page = 1) => buildSearchLink(this.base, this.config, query, page);
36+
link = (query: string, page = 1) => {
37+
const searchParams = new URLSearchParams();
4038

41-
private get base() {
42-
return window.location.href.split('/').slice(0, -this.config.depth).join('/');
43-
}
39+
if (query) {
40+
searchParams.set('query', query);
41+
}
42+
43+
if (page > 1) {
44+
searchParams.set('page', page.toString());
45+
}
46+
47+
const params = searchParams.toString() ? `?${searchParams.toString()}` : '';
48+
const link = `${this.config.link}${params}`;
49+
50+
return link;
51+
};
4452

4553
private async request(message: object) {
4654
return request(await this.worker, message);

src/components/Search/provider/utils/searchLink.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/components/Search/types.ts

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,19 @@
11
import type {ISearchResult, SearchSuggestPageItem} from '@diplodoc/components';
22

3-
export interface AlgoliaQuerySettings {
4-
hitsPerPage?: number;
5-
facetFilters?: string[];
6-
attributesToRetrieve?: string[];
7-
attributesToHighlight?: string[];
8-
[key: string]: unknown;
9-
}
10-
113
export interface SearchData {
124
api: string;
135
link: string;
146
lang: string;
15-
provider?: 'local' | 'algolia';
167
}
178

189
export interface SearchConfig extends SearchData {
19-
depth: number;
20-
appId?: string;
21-
indexName?: string;
22-
searchKey?: string;
23-
querySettings?: AlgoliaQuerySettings;
24-
search?: SearchData;
10+
base: string;
2511
}
2612

2713
export interface WorkerConfig {
2814
api?: string;
2915
base: string;
3016
mark: string;
31-
provider?: 'local' | 'algolia';
32-
33-
appId?: string;
34-
indexName?: string;
35-
searchKey?: string;
36-
querySettings?: {
37-
[key: string]: unknown;
38-
};
3917
}
4018

4119
export interface SearchResultItem {
@@ -93,13 +71,7 @@ export interface SearchResultData extends Partial<ISearchResult> {
9371
url?: string;
9472
link?: string;
9573
description?: string;
96-
content?: string;
97-
text?: string;
9874
section?: string;
99-
hierarchy?: {
100-
lvl0?: string;
101-
lvl1?: string;
102-
};
10375
}
10476

10577
export interface FormattedSearchResultItem {

src/components/Search/useProvider.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import type {ISearchProvider} from '@diplodoc/components';
22

3-
import {useContext, useEffect, useMemo, useState} from 'react';
3+
import {useEffect, useMemo, useState} from 'react';
44

5-
import {RouterContext, SearchContext} from '../index';
5+
import {useRouter, useSearch} from '../index';
66
import {useLang} from '../../hooks/useLang';
77

88
import {createProvider} from './provider';
99

1010
export function useProvider() {
1111
const lang = useLang();
12-
const {depth = 0} = useContext(RouterContext);
13-
const search = useContext(SearchContext);
12+
const {base = './'} = useRouter();
13+
const search = useSearch();
1414
const [provider, setProvider] = useState<ISearchProvider | null>(null);
1515

1616
const config = useMemo(() => {
@@ -20,10 +20,10 @@ export function useProvider() {
2020

2121
return {
2222
...search,
23-
depth,
23+
base,
2424
lang,
2525
};
26-
}, [lang, depth, search]);
26+
}, [lang, base, search]);
2727

2828
useEffect(() => {
2929
if (config) {

0 commit comments

Comments
 (0)