All files / src/pages/inventory/hooks useInventoryRowStyling.ts

100% Statements 34/34
81.81% Branches 9/11
100% Functions 1/1
100% Lines 34/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 351x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 29x 29x 29x 29x 29x 29x 6x 29x 8x  
/**
 * @file useInventoryRowStyling.ts
 * @module pages/inventory/hooks/useInventoryRowStyling
 *
 * @summary
 * Row styling logic based on stock levels.
 * Extracted from useInventoryData for single responsibility.
 *
 * @enterprise
 * - Pure function for row class determination
 * - CSS class names for critical/warning/normal stock levels
 */
 
/**
 * Hook to generate row styling function for inventory items.
 * 
 * Applies visual styling based on stock deficit:
 * - Critical (red): deficit >= 5
 * - Warning (orange): deficit > 0 and < 5
 * - Normal: deficit <= 0
 * 
 * @returns Function that takes (onHand, minQty) and returns CSS class name
 */
export const useInventoryRowStyling = (): ((onHand: number, minQty: number) => string) => {
  return (onHand: number, minQty: number): string => {
    const minRaw = Number(minQty ?? 0);
    const min = Number.isFinite(minRaw) && minRaw > 0 ? minRaw : 5;
    const deficit = min - Number(onHand ?? 0);
 
    if (deficit >= 5) return 'row-critical';
    if (deficit > 0) return 'row-warning';
    return '';
  };
};