Skip to content

Commit 168b5e6

Browse files
committed
refactor: /app/pages/Search/index.js
1 parent f3dd9a4 commit 168b5e6

File tree

1 file changed

+86
-84
lines changed
  • packages/app/src/app/pages/Search

1 file changed

+86
-84
lines changed

packages/app/src/app/pages/Search/index.js

Lines changed: 86 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
} from 'react-instantsearch/dom';
1717

1818
import { Navigation } from 'app/pages/common/Navigation';
19-
import { inject, hooksObserver } from 'app/componentConnectors';
19+
import { useOvermind } from 'app/overmind';
2020

2121
import 'instantsearch.css/themes/reset.css';
2222

@@ -32,89 +32,91 @@ const createURL = state => `?${qs.stringify(state)}`;
3232
const searchStateToUrl = (location, searchState) =>
3333
searchState ? `${location.pathname}${createURL(searchState)}` : '';
3434

35-
const Search = inject('signals')(
36-
hooksObserver(({ history, location, signals: { searchMounted } }) => {
37-
const [searchState, setSearchState] = useState(
38-
qs.parse(location.search.slice(1))
39-
);
40-
const debouncedSetState = useRef(null);
41-
42-
useEffect(() => {
43-
searchMounted();
44-
}, [searchMounted]);
45-
46-
useEffect(() => {
47-
const unlisten = history.listen((loc, action) => {
48-
if (['POP', 'PUSH'].includes(action)) {
49-
setSearchState(qs.parse(loc.search.slice(1)));
50-
}
51-
52-
return unlisten;
53-
});
54-
}, [history]);
55-
56-
const onSearchStateChange = useCallback(
57-
newSearchState => {
58-
clearTimeout(debouncedSetState.current);
59-
60-
debouncedSetState.current = setTimeout(() => {
61-
history.push(
62-
searchStateToUrl(location, newSearchState),
63-
newSearchState
64-
);
65-
}, updateAfter);
66-
67-
setSearchState(newSearchState);
68-
},
69-
[history, location]
70-
);
71-
72-
return (
73-
<Container>
74-
<Helmet>
75-
<title>Search - CodeSandbox</title>
76-
</Helmet>
77-
<Styles />
78-
79-
<MaxWidth>
80-
<Margin vertical={1.5}>
81-
<Navigation title="Search" searchNoInput />
82-
83-
<Content>
84-
<InstantSearch
85-
appId={ALGOLIA_APPLICATION_ID}
86-
apiKey={ALGOLIA_API_KEY}
87-
createURL={createURL}
88-
indexName={ALGOLIA_DEFAULT_INDEX}
89-
onSearchStateChange={onSearchStateChange}
90-
searchState={searchState}
91-
>
92-
<Configure hitsPerPage={12} />
93-
94-
<Main alignItems="flex-start">
95-
<div>
96-
<StyledTitle>Search</StyledTitle>
97-
98-
<PoweredBy />
99-
100-
<SearchBox
101-
autoFocus
102-
translations={{ placeholder: 'Search Sandboxes...' }}
103-
/>
104-
105-
<Results />
106-
</div>
107-
108-
<Filters />
109-
</Main>
110-
</InstantSearch>
111-
</Content>
112-
</Margin>
113-
</MaxWidth>
114-
</Container>
115-
);
116-
})
117-
);
35+
const Search = ({ history, location }) => {
36+
const {
37+
actions: { searchMounted },
38+
} = useOvermind();
39+
40+
const [searchState, setSearchState] = useState(
41+
qs.parse(location.search.slice(1))
42+
);
43+
const debouncedSetState = useRef(null);
44+
45+
useEffect(() => {
46+
searchMounted();
47+
}, [searchMounted]);
48+
49+
useEffect(() => {
50+
const unlisten = history.listen((loc, action) => {
51+
if (['POP', 'PUSH'].includes(action)) {
52+
setSearchState(qs.parse(loc.search.slice(1)));
53+
}
54+
55+
return unlisten;
56+
});
57+
}, [history]);
58+
59+
const onSearchStateChange = useCallback(
60+
newSearchState => {
61+
clearTimeout(debouncedSetState.current);
62+
63+
debouncedSetState.current = setTimeout(() => {
64+
history.push(
65+
searchStateToUrl(location, newSearchState),
66+
newSearchState
67+
);
68+
}, updateAfter);
69+
70+
setSearchState(newSearchState);
71+
},
72+
[history, location]
73+
);
74+
75+
return (
76+
<Container>
77+
<Helmet>
78+
<title>Search - CodeSandbox</title>
79+
</Helmet>
80+
<Styles />
81+
82+
<MaxWidth>
83+
<Margin vertical={1.5}>
84+
<Navigation title="Search" searchNoInput />
85+
86+
<Content>
87+
<InstantSearch
88+
appId={ALGOLIA_APPLICATION_ID}
89+
apiKey={ALGOLIA_API_KEY}
90+
createURL={createURL}
91+
indexName={ALGOLIA_DEFAULT_INDEX}
92+
onSearchStateChange={onSearchStateChange}
93+
searchState={searchState}
94+
>
95+
<Configure hitsPerPage={12} />
96+
97+
<Main alignItems="flex-start">
98+
<div>
99+
<StyledTitle>Search</StyledTitle>
100+
101+
<PoweredBy />
102+
103+
<SearchBox
104+
autoFocus
105+
translations={{ placeholder: 'Search Sandboxes...' }}
106+
/>
107+
108+
<Results />
109+
</div>
110+
111+
<Filters />
112+
</Main>
113+
</InstantSearch>
114+
</Content>
115+
</Margin>
116+
</MaxWidth>
117+
</Container>
118+
);
119+
};
118120

119121
// eslint-disable-next-line import/no-default-export
120122
export default Search;

0 commit comments

Comments
 (0)