diff options
| author | bndw <ben@bdw.to> | 2023-04-26 17:49:25 -0700 |
|---|---|---|
| committer | bndw <ben@bdw.to> | 2023-04-26 17:49:25 -0700 |
| commit | 8686078ae801fdc15df5a40ee158a43373d37c1c (patch) | |
| tree | f8807fa764e917c6c4cdd30dcff31aa4bb060da8 /app/components/calculator.tsx | |
Initial commit
Diffstat (limited to 'app/components/calculator.tsx')
| -rw-r--r-- | app/components/calculator.tsx | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/app/components/calculator.tsx b/app/components/calculator.tsx new file mode 100644 index 0000000..006a755 --- /dev/null +++ b/app/components/calculator.tsx | |||
| @@ -0,0 +1,113 @@ | |||
| 1 | "use client"; | ||
| 2 | |||
| 3 | import { useEffect, useState } from "react"; | ||
| 4 | import styles from "./calculator.module.css"; | ||
| 5 | import { BitcoinPrice } from "../utils/bitcoin-price"; | ||
| 6 | |||
| 7 | export const Calculator = () => { | ||
| 8 | const [sats, setSats] = useState(""); | ||
| 9 | const [btc, setBtc] = useState(""); | ||
| 10 | const [usd, setUsd] = useState(""); | ||
| 11 | |||
| 12 | const formatCurrency = (val: string) => { | ||
| 13 | const formatter = new Intl.NumberFormat("en-US", { | ||
| 14 | style: "currency", | ||
| 15 | currency: "USD", | ||
| 16 | maximumFractionDigits: 2, | ||
| 17 | }); | ||
| 18 | return formatter.format(parseNumber(val)); | ||
| 19 | }; | ||
| 20 | |||
| 21 | const formatDecimal = (val: any) => { | ||
| 22 | return val.toLocaleString("fullwide", { | ||
| 23 | useGrouping: true, | ||
| 24 | maximumSignificantDigits: 6, | ||
| 25 | }); | ||
| 26 | }; | ||
| 27 | |||
| 28 | const parseNumber = (val: string) => { | ||
| 29 | return parseFloat(val.replace(/[^0-9|.]/g, "")); | ||
| 30 | }; | ||
| 31 | |||
| 32 | const btcPrice = parseNumber(BitcoinPrice()); | ||
| 33 | |||
| 34 | const handleUpdate = (type: string, v: string) => { | ||
| 35 | const val = parseNumber(v); | ||
| 36 | |||
| 37 | let newbtc: number; | ||
| 38 | switch (type) { | ||
| 39 | case "sats": | ||
| 40 | setSats(v); | ||
| 41 | |||
| 42 | if (isNaN(val) || v.endsWith(".")) { | ||
| 43 | return; | ||
| 44 | } | ||
| 45 | setSats(formatDecimal(val)); | ||
| 46 | |||
| 47 | newbtc = val / 100000000; | ||
| 48 | setBtc(formatDecimal(newbtc)); | ||
| 49 | setUsd(formatCurrency(formatDecimal(newbtc * btcPrice))); | ||
| 50 | break; | ||
| 51 | case "btc": | ||
| 52 | setBtc(v); | ||
| 53 | |||
| 54 | if (isNaN(val) || v.endsWith(".")) { | ||
| 55 | return; | ||
| 56 | } | ||
| 57 | setBtc(formatDecimal(val)); | ||
| 58 | |||
| 59 | setSats(formatDecimal(val * 100000000)); | ||
| 60 | setUsd(formatCurrency(formatDecimal(val * btcPrice))); | ||
| 61 | break; | ||
| 62 | case "usd": | ||
| 63 | setUsd(formatCurrency(v)); | ||
| 64 | if (isNaN(val)) { | ||
| 65 | return; | ||
| 66 | } | ||
| 67 | |||
| 68 | newbtc = val / btcPrice; | ||
| 69 | setBtc(formatDecimal(newbtc)); | ||
| 70 | setSats(formatDecimal(newbtc * 100000000)); | ||
| 71 | break; | ||
| 72 | } | ||
| 73 | }; | ||
| 74 | |||
| 75 | useEffect(() => { | ||
| 76 | // Initialize the calculator with some numbers | ||
| 77 | handleUpdate("sats", "1000"); | ||
| 78 | }, [btcPrice]); | ||
| 79 | |||
| 80 | return ( | ||
| 81 | <form className="flex flex-col"> | ||
| 82 | <label htmlFor="sats">Sats</label> | ||
| 83 | <input | ||
| 84 | className={styles.input} | ||
| 85 | onChange={(e) => handleUpdate("sats", e.target.value)} | ||
| 86 | value={sats} | ||
| 87 | type="text" | ||
| 88 | id="sats" | ||
| 89 | name="sats" | ||
| 90 | /> | ||
| 91 | |||
| 92 | <label htmlFor="btc">BTC</label> | ||
| 93 | <input | ||
| 94 | className={styles.input} | ||
| 95 | onChange={(e) => handleUpdate("btc", e.target.value)} | ||
| 96 | value={btc} | ||
| 97 | type="text" | ||
| 98 | id="btc" | ||
| 99 | name="btc" | ||
| 100 | /> | ||
| 101 | |||
| 102 | <label htmlFor="usd">USD</label> | ||
| 103 | <input | ||
| 104 | className={styles.input} | ||
| 105 | onChange={(e) => handleUpdate("usd", e.target.value)} | ||
| 106 | value={usd} | ||
| 107 | type="text" | ||
| 108 | id="usd" | ||
| 109 | name="usd" | ||
| 110 | /> | ||
| 111 | </form> | ||
| 112 | ); | ||
| 113 | }; | ||
