Skip to content

Commit

Permalink
refactor(frontend): time interval units
Browse files Browse the repository at this point in the history
Store time interval units as objects with conversion factors, symbols, and unit IDs.
  • Loading branch information
eatyourgreens committed Nov 21, 2024
1 parent 5ec4dd8 commit 5bb2d48
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
2 changes: 1 addition & 1 deletion frontend-v2/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { SimulateResponse } from "./app/backendApi";
export type TimeInterval = {
start: number;
end: number;
unit: string;
unit: { [key: string]: string };
};
type Threshold = { lower: number; upper: number };
export type Thresholds = { [key: string]: Threshold };
Expand Down
46 changes: 31 additions & 15 deletions frontend-v2/src/features/model/secondary/TimeIntervalsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type TimeUnitSelectProps = {
onChange: (interval: TimeInterval) => void;
};

function TimeUnitSelect({ interval, onChange }: TimeUnitSelectProps) {
function useTimeUnits() {
const projectId = useSelector(
(state: RootState) => state.main.selectedProject,
);
Expand All @@ -43,19 +43,31 @@ function TimeUnitSelect({ interval, onChange }: TimeUnitSelectProps) {
{ skip: !project || !project.compound },
);
const hourUnit = units?.find((unit) => unit.symbol === "h");
const timeUnits = hourUnit?.compatible_units.map((unit) => unit.symbol);
return hourUnit?.compatible_units || [];
}

function TimeUnitSelect({ interval, onChange }: TimeUnitSelectProps) {
const timeUnits = useTimeUnits();
const timeUnitOptions =
timeUnits?.map((unit) => ({ value: unit, label: unit })) || [];
timeUnits?.map((unit) => ({ value: unit.symbol, label: unit.symbol })) ||
[];
const defaultTimeUnit = "h";

function onChangeUnit(event: SelectChangeEvent) {
onChange({ ...interval, unit: event.target.value });
const unit = timeUnits?.find((unit) => unit.symbol === event.target.value);
if (unit) {
onChange({ ...interval, unit });
}
}

return (
<FormControl>
<Select value={interval.unit} onChange={onChangeUnit}>
<Select
value={interval.unit.symbol || defaultTimeUnit}
onChange={onChangeUnit}
>
{timeUnitOptions.map((option) => (
<MenuItem key={option.value} value={option.value}>
<MenuItem key={option.label} value={option.value}>
{option.label}
</MenuItem>
))}
Expand Down Expand Up @@ -104,19 +116,23 @@ function IntervalRow({ interval, onDelete, onUpdate }: IntervalRowProps) {

const TimeIntervalsTable: FC<TableProps> = (props) => {
const { intervals, setIntervals } = useContext(SimulationContext);
const timeUnits = useTimeUnits();
const hourUnit = timeUnits?.find((unit) => unit.symbol === "h");

function addInterval() {
const lastInterval = intervals[intervals.length - 1];
let newInterval: TimeInterval = { start: 0, end: 0, unit: "h" };
if (lastInterval) {
const duration = lastInterval.end - lastInterval.start;
newInterval = {
start: lastInterval.end,
end: lastInterval.end + duration,
unit: lastInterval.unit,
};
if (hourUnit) {
let newInterval: TimeInterval = { start: 0, end: 0, unit: hourUnit };
if (lastInterval) {
const duration = lastInterval.end - lastInterval.start;
newInterval = {
start: lastInterval.end,
end: lastInterval.end + duration,
unit: lastInterval.unit,
};
}
setIntervals([...intervals, newInterval]);
}
setIntervals([...intervals, newInterval]);
}
function removeInterval(index: number) {
setIntervals(intervals.filter((_, i) => i !== index));
Expand Down
2 changes: 1 addition & 1 deletion frontend-v2/src/features/results/useTableRows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function useTableRows({
return rows.map((row, index) => {
const header =
"start" in row && "end" in row && "unit" in row
? `${row.start}${row.end} [${row.unit}]`
? `${row.start}${row.end} [${row.unit.symbol}]`
: "name" in row
? row.name
: "";
Expand Down

0 comments on commit 5bb2d48

Please sign in to comment.