diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 27386d7..ca23a2a 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,32 +5,25 @@ - - + + - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + - { + "prStates": [ { - "id": { - "id": "PR_kwDOMJIfts51PkS9", - "number": 22 + "id": { + "id": "PR_kwDOMJIfts51PkS9", + "number": 22 }, - "lastSeen": 1741207144695 + "lastSeen": 1741207144695 }, { - "id": { - "id": "PR_kwDOMJIfts6NiNYl", - "number": 32 + "id": { + "id": "PR_kwDOMJIfts6NiNYl", + "number": 32 }, - "lastSeen": 1741209723869 + "lastSeen": 1741209723869 }, { - "id": { - "id": "PR_kwDOMJIfts6Nheyd", - "number": 31 + "id": { + "id": "PR_kwDOMJIfts6Nheyd", + "number": 31 }, - "lastSeen": 1741213371410 + "lastSeen": 1741213371410 }, { - "id": { - "id": "PR_kwDOMJIfts6NmRBs", - "number": 33 + "id": { + "id": "PR_kwDOMJIfts6NmRBs", + "number": 33 }, - "lastSeen": 1741282429036 + "lastSeen": 1741282429036 }, { - "id": { - "id": "PR_kwDOMJIfts5zyFTs", - "number": 15 + "id": { + "id": "PR_kwDOMJIfts5zyFTs", + "number": 15 }, - "lastSeen": 1741535540953 + "lastSeen": 1741535540953 }, { - "id": { - "id": "PR_kwDOMJIfts6QQB3c", - "number": 59 + "id": { + "id": "PR_kwDOMJIfts6QQB3c", + "number": 59 }, - "lastSeen": 1743018960900 + "lastSeen": 1743018960900 }, { - "id": { - "id": "PR_kwDOMJIfts6QMPEg", - "number": 58 + "id": { + "id": "PR_kwDOMJIfts6QMPEg", + "number": 58 }, - "lastSeen": 1743019452983 + "lastSeen": 1743019452983 }, { - "id": { - "id": "PR_kwDOMJIfts6QZvRI", - "number": 61 + "id": { + "id": "PR_kwDOMJIfts6QZvRI", + "number": 61 }, - "lastSeen": 1743103196866 + "lastSeen": 1743103196866 }, { - "id": { - "id": "PR_kwDOMJIfts6QqPrQ", - "number": 73 + "id": { + "id": "PR_kwDOMJIfts6QqPrQ", + "number": 73 }, - "lastSeen": 1743265865001 + "lastSeen": 1743265865001 }, { - "id": { - "id": "PR_kwDOMJIfts6Qp5nI", - "number": 72 + "id": { + "id": "PR_kwDOMJIfts6Qp5nI", + "number": 72 }, - "lastSeen": 1743338472110 + "lastSeen": 1743338472110 }, { - "id": { - "id": "PR_kwDOMJIfts6QsjlS", - "number": 76 + "id": { + "id": "PR_kwDOMJIfts6QsjlS", + "number": 76 }, - "lastSeen": 1743352150953 + "lastSeen": 1743352150953 } ] -}]]> +} { "selectedUrlAndAccountId": { "url": "https://github.com/iib0011/omni-tools.git", @@ -199,7 +192,7 @@ "Vitest.replaceText function (regexp mode).should return the original text when passed an invalid regexp.executor": "Run", "Vitest.replaceText function.executor": "Run", "Vitest.timeBetweenDates.executor": "Run", - "git-widget-placeholder": "dark-mode", + "git-widget-placeholder": "#89 on generic-calc", "ignore.virus.scanning.warn.message": "true", "kotlin-language-version-configured": "true", "last_opened_file_path": "C:/Users/Ibrahima/IdeaProjects/omni-tools/@types", diff --git a/home/daniel/Projects/omni-tools/src/pages/tools/index.ts b/home/daniel/Projects/omni-tools/src/pages/tools/index.ts deleted file mode 100644 index 71e8eea..0000000 --- a/home/daniel/Projects/omni-tools/src/pages/tools/index.ts +++ /dev/null @@ -1 +0,0 @@ -export const toolsTools = []; diff --git a/home/daniel/Projects/omni-tools/src/pages/tools/number/calculators/index.ts b/home/daniel/Projects/omni-tools/src/pages/tools/number/calculators/index.ts deleted file mode 100644 index f7a9c9d..0000000 --- a/home/daniel/Projects/omni-tools/src/pages/tools/number/calculators/index.ts +++ /dev/null @@ -1 +0,0 @@ -export const calculatorsTools = []; diff --git a/home/daniel/Projects/omni-tools/src/pages/tools/number/index.ts b/home/daniel/Projects/omni-tools/src/pages/tools/number/index.ts deleted file mode 100644 index 90c9f0e..0000000 --- a/home/daniel/Projects/omni-tools/src/pages/tools/number/index.ts +++ /dev/null @@ -1 +0,0 @@ -export const numberTools = []; diff --git a/src/components/ToolContent.tsx b/src/components/ToolContent.tsx index 2e3a6b6..721035d 100644 --- a/src/components/ToolContent.tsx +++ b/src/components/ToolContent.tsx @@ -40,7 +40,7 @@ const FormikListenerComponent = ({ interface ToolContentProps extends ToolComponentProps { inputComponent?: ReactNode; - resultComponent: ReactNode; + resultComponent?: ReactNode; renderCustomInput?: ( values: T, setFieldValue: (fieldName: string, value: any) => void diff --git a/src/components/ToolInputAndResult.tsx b/src/components/ToolInputAndResult.tsx index a07721f..390f560 100644 --- a/src/components/ToolInputAndResult.tsx +++ b/src/components/ToolInputAndResult.tsx @@ -6,18 +6,20 @@ export default function ToolInputAndResult({ result }: { input?: ReactNode; - result: ReactNode; + result?: ReactNode; }) { - return ( - - {input && ( - - {input} + if (input || result) { + return ( + + {input && ( + + {input} + + )} + + {result} - )} - - {result} - - ); + ); + } } diff --git a/src/components/input/NumericInputWithUnit.tsx b/src/components/input/NumericInputWithUnit.tsx index daf00bc..2a12c8d 100644 --- a/src/components/input/NumericInputWithUnit.tsx +++ b/src/components/input/NumericInputWithUnit.tsx @@ -20,7 +20,7 @@ export default function NumericInputWithUnit(props: { value: { value: number; unit: string }; disabled?: boolean; disableChangingUnit?: boolean; - onOwnChange: (value: { value: number; unit: string }) => void; + onOwnChange?: (value: { value: number; unit: string }) => void; defaultPrefix?: string; }) { const [inputValue, setInputValue] = useState(props.value.value); diff --git a/src/datatables/data/index.ts b/src/datatables/data/index.ts deleted file mode 100644 index c5c5420..0000000 --- a/src/datatables/data/index.ts +++ /dev/null @@ -1,10 +0,0 @@ -import type { DataTable } from '../types'; -import wiregauge from './wire_gauge'; -import material_electrical_properties from './material_electrical_properties'; - -const allDataTables: { [key: string]: DataTable } = { - 'wire-gauge': wiregauge, - 'material-electrical-properties': material_electrical_properties -}; - -export default allDataTables; diff --git a/src/datatables/index.ts b/src/datatables/index.ts index 37174b2..e661d11 100644 --- a/src/datatables/index.ts +++ b/src/datatables/index.ts @@ -1,18 +1,8 @@ import type { DataTable } from './types.ts'; -export async function getDataTable(name: string): Promise { - const allDataTables = (await import('./data/index')).default; - return allDataTables[name]; -} - /* Used in case later we want any kind of computed extra data */ export function dataTableLookup(table: DataTable, key: string): any { return table.data[key]; } -export async function listDataTables(): Promise<{ [name: string]: DataTable }> { - const allDataTables = (await import('./data/index')).default; - return allDataTables; -} - export { DataTable }; diff --git a/src/datatables/types.ts b/src/datatables/types.ts index c2199d4..6a77ccc 100644 --- a/src/datatables/types.ts +++ b/src/datatables/types.ts @@ -1,4 +1,4 @@ -/* +/* Represents a set of rows indexed by a key. Used for calculator presets @@ -7,7 +7,7 @@ export interface DataTable { title: string; /* A JSON schema properties */ columns: { - [key: string]: any; + [key: string]: { title: string; type: string; unit: string }; }; data: { [key: string]: { diff --git a/src/pages/tools/number/generic-calc/data/index.ts b/src/pages/tools/number/generic-calc/data/index.ts index 3b27f80..1a700db 100644 --- a/src/pages/tools/number/generic-calc/data/index.ts +++ b/src/pages/tools/number/generic-calc/data/index.ts @@ -1,5 +1,6 @@ -import ohmslaw from './ohms_law'; -import voltagedropinwire from './wire_voltage_drop'; -import { areaSphere, volumeSphere } from './area_volume'; +import ohmslaw from './ohmsLaw'; +import voltageDropInWire from './voltageDropInWire'; +import sphereArea from './sphereArea'; +import sphereVolume from './sphereVolume'; -export default [ohmslaw, voltagedropinwire, areaSphere, volumeSphere]; +export default [ohmslaw, voltageDropInWire, sphereArea, sphereVolume]; diff --git a/src/pages/tools/number/generic-calc/data/ohms_law.ts b/src/pages/tools/number/generic-calc/data/ohmsLaw.ts similarity index 96% rename from src/pages/tools/number/generic-calc/data/ohms_law.ts rename to src/pages/tools/number/generic-calc/data/ohmsLaw.ts index 28b458f..3065152 100644 --- a/src/pages/tools/number/generic-calc/data/ohms_law.ts +++ b/src/pages/tools/number/generic-calc/data/ohmsLaw.ts @@ -5,7 +5,7 @@ const ohmsLawCalc: GenericCalcType = { name: 'ohms-law', description: 'Calculates voltage, current and resistance', formula: 'V = I * R', - selections: [], + presets: [], variables: [ { name: 'V', diff --git a/src/pages/tools/number/generic-calc/data/sphereArea.ts b/src/pages/tools/number/generic-calc/data/sphereArea.ts new file mode 100644 index 0000000..dd84991 --- /dev/null +++ b/src/pages/tools/number/generic-calc/data/sphereArea.ts @@ -0,0 +1,25 @@ +import type { GenericCalcType } from './types'; + +const areaSphere: GenericCalcType = { + title: 'Area of a Sphere', + name: 'area-sphere', + description: 'Area of a Sphere', + formula: 'A = 4 * pi * r**2', + presets: [], + variables: [ + { + name: 'A', + title: 'Area', + unit: 'mm2' + }, + { + name: 'r', + title: 'Radius', + formula: 'r = sqrt(A/pi) / 2', + unit: 'mm', + default: 1 + } + ] +}; + +export default areaSphere; diff --git a/src/pages/tools/number/generic-calc/data/area_volume.ts b/src/pages/tools/number/generic-calc/data/sphereVolume.ts similarity index 51% rename from src/pages/tools/number/generic-calc/data/area_volume.ts rename to src/pages/tools/number/generic-calc/data/sphereVolume.ts index 8d71fa1..5ec4764 100644 --- a/src/pages/tools/number/generic-calc/data/area_volume.ts +++ b/src/pages/tools/number/generic-calc/data/sphereVolume.ts @@ -1,33 +1,11 @@ import type { GenericCalcType } from './types'; -export const areaSphere: GenericCalcType = { - title: 'Area of a Sphere', - name: 'area-sphere', - description: 'Area of a Sphere', - formula: 'A = 4 * pi * r**2', - selections: [], - variables: [ - { - name: 'A', - title: 'Area', - unit: 'mm2' - }, - { - name: 'r', - title: 'Radius', - formula: 'r = sqrt(A/pi) / 2', - unit: 'mm', - default: 1 - } - ] -}; - -export const volumeSphere: GenericCalcType = { +const volumeSphere: GenericCalcType = { title: 'Volume of a Sphere', name: 'volume-sphere', description: 'Volume of a Sphere', formula: 'v = (4/3) * pi * r**3', - selections: [], + presets: [], variables: [ { name: 'v', @@ -49,3 +27,5 @@ export const volumeSphere: GenericCalcType = { } ] }; + +export default volumeSphere; diff --git a/src/pages/tools/number/generic-calc/data/types.ts b/src/pages/tools/number/generic-calc/data/types.ts index 45a3d1a..1ed2148 100644 --- a/src/pages/tools/number/generic-calc/data/types.ts +++ b/src/pages/tools/number/generic-calc/data/types.ts @@ -1,9 +1,12 @@ +import { DataTable } from '../../../../../datatables'; + export interface AlternativeVarInfo { title: string; unit: string; defaultPrefix?: string; formula: string; } + export interface GenericCalcType { title: string; name: string; @@ -17,9 +20,9 @@ export interface GenericCalcType { // Si prefix default defaultPrefix?: string; }[]; - selections?: { + presets?: { title: string; - source: string; + source: DataTable; default: string; bind: { [key: string]: string; diff --git a/src/pages/tools/number/generic-calc/data/wire_voltage_drop.ts b/src/pages/tools/number/generic-calc/data/voltageDropInWire.ts similarity index 78% rename from src/pages/tools/number/generic-calc/data/wire_voltage_drop.ts rename to src/pages/tools/number/generic-calc/data/voltageDropInWire.ts index 9dcf9fe..5b2d0aa 100644 --- a/src/pages/tools/number/generic-calc/data/wire_voltage_drop.ts +++ b/src/pages/tools/number/generic-calc/data/voltageDropInWire.ts @@ -1,14 +1,17 @@ import type { GenericCalcType } from './types'; -const voltagedropinwire: GenericCalcType = { +import material_electrical_properties from '../../../../../datatables/data/material_electrical_properties'; +import wire_gauge from '../../../../../datatables/data/wire_gauge'; + +const voltageDropInWire: GenericCalcType = { title: 'Round trip voltage drop in cable', name: 'cable-voltage-drop', formula: 'x = (((p * L) / (A/10**6) ) *2) * I', description: 'Calculates round trip voltage and power loss in a 2 conductor cable', - selections: [ + presets: [ { title: 'Material', - source: 'material-electrical-properties', + source: material_electrical_properties, default: 'Copper', bind: { p: 'resistivity_20c' @@ -17,7 +20,7 @@ const voltagedropinwire: GenericCalcType = { { title: 'Wire Gauge', - source: 'wire-gauge', + source: wire_gauge, default: '24 AWG', bind: { A: 'area' @@ -72,4 +75,4 @@ const voltagedropinwire: GenericCalcType = { ] }; -export default voltagedropinwire; +export default voltageDropInWire; diff --git a/src/pages/tools/number/generic-calc/index.tsx b/src/pages/tools/number/generic-calc/index.tsx index 9c51797..3dc21f9 100644 --- a/src/pages/tools/number/generic-calc/index.tsx +++ b/src/pages/tools/number/generic-calc/index.tsx @@ -1,31 +1,29 @@ import { - Box, Autocomplete, - TextField, + Box, Radio, Table, TableBody, TableCell, TableHead, - TableRow + TableRow, + TextField } from '@mui/material'; -import React, { useState } from 'react'; +import React, { useContext, useState } from 'react'; import ToolContent from '@components/ToolContent'; import { ToolComponentProps } from '@tools/defineTool'; -import ToolTextResult from '@components/result/ToolTextResult'; import NumericInputWithUnit from '@components/input/NumericInputWithUnit'; import { UpdateField } from '@components/options/ToolOptions'; import { InitialValuesType } from './types'; import type { AlternativeVarInfo, GenericCalcType } from './data/types'; -import type { DataTable } from 'datatables'; -import { getDataTable, dataTableLookup } from 'datatables'; +import { dataTableLookup } from 'datatables'; import nerdamer from 'nerdamer-prime'; import 'nerdamer-prime/Algebra'; import 'nerdamer-prime/Solve'; import 'nerdamer-prime/Calculus'; import Qty from 'js-quantities'; -import { error } from 'console'; +import { CustomSnackBarContext } from 'contexts/CustomSnackBarContext'; function numericSolveEquationFor( equation: string, @@ -59,15 +57,8 @@ export default async function makeTool( presets: {} }; - const dataTables: { [key: string]: DataTable } = {}; - - for (const selection of calcData.selections || []) { - dataTables[selection.source] = await getDataTable(selection.source); - } - return function GenericCalc({ title }: ToolComponentProps) { - const [result, setResult] = useState(''); - + const { showSnackBar } = useContext(CustomSnackBarContext); const [alternatesByVariable, setAlternatesByVariable] = useState<{ [key: string]: { value: { @@ -126,7 +117,7 @@ export default async function makeTool( } } - const selectionData = calcData.selections?.find( + const selectionData = calcData.presets?.find( (sel) => sel.title === selection ); @@ -142,12 +133,11 @@ export default async function makeTool( updateVarField( key, - dataTableLookup(dataTables[selectionData.source], preset)[ + dataTableLookup(selectionData.source, preset)[ selectionData.bind[key] ], - dataTables[selectionData.source].columns[selectionData.bind[key]] - ?.unit || '', + selectionData.source.columns[selectionData.bind[key]]?.unit || '', currentValues, updateFieldFunc ); @@ -178,19 +168,16 @@ export default async function makeTool( } }); - calcData.selections?.forEach((selection) => { + calcData.presets?.forEach((selection) => { initialValues.presets[selection.title] = selection.default; if (selection.default == '') return; for (const key in selection.bind) { initialValues.vars[key] = { - value: dataTableLookup( - dataTables[selection.source], - selection.default - )[selection.bind[key]], + value: dataTableLookup(selection.source, selection.default)[ + selection.bind[key] + ], - unit: - dataTables[selection.source].columns[selection.bind[key]]?.unit || - '' + unit: selection.source.columns[selection.bind[key]]?.unit || '' }; valsBoundToPreset[key] = selection.title; } @@ -264,53 +251,55 @@ export default async function makeTool( calcData.formula }} getGroups={({ values, updateField }) => [ - { - title: 'Presets', - component: ( - - - - - Option - Value - - - - {calcData.selections?.map((preset) => ( - - {preset.title} - - ', - ...Object.keys( - dataTables[preset.source].data - ).sort() - ]} - sx={{ width: 300 }} - onChange={(event, newValue) => { - handleSelectedPresetChange( - preset.title, - newValue || '', - values, - updateField - ); - }} - renderInput={(params) => ( - - )} - > - - - ))} - -
-
- ) - }, + ...(calcData.presets?.length + ? [ + { + title: 'Presets', + component: ( + + + + + Option + Value + + + + {calcData.presets?.map((preset) => ( + + {preset.title} + + ', + ...Object.keys(preset.source.data).sort() + ]} + sx={{ width: 300 }} + onChange={(event, newValue) => { + handleSelectedPresetChange( + preset.title, + newValue || '', + values, + updateField + ); + }} + renderInput={(params) => ( + + )} + > + + + ))} + +
+
+ ) + } + ] + : []), { title: 'Variables', component: ( @@ -330,9 +319,6 @@ export default async function makeTool( {variable.title} @@ -362,9 +347,6 @@ export default async function makeTool( { if (values.outputVariable === '') { - setResult('Please select a solve for variable'); + showSnackBar('Please select a solve for variable', 'error'); return; } let expr: nerdamer.Expression | null = null; @@ -491,8 +473,6 @@ export default async function makeTool( result = (result as unknown as nerdamer.Expression[])[0]; } - setResult(result.toString()); - if (result) { if (values.vars[values.outputVariable] != undefined) { values.vars[values.outputVariable].value = parseFloat( diff --git a/src/pages/tools/number/generic-calc/service.ts b/src/pages/tools/number/generic-calc/service.ts deleted file mode 100644 index 886ff10..0000000 --- a/src/pages/tools/number/generic-calc/service.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { InitialValuesType } from './types'; - -export function main(input: string, options: InitialValuesType): string { - return input + 'pp'; -}