Files
bakery-ia/frontend/src/components/dashboard/InsightsGrid.tsx
Claude 4067ee72e4 fix: Add comprehensive null guards to all dashboard components
This commit fixes React Error #306 (text content mismatch) by ensuring
no component ever tries to render undefined values as text content.

Changes made across all dashboard components:

1. **Component Entry Guards**:
   - Added early returns for null/undefined data
   - Changed `if (loading)` to `if (loading || !data)` pattern
   - Components now show loading skeleton when data is undefined

2. **Property Access Guards**:
   - Added `|| fallback` operators for all rendered text
   - Added optional chaining for nested property access
   - Added conditional rendering for optional/array fields

3. **Specific Component Fixes**:

   **HealthStatusCard.tsx**:
   - Added early return for !healthStatus
   - Added fallback for status: `healthStatus.status || 'green'`
   - Added conditional rendering for nextScheduledRun
   - Added conditional rendering for checklistItems array
   - Added text fallback: `item.text || ''`

   **InsightsGrid.tsx**:
   - Added null check: `if (!insights || !insights.savings || ...)`
   - Added fallbacks: `insights.savings?.label || 'Savings'`
   - Added fallbacks for all 4 insight cards

   **OrchestrationSummaryCard.tsx**:
   - Added early return for !summary
   - Added fallback: `summary.message || ''`
   - Added fallback: `summary.runNumber || 0`
   - Added array checks: `summary.purchaseOrdersSummary && ...`
   - Added fallbacks for PO items: `po.supplierName || 'Unknown Supplier'`
   - Added fallbacks for batch items: `batch.quantity || 0`
   - Added safe date parsing: `batch.readyByTime ? new Date(...) : 'TBD'`

   **ProductionTimelineCard.tsx**:
   - Added early return for !timeline
   - Added array check: `!timeline.timeline || timeline.timeline.length === 0`
   - Added fallbacks for all item properties:
     - `item.statusIcon || '🔵'`
     - `item.productName || 'Product'`
     - `item.quantity || 0`
     - `item.unit || 'units'`
     - `item.batchNumber || 'N/A'`
     - `item.priority || 'NORMAL'`
     - `item.statusText || 'Status'`
     - `item.progress || 0`

   **ActionQueueCard.tsx**:
   - Added early return for !actionQueue
   - Added safe config lookup with fallback to normal urgency
   - Added array check: `!actionQueue.actions || ...`
   - Added property fallbacks:
     - `action.title || 'Action Required'`
     - `action.urgency || 'normal'`
     - `action.subtitle || ''`
     - `action.estimatedTimeMinutes || 5`
   - Added array guard: `(action.actions || []).map(...)`
   - Added fallbacks for counts: `actionQueue.totalActions || 0`

   **useReasoningTranslation.ts**:
   - Enhanced formatPOAction with explicit fallbacks
   - Enhanced formatBatchAction with explicit fallbacks
   - All translation functions return strings with || operators

   **DashboardPage.tsx**:
   - Removed conditional rendering operators (&&)
   - Components now always render and handle their own null states
   - Pattern: `<Component data={data} loading={loading} />`

4. **Testing Strategy**:
   - All components gracefully handle undefined data
   - Loading states shown when data is undefined
   - No undefined values can be rendered as text content
   - Multiple fallback layers ensure strings are always returned

This comprehensive fix addresses the root cause of React Error #306
by making all components bulletproof against undefined values.
2025-11-07 20:15:29 +00:00

118 lines
3.3 KiB
TypeScript

// ================================================================
// frontend/src/components/dashboard/InsightsGrid.tsx
// ================================================================
/**
* Insights Grid - Key metrics at a glance
*
* 2x2 grid of important metrics: savings, inventory, waste, deliveries.
* Mobile-first design with large touch targets.
*/
import React from 'react';
import { Insights } from '../../api/hooks/newDashboard';
interface InsightsGridProps {
insights: Insights;
loading?: boolean;
}
const colorConfig = {
green: {
bg: 'bg-green-50',
border: 'border-green-200',
text: 'text-green-800',
detail: 'text-green-600',
},
amber: {
bg: 'bg-amber-50',
border: 'border-amber-200',
text: 'text-amber-900',
detail: 'text-amber-600',
},
red: {
bg: 'bg-red-50',
border: 'border-red-200',
text: 'text-red-900',
detail: 'text-red-600',
},
};
function InsightCard({
label,
value,
detail,
color,
}: {
label: string;
value: string;
detail: string;
color: 'green' | 'amber' | 'red';
}) {
const config = colorConfig[color];
return (
<div
className={`${config.bg} ${config.border} border-2 rounded-xl p-4 md:p-6 transition-all duration-200 hover:shadow-lg cursor-pointer`}
>
{/* Label */}
<div className="text-sm md:text-base font-bold text-gray-700 mb-2">{label}</div>
{/* Value */}
<div className={`text-xl md:text-2xl font-bold ${config.text} mb-1`}>{value}</div>
{/* Detail */}
<div className={`text-sm ${config.detail} font-medium`}>{detail}</div>
</div>
);
}
export function InsightsGrid({ insights, loading }: InsightsGridProps) {
if (loading) {
return (
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
{[1, 2, 3, 4].map((i) => (
<div key={i} className="animate-pulse bg-gray-100 rounded-xl p-6">
<div className="h-4 bg-gray-200 rounded w-1/2 mb-3"></div>
<div className="h-8 bg-gray-200 rounded w-3/4 mb-2"></div>
<div className="h-4 bg-gray-200 rounded w-2/3"></div>
</div>
))}
</div>
);
}
// Guard against undefined values
if (!insights || !insights.savings || !insights.inventory || !insights.waste || !insights.deliveries) {
return null;
}
return (
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
<InsightCard
label={insights.savings?.label || 'Savings'}
value={insights.savings?.value || '€0'}
detail={insights.savings?.detail || 'This week'}
color={insights.savings?.color || 'green'}
/>
<InsightCard
label={insights.inventory?.label || 'Inventory'}
value={insights.inventory?.value || '0%'}
detail={insights.inventory?.detail || 'Status'}
color={insights.inventory?.color || 'green'}
/>
<InsightCard
label={insights.waste?.label || 'Waste'}
value={insights.waste?.value || '0%'}
detail={insights.waste?.detail || 'Reduction'}
color={insights.waste?.color || 'green'}
/>
<InsightCard
label={insights.deliveries?.label || 'Deliveries'}
value={insights.deliveries?.value || '0%'}
detail={insights.deliveries?.detail || 'On-time'}
color={insights.deliveries?.color || 'green'}
/>
</div>
);
}