refactor: refactor units tool to match calculate blueprint
Rewrites all three units components to use the same glass panel layout, lg:grid-cols-5 two-panel split, and interactive patterns established by the calculate tool. - MainConverter: category sidebar (left 2/5) replaces Select dropdown; converter card + scrollable conversion grid (right 3/5); mobile 'Category | Convert' tab switcher; clickable conversion cards set target unit; glass Grid/Chart toggle - SearchUnits: native input with glass border, glass dropdown panel, compact result rows matching font selector style - VisualComparison: polished gradient bars, tighter spacing, cleaner value display; all drag logic preserved Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,11 +1,7 @@
|
||||
'use client';
|
||||
|
||||
import { useState, useEffect, useCallback } from 'react';
|
||||
import { ArrowLeftRight, BarChart3 } from 'lucide-react';
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { ArrowLeftRight, BarChart3, Grid3X3 } from 'lucide-react';
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
@@ -15,7 +11,6 @@ import {
|
||||
} from '@/components/ui/select';
|
||||
import SearchUnits from './SearchUnits';
|
||||
import VisualComparison from './VisualComparison';
|
||||
|
||||
import {
|
||||
getAllMeasures,
|
||||
getUnitsForMeasure,
|
||||
@@ -27,211 +22,313 @@ import {
|
||||
} from '@/lib/units/units';
|
||||
import { parseNumberInput, formatNumber, cn } from '@/lib/utils';
|
||||
|
||||
type Tab = 'category' | 'convert';
|
||||
|
||||
const CATEGORY_ICONS: Partial<Record<Measure, string>> = {
|
||||
length: '📏', mass: '⚖️', temperature: '🌡️', speed: '⚡', time: '⏱️',
|
||||
area: '⬛', volume: '🧊', digital: '💾', energy: '⚡', pressure: '🔵',
|
||||
power: '🔆', frequency: '〰️', angle: '📐', current: '⚡', voltage: '🔌',
|
||||
};
|
||||
|
||||
export default function MainConverter() {
|
||||
const [selectedMeasure, setSelectedMeasure] = useState<Measure>('length');
|
||||
const [selectedUnit, setSelectedUnit] = useState<string>('m');
|
||||
const [targetUnit, setTargetUnit] = useState<string>('ft');
|
||||
const [inputValue, setInputValue] = useState<string>('1');
|
||||
const [conversions, setConversions] = useState<ConversionResult[]>([]);
|
||||
const [showVisualComparison, setShowVisualComparison] = useState(false);
|
||||
const [isDragging, setIsDragging] = useState(false);
|
||||
const [showChart, setShowChart] = useState(false);
|
||||
const [tab, setTab] = useState<Tab>('category');
|
||||
|
||||
const measures = getAllMeasures();
|
||||
const units = getUnitsForMeasure(selectedMeasure);
|
||||
|
||||
// Update conversions when input changes
|
||||
useEffect(() => {
|
||||
const numValue = parseNumberInput(inputValue);
|
||||
if (numValue !== null && selectedUnit) {
|
||||
const results = convertToAll(numValue, selectedUnit);
|
||||
setConversions(results);
|
||||
setConversions(convertToAll(numValue, selectedUnit));
|
||||
} else {
|
||||
setConversions([]);
|
||||
}
|
||||
}, [inputValue, selectedUnit]);
|
||||
|
||||
// Update selected unit when measure changes
|
||||
useEffect(() => {
|
||||
const availableUnits = getUnitsForMeasure(selectedMeasure);
|
||||
if (availableUnits.length > 0) {
|
||||
setSelectedUnit(availableUnits[0]);
|
||||
setTargetUnit(availableUnits[1] || availableUnits[0]);
|
||||
setTargetUnit(availableUnits[1] ?? availableUnits[0]);
|
||||
}
|
||||
}, [selectedMeasure]);
|
||||
|
||||
// Swap units
|
||||
const handleSwapUnits = useCallback(() => {
|
||||
const temp = selectedUnit;
|
||||
setSelectedUnit(targetUnit);
|
||||
setTargetUnit(temp);
|
||||
|
||||
// Convert the value
|
||||
const numValue = parseNumberInput(inputValue);
|
||||
if (numValue !== null) {
|
||||
const converted = convertUnit(numValue, selectedUnit, targetUnit);
|
||||
setInputValue(converted.toString());
|
||||
setInputValue(convertUnit(numValue, selectedUnit, targetUnit).toString());
|
||||
}
|
||||
setSelectedUnit(targetUnit);
|
||||
setTargetUnit(selectedUnit);
|
||||
}, [selectedUnit, targetUnit, inputValue]);
|
||||
|
||||
// Handle search selection
|
||||
const handleSearchSelect = useCallback((unit: string, measure: Measure) => {
|
||||
setSelectedMeasure(measure);
|
||||
setSelectedUnit(unit);
|
||||
setTab('convert');
|
||||
}, []);
|
||||
|
||||
// Handle value change from draggable bars
|
||||
const handleValueChange = useCallback((value: number, unit: string, dragging: boolean) => {
|
||||
setIsDragging(dragging);
|
||||
const handleCategorySelect = useCallback((measure: Measure) => {
|
||||
setSelectedMeasure(measure);
|
||||
setTab('convert');
|
||||
}, []);
|
||||
|
||||
// Convert the dragged unit's value back to the currently selected unit
|
||||
// This keeps the source unit stable while updating the value
|
||||
const convertedValue = convertUnit(value, unit, selectedUnit);
|
||||
setInputValue(convertedValue.toString());
|
||||
// Keep selectedUnit unchanged
|
||||
}, [selectedUnit]);
|
||||
const handleValueChange = useCallback(
|
||||
(value: number, unit: string, _dragging: boolean) => {
|
||||
setInputValue(convertUnit(value, unit, selectedUnit).toString());
|
||||
},
|
||||
[selectedUnit]
|
||||
);
|
||||
|
||||
const resultValue = (() => {
|
||||
const n = parseNumberInput(inputValue);
|
||||
return n !== null ? convertUnit(n, selectedUnit, targetUnit) : null;
|
||||
})();
|
||||
|
||||
return (
|
||||
<div className="w-full space-y-6">
|
||||
<div className="flex flex-col gap-4">
|
||||
|
||||
{/* Quick Access Row */}
|
||||
<Card>
|
||||
<CardContent className="flex flex-col md:flex-row md:items-center gap-3 justify-between">
|
||||
<div className="flex-1">
|
||||
{/* ── Mobile tab switcher ────────────────────────────────── */}
|
||||
<div className="flex lg:hidden glass rounded-xl p-1 gap-1">
|
||||
{(['category', 'convert'] as Tab[]).map((t) => (
|
||||
<button
|
||||
key={t}
|
||||
onClick={() => setTab(t)}
|
||||
className={cn(
|
||||
'flex-1 py-2.5 rounded-lg text-sm font-medium capitalize transition-all',
|
||||
tab === t
|
||||
? 'bg-primary text-primary-foreground shadow-sm'
|
||||
: 'text-muted-foreground hover:text-foreground'
|
||||
)}
|
||||
>
|
||||
{t === 'category' ? 'Category' : 'Convert'}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* ── Main layout ────────────────────────────────────────── */}
|
||||
<div
|
||||
className="grid grid-cols-1 lg:grid-cols-5 gap-4"
|
||||
style={{ height: 'calc(100svh - 220px)', minHeight: '620px' }}
|
||||
>
|
||||
|
||||
{/* Left panel: search + categories */}
|
||||
<div
|
||||
className={cn(
|
||||
'lg:col-span-2 flex flex-col gap-3 overflow-hidden',
|
||||
tab !== 'category' && 'hidden lg:flex'
|
||||
)}
|
||||
>
|
||||
{/* Search */}
|
||||
<div className="glass rounded-xl p-4 shrink-0">
|
||||
<span className="text-[10px] font-semibold text-muted-foreground uppercase tracking-widest block mb-2">
|
||||
Search
|
||||
</span>
|
||||
<SearchUnits onSelectUnit={handleSearchSelect} />
|
||||
</div>
|
||||
<div className="w-full md:w-56 shrink-0">
|
||||
<Select
|
||||
value={selectedMeasure}
|
||||
onValueChange={(value) => setSelectedMeasure(value as Measure)}
|
||||
>
|
||||
<SelectTrigger className="w-full">
|
||||
<SelectValue placeholder="Measure" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{measures.map((measure) => (
|
||||
<SelectItem key={measure} value={measure}>
|
||||
{formatMeasureName(measure)}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Main Converter Card */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Convert {formatMeasureName(selectedMeasure)}</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="flex flex-col gap-3 md:flex-row md:items-end md:gap-2">
|
||||
<div className="flex-1 w-full">
|
||||
<Label className="text-xs mb-1.5">Value</Label>
|
||||
<Input
|
||||
{/* Category list */}
|
||||
<div className="glass rounded-xl p-3 flex flex-col flex-1 min-h-0 overflow-hidden">
|
||||
<div className="flex items-center justify-between mb-3 shrink-0">
|
||||
<span className="text-[10px] font-semibold text-muted-foreground uppercase tracking-widest">
|
||||
Categories
|
||||
</span>
|
||||
<span className="text-[10px] text-muted-foreground/35 font-mono tabular-nums">
|
||||
{measures.length}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div className="flex-1 min-h-0 overflow-y-auto scrollbar-thin scrollbar-thumb-primary/20 scrollbar-track-transparent space-y-0.5 pr-0.5">
|
||||
{measures.map((measure) => {
|
||||
const isSelected = selectedMeasure === measure;
|
||||
const unitCount = getUnitsForMeasure(measure).length;
|
||||
return (
|
||||
<button
|
||||
key={measure}
|
||||
onClick={() => handleCategorySelect(measure)}
|
||||
className={cn(
|
||||
'w-full flex items-center gap-2 px-2 py-1.5 rounded-lg transition-all text-left',
|
||||
'border-l-2',
|
||||
isSelected
|
||||
? 'bg-primary/10 border-primary text-primary'
|
||||
: 'border-transparent text-foreground/65 hover:bg-primary/8 hover:text-foreground'
|
||||
)}
|
||||
>
|
||||
<span className="text-xs leading-none shrink-0 opacity-70">
|
||||
{CATEGORY_ICONS[measure] ?? '📦'}
|
||||
</span>
|
||||
<span className="flex-1 text-xs font-mono truncate">{formatMeasureName(measure)}</span>
|
||||
<span
|
||||
className={cn(
|
||||
'text-[10px] font-mono tabular-nums shrink-0 px-1.5 py-0.5 rounded',
|
||||
isSelected
|
||||
? 'bg-primary/20 text-primary'
|
||||
: 'bg-muted/40 text-muted-foreground/40'
|
||||
)}
|
||||
>
|
||||
{unitCount}
|
||||
</span>
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Right panel: converter + results */}
|
||||
<div
|
||||
className={cn(
|
||||
'lg:col-span-3 flex flex-col gap-3 overflow-hidden',
|
||||
tab !== 'convert' && 'hidden lg:flex'
|
||||
)}
|
||||
>
|
||||
{/* Converter card */}
|
||||
<div className="glass rounded-xl p-4 shrink-0">
|
||||
<span className="text-[10px] font-semibold text-muted-foreground uppercase tracking-widest block mb-3">
|
||||
Convert {formatMeasureName(selectedMeasure)}
|
||||
</span>
|
||||
|
||||
{/* Input row */}
|
||||
<div className="flex items-center gap-2">
|
||||
{/* Value input */}
|
||||
<input
|
||||
type="text"
|
||||
inputMode="decimal"
|
||||
value={inputValue}
|
||||
onChange={(e) => setInputValue(e.target.value)}
|
||||
placeholder="Enter value"
|
||||
className={cn("text-lg", "w-full", "max-w-full")}
|
||||
placeholder="0"
|
||||
className="flex-1 min-w-0 bg-transparent border border-border/40 rounded-lg px-3 py-2 text-sm font-mono outline-none focus:border-primary/50 transition-colors placeholder:text-muted-foreground/30 tabular-nums"
|
||||
/>
|
||||
</div>
|
||||
<div className="w-full md:w-36">
|
||||
<Label className="text-xs mb-1.5">From</Label>
|
||||
<Select
|
||||
value={selectedUnit}
|
||||
onValueChange={(value) => setSelectedUnit(value)}
|
||||
>
|
||||
<SelectTrigger className="w-full">
|
||||
<SelectValue placeholder="From" />
|
||||
|
||||
{/* From unit */}
|
||||
<Select value={selectedUnit} onValueChange={setSelectedUnit}>
|
||||
<SelectTrigger className="w-28 h-9 shrink-0 text-xs border-border/30 bg-transparent hover:border-primary/30 transition-colors font-mono">
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{units.map((unit) => (
|
||||
<SelectItem key={unit} value={unit}>
|
||||
{unit}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="icon"
|
||||
onClick={handleSwapUnits}
|
||||
className="shrink-0 w-full md:w-7"
|
||||
title="Swap units"
|
||||
>
|
||||
<ArrowLeftRight className="h-3.5 w-3.5" />
|
||||
</Button>
|
||||
<div className="w-full md:w-36">
|
||||
<Label className="text-xs mb-1.5">To</Label>
|
||||
<Select
|
||||
value={targetUnit}
|
||||
onValueChange={(value) => setTargetUnit(value)}
|
||||
>
|
||||
<SelectTrigger className="w-full">
|
||||
<SelectValue placeholder="To" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{units.map((unit) => (
|
||||
<SelectItem key={unit} value={unit}>
|
||||
<SelectItem key={unit} value={unit} className="font-mono text-xs">
|
||||
{unit}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
|
||||
{/* Swap */}
|
||||
<button
|
||||
onClick={handleSwapUnits}
|
||||
title="Swap units"
|
||||
className="shrink-0 w-8 h-8 flex items-center justify-center glass rounded-lg border border-border/30 text-muted-foreground hover:text-primary hover:border-primary/30 hover:bg-primary/10 transition-all"
|
||||
>
|
||||
<ArrowLeftRight className="w-3.5 h-3.5" />
|
||||
</button>
|
||||
|
||||
{/* To unit */}
|
||||
<Select value={targetUnit} onValueChange={setTargetUnit}>
|
||||
<SelectTrigger className="w-28 h-9 shrink-0 text-xs border-border/30 bg-transparent hover:border-primary/30 transition-colors font-mono">
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{units.map((unit) => (
|
||||
<SelectItem key={unit} value={unit} className="font-mono text-xs">
|
||||
{unit}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
{/* Result display */}
|
||||
{resultValue !== null && (
|
||||
<div className="mt-3 px-3 py-2.5 rounded-lg bg-primary/5 border border-primary/15">
|
||||
<div className="text-[10px] text-muted-foreground/50 font-mono mb-0.5">Result</div>
|
||||
<div className="flex items-baseline gap-2">
|
||||
<span className="text-xl font-bold tabular-nums font-mono bg-gradient-to-r from-primary to-pink-400 bg-clip-text text-transparent">
|
||||
{formatNumber(resultValue)}
|
||||
</span>
|
||||
<span className="text-sm text-muted-foreground/60 font-mono">{targetUnit}</span>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{parseNumberInput(inputValue) !== null && (
|
||||
<div className="p-3 rounded-lg bg-primary/5 border border-primary/15">
|
||||
<div className="text-xs text-muted-foreground mb-0.5">Result</div>
|
||||
<div className="text-2xl font-bold text-primary tabular-nums">
|
||||
{formatNumber(convertUnit(parseNumberInput(inputValue)!, selectedUnit, targetUnit))} <span className="text-base font-medium text-muted-foreground">{targetUnit}</span>
|
||||
{/* All conversions */}
|
||||
<div className="glass rounded-xl p-3 flex flex-col flex-1 min-h-0 overflow-hidden">
|
||||
<div className="flex items-center justify-between mb-3 shrink-0">
|
||||
<span className="text-[10px] font-semibold text-muted-foreground uppercase tracking-widest">
|
||||
All Conversions
|
||||
</span>
|
||||
{/* Grid / Chart toggle */}
|
||||
<div className="flex glass rounded-lg p-0.5 gap-0.5">
|
||||
<button
|
||||
onClick={() => setShowChart(false)}
|
||||
title="Grid view"
|
||||
className={cn(
|
||||
'flex items-center gap-1 px-2 py-1 rounded-md text-xs transition-all',
|
||||
!showChart
|
||||
? 'bg-primary text-primary-foreground shadow-sm'
|
||||
: 'text-muted-foreground hover:text-foreground'
|
||||
)}
|
||||
>
|
||||
<Grid3X3 className="w-3 h-3" />
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setShowChart(true)}
|
||||
title="Chart view"
|
||||
className={cn(
|
||||
'flex items-center gap-1 px-2 py-1 rounded-md text-xs transition-all',
|
||||
showChart
|
||||
? 'bg-primary text-primary-foreground shadow-sm'
|
||||
: 'text-muted-foreground hover:text-foreground'
|
||||
)}
|
||||
>
|
||||
<BarChart3 className="w-3 h-3" />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Results */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<div className="flex items-center justify-between">
|
||||
<CardTitle>All Conversions</CardTitle>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="xs"
|
||||
onClick={() => setShowVisualComparison(!showVisualComparison)}
|
||||
>
|
||||
<BarChart3 className="h-3 w-3 mr-1" />
|
||||
{showVisualComparison ? 'Grid' : 'Chart'}
|
||||
</Button>
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{showVisualComparison ? (
|
||||
<VisualComparison
|
||||
conversions={conversions}
|
||||
onValueChange={handleValueChange}
|
||||
/>
|
||||
) : (
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-3">
|
||||
{conversions.map((conversion) => (
|
||||
<div
|
||||
key={conversion.unit}
|
||||
className="p-3 rounded-lg border border-border/50 hover:border-primary/30 transition-colors"
|
||||
>
|
||||
<div className="text-xs text-muted-foreground">{conversion.unitInfo.plural}</div>
|
||||
<div className="text-lg font-bold tabular-nums mt-0.5">{formatNumber(conversion.value)}</div>
|
||||
<div className="text-xs text-muted-foreground">{conversion.unit}</div>
|
||||
<div className="flex-1 min-h-0 overflow-y-auto scrollbar-thin scrollbar-thumb-primary/20 scrollbar-track-transparent pr-0.5">
|
||||
{showChart ? (
|
||||
<VisualComparison conversions={conversions} onValueChange={handleValueChange} />
|
||||
) : (
|
||||
<div className="grid grid-cols-2 lg:grid-cols-3 gap-2">
|
||||
{conversions.map((conversion) => {
|
||||
const isTarget = targetUnit === conversion.unit;
|
||||
return (
|
||||
<button
|
||||
key={conversion.unit}
|
||||
onClick={() => setTargetUnit(conversion.unit)}
|
||||
className={cn(
|
||||
'p-2.5 rounded-lg border text-left transition-all',
|
||||
isTarget
|
||||
? 'border-primary/50 bg-primary/10 text-primary'
|
||||
: 'border-border/30 hover:border-primary/30 hover:bg-primary/6 text-foreground/75'
|
||||
)}
|
||||
>
|
||||
<div className="text-[10px] text-muted-foreground/50 font-mono truncate mb-0.5">
|
||||
{conversion.unitInfo.plural}
|
||||
</div>
|
||||
<div className="text-sm font-bold tabular-nums font-mono leading-none">
|
||||
{formatNumber(conversion.value)}
|
||||
</div>
|
||||
<div className="text-[10px] text-muted-foreground/40 font-mono mt-0.5">
|
||||
{conversion.unit}
|
||||
</div>
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
))}
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user