|
1 | 1 | 'use client' |
2 | 2 |
|
3 | 3 | import { ChangeEvent, useState } from 'react' |
| 4 | +import { ArrowRightLeft } from 'lucide-react' |
| 5 | + |
4 | 6 | import { Textarea } from '~/shared/components/textarea' |
| 7 | +import { CopyButton } from '~/shared/components/copy-button' |
| 8 | +import { Label } from '~/shared/components/label' |
5 | 9 | import { Button } from '~/shared/components/button' |
6 | | -import { Binary, Cpu } from 'lucide-react' |
7 | | -import { convertBases, Base } from './_lib/convert-bases' |
| 10 | + |
| 11 | +import { convertBases, Base, baseActions } from './_lib/convert-bases' |
8 | 12 | import { SelectBase } from './_components/select-base' |
9 | | -import { CopyButton } from '~/shared/components/copy-button' |
10 | | -import { toast } from 'sonner' |
11 | 13 |
|
12 | 14 | export default function Page() { |
13 | | - const [text, setText] = useState('') |
14 | | - const [from, setFrom] = useState<Base>('BIN') |
15 | | - const [to, setTo] = useState<Base>('BIN') |
16 | | - const [output, setOutput] = useState('') |
| 15 | + const [input, setInput] = useState('') |
| 16 | + const [target, setTarget] = useState<{ from: Base; to: Base }>({ |
| 17 | + from: 'BIN', |
| 18 | + to: 'HEX' |
| 19 | + }) |
| 20 | + |
| 21 | + function handleInput(e: ChangeEvent<HTMLTextAreaElement>) { |
| 22 | + // const value = e.target.value.replace(/[^\w\s]/gi, '').replace(/\s/g, '') |
| 23 | + const value = e.target.value |
17 | 24 |
|
18 | | - function handleInputText(e: ChangeEvent<HTMLTextAreaElement>): void { |
19 | | - setText(e.target.value.replace(/[^\w\s]/gi, '').replace(/\s/g, '')) |
| 25 | + setInput(value) |
20 | 26 | } |
21 | 27 |
|
22 | | - function handleConvertButton(): void { |
23 | | - try { |
24 | | - let converted = convertBases(text, from, to) |
25 | | - setOutput(converted) |
26 | | - } catch (e: any) { |
27 | | - toast.error(e.message) |
| 28 | + const { from, to } = target |
| 29 | + const output = convertBases(input, from, to) ?? '' |
| 30 | + const inputIsValid = input ? baseActions[from].validate(input) : true |
| 31 | + |
| 32 | + function handleSetTarget(value: Base, selectedTarget: 'from' | 'to') { |
| 33 | + if (selectedTarget === 'from') { |
| 34 | + const swap = value === target.to ? target.from : target.to |
| 35 | + |
| 36 | + setTarget({ from: value, to: swap }) |
| 37 | + } else if (selectedTarget === 'to') { |
| 38 | + const swap = value === target.from ? target.to : target.from |
| 39 | + |
| 40 | + setTarget({ to: value, from: swap }) |
28 | 41 | } |
29 | 42 | } |
30 | 43 |
|
| 44 | + const swapTargets = () => |
| 45 | + setTarget(curr => ({ from: curr.to, to: curr.from })) |
| 46 | + |
31 | 47 | return ( |
32 | 48 | <div className="space-y-12"> |
33 | | - <div className="space-y-5"> |
| 49 | + <div className="relative space-y-1"> |
| 50 | + <Label htmlFor="input">Input</Label> |
34 | 51 | <Textarea |
35 | | - value={text} |
36 | | - onChange={handleInputText} |
37 | | - placeholder="Text to be converted here..." |
38 | | - className="min-h-32 text-lg" |
| 52 | + id="input" |
| 53 | + value={input} |
| 54 | + onChange={handleInput} |
| 55 | + placeholder="Data to be converted here..." |
| 56 | + data-valid={inputIsValid} |
| 57 | + className="min-h-32 text-lg data-[valid='false']:border-red-500" |
39 | 58 | /> |
| 59 | + <span |
| 60 | + data-valid={inputIsValid} |
| 61 | + className="text-red-500 text-sm absolute bottom-1 left-1 transition-opacity opacity-0 data-[valid='false']:opacity-100" |
| 62 | + > |
| 63 | + {baseActions[from].dictionary.error} |
| 64 | + </span> |
40 | 65 | </div> |
41 | | - <div className="flex gap-2 flex-col lg:flex-row"> |
42 | | - <div className="w-full lg:w-1/2"> |
43 | | - <SelectBase label="From" value={from} onChange={setFrom} /> |
44 | | - </div> |
45 | | - <div className="w-full lg:w-1/2"> |
46 | | - <SelectBase label="To" value={to} onChange={setTo} /> |
| 66 | + <div className="flex gap-2 flex-col items-end lg:flex-row"> |
| 67 | + <div className="flex-1"> |
| 68 | + <SelectBase |
| 69 | + label="From" |
| 70 | + value={from} |
| 71 | + onChange={value => handleSetTarget(value, 'from')} |
| 72 | + /> |
47 | 73 | </div> |
48 | | - <div className="flex items-end justify-end"> |
49 | | - <Button |
50 | | - className="space-x-2" |
51 | | - disabled={text.length < 1} |
52 | | - onClick={handleConvertButton} |
53 | | - > |
54 | | - <Binary size="1em" /> |
55 | | - <span>Convert</span> |
56 | | - </Button> |
| 74 | + <Button onClick={swapTargets} className="text-xl" variant="ghost"> |
| 75 | + <ArrowRightLeft size="1em" strokeWidth="1.25px" /> |
| 76 | + </Button> |
| 77 | + <div className="flex-1"> |
| 78 | + <SelectBase |
| 79 | + label="To" |
| 80 | + value={to} |
| 81 | + onChange={value => handleSetTarget(value, 'to')} |
| 82 | + /> |
57 | 83 | </div> |
58 | 84 | </div> |
59 | | - <div> |
| 85 | + <div className="space-y-1"> |
| 86 | + <Label htmlFor="output">Output</Label> |
60 | 87 | <Textarea |
| 88 | + id="output" |
61 | 89 | value={output} |
62 | | - disabled={true} |
| 90 | + disabled={!output} |
63 | 91 | placeholder="Converted value here..." |
64 | 92 | className="min-h-32 text-lg" |
65 | 93 | /> |
66 | 94 | </div> |
67 | 95 | <div className="flex justify-center flex-wrap gap-5"> |
68 | 96 | <CopyButton |
69 | | - text={output} |
| 97 | + text={output || ''} |
| 98 | + disabled={!output} |
70 | 99 | toastMessage="Converted text copied to clipboard!" |
71 | 100 | variant="secondary" |
72 | 101 | /> |
|
0 commit comments