Files
psysonic/src/hooks/useResizeClientHeight.ts
T
cucadmuh 9d30285ff1 Perf/UI cover cache mainstage (#468)
* Enhance CachedImage and ArtistDetail components with improved image caching and priority handling

- Refactor CachedImage to utilize a priority system for image loading based on viewport visibility, improving performance during scrolling.
- Update useCachedUrl to accept an optional getPriority function for better cache management.
- Optimize ArtistDetail and Artists components by using useMemo for cover art URLs, reducing redundant calculations and improving rendering efficiency.
- Adjust image loading logic in CachedImage to ensure smoother transitions and avoid unnecessary fetch requests.

* perf(ui): unblock IDB cover art, stabilize mainstage rails and virtual lists

Let IndexedDB reads bypass the network concurrency slot so cached thumbnails
paint without queueing behind remote fetches; debounce disk eviction during
heavy scrolling.

Fix mainstage horizontal rails: dedupe album/song ids for React keys, widen
artwork budget overscan, avoid resetting the budget on list append, and raise
Home initial artwork budgets. CachedImage treats already-decoded images as
loaded; rail cards load cover images eagerly.

Refresh dynamic color extraction and extend virtual scrolling / scroll roots on
Albums, Artists, Playlists, and related surfaces.


Remove local agent-only commit instructions from the repository tree.

* perf(virtual): viewport-based overscan for main scroll lists

Drive TanStack Virtual overscan from measured scroll height so each list
renders about one screen of extra rows above and below the viewport for
snappier scrolling on Albums, Artists (list mode), and Tracks virtual song list.

Introduce useResizeClientHeight helpers (ID + ref) for ResizeObserver-based
clientHeight tracking.

* docs(changelog): note PR #468 UI cover cache, rails, and virtual lists

Add a coarse summary under 1.46.0 Changed for cover-art pipeline,
mainstage rails, viewport-based overscan, and library/chrome polish.
2026-05-06 00:15:58 +03:00

40 lines
1.1 KiB
TypeScript

import { type RefObject, useLayoutEffect, useState } from 'react';
/**
* Track an element's `clientHeight` (ResizeObserver). Used so virtualizers can
* set `overscan` to roughly one viewport of rows beyond the visible range.
*/
export function useElementClientHeightById(elementId: string, fallback = 800): number {
const [h, setH] = useState(fallback);
useLayoutEffect(() => {
const el = typeof document !== 'undefined' ? document.getElementById(elementId) : null;
if (!el) {
setH(fallback);
return;
}
const update = () => setH(el.clientHeight);
const ro = new ResizeObserver(update);
ro.observe(el);
update();
return () => ro.disconnect();
}, [elementId, fallback]);
return h;
}
export function useRefElementClientHeight(
ref: RefObject<HTMLElement | null>,
fallback = 600,
): number {
const [h, setH] = useState(fallback);
useLayoutEffect(() => {
const el = ref.current;
if (!el) return;
const update = () => setH(el.clientHeight);
const ro = new ResizeObserver(update);
ro.observe(el);
update();
return () => ro.disconnect();
}, [ref, fallback]);
return h;
}