Skip to content

Commit 6e50a75

Browse files
authored
search: respect the stored platform on non platform pages (#12477)
* search: respect the stored platform on non platform pages
1 parent b8e429a commit 6e50a75

File tree

4 files changed

+57
-8
lines changed

4 files changed

+57
-8
lines changed

app/not-found.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ export default function NotFound() {
2424

2525
<div className="max-w-md pt-8">
2626
<p className="pb-4">Let's give it another shot:</p>
27-
<Search autoFocus path={pathname} searchPlatforms={[]} />
27+
<Search
28+
autoFocus
29+
path={pathname}
30+
searchPlatforms={[]}
31+
showChatBot={false}
32+
useStoredSearchPlatforms={false}
33+
/>
2834
</div>
2935
<div className="pt-8 flex gap-4">
3036
<Button variant="solid" size="3" asChild>

src/components/header.tsx

+13-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,15 @@ type Props = {
2020
pathname: string;
2121
searchPlatforms: string[];
2222
noSearch?: boolean;
23+
useStoredSearchPlatforms?: boolean;
2324
};
2425

25-
export function Header({pathname, searchPlatforms, noSearch}: Props) {
26+
export function Header({
27+
pathname,
28+
searchPlatforms,
29+
noSearch,
30+
useStoredSearchPlatforms,
31+
}: Props) {
2632
return (
2733
<header className="bg-[var(--gray-1)] h-[var(--header-height)] w-full z-50 border-b border-[var(--gray-a3)] fixed top-0">
2834
{/* define a header-height variable for consumption by other components */}
@@ -62,7 +68,12 @@ export function Header({pathname, searchPlatforms, noSearch}: Props) {
6268
</Link>
6369
{!noSearch && (
6470
<div className="hidden md:flex justify-center lg:justify-start w-full px-6">
65-
<Search path={pathname} searchPlatforms={searchPlatforms} />
71+
<Search
72+
path={pathname}
73+
searchPlatforms={searchPlatforms}
74+
showChatBot
75+
useStoredSearchPlatforms={useStoredSearchPlatforms}
76+
/>
6677
</div>
6778
)}
6879
<div className="hidden lg-xl:flex justify-end flex-1 space-x-2 items-center min-w-fit">

src/components/home.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {PlatformFilter} from './platformFilter';
2121
export function Home() {
2222
return (
2323
<div className="tw-app">
24-
<Header pathname="/" searchPlatforms={[]} />
24+
<Header pathname="/" searchPlatforms={[]} useStoredSearchPlatforms={false} />
2525
<div className="mt-[var(--header-height)]">
2626
<Banner />
2727
</div>

src/components/search/index.tsx

+36-4
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,50 @@ type Props = {
6262
autoFocus?: boolean;
6363
path?: string;
6464
searchPlatforms?: string[];
65+
showChatBot?: boolean;
66+
useStoredSearchPlatforms?: boolean;
6567
};
6668

67-
export function Search({path, autoFocus, searchPlatforms = []}: Props) {
69+
const STORAGE_KEY = 'sentry-docs-search-platforms';
70+
71+
export function Search({
72+
path,
73+
autoFocus,
74+
searchPlatforms = [],
75+
useStoredSearchPlatforms = true,
76+
}: Props) {
6877
const ref = useRef<HTMLDivElement>(null);
6978
const [query, setQuery] = useState(``);
7079
const [results, setResults] = useState([] as Result[]);
7180
const [inputFocus, setInputFocus] = useState(false);
7281
const [showOffsiteResults, setShowOffsiteResults] = useState(false);
7382
const [loading, setLoading] = useState(true);
74-
83+
const [currentSearchPlatforms, setCurrentSearchPlatforms] = useState(searchPlatforms);
7584
const pathname = usePathname();
7685

86+
// Load stored platforms on mount
87+
useEffect(() => {
88+
const storedPlatforms = localStorage.getItem(STORAGE_KEY) ?? '[]';
89+
if (!storedPlatforms) {
90+
localStorage.setItem(STORAGE_KEY, JSON.stringify(searchPlatforms));
91+
} else if (
92+
storedPlatforms &&
93+
searchPlatforms.length === 0 &&
94+
useStoredSearchPlatforms
95+
) {
96+
const platforms = JSON.parse(storedPlatforms);
97+
setCurrentSearchPlatforms(platforms);
98+
}
99+
}, [useStoredSearchPlatforms, searchPlatforms]);
100+
101+
// Update stored platforms when they change
102+
useEffect(() => {
103+
if (searchPlatforms.length > 0) {
104+
localStorage.setItem(STORAGE_KEY, JSON.stringify(searchPlatforms));
105+
setCurrentSearchPlatforms(searchPlatforms);
106+
}
107+
}, [searchPlatforms]);
108+
77109
const handleClickOutside = useCallback((ev: MouseEvent) => {
78110
// don't close the search results if the user is clicking the expand button
79111
if (
@@ -143,7 +175,7 @@ export function Search({path, autoFocus, searchPlatforms = []}: Props) {
143175
inputQuery,
144176
{
145177
path,
146-
platforms: searchPlatforms.map(
178+
platforms: currentSearchPlatforms.map(
147179
platform => standardSDKSlug(platform)?.slug ?? ''
148180
),
149181
searchAllIndexes: showOffsiteResults,
@@ -163,7 +195,7 @@ export function Search({path, autoFocus, searchPlatforms = []}: Props) {
163195
setResults(queryResults);
164196
}
165197
},
166-
[path, searchPlatforms, showOffsiteResults, loading]
198+
[path, currentSearchPlatforms, showOffsiteResults, loading]
167199
);
168200

169201
const totalHits = results.reduce((a, x) => a + x.hits.length, 0);

0 commit comments

Comments
 (0)