Files
units-ui/components/converter/MainConverter.tsx
Sebastian Krüger 5a7bb9a05c feat: implement Phase 3 - Advanced UX features and interactivity
Add comprehensive UX enhancements with innovative features:

🔍 Fuzzy Search Component (SearchUnits.tsx):
- Powered by Fuse.js for intelligent fuzzy matching
- Searches across unit abbreviations, names, and categories
- Real-time dropdown with results
- Keyboard shortcut: Press "/" to focus search
- Press Escape to close
- Click outside to dismiss
- Shows measure category with color dot
- Top 10 results displayed
- Smart weighting: abbr (2x), singular/plural (1.5x), measure (1x)

💾 Conversion History (ConversionHistory.tsx):
- LocalStorage persistence (max 50 entries)
- Auto-saves conversions as user types
- Collapsible history panel
- Click to restore previous conversion
- Clear all with confirmation
- Shows relative time (just now, 5m ago, etc.)
- Live updates across tabs with storage events
- Custom event dispatch for same-window updates

🌓 Dark Mode Support:
- ThemeProvider with light/dark/system modes
- Persistent theme preference in localStorage
- Smooth theme transitions
- ThemeToggle component with animated sun/moon icons
- Gradient text adapts to theme
- System preference detection

 Favorites & Copy Features:
- Star button to favorite units (localStorage)
- Copy to clipboard with visual feedback
- Hover to reveal action buttons
- Check icon confirmation for 2 seconds
- Yellow star fill for favorited units

⌨️ Keyboard Shortcuts:
- "/" - Focus search input
- "Escape" - Close search, blur inputs
- More shortcuts ready to add (Tab, Ctrl+K, etc.)

📦 LocalStorage Utilities (lib/storage.ts):
- saveToHistory() - Add conversion record
- getHistory() - Retrieve history
- clearHistory() - Clear all history
- getFavorites() / addToFavorites() / removeFromFavorites()
- toggleFavorite() - Toggle favorite status
- Type-safe ConversionRecord interface
- Automatic error handling

🎨 Enhanced MainConverter:
- Integrated search at top
- Conversion history at bottom
- Copy & favorite buttons on each result card
- Hover effects with opacity transitions
- Auto-save to history on conversion
- Click history item to restore conversion
- Visual feedback for all interactions

📱 Updated Layout & Page:
- ThemeProvider wraps entire app
- suppressHydrationWarning for SSR
- Top navigation bar with theme toggle
- Keyboard hint for search
- Dark mode gradient text variants

Dependencies Added:
- fuse.js 7.1.0 - Fuzzy search engine
- lucide-react 0.553.0 - Icon library (Search, Copy, Star, Check, etc.)

Features Now Working:
 Intelligent fuzzy search across 187 units
 Conversion history with persistence
 Dark mode with system detection
 Copy any result to clipboard
 Favorite units for quick access
 Keyboard shortcuts (/, Esc)
 Smooth animations and transitions
 Mobile-responsive design

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-08 10:14:03 +01:00

247 lines
8.6 KiB
TypeScript

