| name | data-to-ui |
| description | Data-to-UI pipeline patterns. Use when transforming JSON data into React components, creating TypeScript types from schemas, building derived types, or creating data utilities. |
Data to UI
Overview
Patterns for transforming static data into type-safe React components. This skill covers JSON → TypeScript → React pipelines with emphasis on semantic color systems, derived types, and formatting utilities.
Workflows
1. JSON Schema → TypeScript Types
2. Derived Types for UI
3. Color Mapping Systems
4. Icon Mapping
5. Formatting Utilities
6. Aggregation & Grouping
Reference Implementation
Color Mapping System
export interface SeverityColors {
badge: string;
bg: string;
text: string;
border: string;
dot: string;
}
const SEVERITY_COLOR_MAP: Record<Severity, SeverityColors> = {
safety_hazard: {
badge: 'text-red-600 bg-red-100',
bg: 'bg-red-500',
text: 'text-red-600',
dot: 'bg-red-500',
border: 'border-red-500',
},
};
export function getSeverityColors(severity: Severity): SeverityColors {
return SEVERITY_COLOR_MAP[severity];
}
Icon Mapping
export function getSeverityIcon(severity: Severity): string {
const icons: Record<Severity, string> = {
safety_hazard: 'AlertTriangle',
repair_needed: 'Wrench',
maintenance_item: 'Settings',
monitor: 'Eye',
informational: 'Info'
};
return icons[severity];
}
Derived Types
export interface Finding {
id: string;
assetId?: string | null;
severity: Severity;
title: string;
}
export interface FindingWithAsset extends Finding {
asset?: Asset;
}
export interface PropertyWithDetails {
property: Property;
inspectionReport: InspectionReport;
findings: Finding[];
assets: Asset[];
}
Formatting Utilities
export function formatCurrency(value: number): string {
return new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 0,
maximumFractionDigits: 0
}).format(value);
}
export function formatDate(dateString: string): string {
return new Date(dateString).toLocaleDateString('en-US', {
year: 'numeric',
month: 'short',
day: 'numeric'
});
}
export function yearsSince(dateString: string): number {
const date = new Date(dateString);
const now = new Date();
return Math.floor((now.getTime() - date.getTime()) / (365.25 * 24 * 60 * 60 * 1000));
}
Aggregation Patterns
export function groupFindingsBySeverity(findings: Finding[]): Record<Severity, Finding[]> {
const grouped: Record<Severity, Finding[]> = {
safety_hazard: [],
repair_needed: [],
maintenance_item: [],
monitor: [],
informational: []
};
findings.forEach(f => grouped[f.severity].push(f));
return grouped;
}
export function sortFindingsBySeverity(findings: Finding[]): Finding[] {
const severityOrder: Record<Severity, number> = {
safety_hazard: 0,
repair_needed: 1,
maintenance_item: 2,
monitor: 3,
informational: 4
};
return [...findings].sort((a, b) =>
severityOrder[a.severity] - severityOrder[b.severity]
);
}
Best Practices
- Single Source of Truth: All color/icon mappings in one place with accessor functions
- Multi-Format Colors: Provide badge, bg, text, border, dot variants for flexibility
- Type Safety: Use
Record<EnumType, Value> instead of plain objects
- Intl APIs: Use
Intl.NumberFormat and Intl.DateTimeFormat for localization
- Immutability: Use spread operator when sorting/filtering arrays
- Documentation: Add JSDoc comments explaining color choices and data structures
- Colocate Utilities: Keep types and utilities in same file for easy import
Anti-Patterns
- DO NOT use generic color names without semantic meaning (e.g.,
'red' instead of 'safety_hazard')
- DO NOT inline color classes in components; always use mapping functions
- DO NOT use
any types; prefer unknown and type guards
- DO NOT mutate input arrays in sort/filter functions; always create copies
- DO NOT hardcode date formats; use
Intl.DateTimeFormat for consistency
- DO NOT create separate files for simple utilities; colocate with types
- DO NOT forget to handle null/undefined in optional fields
- DO NOT use snake_case or kebab-case for TypeScript file names; use camelCase
Feedback Loops
- Type Checking: Run
tsc --noEmit to validate types
- Runtime Validation: Consider Zod for JSON schema validation at runtime
- Visual Testing: Build Storybook stories to verify color systems
- Data Consistency: Compare aggregated stats with source data counts
- Import Verification: Ensure all utilities are exported and importable
Related Skills
interface-design - Use color systems in React components
refactoring-code - Consolidate duplicate color/formatting logic