swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 1 | const React = require("preact"); |
swissChili | 0512b1c | 2024-12-26 19:13:51 -0800 | [diff] [blame^] | 2 | const { useState, useEffect } = require('preact/hooks'); |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 3 | |
| 4 | export const UnitsApp = () => { |
| 5 | const [hist, setHist] = useState([]); |
| 6 | const [query, setQuery] = useState(""); |
| 7 | const [to, setTo] = useState(""); |
swissChili | 4f0a0a8 | 2024-03-08 18:58:38 -0500 | [diff] [blame] | 8 | const [unitSystem, setUnitSystem] = useState('si'); |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 9 | |
swissChili | 0512b1c | 2024-12-26 19:13:51 -0800 | [diff] [blame^] | 10 | function doConversion(e, q, t, convert) { |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 11 | e.preventDefault(); |
| 12 | console.log("conversion"); |
swissChili | 4f0a0a8 | 2024-03-08 18:58:38 -0500 | [diff] [blame] | 13 | |
swissChili | 0512b1c | 2024-12-26 19:13:51 -0800 | [diff] [blame^] | 14 | if (q === '') { |
| 15 | console.log('empty query'); |
swissChili | 4f0a0a8 | 2024-03-08 18:58:38 -0500 | [diff] [blame] | 16 | return; |
swissChili | 0512b1c | 2024-12-26 19:13:51 -0800 | [diff] [blame^] | 17 | } |
swissChili | 4f0a0a8 | 2024-03-08 18:58:38 -0500 | [diff] [blame] | 18 | |
| 19 | console.log("units", unitSystem); |
| 20 | |
swissChili | 0512b1c | 2024-12-26 19:13:51 -0800 | [diff] [blame^] | 21 | convert(q, t, unitSystem).then(res => { |
| 22 | setHist([...hist, [q, t, res]]); |
swissChili | 4f0a0a8 | 2024-03-08 18:58:38 -0500 | [diff] [blame] | 23 | }); |
| 24 | |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 25 | setQuery(''); |
| 26 | setTo(''); |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 27 | } |
| 28 | |
swissChili | 0512b1c | 2024-12-26 19:13:51 -0800 | [diff] [blame^] | 29 | 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 | |
swissChili | 281af44 | 2024-11-14 23:30:29 -0500 | [diff] [blame] | 40 | function afterEquals(str) { |
| 41 | let parts = str.split('='); |
| 42 | return parts[parts.length - 1]; |
| 43 | } |
| 44 | |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 45 | return ( |
swissChili | 16de322 | 2024-03-08 17:55:11 -0500 | [diff] [blame] | 46 | <> |
swissChili | 0512b1c | 2024-12-26 19:13:51 -0800 | [diff] [blame^] | 47 | <form class="query-row noselect" onSubmit={e => doConversion(e, query, to, window.convert)}> |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 48 | <input |
| 49 | type="text" |
| 50 | id="query" |
| 51 | value={query} |
| 52 | placeholder="From" |
swissChili | 281af44 | 2024-11-14 23:30:29 -0500 | [diff] [blame] | 53 | class="noselect" |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 54 | onChange={(val) => setQuery(val.target.value)} |
| 55 | ></input> |
| 56 | <input |
| 57 | type="text" |
| 58 | id="to" |
| 59 | value={to} |
| 60 | placeholder="To" |
swissChili | 281af44 | 2024-11-14 23:30:29 -0500 | [diff] [blame] | 61 | class="noselect" |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 62 | onChange={(val) => setTo(val.target.value)} |
| 63 | ></input> |
swissChili | 4f0a0a8 | 2024-03-08 18:58:38 -0500 | [diff] [blame] | 64 | |
swissChili | 16de322 | 2024-03-08 17:55:11 -0500 | [diff] [blame] | 65 | <input value="Convert" type="submit"></input> |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 66 | </form> |
| 67 | |
swissChili | 4f0a0a8 | 2024-03-08 18:58:38 -0500 | [diff] [blame] | 68 | <div class="table-container"> |
swissChili | 281af44 | 2024-11-14 23:30:29 -0500 | [diff] [blame] | 69 | <div class="table-title"> |
| 70 | <span>Calculation</span> |
| 71 | <span>Result</span> |
| 72 | </div> |
| 73 | <div class="history"> |
swissChili | 4f0a0a8 | 2024-03-08 18:58:38 -0500 | [diff] [blame] | 74 | {hist.map((entry) => ( |
swissChili | 281af44 | 2024-11-14 23:30:29 -0500 | [diff] [blame] | 75 | <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> |
swissChili | 4f0a0a8 | 2024-03-08 18:58:38 -0500 | [diff] [blame] | 86 | </div> |
| 87 | |
swissChili | 281af44 | 2024-11-14 23:30:29 -0500 | [diff] [blame] | 88 | <div class="bottom-menu noselect"> |
swissChili | 4f0a0a8 | 2024-03-08 18:58:38 -0500 | [diff] [blame] | 89 | <span> |
| 90 | Units: |
| 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 | |
swissChili | 0512b1c | 2024-12-26 19:13:51 -0800 | [diff] [blame^] | 100 | <button onClick={() => setHist([])}>Clear</button> |
swissChili | 4f0a0a8 | 2024-03-08 18:58:38 -0500 | [diff] [blame] | 101 | </div> |
| 102 | |
swissChili | 16de322 | 2024-03-08 17:55:11 -0500 | [diff] [blame] | 103 | </> |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 104 | ); |
| 105 | }; |