1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
"use client";
import { useEffect, useState } from "react";
import styles from "./calculator.module.css";
import { BitcoinPrice } from "../utils/bitcoin-price";
export const Calculator = () => {
const [querySats, setQuerySats] = useState("");
const [queryBtc, setQueryBtc] = useState("");
const [queryUsd, setQueryUsd] = useState("");
const [sats, setSats] = useState("");
const [btc, setBtc] = useState("");
const [usd, setUsd] = useState("");
const formatDecimal = (val: any) => {
return val.toLocaleString("fullwide", {
useGrouping: true,
maximumSignificantDigits: 6,
});
};
const parseNumber = (val: string) => {
return parseFloat(val.replace(/[^0-9|.]/g, ""));
};
const btcPrice = parseNumber(BitcoinPrice());
const handleUpdate = (type: string, v: string) => {
const val = parseNumber(v);
let newbtc: number;
switch (type) {
case "sats":
setSats(v);
if (isNaN(val) || v.endsWith(".")) {
return;
}
setSats(formatDecimal(val));
newbtc = val / 100000000;
setBtc(formatDecimal(newbtc));
setUsd(formatDecimal(newbtc * btcPrice));
break;
case "btc":
setBtc(v);
if (isNaN(val) || v.endsWith(".")) {
return;
}
setBtc(formatDecimal(val));
setSats(formatDecimal(val * 100000000));
setUsd(formatDecimal(val * btcPrice));
break;
case "usd":
setUsd(v);
if (isNaN(val) || v.endsWith(".")) {
return;
}
setUsd(formatDecimal(val));
newbtc = val / btcPrice;
setBtc(formatDecimal(newbtc));
setSats(formatDecimal(newbtc * 100000000));
break;
}
};
useEffect(() => {
// Initialize the calculator with any query params
querySats && handleUpdate("sats", querySats);
queryBtc && handleUpdate("btc", queryBtc);
queryUsd && handleUpdate("usd", queryUsd);
// Otherwise set default values
if (!(querySats || queryBtc || queryUsd) && btcPrice) {
handleUpdate("sats", "1000");
}
}, [btcPrice, querySats, queryBtc]);
useEffect(() => {
const params = new URLSearchParams(window.location.search);
setQuerySats(params.get("sats") || "");
setQueryBtc(params.get("btc") || "");
setQueryUsd(params.get("usd") || "");
}, []);
return (
<form className="flex flex-col">
<label htmlFor="sats">Sats</label>
<input
className={styles.input}
onChange={(e) => handleUpdate("sats", e.target.value)}
value={sats}
type="text"
pattern="[0-9]*"
id="sats"
name="sats"
/>
<label htmlFor="btc">BTC</label>
<input
className={styles.input}
onChange={(e) => handleUpdate("btc", e.target.value)}
value={btc}
type="text"
pattern="[0-9]*"
id="btc"
name="btc"
/>
<label htmlFor="usd">USD</label>
<div className="flex">
<span className={styles.usd}>$</span>
<input
className={styles.input}
style={{ padding: "0" }}
onChange={(e) => handleUpdate("usd", e.target.value)}
value={usd}
type="text"
pattern="[0-9]*"
id="usd"
name="usd"
/>
</div>
</form>
);
};
|