Skip to content

Commit 7e41704

Browse files
committed
Sync URL search params with search term
1 parent 1afb37e commit 7e41704

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

src/Elastic.Documentation.Site/Assets/web-components/SearchOrAskAi/SearchOrAskAiButton.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import {
1414
} from '@elastic/eui'
1515
import { css } from '@emotion/react'
1616
import * as React from 'react'
17-
import { useEffect, Suspense, lazy } from 'react'
17+
import { useEffect, Suspense, lazy, useState } from 'react'
18+
import { useSyncSearchParams } from "./useSyncURLSearchParams";
1819

1920
// Lazy load the modal component
2021
const SearchOrAskAiModal = lazy(() =>
@@ -25,9 +26,12 @@ const SearchOrAskAiModal = lazy(() =>
2526

2627
export const SearchOrAskAiButton = () => {
2728
const searchTerm = useSearchTerm()
28-
const { clearSearchTerm } = useSearchActions()
29+
const { clearSearchTerm } =
30+
useSearchActions()
2931
const isModalOpen = useModalIsOpen()
3032
const { openModal, closeModal, toggleModal } = useModalActions()
33+
34+
useSyncSearchParams();
3135

3236
const positionCss = css`
3337
position: absolute;
@@ -61,7 +65,6 @@ export const SearchOrAskAiButton = () => {
6165
window.removeEventListener('keydown', handleKeydown)
6266
}
6367
}, [])
64-
6568
return (
6669
<>
6770
<EuiButton

src/Elastic.Documentation.Site/Assets/web-components/SearchOrAskAi/SearchOrAskAiModal.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export const SearchOrAskAiModal = () => {
1717
const searchTerm = useSearchTerm()
1818
const askAiTerm = useAskAiTerm()
1919
const { setSearchTerm, submitAskAiTerm } = useSearchActions()
20-
2120
return (
2221
<div
2322
css={css`
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { useEffect, useState } from "react";
2+
import { useSearchActions, useSearchTerm } from "./search.store";
3+
import { useModalActions } from "./modal.store";
4+
5+
export const useSyncSearchParams = () => {
6+
const searchTerm = useSearchTerm()
7+
const { setSearchTerm, submitAskAiTerm } = useSearchActions()
8+
const { openModal } = useModalActions()
9+
10+
const searchQueryParamKey = 'q'
11+
const [isInitialized, setIsInitialized] = useState(false)
12+
useEffect(() => {
13+
const params = new URLSearchParams(window.location.search)
14+
const urlQuery = params.get(searchQueryParamKey)
15+
if (urlQuery) {
16+
setSearchTerm(urlQuery)
17+
submitAskAiTerm(urlQuery)
18+
}
19+
setIsInitialized(true)
20+
}, [])
21+
22+
useEffect(() => {
23+
if (!isInitialized) {
24+
return
25+
}
26+
const url = new URL(window.location)
27+
if (searchTerm) {
28+
url.searchParams.set(searchQueryParamKey, searchTerm)
29+
} else {
30+
url.searchParams.delete(searchQueryParamKey)
31+
}
32+
window.history.replaceState({}, '', url)
33+
}, [searchTerm, isInitialized])
34+
35+
useEffect(() => {
36+
const url = new URL(window.location)
37+
if (url.searchParams.has(searchQueryParamKey)) {
38+
openModal()
39+
}
40+
}, [])
41+
};

0 commit comments

Comments
 (0)