diff --git a/app/src/DashboardPage.js b/app/src/DashboardPage.js index b808cc70..9f20d7d7 100644 --- a/app/src/DashboardPage.js +++ b/app/src/DashboardPage.js @@ -5,6 +5,7 @@ import {DownloadMenu} from "./components/Download"; import { Button, Divider, + Form, Header, Modal, ModalContent, @@ -16,7 +17,7 @@ import { import {Link, useNavigate} from "react-router-dom"; import {BandwidthProgressCombined, CPUUsageProgress} from "./components/admin/Status"; import {ProgressPlaceholder} from "./components/Placeholder"; -import {GridColumn, GridRow, Icon, Message} from "semantic-ui-react"; +import {GridColumn, GridRow, Icon, Input, Message} from "semantic-ui-react"; import Grid from "semantic-ui-react/dist/commonjs/collections/Grid"; import {refreshFiles} from "./api"; import _ from "lodash"; @@ -27,6 +28,8 @@ import {KiwixRestartMessage, OutdatedZimsMessage} from "./components/Zim"; import {useSearchFilter, useWROLMode} from "./hooks/customHooks"; import {FileSearchFilterButton} from "./components/Files"; import {DateSelectorButton} from "./components/DatesSelector"; +import {useCalculators} from "./components/Calculators"; +import {evaluate} from "mathjs"; function FlagsMessages() { const {settings, fetchSettings} = React.useContext(SettingsContext); @@ -93,6 +96,84 @@ function FlagsMessages() { } +function EvaluateForm({defaultEvaluatedValue = 'Enter an equation above.'}) { + + const helpContents = +

Enter an equation above.

+ +
Examples:
+
2 + 3
+
sqrt(15)
+
15 km to miles
+
+ + const inputRef = React.useRef(); + const [showMessage, setShowMessage] = React.useState(false); + const [inputValue, setInputValue] = React.useState(''); + const [evaluatedValue, setEvaluatedValue] = React.useState(helpContents); + + const doEvaluate = () => { + setEvaluatedValue(''); // Clear input temporarily so the user can tell something happened. + try { + setEvaluatedValue(evaluate(inputValue).toString()); + } catch (error) { + console.error(error); + setEvaluatedValue('Unable to evaluate.'); + } + } + + return
+ setShowMessage(true)} + onChange={(e, {value}) => setInputValue(value)} + action={} + /> + {showMessage && + + Result + + {evaluatedValue} + + + } +
+} + +function DashboardCalculators() { + const [calc, setCalc] = React.useState(null); + const {calculatorLinks} = useCalculators(); + + return + +
Calculators
+ + + + + + + + + {calculatorLinks} + + + +
+ setCalc(null)} + > + + {calc?.contents} + + +
+} + export function Getters() { const {status} = useContext(StatusContext); const wrolModeEnabled = useWROLMode(); @@ -212,6 +293,7 @@ export function DashboardPage() { + ; if (searchStr || (activeTags && activeTags.length > 0)) { // User has submitted and wants full search. diff --git a/app/src/components/Calculators.js b/app/src/components/Calculators.js index 82aae8cc..e20c6a08 100644 --- a/app/src/components/Calculators.js +++ b/app/src/components/Calculators.js @@ -8,35 +8,39 @@ import {DipoleAntennaCalculator} from "./calculators/HamCalculators"; import {Link} from "react-router-dom"; import {RatioCalculators} from "./calculators/RatioCalculator"; -export function CalculatorsPage() { - const {calc, setCalc} = useCalcQuery(); +export const useCalculators = () => { + const [calc, setCalc] = useCalcQuery(); const calculators = [ + {key: 'ratio', icon: 'th large', button: 'Ratio', contents: }, {key: 'electrical', icon: 'lightning', button: 'Electrical', contents: }, {key: 'antenna', icon: 'signal', button: 'Antenna', contents: }, {key: 'temperature', icon: 'thermometer', button: 'Temperature', contents: }, - {key: ' ratio', icon: 'th large', button: 'Ratio', contents: }, ]; - const activeCalculator = calc ? - calculators.filter(i => i.key === calc)[0] - : null; - const {icon, button, title, contents} = activeCalculator || {}; + const activeCalculator = calc ? calculators.find(i => i.key === calc) : null; - const name = activeCalculator ? title || button + ' Calculator' : 'Calculators'; - useTitle(name); - - const calculatorButtons = calculators.map(i => { + const calculatorLinks = calculators.map(i => { const {button, icon, key} = i; return - }); - const body = activeCalculator ? {contents} : calculatorButtons; + return {calc, calculators, activeCalculator, calculatorLinks} +} + +export function CalculatorsPage() { + const {activeCalculator, calculatorLinks} = useCalculators(); + + const {icon, button, title, contents} = activeCalculator || {}; + const name = activeCalculator ? title || button + ' Calculator' : 'Calculators'; + useTitle(name); + + const body = activeCalculator ? {contents} : calculatorLinks; // Make first breadcrumb a link back to Calculators only when a calculator is active. const crumbs = activeCalculator diff --git a/app/src/components/Common.js b/app/src/components/Common.js index 4dc506c6..901869de 100644 --- a/app/src/components/Common.js +++ b/app/src/components/Common.js @@ -724,11 +724,19 @@ export function CardPoster({to, file}) { } } -export function HelpPopup({icon = 'question', size = null, content, position = 'left center', iconSize = 'small'}) { +export function HelpPopup({ + icon = 'question', + size = null, + content, + position = 'left center', + iconSize = 'small', + header = '' + }) { return } diff --git a/app/src/components/calculators/ElectricalCalculators.js b/app/src/components/calculators/ElectricalCalculators.js index 921d49db..b0dc6d5c 100644 --- a/app/src/components/calculators/ElectricalCalculators.js +++ b/app/src/components/calculators/ElectricalCalculators.js @@ -121,7 +121,7 @@ export function OhmsLawCalculator({setVolts}) { type: 'number', labelPosition: 'right', onSelect: e => e.target.select(), // Select the contents of the Input when a user selects it. - autocomplete: 'off', + autoComplete: 'off', }; React.useEffect(() => { @@ -279,7 +279,7 @@ const PowerLossCalculator = ({volts, setVolts}) => { type='number' value={volts} onChange={(e, {value}) => setVolts(parseInt(value))} - autocomplete="off" + autoComplete="off" />; const saeToggle = { type='number' value={length} onChange={(e, {value}) => setLength(parseInt(value))} - autocomplete="off" + autoComplete="off" /> diff --git a/app/src/components/calculators/RatioCalculator.js b/app/src/components/calculators/RatioCalculator.js index 330b4ff4..0b766ec5 100644 --- a/app/src/components/calculators/RatioCalculator.js +++ b/app/src/components/calculators/RatioCalculator.js @@ -1,16 +1,15 @@ import {Container, Dropdown} from "semantic-ui-react"; import {Form, Header} from "../Theme"; import Grid from "semantic-ui-react/dist/commonjs/collections/Grid"; -import {ThemeContext} from "../../contexts/contexts"; import React from "react"; -import {roundDigits} from "../Common"; +import {roundDigits, useLocalStorage} from "../Common"; import {createUnit, multiply, unit} from "mathjs"; const nullUnit = createUnit('null'); function ratioReducer(prevState, e) { const {value, name} = Object.keys(e).indexOf('target') >= 0 ? e.target : e; - let {a, aUnit, b, bUnit, c, cUnit, d, dUnit, base, lastUpdated} = prevState; + let {a, aUnit, b, bUnit, c, cUnit, d, dUnit, base, lastUpdated, recentUnits} = prevState; console.log('ratioReducer', name, value, lastUpdated); a = unit(a, aUnit); @@ -46,17 +45,22 @@ function ratioReducer(prevState, e) { dUnit = value; d = d.to(value); } else if (name === 'a') { + recentUnits[base] = aUnit; a = unit(value, aUnit); } else if (name === 'b') { + recentUnits[base] = bUnit; b = unit(value, bUnit); } else if (name === 'c') { + recentUnits[base] = cUnit; c = unit(value, cUnit); } else if (name === 'd') { + recentUnits[base] = dUnit; d = unit(value, dUnit); } else if (name === 'base') { // Base is changing, reset units and values. base = value; - aUnit = bUnit = cUnit = dUnit = baseToUnitsMap[base][0]['key']; + // Get the unit of this base that the user used most recently. + aUnit = bUnit = cUnit = dUnit = recentUnits[base]; a = b = c = d = unit(1, aUnit); // Reset last updated. newLastUpdated = []; @@ -86,6 +90,7 @@ function ratioReducer(prevState, e) { bUnit, cUnit, dUnit, + recentUnits, } } @@ -129,13 +134,26 @@ const baseToUnitsMap = { {key: 'joule', value: 'joule', text: 'joule'}, {key: 'Wh', value: 'Wh', text: 'Wh'}, {key: 'BTU', value: 'BTU', text: 'BTU'}, - ], - 'power': [ {key: 'watt', value: 'watt', text: 'watt'}, {key: 'hp', value: 'hp', text: 'hp'}, ], +}; + +let unitsToBaseMap = {}; +for (const [base, units] of Object.entries(baseToUnitsMap)) { + for (const {key} of units) { + unitsToBaseMap[key] = base; + } } +const defaultUnits = { + // These will be replaced using local storage. + length: baseToUnitsMap.length[0]['key'], + area: baseToUnitsMap.area[0]['key'], + volume: baseToUnitsMap.volume[0]['key'], + mass: baseToUnitsMap.mass[0]['key'], + energy: baseToUnitsMap.energy[0]['key'], +}; const initialState = { base: null, lastUpdated: [], @@ -147,23 +165,41 @@ const initialState = { bUnit: nullUnit, cUnit: nullUnit, dUnit: nullUnit, + recentUnits: defaultUnits, }; const RatioCalculator = () => { - const {t} = React.useContext(ThemeContext); - + const [recentUnits, setRecentUnits] = useLocalStorage('ratio_calculator_recent_units', + initialState.recentUnits); + initialState.recentUnits = recentUnits; const [state, dispatch] = React.useReducer(ratioReducer, initialState); + React.useEffect(() => { + const key = unitsToBaseMap[state.aUnit]; + setRecentUnits({...recentUnits, [key]: state.aUnit}); + }, [state.aUnit]); + React.useEffect(() => { + const key = unitsToBaseMap[state.bUnit]; + setRecentUnits({...recentUnits, [key]: state.bUnit}); + }, [state.bUnit]); + React.useEffect(() => { + const key = unitsToBaseMap[state.cUnit]; + setRecentUnits({...recentUnits, [key]: state.cUnit}); + }, [state.cUnit]); + React.useEffect(() => { + const key = unitsToBaseMap[state.dUnit]; + setRecentUnits({...recentUnits, [key]: state.dUnit}); + }, [state.dUnit]); + const unitOptions = baseToUnitsMap[state.base]; const baseOptions = [ {key: null, value: null, text: 'None'}, - {key: 'length', value: 'length', text: 'meter, feet, inch, etc.', label: 'Length'}, - {key: 'area', value: 'area', text: 'inch², m², etc.', label: 'Area'}, - {key: 'volume', value: 'volume', text: 'liter, m³, cup, etc.', label: 'Volume'}, - {key: 'mass', value: 'mass', text: 'gram, pound, etc.', label: 'Mass'}, - {key: 'energy', value: 'energy', text: 'joule, Wh, BTU', label: 'Energy'}, - {key: 'power', value: 'power', text: 'watt, horse power', label: 'Power'}, + {key: 'length', value: 'length', label: 'meter, feet, inch, etc.', text: 'Length'}, + {key: 'area', value: 'area', label: 'inch², m², etc.', text: 'Area'}, + {key: 'volume', value: 'volume', label: 'liter, m³, cup, etc.', text: 'Volume'}, + {key: 'mass', value: 'mass', label: 'gram, pound, etc.', text: 'Mass'}, + {key: 'energy', value: 'energy', label: 'joule, watt, etc.', text: 'Energy'}, ]; const unitDropdownOptions = { @@ -185,9 +221,9 @@ const RatioCalculator = () => { dispatch(data)} /> diff --git a/app/src/hooks/customHooks.js b/app/src/hooks/customHooks.js index 86e5aeb5..c4963a42 100644 --- a/app/src/hooks/customHooks.js +++ b/app/src/hooks/customHooks.js @@ -1275,7 +1275,7 @@ export const useDockerized = () => { export const useCalcQuery = () => { const [calc, setCalc] = useOneQuery('calc'); - return {calc, setCalc} + return [calc, setCalc] } export const useSearchChannels = (defaultTagNames) => {