feat: Sort and scroll search results, preserving scroll position

This commit is contained in:
2025-08-15 19:41:51 +00:00
parent 5bc64bec13
commit 0a7f29e1d0

View File

@@ -315,6 +315,23 @@ function Menu({data, duration, setDuration, end, setEnd, slider, setSlider, subm
const [showRange, setShowRange] = useState(false); const [showRange, setShowRange] = useState(false);
const [isSearching, setIsSearching] = useState(false); const [isSearching, setIsSearching] = useState(false);
const [searchResults, setSearchResults] = useState(null); const [searchResults, setSearchResults] = useState(null);
const scrollContainerRef = useRef(null);
const scrollPositionRef = useRef(0);
useEffect(() => {
const container = scrollContainerRef.current;
if (container) {
// Restore scroll position when results are shown
container.scrollTop = scrollPositionRef.current;
}
return () => {
// Save scroll position when results are hidden
if (container) {
scrollPositionRef.current = container.scrollTop;
}
};
}, [searchResults]);
const chooseDuration = (x) => { const chooseDuration = (x) => {
setSubmenu(false); setSubmenu(false);
@@ -648,10 +665,12 @@ function Menu({data, duration, setDuration, end, setEnd, slider, setSlider, subm
)} )}
</div> </div>
{searchResults ? ( {searchResults ? (
<> <div ref={scrollContainerRef} style={{ maxHeight: '50vh', overflowY: 'auto' }}>
{searchResults.length > 0 ? ( {searchResults.length > 0 ? (
(() => { (() => {
const groupedResults = searchResults.reduce((acc, result) => { const sortedResults = [...searchResults].sort((a, b) => b.start - a.start);
const groupedResults = sortedResults.reduce((acc, result) => {
const groupKey = moment.unix(result.start).format('YYYY'); const groupKey = moment.unix(result.start).format('YYYY');
if (!acc[groupKey]) { if (!acc[groupKey]) {
acc[groupKey] = []; acc[groupKey] = [];
@@ -669,7 +688,9 @@ function Menu({data, duration, setDuration, end, setEnd, slider, setSlider, subm
} }
}; };
return Object.entries(groupedResults).map(([groupKey, results]) => ( return Object.entries(groupedResults)
.sort(([yearA], [yearB]) => yearB - yearA)
.map(([groupKey, results]) => (
<div key={groupKey}> <div key={groupKey}>
<h3 style={{ color: 'white', margin: '0.5em 0 0.25em', fontSize: '1em', fontWeight: 'normal', textAlign: 'center' }}>{groupKey}</h3> <h3 style={{ color: 'white', margin: '0.5em 0 0.25em', fontSize: '1em', fontWeight: 'normal', textAlign: 'center' }}>{groupKey}</h3>
{results.map((result, index) => ( {results.map((result, index) => (
@@ -683,7 +704,7 @@ function Menu({data, duration, setDuration, end, setEnd, slider, setSlider, subm
) : ( ) : (
<p style={{color: 'white', textAlign: 'center', padding: '1em 0'}}>No results found.</p> <p style={{color: 'white', textAlign: 'center', padding: '1em 0'}}>No results found.</p>
)} )}
</> </div>
) : ( ) : (
<> <>
<label className="submenu-checkbox-label"> <label className="submenu-checkbox-label">