Skip to content

Commit 9cb58f6

Browse files
committed
refactor: clean up modal state
1 parent 46f2124 commit 9cb58f6

File tree

7 files changed

+28
-88
lines changed

7 files changed

+28
-88
lines changed

apps/site/components/Common/Searchbox/InnerSearchboxModal/index.module.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@reference "../../../../styles/index.css";
2+
13
.mobileChatContainer {
24
@apply flex
35
grow

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@ import { useSearchbox } from '#site/providers/searchboxProvider';
77
import { ChatInput } from '../ChatInput';
88
import { ChatInteractionsContainer } from '../ChatInteractions';
99
import { Footer } from '../Footer';
10-
import styles from '../index.module.css';
10+
import styles from './index.module.css';
1111
import { MobileTopBar } from '../MobileTopBar';
1212
import { Search } from '../Search';
1313
import { SlidingChatPanel } from '../SlidingChatPanel';
1414

15-
export const InnerSearchboxModal: FC<
16-
PropsWithChildren<{ onClose: () => void }>
17-
> = ({ onClose }) => {
15+
export const InnerSearchboxModal: FC<PropsWithChildren> = () => {
1816
const searchbox = useSearchbox();
1917
const [isMobileScreen, setIsMobileScreen] = useState(false);
2018
const searchInputRef = useRef<HTMLInputElement>(null);
@@ -38,7 +36,6 @@ export const InnerSearchboxModal: FC<
3836
{isMobileScreen && (
3937
<MobileTopBar
4038
isChatOpen={searchbox.mode === 'chat'}
41-
onClose={onClose}
4239
onSelect={searchbox.switchTo}
4340
/>
4441
)}

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
ArrowLeftIcon,
66
SparklesIcon,
77
} from '@heroicons/react/24/solid';
8+
import { Modal } from '@orama/ui/components/Modal';
89
import classNames from 'classnames';
910
import type { FC } from 'react';
1011
import { useState } from 'react';
@@ -15,9 +16,8 @@ import styles from './index.module.css';
1516

1617
export const MobileTopBar: FC<{
1718
isChatOpen: boolean;
18-
onClose: () => void;
1919
onSelect: (mode: 'search' | 'chat') => void;
20-
}> = ({ isChatOpen, onClose, onSelect }) => {
20+
}> = ({ isChatOpen, onSelect }) => {
2121
const [animated, setAnimated] = useState(false);
2222

2323
function selectMode(mode: 'search' | 'chat') {
@@ -30,13 +30,12 @@ export const MobileTopBar: FC<{
3030

3131
return (
3232
<div className={styles.topBar}>
33-
<button
33+
<Modal.Close
3434
className={styles.topBarArrow}
35-
onClick={onClose}
36-
aria-label="Close"
35+
aria-label="Close search modal"
3736
>
3837
<ArrowLeftIcon />
39-
</button>
38+
</Modal.Close>
4039
<div className={styles.topBarTabs}>
4140
<button
4241
className={classNames(styles.topBarTab, {

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

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,17 @@
33
import { MagnifyingGlassIcon } from '@heroicons/react/24/solid';
44
import { SearchRoot, ChatRoot, Modal } from '@orama/ui/components';
55
import { useTranslations } from 'next-intl';
6-
import type { FC, PropsWithChildren } from 'react';
6+
import type { FC } from 'react';
77

88
import '@orama/ui/styles.css';
99

1010
import { OramaProvider, useOrama } from '#site/providers/oramaProvider';
11-
import {
12-
SearchboxProvider,
13-
useSearchbox,
14-
} from '#site/providers/searchboxProvider';
11+
import { SearchboxProvider } from '#site/providers/searchboxProvider';
1512

1613
import styles from './index.module.css';
1714
import { InnerSearchboxModal } from './InnerSearchboxModal';
1815

1916
const Searchbox: FC = () => {
20-
const searchbox = useSearchbox();
2117
const orama = useOrama();
2218
const t = useTranslations();
2319

@@ -26,8 +22,6 @@ const Searchbox: FC = () => {
2622
<Modal.Root>
2723
<Modal.Trigger
2824
type="button"
29-
data-testid="orama-button"
30-
onClick={searchbox.toggleModal}
3125
disabled={!orama}
3226
enableCmdK
3327
className={styles.searchButton}
@@ -40,41 +34,34 @@ const Searchbox: FC = () => {
4034
</Modal.Trigger>
4135

4236
<Modal.Wrapper
43-
open={searchbox.isOpen}
44-
onModalClosed={searchbox.closeModal}
4537
closeOnOutsideClick
4638
closeOnEscape
4739
className={styles.modalWrapper}
4840
>
49-
<Modal.Inner className={styles.modalInner}>
50-
<Modal.Content className={styles.modalContent}>
51-
<InnerSearchboxModal onClose={searchbox.closeModal} />
52-
</Modal.Content>
53-
</Modal.Inner>
41+
<SearchRoot client={orama ?? null}>
42+
<ChatRoot
43+
client={orama ?? null}
44+
askOptions={{ throttle_delay: 50 }}
45+
>
46+
<Modal.Inner className={styles.modalInner}>
47+
<Modal.Content className={styles.modalContent}>
48+
<InnerSearchboxModal />
49+
</Modal.Content>
50+
</Modal.Inner>
51+
</ChatRoot>
52+
</SearchRoot>
5453
</Modal.Wrapper>
5554
</Modal.Root>
5655
</div>
5756
);
5857
};
5958

60-
const OramaSearch: FC<PropsWithChildren> = () => {
61-
const orama = useOrama();
62-
63-
return (
64-
<SearchRoot client={orama ?? null}>
65-
<ChatRoot client={orama ?? null} askOptions={{ throttle_delay: 50 }}>
66-
<SearchboxProvider>
67-
<Searchbox />
68-
</SearchboxProvider>
69-
</ChatRoot>
70-
</SearchRoot>
71-
);
72-
};
73-
7459
const OramaSearchWrapper = () => {
7560
return (
7661
<OramaProvider>
77-
<OramaSearch />
62+
<SearchboxProvider>
63+
<Searchbox />
64+
</SearchboxProvider>
7865
</OramaProvider>
7966
);
8067
};

apps/site/providers/searchboxProvider.tsx

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
'use client';
22

3-
import { useSearchDispatch } from '@orama/ui/contexts';
43
import type { FC, PropsWithChildren } from 'react';
5-
import {
6-
createContext,
7-
useContext,
8-
useEffect,
9-
useReducer,
10-
useCallback,
11-
} from 'react';
4+
import { createContext, useContext, useReducer } from 'react';
125

136
import searchboxReducer, {
147
searchboxState,
@@ -17,36 +10,17 @@ import searchboxReducer, {
1710
import type * as Types from '#site/types/searchbox';
1811

1912
type SearchboxContextType = Types.SearchboxState &
20-
Types.SearchboxDispatchActions & {
21-
clearAll: () => void;
22-
};
13+
Types.SearchboxDispatchActions;
2314

2415
const SearchboxContext = createContext<SearchboxContextType | null>(null);
2516

2617
export const SearchboxProvider: FC<PropsWithChildren> = ({ children }) => {
2718
const [state, dispatch] = useReducer(searchboxReducer, searchboxState);
2819
const actions = getActions(dispatch);
29-
const searchDispatch = useSearchDispatch();
30-
31-
const clearAll = useCallback(() => {
32-
searchDispatch({ type: 'SET_SEARCH_TERM', payload: { searchTerm: '' } });
33-
searchDispatch({
34-
type: 'SET_SELECTED_FACET',
35-
payload: { selectedFacet: 'All' },
36-
});
37-
searchDispatch({ type: 'SET_RESULTS', payload: { results: [] } });
38-
}, [searchDispatch]);
39-
40-
useEffect(() => {
41-
if (!state.isOpen) {
42-
clearAll();
43-
}
44-
}, [state.isOpen, clearAll]);
4520

4621
const contextValue: SearchboxContextType = {
4722
...state,
4823
...actions,
49-
clearAll,
5024
};
5125

5226
return (

apps/site/reducers/searchboxReducer.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import type { Dispatch } from 'react';
33
import type * as Types from '#site/types/searchbox';
44

55
export const searchboxState: Types.SearchboxState = {
6-
// The modal starts closed
7-
isOpen: false,
86
// Default mode is search
97
mode: 'search',
108
// Chat panel starts closed
@@ -14,9 +12,6 @@ export const searchboxState: Types.SearchboxState = {
1412
export const getActions = (
1513
dispatch: Dispatch<Types.SearchboxAction>
1614
): Types.SearchboxDispatchActions => ({
17-
openModal: () => dispatch({ type: 'OPEN_MODAL' }),
18-
closeModal: () => dispatch({ type: 'CLOSE_MODAL' }),
19-
toggleModal: () => dispatch({ type: 'TOGGLE_MODAL' }),
2015
setMode: payload => dispatch({ type: 'SET_MODE', payload }),
2116
setChatOpen: payload => dispatch({ type: 'SET_CHAT_OPEN', payload }),
2217
closeChatAndReset: (onComplete?: () => void) =>
@@ -32,12 +27,6 @@ const reducer = (
3227
action: Types.SearchboxAction
3328
): Types.SearchboxState => {
3429
switch (action.type) {
35-
case 'OPEN_MODAL':
36-
return { ...state, isOpen: true };
37-
case 'CLOSE_MODAL':
38-
return { ...state, isOpen: false };
39-
case 'TOGGLE_MODAL':
40-
return { ...state, isOpen: !state.isOpen };
4130
case 'SET_MODE':
4231
return { ...state, mode: action.payload };
4332
case 'SET_CHAT_OPEN':

apps/site/types/searchbox.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
export type SearchboxMode = 'search' | 'chat';
22

33
export type SearchboxState = {
4-
// Whether the search modal is open
5-
isOpen: boolean;
64
// The current mode (search or chat)
75
mode: SearchboxMode;
86
// Whether chat panel is open (for desktop)
97
isChatOpen: boolean;
108
};
119

1210
export type SearchboxAction =
13-
| { type: 'OPEN_MODAL' }
14-
| { type: 'CLOSE_MODAL' }
15-
| { type: 'TOGGLE_MODAL' }
1611
| { type: 'SET_MODE'; payload: SearchboxMode }
1712
| { type: 'SET_CHAT_OPEN'; payload: boolean }
1813
| {
@@ -22,9 +17,6 @@ export type SearchboxAction =
2217
| { type: 'SWITCH_TO'; payload: SearchboxMode };
2318

2419
export type SearchboxDispatchActions = {
25-
openModal: () => void;
26-
closeModal: () => void;
27-
toggleModal: () => void;
2820
setMode: (mode: SearchboxMode) => void;
2921
setChatOpen: (isOpen: boolean) => void;
3022
closeChatAndReset: (onComplete?: () => void) => void;

0 commit comments

Comments
 (0)