mirror of
https://github.com/kilyabin/psysonic.git
synced 2026-07-22 14:35:41 +00:00
0f5ece6d03
Persist libraryGridMaxColumns, wire useCardGridMetrics and card grid layout, add i18n and settings search index; document performance hint in copy.
48 lines
1.6 KiB
TypeScript
48 lines
1.6 KiB
TypeScript
import { useLayoutEffect, useState, type RefObject } from 'react';
|
|
import { useAuthStore } from '../store/authStore';
|
|
import { clampLibraryGridMaxColumns } from '../store/authStoreHelpers';
|
|
import {
|
|
type CardGridRowHeightVariant,
|
|
computeCardGridColumnCount,
|
|
computeCellWidthPx,
|
|
estimateRowHeightPx,
|
|
} from '../utils/cardGridLayout';
|
|
|
|
/**
|
|
* ResizeObserver-driven column count (capped by Settings → Library) and
|
|
* virtual row height estimate from the measured cell width.
|
|
*/
|
|
export function useCardGridMetrics(
|
|
measureRef: RefObject<HTMLElement | null>,
|
|
observerEnabled: boolean,
|
|
variant: CardGridRowHeightVariant,
|
|
layoutSignal: number,
|
|
): { gridCols: number; rowHeightEst: number } {
|
|
const maxCols = useAuthStore(s => clampLibraryGridMaxColumns(s.libraryGridMaxColumns));
|
|
const [gridCols, setGridCols] = useState(4);
|
|
const [rowHeightEst, setRowHeightEst] = useState(() =>
|
|
estimateRowHeightPx(
|
|
computeCellWidthPx(960, clampLibraryGridMaxColumns(useAuthStore.getState().libraryGridMaxColumns)),
|
|
variant,
|
|
),
|
|
);
|
|
|
|
useLayoutEffect(() => {
|
|
if (!observerEnabled) return;
|
|
const el = measureRef.current;
|
|
if (!el) return;
|
|
const onResize = () => {
|
|
const w = el.clientWidth;
|
|
const cols = computeCardGridColumnCount(w, maxCols);
|
|
setGridCols(cols);
|
|
setRowHeightEst(estimateRowHeightPx(computeCellWidthPx(w, cols), variant));
|
|
};
|
|
onResize();
|
|
const ro = new ResizeObserver(onResize);
|
|
ro.observe(el);
|
|
return () => ro.disconnect();
|
|
}, [observerEnabled, variant, layoutSignal, maxCols]);
|
|
|
|
return { gridCols, rowHeightEst };
|
|
}
|