refactor: init

This commit is contained in:
Ibrahima G. Coulibaly
2025-04-05 04:36:22 +00:00
parent 3014443163
commit ab503c642d
18 changed files with 197 additions and 238 deletions

View File

@@ -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];

View File

@@ -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',

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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<string>('');
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 == '<custom>') 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: (
<Box>
<Table>
<TableHead>
<TableRow>
<TableCell>Option</TableCell>
<TableCell>Value</TableCell>
</TableRow>
</TableHead>
<TableBody>
{calcData.selections?.map((preset) => (
<TableRow key={preset.title}>
<TableCell>{preset.title}</TableCell>
<TableCell>
<Autocomplete
disablePortal
id="combo-box-demo"
value={values.presets[preset.title]}
options={[
'<custom>',
...Object.keys(
dataTables[preset.source].data
).sort()
]}
sx={{ width: 300 }}
onChange={(event, newValue) => {
handleSelectedPresetChange(
preset.title,
newValue || '',
values,
updateField
);
}}
renderInput={(params) => (
<TextField {...params} label="Preset" />
)}
></Autocomplete>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</Box>
)
},
...(calcData.presets?.length
? [
{
title: 'Presets',
component: (
<Box>
<Table>
<TableHead>
<TableRow>
<TableCell>Option</TableCell>
<TableCell>Value</TableCell>
</TableRow>
</TableHead>
<TableBody>
{calcData.presets?.map((preset) => (
<TableRow key={preset.title}>
<TableCell>{preset.title}</TableCell>
<TableCell>
<Autocomplete
disablePortal
id="combo-box-demo"
value={values.presets[preset.title]}
options={[
'<custom>',
...Object.keys(preset.source.data).sort()
]}
sx={{ width: 300 }}
onChange={(event, newValue) => {
handleSelectedPresetChange(
preset.title,
newValue || '',
values,
updateField
);
}}
renderInput={(params) => (
<TextField {...params} label="Preset" />
)}
></Autocomplete>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</Box>
)
}
]
: []),
{
title: 'Variables',
component: (
@@ -330,9 +319,6 @@ export default async function makeTool(
<TableCell>{variable.title}</TableCell>
<TableCell>
<NumericInputWithUnit
description={
valsBoundToPreset[variable.name] || ''
}
defaultPrefix={variable.defaultPrefix}
value={values.vars[variable.name]}
disabled={
@@ -351,7 +337,6 @@ export default async function makeTool(
updateField
)
}
type="number"
/>
</TableCell>
</TableRow>
@@ -362,9 +347,6 @@ export default async function makeTool(
<TableCell>
<NumericInputWithUnit
key={alt.title}
description={
valsBoundToPreset[alt.title] || ''
}
defaultPrefix={alt.defaultPrefix || ''}
value={{
value:
@@ -442,7 +424,7 @@ export default async function makeTool(
]}
compute={(values) => {
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(

View File

@@ -1,5 +0,0 @@
import { InitialValuesType } from './types';
export function main(input: string, options: InitialValuesType): string {
return input + 'pp';
}