'use client';
import { useState, useEffect, useCallback } from 'react';
import { Copy, Star, Check } from 'lucide-react';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { Input } from '@/components/ui/input';
import { Button } from '@/components/ui/button';
import SearchUnits from './SearchUnits';
import ConversionHistory from './ConversionHistory';
import {
getAllMeasures,
getUnitsForMeasure,
convertToAll,
convertUnit,
formatMeasureName,
getCategoryColor,
type Measure,
type ConversionResult,
} from '@/lib/units';
import { parseNumberInput, formatNumber, cn } from '@/lib/utils';
import { saveToHistory, getFavorites, toggleFavorite } from '@/lib/storage';
export default function MainConverter() {
const [selectedMeasure, setSelectedMeasure] = useState<Measure>('length');
const [selectedUnit, setSelectedUnit] = useState<string>('m');
const [inputValue, setInputValue] = useState<string>('1');
const [conversions, setConversions] = useState<ConversionResult[]>([]);
const [favorites, setFavorites] = useState<string[]>([]);
const [copiedUnit, setCopiedUnit] = useState<string | null>(null);
const measures = getAllMeasures();
const units = getUnitsForMeasure(selectedMeasure);
// Load favorites
useEffect(() => {
setFavorites(getFavorites());
}, []);
// Update conversions when input changes
useEffect(() => {
const numValue = parseNumberInput(inputValue);
if (numValue !== null && selectedUnit) {
const results = convertToAll(numValue, selectedUnit);
setConversions(results);
} else {
setConversions([]);
}
}, [inputValue, selectedUnit]);
// Update selected unit when measure changes
useEffect(() => {
const availableUnits = getUnitsForMeasure(selectedMeasure);
if (availableUnits.length > 0) {
setSelectedUnit(availableUnits[0]);
}
}, [selectedMeasure]);
// Copy to clipboard
const copyToClipboard = useCallback(async (value: number, unit: string) => {
try {
await navigator.clipboard.writeText(`${formatNumber(value)} ${unit}`);
setCopiedUnit(unit);
setTimeout(() => setCopiedUnit(null), 2000);
} catch (error) {
console.error('Failed to copy:', error);
}
}, []);
// Toggle favorite
const handleToggleFavorite = useCallback((unit: string) => {
const isFavorite = toggleFavorite(unit);
setFavorites(getFavorites());
}, []);
// Save to history when conversion happens
useEffect(() => {
const numValue = parseNumberInput(inputValue);
if (numValue !== null && selectedUnit && conversions.length > 0) {
// Save first conversion to history
const firstConversion = conversions.find(c => c.unit !== selectedUnit);
if (firstConversion) {
saveToHistory({
from: { value: numValue, unit: selectedUnit },
to: { value: firstConversion.value, unit: firstConversion.unit },
measure: selectedMeasure,
});
// Dispatch custom event for same-window updates
window.dispatchEvent(new Event('historyUpdated'));
}
}
}, [inputValue, selectedUnit, conversions, selectedMeasure]);
// Handle search selection
const handleSearchSelect = useCallback((unit: string, measure: Measure) => {
setSelectedMeasure(measure);
setSelectedUnit(unit);
}, []);
// Handle history selection
const handleHistorySelect = useCallback((record: any) => {
setInputValue(record.from.value.toString());
setSelectedMeasure(record.measure);
setSelectedUnit(record.from.unit);
}, []);
return (
<div className="w-full max-w-6xl mx-auto space-y-6">
{/* Search */}
<div className="flex justify-center">
<SearchUnits onSelectUnit={handleSearchSelect} />
</div>
{/* Category Selection */}
<Card>
<CardHeader>
<CardTitle>Select Category</CardTitle>
</CardHeader>
<CardContent>
<div className="grid grid-cols-2 md:grid-cols-4 lg:grid-cols-6 gap-2">
{measures.map((measure) => (
<Button
key={measure}
variant={selectedMeasure === measure ? 'default' : 'outline'}
size="sm"
onClick={() => setSelectedMeasure(measure)}
className="justify-start"
style={{
backgroundColor:
selectedMeasure === measure
? `var(--color-${getCategoryColor(measure)})`
: undefined,
}}
>
{formatMeasureName(measure)}
</Button>
))}
</div>
</CardContent>
</Card>
{/* Input Section */}
<Card>
<CardHeader>
<CardTitle>Convert {formatMeasureName(selectedMeasure)}</CardTitle>
</CardHeader>
<CardContent className="space-y-4">
<div className="flex gap-4">
<div className="flex-1">
<label className="text-sm font-medium mb-2 block">Value</label>
<Input
type="text"
inputMode="decimal"
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
placeholder="Enter value"
className="text-lg"
/>
</div>
<div className="w-48">
<label className="text-sm font-medium mb-2 block">From Unit</label>
<select
value={selectedUnit}
onChange={(e) => setSelectedUnit(e.target.value)}
className="flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2"
>
{units.map((unit) => (
<option key={unit} value={unit}>
{unit}
</option>
))}
</select>
</div>
</div>
</CardContent>
</Card>
{/* Results */}
<Card>
<CardHeader>
<CardTitle>Conversions</CardTitle>
</CardHeader>
<CardContent>
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
{conversions.map((conversion) => {
const isFavorite = favorites.includes(conversion.unit);
const isCopied = copiedUnit === conversion.unit;
return (
<div
key={conversion.unit}
className="group relative p-4 rounded-lg border bg-card hover:bg-accent/50 transition-colors"
style={{
borderLeftWidth: '4px',
borderLeftColor: `var(--color-${getCategoryColor(selectedMeasure)})`,
}}
>
{/* Favorite & Copy buttons */}
<div className="absolute top-2 right-2 flex gap-1 opacity-0 group-hover:opacity-100 transition-opacity">
<Button
variant="ghost"
size="icon"
className="h-8 w-8"
onClick={() => handleToggleFavorite(conversion.unit)}
>
<Star
className={cn(
'h-4 w-4',
isFavorite && 'fill-yellow-400 text-yellow-400'
)}
/>
</Button>
<Button
variant="ghost"
size="icon"
className="h-8 w-8"
onClick={() => copyToClipboard(conversion.value, conversion.unit)}
>
{isCopied ? (
<Check className="h-4 w-4 text-green-500" />
) : (
<Copy className="h-4 w-4" />
)}
</Button>
</div>
<div className="text-sm text-muted-foreground mb-1">
{conversion.unitInfo.plural}
</div>
<div className="text-2xl font-bold">
{formatNumber(conversion.value)}
</div>
<div className="text-sm text-muted-foreground mt-1">
{conversion.unit}
</div>
</div>
);
})}
</div>
</CardContent>
</Card>
{/* Conversion History */}
<ConversionHistory onSelectConversion={handleHistorySelect} />
</div>
);
}