Skip to content

Commit 1e276ab

Browse files
committed
HOTFIX: keep search bar state when results show up
During our React Router v6 upgrade (#1099), I unintentionally created a situation where the search bar owuld be blown away and its state (and your current query) would be lost when the search actually runs and results are shown. This solves the issue, although it takes some hacky code and makes it hackier still. :(
1 parent 9ae411d commit 1e276ab

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

src/components/web-monitoring-ui.jsx

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,39 @@ const api = new WebMonitoringDb({
1717
});
1818

1919
const localApi = new WebMonitoringApi(api);
20+
21+
// TODO: this messy page list stuff should really be turned into a context
22+
// for more understandable code.
23+
const withPages = (ComponentType) => {
24+
const wrapper = ({ pages, loadPages, ...props }) => {
25+
useEffect(() => {
26+
if (!pages) {
27+
loadPages();
28+
}
29+
});
30+
31+
return <ComponentType
32+
pages={pages}
33+
{...props}
34+
/>;
35+
};
36+
wrapper.displayName = `${ComponentType.displayName || ComponentType.name}WithPages`;
37+
return wrapper;
38+
};
39+
// Dumb function wrapper since we have mostly class components and React
40+
// Router now pretty much requires hooks.
41+
const withUrlParams = (ComponentType) => {
42+
const wrapper = (props) => {
43+
const urlParams = useParams();
44+
const navigate = useNavigate();
45+
return <ComponentType navigate={navigate} urlParams={urlParams} {...props} />;
46+
};
47+
wrapper.displayName = `${ComponentType.displayName || ComponentType.name}WithUrlParams`;
48+
return wrapper;
49+
};
50+
const PageListWithLoading = withUrlParams(withPages(PageList));
51+
const PageDetailsWithParams = withUrlParams(PageDetails);
52+
2053
/**
2154
* WebMonitoringUi represents the root container for the app. It also maintains
2255
* a top-level lsit of pages to share across the app. We do this here instead
@@ -136,38 +169,6 @@ export default class WebMonitoringUi extends Component {
136169
return this.renderLoginDialog();
137170
}
138171

139-
// TODO: this messy page list stuff should really be turned into a context
140-
// for more understandable code.
141-
const withPages = (ComponentType) => {
142-
const wrapper = ({ pages, ...props }) => {
143-
useEffect(() => {
144-
if (!pages) {
145-
this.loadPages();
146-
}
147-
});
148-
149-
return <ComponentType
150-
pages={pages}
151-
{...props}
152-
/>;
153-
};
154-
wrapper.displayName = `${ComponentType.displayName || ComponentType.name}WithPages`;
155-
return wrapper;
156-
};
157-
// Dumb function wrapper since we have mostly class components and React
158-
// Router now pretty much requires hooks.
159-
const withUrlParams = (ComponentType) => {
160-
const wrapper = (props) => {
161-
const urlParams = useParams();
162-
const navigate = useNavigate();
163-
return <ComponentType navigate={navigate} urlParams={urlParams} {...props} />;
164-
};
165-
wrapper.displayName = `${ComponentType.displayName || ComponentType.name}WithUrlParams`;
166-
return wrapper;
167-
};
168-
const PageListWithLoading = withUrlParams(withPages(PageList));
169-
const PageDetailsWithParams = withUrlParams(PageDetails);
170-
171172
const modal = this.state.showLogin ? this.renderLoginDialog() : null;
172173

173174
return (
@@ -189,6 +190,7 @@ export default class WebMonitoringUi extends Component {
189190
element={
190191
<PageListWithLoading
191192
pages={this.state.pages}
193+
loadPages={this.loadPages}
192194
user={this.state.user}
193195
onSearch={this.search}
194196
/>

0 commit comments

Comments
 (0)