Skip to content

Commit

Permalink
[add,mod] adds accept all predictions button
Browse files Browse the repository at this point in the history
 - renames ShowPredictions component
 - adds delayed list item button
  • Loading branch information
Andrea-Papaleo committed Feb 13, 2024
1 parent e0a523d commit cb7c87e
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {

type TooltipType = Omit<TooltipProps, "children" | "title">;

type CustomListItemButtonProps = Pick<
export type CustomListItemButtonProps = Pick<
ListItemButtonProps,
| "alignItems"
| "disabled"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import React, { ReactElement, useEffect, useState } from "react";

import {
ListItem,
ListItemButton,
ListItemIcon,
ListItemText,
ListItemTextProps,
} from "@mui/material";

export const ListItemHoldButton = ({
onHoldComplete,
holdDuration,
primaryText,
icon,
primaryTypographyProps,
}: {
onHoldComplete: () => void;
holdDuration: number;
primaryText: string | ReactElement;
icon?: ReactElement;
} & Pick<ListItemTextProps, "primaryTypographyProps">) => {
const [holdElapsed, setHoldElapsed] = useState<number>(0);
const [intervalId, setIntervalId] = useState<NodeJS.Timer>();
const [loadPercent, setLoadPercent] = useState<number>(0);

const handleMouseDown = () => {
const intervalId = setInterval(() => {
setHoldElapsed((t) => t + 1);
}, 10);
setIntervalId(intervalId);
};

const handleMouseUp = () => {
clearInterval(intervalId);

setHoldElapsed(0);
};

useEffect(() => {
setLoadPercent((holdElapsed / holdDuration) * 100);

if (holdElapsed >= holdDuration) {
clearInterval(intervalId);
setHoldElapsed(0);
onHoldComplete();
}
}, [holdElapsed, intervalId, holdDuration, onHoldComplete]);

return (
<ListItem
disablePadding
sx={{
backgroundImage: `linear-gradient(to right, rgba(255,255,255,0.3) ${loadPercent}%, rgba(255,255,255,0) 1%)`,
}}
>
<ListItemButton
onMouseDown={handleMouseDown}
onMouseUp={handleMouseUp}
onMouseLeave={handleMouseUp}
onTouchStart={handleMouseDown}
onTouchEnd={handleMouseUp}
>
{icon && <ListItemIcon>{icon}</ListItemIcon>}

<ListItemText
primary={primaryText}
primaryTypographyProps={primaryTypographyProps}
/>
</ListItemButton>
</ListItem>
);
};
1 change: 1 addition & 0 deletions src/components/list-items/ListItemHoldButton/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { ListItemHoldButton } from "./ListItemHoldButton";
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Clear as ClearIcon,
Visibility as VisibilityIcon,
VisibilityOff as VisibilityOffIcon,
Check as CheckIcon,
} from "@mui/icons-material";

import { useTranslation } from "hooks";
Expand All @@ -16,6 +17,7 @@ import { Partition } from "types";
import { ModelStatus } from "types/ModelType";
import { CustomListItemButton } from "../CustomListItemButton";
import { projectSlice } from "store/slices/project";
import { ListItemHoldButton } from "../ListItemHoldButton";

export const PredictionListItems = () => {
const dispatch = useDispatch();
Expand Down Expand Up @@ -65,21 +67,36 @@ export const PredictionListItems = () => {
})
);
};
const acceptPredictions = () => {
dispatch(dataSlice.actions.acceptPredictions({ isPermanent: true }));
dispatch(
classifierSlice.actions.updateModelStatus({
modelStatus: ModelStatus.Trained,
execSaga: false,
})
);
};

return (
<>
<CustomListItemButton
primaryText={t("Clear predictions")}
onClick={clearPredictions}
icon={<ClearIcon />}
/>
<CustomListItemButton
primaryText={t(
`${labeledImagesVisible ? "Hide" : "Show"} labeled images`
)}
onClick={toggleShowLabeledImages}
icon={labeledImagesVisible ? <VisibilityOffIcon /> : <VisibilityIcon />}
/>
<CustomListItemButton
primaryText={t("Clear predictions")}
onClick={clearPredictions}
icon={<ClearIcon />}
/>
<ListItemHoldButton
onHoldComplete={acceptPredictions}
primaryText="Accept Predictions (Hold)"
icon={<CheckIcon />}
holdDuration={100}
/>
</>
);
};
1 change: 1 addition & 0 deletions src/components/list-items/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ export { ClassifierOptimizerListItem } from "./ClassifierOptimizerListItem";
export { ClassifierPreprocessingListItem } from "./ClassifierPreprocessingListItem";
export { ClassifierDatasetListItem } from "./ClassifierDatasetListItem";
export { PredictionListItems } from "./PredictionListItems";
export { ListItemHoldButton } from "./ListItemHoldButton";
33 changes: 33 additions & 0 deletions src/store/slices/data/dataSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,39 @@ export const dataSlice = createSlice({
},
});
},
acceptPredictions(state, action: PayloadAction<{ isPermanent?: boolean }>) {
const { isPermanent } = action.payload;

const updates: Array<{ id: string } & Partial<ImageType>> = [];

state.images.ids.forEach((id) => {
const imagePartition = getDeferredProperty(
state.images.entities[id],
"partition"
);
const categoryId = getDeferredProperty(
state.images.entities[id],
"categoryId"
);
if (
imagePartition === Partition.Inference &&
categoryId !== UNKNOWN_IMAGE_CATEGORY_ID
) {
updates.push({
id: id as string,
partition: Partition.Unassigned,
});
}
});

dataSlice.caseReducers.updateImages(state, {
type: "updateImages",
payload: {
updates: updates,
isPermanent: isPermanent,
},
});
},
updateImages(
state,
action: PayloadAction<{
Expand Down

0 comments on commit cb7c87e

Please sign in to comment.