Files
iNaturalistReactNative/src/sharedHooks/useGridLayout.ts
T
Ryan Stelly b78be9243d lint rule & autofix for "trailing comma" (#3299)
* (lint) MOB-1063 enforce trailing commas

* autofix trailing commas

* manually fix newly introduced maxlen violations

* add trailing comma convention to i18n build
2025-12-22 20:17:13 -06:00

55 lines
1.4 KiB
TypeScript

import { useMemo } from "react";
import { BREAKPOINTS } from "sharedHelpers/breakpoint";
import { useDeviceOrientation } from "sharedHooks";
const GUTTER = 15;
const HALF_GUTTER = GUTTER / 2;
const TAB_BAR_HEIGHT = 80;
const flashListStyle = {
paddingTop: HALF_GUTTER,
paddingLeft: HALF_GUTTER,
paddingRight: HALF_GUTTER,
paddingBottom: TAB_BAR_HEIGHT + HALF_GUTTER,
};
const useGridLayout = ( layout?: "list" ) => {
const {
isLandscapeMode, isTablet, screenWidth, screenHeight,
} = useDeviceOrientation();
const calculateNumColumns = () => {
if ( layout === "list" || screenWidth <= BREAKPOINTS.sm ) {
return 1;
}
if ( !isTablet ) return 2;
if ( isLandscapeMode ) return 6;
if ( screenWidth <= BREAKPOINTS.xl ) return 2;
return 4;
};
const numColumns = calculateNumColumns();
const calculateGridItemWidth = () => {
const combinedGutter = ( numColumns + 1 ) * GUTTER;
const gridWidth = isTablet
? screenWidth
: Math.min( screenWidth, screenHeight );
return Math.floor( ( gridWidth - combinedGutter ) / numColumns );
};
const gridItemWidth = calculateGridItemWidth();
const gridItemStyle = useMemo( ( ) => ( {
height: gridItemWidth,
width: gridItemWidth,
margin: HALF_GUTTER,
} ), [gridItemWidth] );
return {
flashListStyle,
gridItemStyle,
numColumns,
};
};
export default useGridLayout;