blob: d4aafbfe769eea5219aeb2c7d86f478d83e5db35 [file] [log] [blame]
swissChiliab615d82024-03-08 17:15:13 -05001const React = require("preact");
swissChili0512b1c2024-12-26 19:13:51 -08002const { useState, useEffect } = require('preact/hooks');
swissChiliab615d82024-03-08 17:15:13 -05003
4export const UnitsApp = () => {
5 const [hist, setHist] = useState([]);
6 const [query, setQuery] = useState("");
7 const [to, setTo] = useState("");
swissChili4f0a0a82024-03-08 18:58:38 -05008 const [unitSystem, setUnitSystem] = useState('si');
swissChiliab615d82024-03-08 17:15:13 -05009
swissChili0512b1c2024-12-26 19:13:51 -080010 function doConversion(e, q, t, convert) {
swissChiliab615d82024-03-08 17:15:13 -050011 e.preventDefault();
12 console.log("conversion");
swissChili4f0a0a82024-03-08 18:58:38 -050013
swissChili0512b1c2024-12-26 19:13:51 -080014 if (q === '') {
15 console.log('empty query');
swissChili4f0a0a82024-03-08 18:58:38 -050016 return;
swissChili0512b1c2024-12-26 19:13:51 -080017 }
swissChili4f0a0a82024-03-08 18:58:38 -050018
19 console.log("units", unitSystem);
20
swissChili0512b1c2024-12-26 19:13:51 -080021 convert(q, t, unitSystem).then(res => {
22 setHist([...hist, [q, t, res]]);
swissChili4f0a0a82024-03-08 18:58:38 -050023 });
24
swissChiliab615d82024-03-08 17:15:13 -050025 setQuery('');
26 setTo('');
swissChiliab615d82024-03-08 17:15:13 -050027 }
28
swissChili0512b1c2024-12-26 19:13:51 -080029 useEffect(() => {
30 const q = new URLSearchParams(window.location.search);
31 if (q.get('from') !== null) {
32 window.convertPromise.then(convert => {
33 setQuery(q.get('from'));
34 setTo(q.get('to') || '');
35 doConversion({preventDefault: () => {}}, q.get('from'), q.get('to'), convert);
36 });
37 }
38 }, []);
39
swissChili281af442024-11-14 23:30:29 -050040 function afterEquals(str) {
41 let parts = str.split('=');
42 return parts[parts.length - 1];
43 }
44
swissChiliab615d82024-03-08 17:15:13 -050045 return (
swissChili16de3222024-03-08 17:55:11 -050046 <>
swissChili0512b1c2024-12-26 19:13:51 -080047 <form class="query-row noselect" onSubmit={e => doConversion(e, query, to, window.convert)}>
swissChiliab615d82024-03-08 17:15:13 -050048 <input
49 type="text"
50 id="query"
51 value={query}
52 placeholder="From"
swissChili281af442024-11-14 23:30:29 -050053 class="noselect"
swissChiliab615d82024-03-08 17:15:13 -050054 onChange={(val) => setQuery(val.target.value)}
55 ></input>
56 <input
57 type="text"
58 id="to"
59 value={to}
60 placeholder="To"
swissChili281af442024-11-14 23:30:29 -050061 class="noselect"
swissChiliab615d82024-03-08 17:15:13 -050062 onChange={(val) => setTo(val.target.value)}
63 ></input>
swissChili4f0a0a82024-03-08 18:58:38 -050064
swissChili16de3222024-03-08 17:55:11 -050065 <input value="Convert" type="submit"></input>
swissChiliab615d82024-03-08 17:15:13 -050066 </form>
67
swissChili4f0a0a82024-03-08 18:58:38 -050068 <div class="table-container">
swissChili281af442024-11-14 23:30:29 -050069 <div class="table-title">
70 <span>Calculation</span>
71 <span>Result</span>
72 </div>
73 <div class="history">
swissChili4f0a0a82024-03-08 18:58:38 -050074 {hist.map((entry) => (
swissChili281af442024-11-14 23:30:29 -050075 <div class="entry">
76 <span class="from">{entry[0]}</span>
77 { entry[1] !== "" ? <span class="to"> {entry[1]}</span> : <span />}
78 <span class="filler">
79 <span class="equals noselect">=</span>
80 <span class="res">{afterEquals(entry[2])}</span>
81 </span>
82 </div>
83 ))
84 }
85 </div>
swissChili4f0a0a82024-03-08 18:58:38 -050086 </div>
87
swissChili281af442024-11-14 23:30:29 -050088 <div class="bottom-menu noselect">
swissChili4f0a0a82024-03-08 18:58:38 -050089 <span>
90 Units: &nbsp;
91 <select onChange={val => setUnitSystem(val.target.value)}>
92 <option value="si">SI</option>
93 <option value="gauss">CGS Gaussian</option>
94 <option value="esu">CGS ESU</option>
95 <option value="emu">CGS EMU</option>
96 <option value="lhu">CGS LHU</option>
97 </select>
98 </span>
99
swissChili0512b1c2024-12-26 19:13:51 -0800100 <button onClick={() => setHist([])}>Clear</button>
swissChili4f0a0a82024-03-08 18:58:38 -0500101 </div>
102
swissChili16de3222024-03-08 17:55:11 -0500103 </>
swissChiliab615d82024-03-08 17:15:13 -0500104 );
105};