Skip to content

Commit 594d786

Browse files
committed
Fixed broken retain sort filter
1 parent 77fc9bc commit 594d786

1 file changed

Lines changed: 30 additions & 2 deletions

File tree

src/pages/Library.tsx

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,25 @@ const LIB_ORDER_KEY = 'app_library_order';
3434
export default function Library() {
3535
const { serverUrl, user } = useAuth();
3636
const [search, setSearch] = useState("");
37-
const [sortBy, setSortBy] = useState("sort_title");
38-
const [order, setOrder] = useState<"ASC" | "DESC">("ASC");
37+
// Initialize sort/order from localStorage if retention is enabled
38+
const [sortBy, setSortBy] = useState(() => {
39+
try {
40+
if (typeof window !== 'undefined' && localStorage.getItem(RETAIN_KEY) === '1') {
41+
const saved = localStorage.getItem(LIB_SORT_KEY);
42+
if (saved && SORT_BY.some(o => o.value === saved)) return saved;
43+
}
44+
} catch {}
45+
return "sort_title";
46+
});
47+
const [order, setOrder] = useState<"ASC" | "DESC">(() => {
48+
try {
49+
if (typeof window !== 'undefined' && localStorage.getItem(RETAIN_KEY) === '1') {
50+
const saved = localStorage.getItem(LIB_ORDER_KEY) as ("ASC" | "DESC" | null);
51+
if (saved === 'ASC' || saved === 'DESC') return saved;
52+
}
53+
} catch {}
54+
return "ASC";
55+
});
3956
const [showAdvanced, setShowAdvanced] = useState(false);
4057
const [bookmarkedOnly, setBookmarkedOnly] = useState(false);
4158
// Defer search to avoid spamming requests while user types quickly
@@ -55,6 +72,17 @@ export default function Library() {
5572
refetch();
5673
}, [deferredSearch, sortBy, order, bookmarkedOnly, refetch]);
5774

75+
// Persist sort/order if retention enabled
76+
useEffect(() => {
77+
if (typeof window === 'undefined') return;
78+
if (localStorage.getItem(RETAIN_KEY) === '1') {
79+
try {
80+
localStorage.setItem(LIB_SORT_KEY, sortBy);
81+
localStorage.setItem(LIB_ORDER_KEY, order);
82+
} catch {}
83+
}
84+
}, [sortBy, order]);
85+
5886
// Sync bookmark filter in URL search params for shareable links
5987
useEffect(() => {
6088
const url = new URL(window.location.href);

0 commit comments

Comments
 (0)