Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ToastView to Python Panels #4968

Merged
merged 6 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 59 additions & 11 deletions app/packages/components/src/components/Toast/Toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,57 @@ import React from "react";
import { atom, useRecoilState } from "recoil";
import { Box, Snackbar, SnackbarContent } from "@mui/material";

// Define types for the props
interface ToastProps {
message: React.ReactNode;
primary: (setOpen: React.Dispatch<React.SetStateAction<boolean>>) => React.ReactNode;
secondary: (setOpen: React.Dispatch<React.SetStateAction<boolean>>) => React.ReactNode;
duration?: number; // Optional duration, with a default value
primary?: any;
secondary?: any;
Br2850 marked this conversation as resolved.
Show resolved Hide resolved
duration?: number;
layout?: {
vertical?: "top" | "bottom";
horizontal?: "left" | "center" | "right";
height?: number | string;
top?: number | string;
bottom?: number | string;
backgroundColor?: string;
color?: string;
fontSize?: string;
textAlign?: string;
};
}

const toastStateAtom = atom({
key: "toastOpenState",
default: true,
});

const Toast: React.FC<ToastProps> = ({message, primary, secondary, duration = 5000 }) => {
const Toast: React.FC<ToastProps> = ({
message,
primary,
secondary,
duration = 5000,
layout = {},
}) => {
Br2850 marked this conversation as resolved.
Show resolved Hide resolved
const snackbarStyle = {
height: layout?.height || 5,
...(layout?.top && {
top: {
xs: layout.top,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these values supposed to be the same? Just checking :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this will conditionally add top or bottom only if they exist coming in from the Python Panel context

sm: layout.top,
md: layout.top,
lg: layout.top,
xl: layout.top,
},
}),
...(layout?.bottom && {
bottom: {
xs: layout.bottom,
sm: layout.bottom,
md: layout.bottom,
lg: layout.bottom,
xl: layout.bottom,
},
}),
};
Br2850 marked this conversation as resolved.
Show resolved Hide resolved

const [open, setOpen] = useRecoilState(toastStateAtom); // State management for toast visibility

Expand All @@ -29,27 +66,38 @@ const Toast: React.FC<ToastProps> = ({message, primary, secondary, duration = 50
const action = (
<div>
<Box display="flex" justifyContent="flex-end">
{primary(setOpen)} {/* Pass setOpen to primary button */}
{secondary(setOpen)} {/* Pass setOpen to secondary button */}
{/* note: Not implemented within Python Panels context */}
{primary && primary(setOpen)} {/* Pass setOpen to primary button */}
{secondary && secondary(setOpen)}{" "}
{/* Pass setOpen to secondary button */}
Br2850 marked this conversation as resolved.
Show resolved Hide resolved
</Box>
</div>
);

return (
<Snackbar
anchorOrigin={{ vertical: "bottom", horizontal: "center" }}
anchorOrigin={{
vertical: layout?.vertical || "bottom",
horizontal: layout?.horizontal || "center",
}}
open={open}
onClose={handleClose}
autoHideDuration={duration}
sx={{ height: 5 }}
sx={snackbarStyle}
>
<SnackbarContent
message={message}
action={action}
style={{ backgroundColor: "#333", color: "#fff" }}
sx={{
backgroundColor: layout?.backgroundColor || "#333",
color: layout?.color || "#fff",
fontSize: layout?.fontSize || "14px",
display: "block",
textAlign: layout?.textAlign || "left",
}}
/>
</Snackbar>
);
}
};

export default Toast;
10 changes: 10 additions & 0 deletions app/packages/core/src/plugins/SchemaIO/components/ToastView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from "react";
import { Toast } from "@fiftyone/components";

export default function ToastView(props) {
const { schema } = props;
const { view = {} } = schema;
const { message, duration, layout } = view;

Br2850 marked this conversation as resolved.
Show resolved Hide resolved
return <Toast message={message} duration={duration} layout={layout} />;
Br2850 marked this conversation as resolved.
Show resolved Hide resolved
}
1 change: 1 addition & 0 deletions app/packages/core/src/plugins/SchemaIO/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@ export { default as TabsView } from "./TabsView";
export { default as TagsView } from "./TagsView";
export { default as TextView } from "./TextView";
export { default as TextFieldView } from "./TextFieldView";
export { default as ToastView } from "./ToastView";
export { default as TupleView } from "./TupleView";
export { default as UnsupportedView } from "./UnsupportedView";
15 changes: 15 additions & 0 deletions app/packages/operators/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,20 @@ export class ModalView extends Button {
}
}

/**
* Operator class for describing a ToastView {@link View} for an
* operator type.
*/
export class ToastView extends View {
constructor(options: ViewProps) {
super(options);
this.name = "ToastView";
}
static fromJSON(json) {
return new ToastView(json);
}
}

/**
* Places where you can have your operator placement rendered.
*/
Expand Down Expand Up @@ -1267,6 +1281,7 @@ const VIEWS = {
LazyFieldView,
PillBadgeView,
ModalView,
ToastView,
};

export function typeFromJSON({ name, ...rest }): ANY_TYPE {
Expand Down
27 changes: 27 additions & 0 deletions fiftyone/operators/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1813,6 +1813,33 @@ def to_json(self):
return {**super().to_json(), "severity": self.severity}


class ToastView(View):
"""Displays a snackbar style toast element.

Examples::

schema = {
"message": "Test",
"duration": 30000,
"layout": {
"vertical": "top",
"horizontal": "center",
"top": "200px"
},
}
snackbar = types.ToastView(**schema)
panel.obj("toast", view=snackbar)

Args:
message: the message to display
duration (None): the duration to stay on screen in milliseconds
layout (None): the layout of the toast
"""

def __init__(self, **kwargs):
super().__init__(**kwargs)


Br2850 marked this conversation as resolved.
Show resolved Hide resolved
class CheckboxView(View):
"""Displays a checkbox.

Expand Down
Loading