Skip to content

Commit 76dbc7c

Browse files
committed
Added 'Retain Sorting and Filter preferences' setting
1 parent 032888d commit 76dbc7c

2 files changed

Lines changed: 60 additions & 2 deletions

File tree

src/pages/Library.tsx

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,39 @@ const ORDER_BY: { label: string; value: "ASC" | "DESC" }[] = [
2323
{ label: "Ascending", value: "ASC" },
2424
];
2525

26+
const RETAIN_KEY = 'app_retain_library_prefs';
27+
const LIB_SORT_KEY = 'app_library_sort';
28+
const LIB_ORDER_KEY = 'app_library_order';
29+
2630
export default function Library() {
2731
const { serverUrl } = useAuth();
2832
const [search, setSearch] = useState("");
29-
const [sortBy, setSortBy] = useState("sort_title");
30-
const [order, setOrder] = useState<"ASC" | "DESC">("ASC");
33+
const [sortBy, setSortBy] = useState(() => {
34+
try {
35+
if (localStorage.getItem(RETAIN_KEY) === '1') {
36+
return localStorage.getItem(LIB_SORT_KEY) || 'sort_title';
37+
}
38+
} catch {}
39+
return 'sort_title';
40+
});
41+
const [order, setOrder] = useState<"ASC" | "DESC">(() => {
42+
try {
43+
if (localStorage.getItem(RETAIN_KEY) === '1') {
44+
const v = localStorage.getItem(LIB_ORDER_KEY) as 'ASC' | 'DESC' | null;
45+
if (v === 'ASC' || v === 'DESC') return v;
46+
}
47+
} catch {}
48+
return 'ASC';
49+
});
50+
// Persist when changed if retention enabled
51+
useEffect(() => {
52+
try {
53+
if (localStorage.getItem(RETAIN_KEY) === '1') {
54+
localStorage.setItem(LIB_SORT_KEY, sortBy);
55+
localStorage.setItem(LIB_ORDER_KEY, order);
56+
}
57+
} catch {}
58+
}, [sortBy, order]);
3159
// Defer search to avoid spamming requests while user types quickly
3260
const deferredSearch = useDeferredValue(search);
3361
const { count, games, loading, error, loadMore, hasMore, refetch } = useGames(

src/pages/Settings.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,25 @@ import { Divider } from "@tw/divider";
22
import { Heading, Subheading } from "@tw/heading";
33
import { Input } from "@tw/input";
44
import { useDownloads } from "@/context/DownloadContext";
5+
import { SwitchField, Switch } from "@tw/switch";
6+
import { Label } from "@/components/tailwind/fieldset";
7+
import { useEffect, useState } from "react";
8+
9+
const RETAIN_KEY = 'app_retain_library_prefs';
510

611
export default function Settings() {
712
const { speedLimitKB, setSpeedLimitKB, formatSpeed, formatLimit } = useDownloads() as any;
813
// speedLimitKB stored directly as KB/s (decimal). 0 = unlimited.
914
const kbValue = speedLimitKB;
15+
const [retainLibraryPrefs, setRetainLibraryPrefs] = useState<boolean>(() => {
16+
try {
17+
const v = localStorage.getItem(RETAIN_KEY);
18+
return v === '1';
19+
} catch { return false; }
20+
});
21+
useEffect(() => {
22+
try { localStorage.setItem(RETAIN_KEY, retainLibraryPrefs ? '1' : '0'); } catch {}
23+
}, [retainLibraryPrefs]);
1024

1125
return (
1226
<div className="flex flex-col h-full overflow-auto pb-12">
@@ -43,6 +57,22 @@ export default function Settings() {
4357
</label>
4458
</div>
4559
</section>
60+
61+
<section>
62+
<Subheading level={2}>Library</Subheading>
63+
<div className="flex flex-col gap-4">
64+
<SwitchField>
65+
<Switch
66+
name="retainLibraryPrefs"
67+
color="indigo"
68+
aria-label="Retain Library sorting and filter preferences"
69+
checked={retainLibraryPrefs}
70+
onChange={(v: boolean) => setRetainLibraryPrefs(v)}
71+
/>
72+
<Label>Retain Sorting and Filter preferences</Label>
73+
</SwitchField>
74+
</div>
75+
</section>
4676
</div>
4777
</div>
4878
);

0 commit comments

Comments
 (0)