Skip to content

Commit e2cee95

Browse files
committed
fix: remove throw error
1 parent c7a49ea commit e2cee95

File tree

6 files changed

+20
-22
lines changed

6 files changed

+20
-22
lines changed

apps/site/components/Common/Searchbox/EmptyResults/index.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import styles from './index.module.css';
1212
export const EmptyResults: FC = () => {
1313
const t = useTranslations();
1414
const searchbox = useSearchbox();
15-
const isSearchMode = searchbox.mode === 'search';
15+
const isSearchMode = searchbox?.mode === 'search';
1616

1717
return (
1818
<SearchResults.NoResults>
@@ -30,7 +30,7 @@ export const EmptyResults: FC = () => {
3030
{t('components.search.suggestions')}
3131
</p>
3232
<Suggestions.Item
33-
onClick={() => searchbox.switchTo('chat')}
33+
onClick={() => searchbox?.switchTo('chat')}
3434
tabIndex={isSearchMode ? 0 : -1}
3535
aria-hidden={!isSearchMode}
3636
className={styles.suggestionItem}
@@ -39,7 +39,7 @@ export const EmptyResults: FC = () => {
3939
{t('components.search.suggestionOne')}
4040
</Suggestions.Item>
4141
<Suggestions.Item
42-
onClick={() => searchbox.switchTo('chat')}
42+
onClick={() => searchbox?.switchTo('chat')}
4343
tabIndex={isSearchMode ? 0 : -1}
4444
aria-hidden={!isSearchMode}
4545
className={styles.suggestionItem}
@@ -50,7 +50,7 @@ export const EmptyResults: FC = () => {
5050
<Suggestions.Item
5151
tabIndex={isSearchMode ? 0 : -1}
5252
aria-hidden={!isSearchMode}
53-
onClick={() => searchbox.switchTo('chat')}
53+
onClick={() => searchbox?.switchTo('chat')}
5454
className={styles.suggestionItem}
5555
>
5656
<SparklesIcon />

apps/site/components/Common/Searchbox/InnerSearchboxModal/index.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const InnerSearchboxModal: FC<PropsWithChildren> = () => {
1818
const searchInputRef = useRef<HTMLInputElement>(null);
1919

2020
const displaySearch =
21-
!isMobileScreen || (isMobileScreen && searchbox.mode === 'search');
21+
!isMobileScreen || (isMobileScreen && searchbox?.mode === 'search');
2222

2323
useEffect(() => {
2424
const checkScreenSize = () => {
@@ -35,12 +35,12 @@ export const InnerSearchboxModal: FC<PropsWithChildren> = () => {
3535
<>
3636
{isMobileScreen && (
3737
<MobileTopBar
38-
isChatOpen={searchbox.mode === 'chat'}
39-
onSelect={searchbox.switchTo}
38+
isChatOpen={searchbox?.mode === 'chat'}
39+
onSelect={searchbox?.switchTo}
4040
/>
4141
)}
4242
{displaySearch && <Search ref={searchInputRef} />}
43-
{isMobileScreen && searchbox.mode === 'chat' && (
43+
{isMobileScreen && searchbox?.mode === 'chat' && (
4444
<>
4545
<div className={styles.mobileChatContainer}>
4646
<div className={styles.mobileChatTop}>
@@ -53,11 +53,11 @@ export const InnerSearchboxModal: FC<PropsWithChildren> = () => {
5353
<Footer />
5454
</>
5555
)}
56-
{!isMobileScreen && searchbox.mode === 'chat' && (
56+
{!isMobileScreen && searchbox?.mode === 'chat' && (
5757
<SlidingChatPanel
58-
open={searchbox.isChatOpen}
58+
open={searchbox?.isChatOpen}
5959
onClose={() => {
60-
searchbox.closeChatAndReset(() => {
60+
searchbox?.closeChatAndReset(() => {
6161
searchInputRef.current?.focus();
6262
});
6363
}}

apps/site/components/Common/Searchbox/MobileTopBar/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ import styles from './index.module.css';
1616

1717
export const MobileTopBar: FC<{
1818
isChatOpen: boolean;
19-
onSelect: (mode: 'search' | 'chat') => void;
19+
onSelect?: (mode: 'search' | 'chat') => void;
2020
}> = ({ isChatOpen, onSelect }) => {
2121
const [animated, setAnimated] = useState(false);
2222

2323
function selectMode(mode: 'search' | 'chat') {
24-
onSelect(mode);
24+
onSelect?.(mode);
2525

2626
if (!animated) {
2727
setAnimated(true);

apps/site/components/Common/Searchbox/Search/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type SearchProps = PropsWithChildren & React.RefAttributes<HTMLInputElement>;
1717
export const Search: FC<SearchProps> = ({ ref }) => {
1818
const t = useTranslations();
1919
const searchbox = useSearchbox();
20-
const isSearchMode = searchbox.mode === 'search';
20+
const isSearchMode = searchbox?.mode === 'search';
2121

2222
return (
2323
<div className={styles.searchContainer}>

apps/site/components/Common/Searchbox/SearchResults/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ export const SearchResultsWrapper: FC = () => {
2121
context: { searchTerm, selectedFacet },
2222
} = useSearch();
2323
const searchbox = useSearchbox();
24-
const isSearchMode = searchbox.mode === 'search';
24+
const isSearchMode = searchbox?.mode === 'search';
2525

2626
return (
2727
<div className={styles.searchResultsContainer}>
2828
<div className={styles.chatButtonWrapper}>
2929
<SlidingPanel.Trigger
30-
onClick={() => searchbox.switchTo('chat')}
30+
onClick={() => searchbox?.switchTo('chat')}
3131
className={classNames(styles.chatButton, {
3232
[styles.chatButtonWithSearch]: searchTerm,
3333
})}

apps/site/providers/searchboxProvider.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import searchboxReducer, {
99
} from '#site/reducers/searchboxReducer';
1010
import type * as Types from '#site/types/searchbox';
1111

12-
type SearchboxContextType = Types.SearchboxState &
13-
Types.SearchboxDispatchActions;
12+
type SearchboxContextType =
13+
| (Types.SearchboxState & Types.SearchboxDispatchActions)
14+
| null;
1415

15-
const SearchboxContext = createContext<SearchboxContextType | null>(null);
16+
const SearchboxContext = createContext<SearchboxContextType>(null);
1617

1718
export const SearchboxProvider: FC<PropsWithChildren> = ({ children }) => {
1819
const [state, dispatch] = useReducer(searchboxReducer, searchboxState);
@@ -32,8 +33,5 @@ export const SearchboxProvider: FC<PropsWithChildren> = ({ children }) => {
3233

3334
export const useSearchbox = (): SearchboxContextType => {
3435
const context = useContext(SearchboxContext);
35-
if (!context) {
36-
throw new Error('useSearchbox must be used within a SearchboxProvider');
37-
}
3836
return context;
3937
};

0 commit comments

Comments
 (0)