Skip to content

Commit 2288377

Browse files
authored
Merge pull request #54 from javaevolved/copilot/update-url-with-filter-hash
Update URL hash when category filter is applied
2 parents 535c27a + 57f0102 commit 2288377

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

site/app.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,18 +180,41 @@
180180
}
181181
});
182182

183+
// Update URL hash to reflect active filter
184+
const activeFilter = pill.classList.contains('active') ? category : null;
185+
if (activeFilter && activeFilter !== 'all') {
186+
history.replaceState(null, '', '#' + activeFilter);
187+
} else {
188+
history.replaceState(null, '', window.location.pathname + window.location.search);
189+
}
190+
183191
// Update view toggle button state
184192
if (window.updateViewToggleState) {
185193
window.updateViewToggleState();
186194
}
187195
});
188196
});
189197

190-
// Auto-click "All" button on page load to show all cards
191-
const allButton = document.querySelector('.filter-pill[data-filter="all"]');
192-
if (allButton) {
193-
allButton.click();
194-
}
198+
// Apply filter from a given category string (or "all" / empty for no filter)
199+
const applyHashFilter = (category) => {
200+
const target = category
201+
? document.querySelector(`.filter-pill[data-filter="${category}"]`)
202+
: null;
203+
if (target) {
204+
target.click();
205+
} else {
206+
const allButton = document.querySelector('.filter-pill[data-filter="all"]');
207+
if (allButton) allButton.click();
208+
}
209+
};
210+
211+
// On load, apply filter from URL hash or default to "All"
212+
applyHashFilter(window.location.hash.slice(1));
213+
214+
// Also react to browser back/forward hash changes
215+
window.addEventListener('hashchange', () => {
216+
applyHashFilter(window.location.hash.slice(1));
217+
});
195218
};
196219

197220
/* ==========================================================

0 commit comments

Comments
 (0)