Skip to content

Commit

Permalink
Update FrequencyRange component
Browse files Browse the repository at this point in the history
  • Loading branch information
toni-neurosc committed Sep 24, 2024
1 parent 1296a81 commit 1037479
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 152 deletions.
Binary file modified gui_dev/bun.lockb
Binary file not shown.
16 changes: 8 additions & 8 deletions gui_dev/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ export const App = () => {
const connectSocket = useSocketStore((state) => state.connectSocket);
const disconnectSocket = useSocketStore((state) => state.disconnectSocket);

useEffect(() => {
console.log("Connecting socket from App component...");
connectSocket();
return () => {
console.log("Disconnecting socket from App component...");
disconnectSocket();
};
}, [connectSocket, disconnectSocket]);
// useEffect(() => {
// console.log("Connecting socket from App component...");
// connectSocket();
// return () => {
// console.log("Disconnecting socket from App component...");
// disconnectSocket();
// };
// }, [connectSocket, disconnectSocket]);

return (
<ThemeProvider theme={theme}>
Expand Down
8 changes: 4 additions & 4 deletions gui_dev/src/components/TitledBox.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Container } from "@mui/material";
import { Box, Container } from "@mui/material";

/**
* Component that uses the Box component to render an HTML fieldset element
Expand All @@ -13,14 +13,14 @@ export const TitledBox = ({
children,
...props
}) => (
<Container
<Box
component="fieldset"
{...props}
sx={{
borderRadius: 5,
border: "1px solid #555",
backgroundColor: "#424242",
padding: 2,
padding: 1,
width: "100%",
gap: 2,
display: "flex",
Expand All @@ -31,5 +31,5 @@ export const TitledBox = ({
>
<legend>{title}</legend>
{children}
</Container>
</Box>
);
156 changes: 81 additions & 75 deletions gui_dev/src/pages/Settings/FrequencyRange.jsx
Original file line number Diff line number Diff line change
@@ -1,88 +1,94 @@
import { useState } from "react";
import { TextField, Button, Box, Typography, IconButton } from "@mui/material";
import { Add, Close } from "@mui/icons-material";

// const onChange = (key, newValue) => {
// settings.frequencyRanges[key] = newValue;
// };

// Object.entries(settings.frequencyRanges).map(([key, value]) => (
// <FrequencySettings key={key} freqRange={value} onChange={onChange} />
// ));
export const FrequencyRange = ({ name, range, onChange, onRemove }) => {
const handleChange = (field, value) => {
onChange(name, { ...range, [field]: value });
};

/** */
return (
<Box sx={{ display: "flex", alignItems: "center", mb: 2 }}>
<TextField
size="small"
value={name}
onChange={(e) => onChange(e.target.value, range, name)}
sx={{ mr: 1, width: "30%" }}
/>
<TextField
size="small"
type="number"
value={range.frequency_low_hz}
onChange={(e) => handleChange("frequency_low_hz", e.target.value)}
label="Low Hz"
sx={{ mr: 1, width: "30%" }}
/>
<TextField
size="small"
type="number"
value={range.frequency_high_hz}
onChange={(e) => handleChange("frequency_high_hz", e.target.value)}
label="High Hz"
sx={{ mr: 1, width: "30%" }}
/>
<IconButton onClick={() => onRemove(name)} color="error">
<Close />
</IconButton>
</Box>
);
};

/**
*
* @param {String} key
* @param {Array} freqRange
* @param {Function} onChange
* @returns
*/
export const FrequencyRange = ({ settings }) => {
const [frequencyRanges, setFrequencyRanges] = useState(settings || {});
// Handle changes in the text fields
const handleInputChange = (label, key, newValue) => {
setFrequencyRanges((prevState) => ({
...prevState,
[label]: {
...prevState[label],
[key]: newValue,
},
}));
export const FrequencyRangeList = ({ ranges, onChange }) => {
const handleChange = (newName, newRange, oldName = newName) => {
const updatedRanges = { ...ranges };
if (newName !== oldName) {
delete updatedRanges[oldName];
}
updatedRanges[newName] = newRange;
onChange(["frequency_ranges_hz"], updatedRanges);
};

// Add a new band
const addBand = () => {
const newLabel = `Band ${Object.keys(frequencyRanges).length + 1}`;
setFrequencyRanges((prevState) => ({
...prevState,
[newLabel]: { frequency_high_hz: "", frequency_low_hz: "" },
}));
const handleRemove = (name) => {
const updatedRanges = { ...ranges };
delete updatedRanges[name];
onChange(["frequency_ranges_hz"], updatedRanges);
};

// Remove a band
const removeBand = (label) => {
const updatedRanges = { ...frequencyRanges };
delete updatedRanges[label];
setFrequencyRanges(updatedRanges);
const addRange = () => {
let newName = "NewRange";
let counter = 0;
while (ranges.hasOwnProperty(newName)) {
counter++;
newName = `NewRange${counter}`;
}
const updatedRanges = {
...ranges,
[newName]: { frequency_low_hz: "", frequency_high_hz: "" },
};
onChange(["frequency_ranges_hz"], updatedRanges);
};

return (
<div>
<div>Frequency Bands</div>
{Object.keys(frequencyRanges).map((label) => (
<div key={label}>
<input
type="text"
value={label}
onChange={(e) => {
const newLabel = e.target.value;
const updatedRanges = { ...frequencyRanges };
updatedRanges[newLabel] = updatedRanges[label];
delete updatedRanges[label];
setFrequencyRanges(updatedRanges);
}}
placeholder="Band Name"
/>
<input
type="text"
value={frequencyRanges[label].frequency_high_hz}
onChange={(e) =>
handleInputChange(label, "frequency_high_hz", e.target.value)
}
placeholder="High Hz"
/>
<input
type="text"
value={frequencyRanges[label].frequency_low_hz}
onChange={(e) =>
handleInputChange(label, "frequency_low_hz", e.target.value)
}
placeholder="Low Hz"
/>
<button onClick={() => removeBand(label)}></button>
</div>
<Box>
<Typography variant="h6" gutterBottom>
Frequency Ranges
</Typography>
{Object.entries(ranges).map(([name, range]) => (
<FrequencyRange
key={name}
name={name}
range={range}
onChange={handleChange}
onRemove={handleRemove}
/>
))}
<button onClick={addBand}>+ Add Band</button>
</div>
<Button
variant="outlined"
startIcon={<Add />}
onClick={addRange}
sx={{ mt: 1 }}
>
Add Range
</Button>
</Box>
);
};
Loading

0 comments on commit 1037479

Please sign in to comment.