2026-02-22 21:35:53 +01:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import { useState, useEffect, useCallback } from 'react';
|
|
|
|
|
import { Copy, Star, Check, ArrowLeftRight, BarChart3 } from 'lucide-react';
|
2026-02-24 16:20:35 +01:00
|
|
|
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
|
|
|
|
import { Input } from '@/components/ui/input';
|
|
|
|
|
import { Button } from '@/components/ui/button';
|
|
|
|
|
import {
|
|
|
|
|
Select,
|
|
|
|
|
SelectContent,
|
|
|
|
|
SelectItem,
|
|
|
|
|
SelectTrigger,
|
|
|
|
|
SelectValue,
|
|
|
|
|
} from '@/components/ui/select';
|
2026-02-22 21:35:53 +01:00
|
|
|
import SearchUnits from './SearchUnits';
|
|
|
|
|
import VisualComparison from './VisualComparison';
|
2026-02-25 10:06:50 +01:00
|
|
|
|
2026-02-22 21:35:53 +01:00
|
|
|
import {
|
|
|
|
|
getAllMeasures,
|
|
|
|
|
getUnitsForMeasure,
|
|
|
|
|
convertToAll,
|
|
|
|
|
convertUnit,
|
|
|
|
|
formatMeasureName,
|
|
|
|
|
getCategoryColor,
|
|
|
|
|
getCategoryColorHex,
|
|
|
|
|
type Measure,
|
|
|
|
|
type ConversionResult,
|
|
|
|
|
} from '@/lib/units/units';
|
2026-02-23 00:40:45 +01:00
|
|
|
import { parseNumberInput, formatNumber, cn } from '@/lib/utils';
|
2026-02-23 07:45:15 +01:00
|
|
|
import { getFavorites, toggleFavorite } from '@/lib/units/storage';
|
2026-02-22 21:35:53 +01:00
|
|
|
|
|
|
|
|
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 [favorites, setFavorites] = useState<string[]>([]);
|
|
|
|
|
const [copiedUnit, setCopiedUnit] = useState<string | null>(null);
|
|
|
|
|
const [showVisualComparison, setShowVisualComparison] = useState(false);
|
|
|
|
|
const [isDragging, setIsDragging] = useState(false);
|
|
|
|
|
|
|
|
|
|
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]);
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
}, [selectedUnit, targetUnit, inputValue]);
|
|
|
|
|
|
|
|
|
|
// 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());
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
// Handle search selection
|
|
|
|
|
const handleSearchSelect = useCallback((unit: string, measure: Measure) => {
|
|
|
|
|
setSelectedMeasure(measure);
|
|
|
|
|
setSelectedUnit(unit);
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
// Handle value change from draggable bars
|
|
|
|
|
const handleValueChange = useCallback((value: number, unit: string, dragging: boolean) => {
|
|
|
|
|
setIsDragging(dragging);
|
|
|
|
|
|
|
|
|
|
// 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]);
|
|
|
|
|
|
|
|
|
|
return (
|
2026-02-23 08:09:51 +01:00
|
|
|
<div className="w-full space-y-8">
|
2026-02-22 21:35:53 +01:00
|
|
|
|
2026-02-23 08:09:51 +01:00
|
|
|
{/* Quick Access Row */}
|
|
|
|
|
<div className="flex flex-col md:flex-row md:items-center gap-4 justify-between bg-card p-4 rounded-lg border">
|
|
|
|
|
<div className="flex-1">
|
|
|
|
|
<SearchUnits onSelectUnit={handleSearchSelect} />
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="w-full md:w-64 shrink-0">
|
|
|
|
|
<Select
|
|
|
|
|
value={selectedMeasure}
|
2026-02-24 16:20:35 +01:00
|
|
|
onValueChange={(value) => setSelectedMeasure(value as Measure)}
|
2026-02-23 08:09:51 +01:00
|
|
|
>
|
2026-02-24 16:20:35 +01:00
|
|
|
<SelectTrigger
|
2026-02-24 16:58:17 +01:00
|
|
|
className="w-full"
|
2026-02-24 16:20:35 +01:00
|
|
|
style={{
|
|
|
|
|
borderLeft: `4px solid ${getCategoryColorHex(selectedMeasure)}`,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<SelectValue placeholder="Measure" />
|
|
|
|
|
</SelectTrigger>
|
|
|
|
|
<SelectContent>
|
|
|
|
|
{measures.map((measure) => (
|
|
|
|
|
<SelectItem key={measure} value={measure}>
|
|
|
|
|
{formatMeasureName(measure)}
|
|
|
|
|
</SelectItem>
|
|
|
|
|
))}
|
|
|
|
|
</SelectContent>
|
2026-02-23 08:09:51 +01:00
|
|
|
</Select>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
2026-02-23 07:48:06 +01:00
|
|
|
{/* Main Converter Card */}
|
2026-02-22 21:35:53 +01:00
|
|
|
<Card>
|
2026-02-23 08:09:51 +01:00
|
|
|
<CardHeader>
|
|
|
|
|
<CardTitle>Convert {formatMeasureName(selectedMeasure)}</CardTitle>
|
2026-02-22 21:35:53 +01:00
|
|
|
</CardHeader>
|
2026-02-23 08:09:51 +01:00
|
|
|
<CardContent className="space-y-6 pt-0">
|
2026-02-23 14:00:09 +01:00
|
|
|
{/* Input row, stacks vertically on mobile, horizontal on desktop */}
|
|
|
|
|
<div className="flex flex-col gap-4 md:flex-row md:items-end md:gap-2">
|
|
|
|
|
{/* Value Input */}
|
|
|
|
|
<div className="flex-1 w-full">
|
2026-02-22 21:35:53 +01:00
|
|
|
<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"
|
2026-02-23 14:00:09 +01:00
|
|
|
className={cn("text-lg", "w-full", "max-w-full")}
|
2026-02-22 21:35:53 +01:00
|
|
|
/>
|
|
|
|
|
</div>
|
2026-02-23 14:00:09 +01:00
|
|
|
{/* From Unit Select */}
|
|
|
|
|
<div className="w-full md:w-40">
|
2026-02-22 21:35:53 +01:00
|
|
|
<label className="text-sm font-medium mb-2 block">From</label>
|
2026-02-23 07:56:16 +01:00
|
|
|
<Select
|
2026-02-22 21:35:53 +01:00
|
|
|
value={selectedUnit}
|
2026-02-24 16:20:35 +01:00
|
|
|
onValueChange={(value) => setSelectedUnit(value)}
|
2026-02-22 21:35:53 +01:00
|
|
|
>
|
2026-02-24 16:58:17 +01:00
|
|
|
<SelectTrigger className="w-full">
|
2026-02-24 16:20:35 +01:00
|
|
|
<SelectValue placeholder="From" />
|
|
|
|
|
</SelectTrigger>
|
|
|
|
|
<SelectContent>
|
|
|
|
|
{units.map((unit) => (
|
|
|
|
|
<SelectItem key={unit} value={unit}>
|
|
|
|
|
{unit}
|
|
|
|
|
</SelectItem>
|
|
|
|
|
))}
|
|
|
|
|
</SelectContent>
|
2026-02-23 07:56:16 +01:00
|
|
|
</Select>
|
2026-02-22 21:35:53 +01:00
|
|
|
</div>
|
2026-02-23 14:00:09 +01:00
|
|
|
{/* Swap Button */}
|
2026-02-22 21:35:53 +01:00
|
|
|
<Button
|
|
|
|
|
variant="outline"
|
|
|
|
|
size="icon"
|
|
|
|
|
onClick={handleSwapUnits}
|
2026-02-23 14:00:09 +01:00
|
|
|
className="flex-shrink-0 w-full md:w-40" // Make button full width on mobile too
|
2026-02-22 21:35:53 +01:00
|
|
|
title="Swap units"
|
|
|
|
|
>
|
|
|
|
|
<ArrowLeftRight className="h-4 w-4" />
|
|
|
|
|
</Button>
|
2026-02-23 14:00:09 +01:00
|
|
|
{/* To Unit Select */}
|
|
|
|
|
<div className="w-full md:w-40">
|
2026-02-22 21:35:53 +01:00
|
|
|
<label className="text-sm font-medium mb-2 block">To</label>
|
2026-02-23 07:56:16 +01:00
|
|
|
<Select
|
2026-02-22 21:35:53 +01:00
|
|
|
value={targetUnit}
|
2026-02-24 16:20:35 +01:00
|
|
|
onValueChange={(value) => setTargetUnit(value)}
|
2026-02-22 21:35:53 +01:00
|
|
|
>
|
2026-02-24 16:58:17 +01:00
|
|
|
<SelectTrigger className="w-full">
|
2026-02-24 16:20:35 +01:00
|
|
|
<SelectValue placeholder="To" />
|
|
|
|
|
</SelectTrigger>
|
|
|
|
|
<SelectContent>
|
|
|
|
|
{units.map((unit) => (
|
|
|
|
|
<SelectItem key={unit} value={unit}>
|
|
|
|
|
{unit}
|
|
|
|
|
</SelectItem>
|
|
|
|
|
))}
|
|
|
|
|
</SelectContent>
|
2026-02-23 07:56:16 +01:00
|
|
|
</Select>
|
2026-02-22 21:35:53 +01:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Quick result */}
|
|
|
|
|
{parseNumberInput(inputValue) !== null && (
|
|
|
|
|
<div className="p-4 rounded-lg bg-accent/50 border-l-4" style={{
|
|
|
|
|
borderLeftColor: getCategoryColorHex(selectedMeasure),
|
|
|
|
|
}}>
|
|
|
|
|
<div className="text-sm text-muted-foreground">Result</div>
|
|
|
|
|
<div className="text-3xl font-bold mt-1" style={{
|
|
|
|
|
color: getCategoryColorHex(selectedMeasure),
|
|
|
|
|
}}>
|
|
|
|
|
{formatNumber(convertUnit(parseNumberInput(inputValue)!, selectedUnit, targetUnit))} {targetUnit}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</CardContent>
|
|
|
|
|
</Card>
|
|
|
|
|
|
|
|
|
|
{/* Results */}
|
|
|
|
|
<Card>
|
|
|
|
|
<CardHeader>
|
|
|
|
|
<div className="flex items-center justify-between">
|
|
|
|
|
<CardTitle>All Conversions</CardTitle>
|
|
|
|
|
<Button
|
|
|
|
|
variant="outline"
|
|
|
|
|
size="sm"
|
|
|
|
|
onClick={() => setShowVisualComparison(!showVisualComparison)}
|
|
|
|
|
>
|
|
|
|
|
<BarChart3 className="h-4 w-4 mr-2" />
|
|
|
|
|
{showVisualComparison ? 'Grid View' : 'Chart View'}
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</CardHeader>
|
|
|
|
|
<CardContent>
|
|
|
|
|
{showVisualComparison ? (
|
|
|
|
|
<VisualComparison
|
|
|
|
|
conversions={conversions}
|
|
|
|
|
color={getCategoryColorHex(selectedMeasure)}
|
|
|
|
|
onValueChange={handleValueChange}
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<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: getCategoryColorHex(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>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|