-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathSearchResults.tsx
More file actions
405 lines (355 loc) · 12.6 KB
/
SearchResults.tsx
File metadata and controls
405 lines (355 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import type { RMPInstructor } from "@utd-grades/db";
import { Col, Row } from "antd";
import debounce from "lodash.debounce";
import type { NextRouter } from "next/router";
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useQuery } from "react-query";
import { animateScroll as scroll } from "react-scroll";
import styled from "styled-components";
import type { SearchQuery } from "../types";
import { normalizeName } from "../utils/index";
import { useDb } from "../utils/useDb";
import Search from "./Search";
import SearchResultsContent from "./SearchResultsContent";
import { SectionList } from "./SectionList";
const Container = styled.div`
display: block;
position: relative;
`;
const ResultsContainer = styled(Col)`
padding-bottom: 20px;
margin-top: 35px;
border-radius: 5px;
& .ant-list-pagination {
padding-left: 10px;
}
& .ant-list-pagination li {
margin-bottom: 10px;
font-family: var(--font-family);
}
@media (max-width: 992px) {
& {
box-shadow: none;
}
}
@media (min-width: 992px) {
& {
box-shadow: 0 15px 50px rgba(233, 233, 233, 0.7);
}
}
`;
interface ResultsProps {
search: string;
sectionId: number;
router: NextRouter;
}
const Results = React.memo(function Results({ search, sectionId, router }: ResultsProps) {
// Track current page for SectionList pagination
const [currentPage, setCurrentPage] = useState(1);
const scrollRef = useRef<HTMLDivElement>(null);
const hasAutoSelected = useRef(false);
const { data: db } = useDb();
// this is to get all of the other sections of the same class (for the side bar)
const {
data: sections,
status: sectionsStatus,
error: sectionsError,
} = useQuery(
["sections", search],
// db can't be undefined, because it's only enabled once db is defined
() => db!.getSectionsBySearch(search),
{
enabled: !!db && !!search,
staleTime: 5 * 60 * 1000, // 5 minutes
cacheTime: 10 * 60 * 1000, // 10 minutes
refetchOnWindowFocus: false, // Don't refetch when window gets focus
refetchOnReconnect: false, // Don't refetch on network reconnect
}
);
// Auto-select first section when sections load and no section is selected
useEffect(() => {
if (sections && sections.length > 0 && !sectionId && !hasAutoSelected.current) {
hasAutoSelected.current = true;
const firstSection = sections[0];
if (firstSection) {
void router.push({
pathname: "/results",
query: { search, sectionId: firstSection.id },
}, undefined, { shallow: true });
}
}
}, [sections, sectionId, search, router]);
// Reset auto-select flag when search changes
useEffect(() => {
hasAutoSelected.current = false;
}, [search]);
// Update page when sectionId changes (arrow navigation or click)
useEffect(() => {
if (sections && sections.length > 0) {
const idx = sections.findIndex(s => s.id === sectionId);
if (idx !== -1) {
const newPage = Math.floor(idx / 5) + 1;
setCurrentPage(newPage);
}
}
}, [sectionId, sections]);
// get the section data
const {
data: section,
status: sectionStatus,
error: sectionError,
// db can't be undefined, because it's only enabled once db is defined
} = useQuery(["section", sectionId], () => db!.getSectionById(sectionId), {
enabled: !!db && !isNaN(sectionId) && sectionId > 0,
staleTime: 5 * 60 * 1000, // 5 minutes
cacheTime: 10 * 60 * 1000, // 10 minutes
refetchOnWindowFocus: false, // Don't refetch when window gets focus
refetchOnReconnect: false, // Don't refetch on network reconnect
});
const { data: relatedSections } = useQuery(
[
"relatedSections",
section?.catalogNumber,
section?.subject,
],
() =>
// TODO (field search)
// db can't be undefined, because it's only enabled once section is defined which implies db is defined
db!.getSectionsBySearch(
`${section!.catalogNumber} ${section!.subject}` // can't be null because we guard on `section`
),
{
enabled: !!section && !!db,
staleTime: 5 * 60 * 1000,
cacheTime: 10 * 60 * 1000,
refetchOnWindowFocus: false,
refetchOnReconnect: false,
}
);
// some professors have the same name so we need to get the whole list
const normalName: string[] = useMemo(() =>
section ? normalizeName(`${section.instructor1?.first} ${section.instructor1?.last}`) : [],
[section]
);
const { data: instructors } = useQuery<RMPInstructor[]>(
["instructors", section?.instructor1?.first, section?.instructor1?.last],
async () => {
if (!normalName.length || !db) return [];
const results = await Promise.all(normalName.map((name) => db.getInstructorsByName(name)));
return results.flat();
},
{
enabled: !!section && !!db && normalName.length > 0,
staleTime: 5 * 60 * 1000,
cacheTime: 10 * 60 * 1000,
refetchOnWindowFocus: false,
refetchOnReconnect: false,
}
);
// Minimal debug logging
// from that list, we need to find the one that holds the session -> update the instructor and course rating
const [instructor, setInstructor] = useState<RMPInstructor>();
const [courseRating, setCourseRating] = useState<number | null>(null);
useEffect(() => {
if (!instructors || !section || !db) {
setInstructor(undefined);
setCourseRating(null);
return;
}
// when there is only professor that matches the needed name -> set the instructor to that prof
// this helps prevent that some of the courses may not be listed in the RMP data but we still want to the prof data
// however, if there're 2 profs with the same name and the course we're looking for is not listed in either instructor's RMP courses
// then we don't know who to return
// this will not be a problem when the new RMP data is updated
if (instructors.length === 1) {
const inst = instructors[0];
setInstructor(inst);
const rating = db.getCourseRating(
inst!.instructor_id,
`${section.subject}${section.catalogNumber}`
);
setCourseRating(rating);
} else {
let foundInstructor: RMPInstructor | undefined;
let foundRating: number | null = null;
for (const ins of instructors) {
const rating = db.getCourseRating(
ins.instructor_id,
`${section.subject}${section.catalogNumber}`
);
if (rating) {
foundInstructor = ins;
foundRating = rating;
break;
}
}
setInstructor(foundInstructor);
setCourseRating(foundRating);
}
}, [instructors, section, db]);
const stableRouter = useRef(router);
const stableSearch = useRef(search);
const navigationInProgress = useRef(false);
// Update refs when props change
useEffect(() => {
stableRouter.current = router;
stableSearch.current = search;
}, [router, search]);
// Debounced navigation to prevent rapid clicks
const debouncedNavigate = useMemo(
() => debounce(async (id: number) => {
if (navigationInProgress.current) {
return;
}
navigationInProgress.current = true;
try {
// Use shallow routing to prevent page scroll reset
await stableRouter.current.push({
pathname: "/results",
query: { search: stableSearch.current, sectionId: id },
}, undefined, { shallow: false, scroll: false });
// Always scroll to show the graph/content area when a section is clicked
if (scrollRef.current) {
const contentArea = scrollRef.current;
const contentRect = contentArea.getBoundingClientRect();
const offset = window.innerWidth <= 992 ? 5 : 35; // diff offsets for mobile vs desktop
const targetScrollY = window.scrollY + contentRect.top - offset;
scroll.scrollTo(Math.max(0, targetScrollY), {
duration: 400,
smooth: true
});
}
} catch (error: unknown) {
if (error instanceof Error && !error.message.includes('Abort')) {
console.error('Navigation error:', error);
}
} finally {
navigationInProgress.current = false;
}
}, 300),
[]
);
const handleClick = useCallback((id: number) => {
if (!scrollRef.current) return;
// Don't navigate if we're already on this section
if (id === sectionId) {
return;
}
void debouncedNavigate(id);
}, [sectionId, debouncedNavigate]);
// Arrow key navigation between sections
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
// Don't handle arrow keys if user is typing in an input field or textarea
const target = event.target as HTMLElement;
if (
target.tagName === 'INPUT' ||
target.tagName === 'TEXTAREA' ||
target.isContentEditable
) {
return;
}
// Only handle arrow keys when a section is selected
if (!sections || sections.length === 0 || !sectionId) {
return;
}
// Find the current section index
const currentIndex = sections.findIndex((s) => s.id === sectionId);
if (currentIndex === -1) {
return;
}
let newIndex = -1;
if (event.key === "ArrowLeft") {
// Navigate to previous section
newIndex = currentIndex > 0 ? currentIndex - 1 : currentIndex;
event.preventDefault();
} else if (event.key === "ArrowRight") {
// Navigate to next section
newIndex = currentIndex < sections.length - 1 ? currentIndex + 1 : currentIndex;
event.preventDefault();
}
// Navigate to the new section if index changed
if (newIndex !== -1 && newIndex !== currentIndex) {
const target = sections[newIndex];
if (target && typeof target.id === "number") {
handleClick(target.id);
}
}
};
// Add event listener
window.addEventListener("keydown", handleKeyDown);
// Cleanup
return () => {
window.removeEventListener("keydown", handleKeyDown);
};
}, [sections, sectionId, handleClick]);
const handleSubmit = useCallback(({ search }: SearchQuery) => {
void stableRouter.current.push({
pathname: "/results",
query: { search },
}).catch((error: unknown) => {
console.error('Navigation error:', error);
});
}, []);
const handleRelatedSectionClick = useCallback((search: string, id: number) => {
void stableRouter.current.push({
pathname: "/results",
query: { search, sectionId: id },
}, undefined, { shallow: false, scroll: false }).then(() => {
if (!scrollRef.current) return;
const contentArea = scrollRef.current;
const contentRect = contentArea.getBoundingClientRect();
const offset = window.innerWidth <= 992 ? 5 : 35; // diff offsets for mobile vs desktop
const targetScrollY = window.scrollY + contentRect.top - offset;
scroll.scrollTo(Math.max(0, targetScrollY), {
duration: 400,
smooth: true
});
}).catch((error: unknown) => {
console.error('Navigation error:', error);
});
}, []);
return (
<Container>
<Row>
<Col lg={{ span: 8, offset: 8 }} sm={{ span: 18, offset: 3 }} xs={{ span: 20, offset: 2 }}>
<Search onSubmit={handleSubmit} initialSearchValue={search} />
</Col>
</Row>
<Row>
<ResultsContainer lg={{ span: 20, offset: 2 }} xs={{ span: 24, offset: 0 }}>
<Row>
<Col lg={6} xs={24}>
<SectionList
data={sections}
onClick={handleClick}
loading={sectionsStatus === "loading"}
id={sectionId}
error={sectionsError}
page={currentPage}
setPage={setCurrentPage}
/>
</Col>
<Col lg={18} xs={24}>
<div style={{ width: "100%", height: "100%" }} ref={scrollRef}>
<SearchResultsContent
section={section!} // FIXME: need to actually do something if these are null
relatedSections={relatedSections!}
instructor={instructor!}
courseRating={courseRating}
loadingSection={sectionStatus === "loading"}
handleRelatedSectionClick={handleRelatedSectionClick}
error={sectionError}
/>
</div>
</Col>
</Row>
</ResultsContainer>
</Row>
</Container>
);
}, (prevProps, nextProps) => {
return prevProps.search === nextProps.search &&
prevProps.sectionId === nextProps.sectionId;
});
export default Results;