Files
units-ui/components/converter/ConversionHistory.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

123 lines
3.7 KiB
TypeScript

'use client';
import { useState, useEffect } from 'react';
import { History, Trash2, ArrowRight } from 'lucide-react';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import {
getHistory,
clearHistory,
type ConversionRecord,
} from '@/lib/storage';
import { getRelativeTime, formatNumber } from '@/lib/utils';
import { formatMeasureName } from '@/lib/units';
interface ConversionHistoryProps {
onSelectConversion?: (record: ConversionRecord) => void;
}
export default function ConversionHistory({
onSelectConversion,
}: ConversionHistoryProps) {
const [history, setHistory] = useState<ConversionRecord[]>([]);
const [isOpen, setIsOpen] = useState(false);
useEffect(() => {
loadHistory();
// Listen for storage changes
const handleStorageChange = () => {
loadHistory();
};
window.addEventListener('storage', handleStorageChange);
// Also listen for custom event from same window
window.addEventListener('historyUpdated', handleStorageChange);
return () => {
window.removeEventListener('storage', handleStorageChange);
window.removeEventListener('historyUpdated', handleStorageChange);
};
}, []);
const loadHistory = () => {
setHistory(getHistory());
};
const handleClearHistory = () => {
if (confirm('Clear all conversion history?')) {
clearHistory();
loadHistory();
}
};
if (history.length === 0) {
return null;
}
return (
<Card>
<CardHeader>
<div className="flex items-center justify-between">
<CardTitle className="flex items-center gap-2">
<History className="h-5 w-5" />
Recent Conversions
</CardTitle>
<div className="flex items-center gap-2">
<Button
variant="ghost"
size="sm"
onClick={() => setIsOpen(!isOpen)}
>
{isOpen ? 'Hide' : `Show (${history.length})`}
</Button>
{history.length > 0 && (
<Button
variant="ghost"
size="icon"
onClick={handleClearHistory}
className="h-8 w-8"
>
<Trash2 className="h-4 w-4" />
</Button>
)}
</div>
</div>
</CardHeader>
{isOpen && (
<CardContent>
<div className="space-y-2">
{history.map((record) => (
<button
key={record.id}
onClick={() => onSelectConversion?.(record)}
className="w-full p-3 rounded-lg border hover:bg-accent transition-colors text-left"
>
<div className="flex items-center justify-between gap-4">
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2 text-sm font-medium">
<span className="truncate">
{formatNumber(record.from.value)} {record.from.unit}
</span>
<ArrowRight className="h-3 w-3 flex-shrink-0" />
<span className="truncate">
{formatNumber(record.to.value)} {record.to.unit}
</span>
</div>
<div className="flex items-center gap-2 text-xs text-muted-foreground mt-1">
<span>{formatMeasureName(record.measure as any)}</span>
<span></span>
<span>{getRelativeTime(record.timestamp)}</span>
</div>
</div>
</div>
</button>
))}
</div>
</CardContent>
)}
</Card>
);
}