refactor(filters): stop mirroring filters into the URL; localStorage is canonical
Filters/sort/search/scope were written to the address bar on every change (a leftover from sharing reproducible examples), giving two sources of truth. Make localStorage the single source: drop the automatic syncUrl from setFilters/setPage. A "Share view" link still hydrates filters on first load, after which the query is stripped from the URL (stripUrlParams) so it stays clean. syncUrl is replaced by shareUrl (builds the link on demand); the serializer now also round-trips scope.
This commit is contained in:
parent
282cc978f6
commit
eef64ef811
2 changed files with 30 additions and 12 deletions
|
|
@ -1,12 +1,14 @@
|
|||
// Serialize feed filters to/from a compact, human-readable URL query string, so a
|
||||
// pasted URL reproduces exactly what's on screen (handy for "here's the URL, I see
|
||||
// this bug"). Only non-default values are emitted to keep URLs clean.
|
||||
// Serialize feed filters to/from a compact, human-readable URL query string. Filters are
|
||||
// NOT kept in the address bar during normal use (localStorage is the single source of
|
||||
// truth); this serializer powers the explicit "Share view" link and the one-time hydration
|
||||
// when someone opens such a link. Only non-default values are emitted to keep URLs clean.
|
||||
import type { FeedFilters } from "./api";
|
||||
|
||||
const KEYS = [
|
||||
"q",
|
||||
"show",
|
||||
"sort",
|
||||
"scope",
|
||||
"normal",
|
||||
"shorts",
|
||||
"live",
|
||||
|
|
@ -25,6 +27,7 @@ export function filtersToParams(f: FeedFilters): URLSearchParams {
|
|||
if (f.q) p.set("q", f.q);
|
||||
if (f.show && f.show !== "unwatched") p.set("show", f.show);
|
||||
if (f.sort && f.sort !== "newest") p.set("sort", f.sort);
|
||||
if (f.scope === "all") p.set("scope", "all");
|
||||
if (!f.includeNormal) p.set("normal", "0");
|
||||
if (f.includeShorts) p.set("shorts", "1");
|
||||
if (f.includeLive) p.set("live", "1");
|
||||
|
|
@ -51,6 +54,7 @@ export function paramsToFilters(params: URLSearchParams, base: FeedFilters): Fee
|
|||
if (params.has("q")) f.q = params.get("q") ?? "";
|
||||
if (params.has("show")) f.show = params.get("show") || "unwatched";
|
||||
if (params.has("sort")) f.sort = params.get("sort") || "newest";
|
||||
if (params.has("scope")) f.scope = params.get("scope") === "all" ? "all" : "my";
|
||||
if (params.has("normal")) f.includeNormal = params.get("normal") !== "0";
|
||||
if (params.has("shorts")) f.includeShorts = params.get("shorts") === "1";
|
||||
if (params.has("live")) f.includeLive = params.get("live") === "1";
|
||||
|
|
@ -81,11 +85,19 @@ export function readPage(): Page {
|
|||
return p === "channels" || p === "stats" ? p : "feed";
|
||||
}
|
||||
|
||||
/** Reflect the current filters + page into the address bar without adding history entries. */
|
||||
export function syncUrl(f: FeedFilters, page: Page = "feed"): void {
|
||||
/** Build a shareable absolute URL that reproduces the current filters (+ page). Used by the
|
||||
* opt-in "Share view" action — filters are not otherwise written to the address bar. */
|
||||
export function shareUrl(f: FeedFilters, page: Page = "feed"): string {
|
||||
const params = filtersToParams(f);
|
||||
if (page !== "feed") params.set("page", page);
|
||||
const qs = params.toString();
|
||||
const url = qs ? `${window.location.pathname}?${qs}` : window.location.pathname;
|
||||
window.history.replaceState(null, "", url);
|
||||
return `${window.location.origin}${window.location.pathname}${qs ? `?${qs}` : ""}`;
|
||||
}
|
||||
|
||||
/** Strip any filter/page query params from the address bar (after hydrating from a share
|
||||
* link), leaving a clean URL without touching history. */
|
||||
export function stripUrlParams(): void {
|
||||
if (window.location.search) {
|
||||
window.history.replaceState(null, "", window.location.pathname);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue