Skip to content

Commit 2e7850e

Browse files
authored
fix: Selected saved filter is not expanded in sidebar when reloading browser page (#3029)
1 parent 7c43d41 commit 2e7850e

File tree

1 file changed

+59
-29
lines changed

1 file changed

+59
-29
lines changed

src/components/CategoryList/CategoryList.react.js

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,16 @@ export default class CategoryList extends React.Component {
2929
this.highlight = document.createElement('div');
3030
this.highlight.className = styles.highlight;
3131
listWrapper.appendChild(this.highlight);
32+
this._autoExpandForSelectedFilter();
3233
this._updateHighlight();
3334
}
3435
}
3536

36-
componentDidUpdate() {
37+
componentDidUpdate(prevProps) {
38+
// Auto-expand if the URL params changed (e.g., user navigated to a different filter)
39+
if (prevProps.params !== this.props.params) {
40+
this._autoExpandForSelectedFilter();
41+
}
3742
this._updateHighlight();
3843
}
3944

@@ -43,6 +48,53 @@ export default class CategoryList extends React.Component {
4348
}
4449
}
4550

51+
_findMatchingFilterIndex(filters, queryFilter, queryFilterId) {
52+
// Find the index of a filter that matches the URL parameters
53+
// Prioritize filterId matching, then fall back to content comparison
54+
for (let i = 0; i < filters?.length; i++) {
55+
const filter = filters[i];
56+
// First priority: match by filterId if both URL and filter have an ID
57+
if (queryFilterId && queryFilterId === filter.id) {
58+
return i;
59+
}
60+
}
61+
// Second priority: match by filter content (legacy fallback)
62+
for (let i = 0; i < filters?.length; i++) {
63+
const filter = filters[i];
64+
if (queryFilter === filter.filter) {
65+
return i;
66+
}
67+
}
68+
return -1;
69+
}
70+
71+
_autoExpandForSelectedFilter() {
72+
// Check if a saved filter is selected in the URL and auto-expand the corresponding class
73+
const query = new URLSearchParams(this.props.params);
74+
if (query.has('filters')) {
75+
const queryFilter = query.get('filters');
76+
const filterId = query.get('filterId');
77+
78+
// Find the class that contains the selected filter
79+
for (let i = 0; i < this.props.categories.length; i++) {
80+
const c = this.props.categories[i];
81+
const id = c.id || c.name;
82+
83+
// Check if this is the current class and it has filters
84+
if (id === this.props.current && c.filters && c.filters.length > 0) {
85+
// Check if any filter in this class matches the URL filter
86+
const matchIndex = this._findMatchingFilterIndex(c.filters, queryFilter, filterId);
87+
88+
// If a matching filter is found, auto-expand this class
89+
if (matchIndex !== -1 && !this.state.openClasses.includes(id)) {
90+
this.setState({ openClasses: [id] });
91+
}
92+
break;
93+
}
94+
}
95+
}
96+
}
97+
4698
_updateHighlight() {
4799
if (this.highlight) {
48100
let height = 0;
@@ -55,19 +107,9 @@ export default class CategoryList extends React.Component {
55107
if (query.has('filters')) {
56108
const queryFilter = query.get('filters');
57109
const filterId = query.get('filterId');
58-
for (let i = 0; i < c.filters?.length; i++) {
59-
const filter = c.filters[i];
60-
// Prioritize filterId matching, only fall back to content comparison if no filterId
61-
if (filterId) {
62-
if (filterId === filter.id) {
63-
height += (i + 1) * 20;
64-
break;
65-
}
66-
} else if (queryFilter === filter.filter) {
67-
// Legacy fallback: match by filter content when no filterId is present
68-
height += (i + 1) * 20;
69-
break;
70-
}
110+
const matchIndex = this._findMatchingFilterIndex(c.filters, queryFilter, filterId);
111+
if (matchIndex !== -1) {
112+
height += (matchIndex + 1) * 20;
71113
}
72114
}
73115
}
@@ -118,21 +160,9 @@ export default class CategoryList extends React.Component {
118160
if (query.has('filters')) {
119161
const queryFilter = query.get('filters');
120162
const queryFilterId = query.get('filterId');
121-
for (let i = 0; i < c.filters?.length; i++) {
122-
const filter = c.filters[i];
123-
// Prioritize filterId matching, only fall back to content comparison if no filterId
124-
if (queryFilterId) {
125-
if (queryFilterId === filter.id) {
126-
selectedFilter = i;
127-
className = '';
128-
break;
129-
}
130-
} else if (queryFilter === filter.filter) {
131-
// Legacy fallback: match by filter content when no filterId is present
132-
selectedFilter = i;
133-
className = '';
134-
break;
135-
}
163+
selectedFilter = this._findMatchingFilterIndex(c.filters, queryFilter, queryFilterId);
164+
if (selectedFilter !== -1) {
165+
className = '';
136166
}
137167
}
138168
}

0 commit comments

Comments
 (0)