PromucFlow_constructor/app/client/src/components/ads/Toast.tsx

221 lines
6.1 KiB
TypeScript
Raw Normal View History

2020-11-24 07:01:37 +00:00
import React from "react";
import { CommonComponentProps, Classes, Variant } from "./common";
import styled from "styled-components";
import Icon, { IconSize } from "./Icon";
import Text, { TextType } from "./Text";
import { toast, ToastOptions, ToastContainer } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";
import { ReduxActionType } from "constants/ReduxActionConstants";
import { useDispatch } from "react-redux";
2021-04-23 13:50:55 +00:00
import { Colors } from "constants/Colors";
import DebugButton from "components/editorComponents/Debugger/DebugCTA";
2020-11-24 07:01:37 +00:00
type ToastProps = ToastOptions &
CommonComponentProps & {
text: string;
feat: Undo/Redo (#6654) * Scaffolding for undo-redo * undo redo working Poc commit * memory performance improvements by diffing * dont run update on undo/redo" * merging widget postion update and canvas bottom row update into one dsl update. * fix tabs widget * Visible updates per undo redo action (#6838) Co-authored-by: Rahul R <rahulramesha@Rahuls-MacBook-Pro.local> * resize atomic operation * fix switch control state issue * disallow undo/redo for snipping and comment mode * disallow undo/redo for snipping and comment mode * fix color picker issue in undo/redo * add test for replayDSL * option control fix, adding logs * minor position change undo redo updates * add test cases for replayHelpers * property Upade visual change * remove unused code * global hot key jest test for undo redo * Fixing batch updates on property change.. * add tests for toggle control in property pane * unwanted utils. * add tests for text control * add tests for deletion * add tests for dropping a new widget * adding jest test for replayUtils * add move widget tests * add tests for color picker control * add analytics for undo/redo * add analytics for undo/redo * tab addition atomic * cypress tests for propertyPane, toasts and radiowidget optionControl * replayDSL end of redo stack fix * property update changes * menu option control debounce input * color picker empty undo fix * fix cypress tests * widget add/remove atomic * revert alternative approach to handle atomic operations * update replayDSL test * add some comments * addressing review comments * flash color for property pane controls * Fixing adding of tabs widget as well. * code review comments. * merging widget postion update and canvas bottom row update into one dsl update. * fix ordering of tabs property control * meta property update canvas min height. * fixing failed specs. * Fixing entity explorer update on deleting tab from entity explorer. * address review comments and minor property update changes * fixing failing tests * merge conflicts * changes to cater widget api. * fix suggested widget table issue * draggable list for undo redo * fix widget name focus * excluding canvas updates. * fixing codeEditor update on propertySection collapse * fixed failing test case Co-authored-by: Abhinav Jha <abhinav@appsmith.com> Co-authored-by: Rahul R <rahulramesha@Rahuls-MacBook-Pro.local> Co-authored-by: root <root@DESKTOP-9GENCK0.localdomain> Co-authored-by: Ashok Kumar M <35134347+marks0351@users.noreply.github.com> Co-authored-by: Pawan Kumar <pawankumar@Pawans-MacBook-Pro.local>
2021-09-21 07:55:56 +00:00
actionElement?: JSX.Element;
2020-11-24 07:01:37 +00:00
variant?: Variant;
duration?: number;
onUndo?: () => void;
dispatchableAction?: { type: ReduxActionType; payload: any };
showDebugButton?: boolean;
2020-11-24 07:01:37 +00:00
hideProgressBar?: boolean;
};
const WrappedToastContainer = styled.div`
.Toastify__toast-container {
width: auto;
padding: 0px;
}
.Toastify__toast--default {
background: transparent;
}
.Toastify__toast {
cursor: auto;
min-height: auto;
border-radius: 0px !important;
2020-12-24 04:32:25 +00:00
font-family: ${(props) => props.theme.fonts.text};
margin-bottom: ${(props) => props.theme.spaces[4]}px;
2020-11-24 07:01:37 +00:00
}
.Toastify__toast-container--top-right {
top: 8em;
2020-11-24 07:01:37 +00:00
}
`;
export function StyledToastContainer(props: ToastOptions) {
2020-11-24 07:01:37 +00:00
return (
<WrappedToastContainer>
<ToastContainer {...props} />
</WrappedToastContainer>
);
}
2020-11-24 07:01:37 +00:00
const ToastBody = styled.div<{
variant?: Variant;
isUndo?: boolean;
2020-11-24 07:01:37 +00:00
dispatchableAction?: { type: ReduxActionType; payload: any };
}>`
width: 264px;
2020-12-24 04:32:25 +00:00
background: ${(props) => props.theme.colors.toast.bg};
padding: ${(props) => props.theme.spaces[4]}px
${(props) => props.theme.spaces[5]}px;
2020-11-24 07:01:37 +00:00
display: flex;
align-items: center;
justify-content: space-between;
// Using word-break here, as overflow-wrap: anywhere
// has no effect in safari
word-break: break-word;
2021-02-03 05:13:55 +00:00
overflow-wrap: anywhere;
2020-11-24 07:01:37 +00:00
2021-04-23 13:50:55 +00:00
div > .${Classes.ICON} {
2020-11-24 07:01:37 +00:00
cursor: auto;
2020-12-24 04:32:25 +00:00
margin-right: ${(props) => props.theme.spaces[3]}px;
2021-02-03 05:13:55 +00:00
margin-top: ${(props) => props.theme.spaces[1] / 2}px;
2020-11-24 07:01:37 +00:00
svg {
path {
2020-12-24 04:32:25 +00:00
fill: ${(props) =>
2020-11-24 07:01:37 +00:00
props.variant === Variant.warning
? props.theme.colors.toast.warningColor
: props.variant === Variant.danger
? "#FFFFFF"
: "#9F9F9F"};
}
rect {
2020-12-24 04:32:25 +00:00
${(props) =>
2020-11-24 07:01:37 +00:00
props.variant === Variant.danger
? `fill: ${props.theme.colors.toast.dangerColor}`
: null};
}
}
}
.${Classes.TEXT} {
2020-12-24 04:32:25 +00:00
color: ${(props) => props.theme.colors.toast.textColor};
2020-11-24 07:01:37 +00:00
}
2020-12-24 04:32:25 +00:00
${(props) =>
props.isUndo || props.dispatchableAction
2020-11-24 07:01:37 +00:00
? `
.undo-section .${Classes.TEXT} {
2020-11-24 07:01:37 +00:00
cursor: pointer;
margin-left: ${props.theme.spaces[3]}px;
color: ${props.theme.colors.toast.undo};
line-height: 18px;
font-weight: 600;
white-space: nowrap
2020-11-24 07:01:37 +00:00
}
`
: null}
`;
const FlexContainer = styled.div`
display: flex;
2021-02-03 05:13:55 +00:00
align-items: flex-start;
flex: 1;
`;
const ToastTextWrapper = styled.div`
flex: 1;
`;
2021-04-23 13:50:55 +00:00
const StyledDebugButton = styled(DebugButton)`
margin-left: auto;
`;
feat: Undo/Redo (#6654) * Scaffolding for undo-redo * undo redo working Poc commit * memory performance improvements by diffing * dont run update on undo/redo" * merging widget postion update and canvas bottom row update into one dsl update. * fix tabs widget * Visible updates per undo redo action (#6838) Co-authored-by: Rahul R <rahulramesha@Rahuls-MacBook-Pro.local> * resize atomic operation * fix switch control state issue * disallow undo/redo for snipping and comment mode * disallow undo/redo for snipping and comment mode * fix color picker issue in undo/redo * add test for replayDSL * option control fix, adding logs * minor position change undo redo updates * add test cases for replayHelpers * property Upade visual change * remove unused code * global hot key jest test for undo redo * Fixing batch updates on property change.. * add tests for toggle control in property pane * unwanted utils. * add tests for text control * add tests for deletion * add tests for dropping a new widget * adding jest test for replayUtils * add move widget tests * add tests for color picker control * add analytics for undo/redo * add analytics for undo/redo * tab addition atomic * cypress tests for propertyPane, toasts and radiowidget optionControl * replayDSL end of redo stack fix * property update changes * menu option control debounce input * color picker empty undo fix * fix cypress tests * widget add/remove atomic * revert alternative approach to handle atomic operations * update replayDSL test * add some comments * addressing review comments * flash color for property pane controls * Fixing adding of tabs widget as well. * code review comments. * merging widget postion update and canvas bottom row update into one dsl update. * fix ordering of tabs property control * meta property update canvas min height. * fixing failed specs. * Fixing entity explorer update on deleting tab from entity explorer. * address review comments and minor property update changes * fixing failing tests * merge conflicts * changes to cater widget api. * fix suggested widget table issue * draggable list for undo redo * fix widget name focus * excluding canvas updates. * fixing codeEditor update on propertySection collapse * fixed failing test case Co-authored-by: Abhinav Jha <abhinav@appsmith.com> Co-authored-by: Rahul R <rahulramesha@Rahuls-MacBook-Pro.local> Co-authored-by: root <root@DESKTOP-9GENCK0.localdomain> Co-authored-by: Ashok Kumar M <35134347+marks0351@users.noreply.github.com> Co-authored-by: Pawan Kumar <pawankumar@Pawans-MacBook-Pro.local>
2021-09-21 07:55:56 +00:00
const StyledActionText = styled(Text)`
color: ${(props) => props.theme.colors.toast.undoRedoColor} !important;
`;
function ToastComponent(props: ToastProps & { undoAction?: () => void }) {
2020-11-24 07:01:37 +00:00
const dispatch = useDispatch();
return (
<ToastBody
className="t--toast-action"
dispatchableAction={props.dispatchableAction}
isUndo={!!props.onUndo}
variant={props.variant || Variant.info}
2020-11-24 07:01:37 +00:00
>
<FlexContainer>
{props.variant === Variant.success ? (
<Icon fillColor={Colors.GREEN} name="success" size={IconSize.XXL} />
) : props.variant === Variant.warning ? (
<Icon name="warning" size={IconSize.XXL} />
) : null}
{props.variant === Variant.danger ? (
<Icon name="error" size={IconSize.XXL} />
) : null}
<ToastTextWrapper>
2021-04-23 13:50:55 +00:00
<Text type={TextType.P1}>{props.text}</Text>
feat: Undo/Redo (#6654) * Scaffolding for undo-redo * undo redo working Poc commit * memory performance improvements by diffing * dont run update on undo/redo" * merging widget postion update and canvas bottom row update into one dsl update. * fix tabs widget * Visible updates per undo redo action (#6838) Co-authored-by: Rahul R <rahulramesha@Rahuls-MacBook-Pro.local> * resize atomic operation * fix switch control state issue * disallow undo/redo for snipping and comment mode * disallow undo/redo for snipping and comment mode * fix color picker issue in undo/redo * add test for replayDSL * option control fix, adding logs * minor position change undo redo updates * add test cases for replayHelpers * property Upade visual change * remove unused code * global hot key jest test for undo redo * Fixing batch updates on property change.. * add tests for toggle control in property pane * unwanted utils. * add tests for text control * add tests for deletion * add tests for dropping a new widget * adding jest test for replayUtils * add move widget tests * add tests for color picker control * add analytics for undo/redo * add analytics for undo/redo * tab addition atomic * cypress tests for propertyPane, toasts and radiowidget optionControl * replayDSL end of redo stack fix * property update changes * menu option control debounce input * color picker empty undo fix * fix cypress tests * widget add/remove atomic * revert alternative approach to handle atomic operations * update replayDSL test * add some comments * addressing review comments * flash color for property pane controls * Fixing adding of tabs widget as well. * code review comments. * merging widget postion update and canvas bottom row update into one dsl update. * fix ordering of tabs property control * meta property update canvas min height. * fixing failed specs. * Fixing entity explorer update on deleting tab from entity explorer. * address review comments and minor property update changes * fixing failing tests * merge conflicts * changes to cater widget api. * fix suggested widget table issue * draggable list for undo redo * fix widget name focus * excluding canvas updates. * fixing codeEditor update on propertySection collapse * fixed failing test case Co-authored-by: Abhinav Jha <abhinav@appsmith.com> Co-authored-by: Rahul R <rahulramesha@Rahuls-MacBook-Pro.local> Co-authored-by: root <root@DESKTOP-9GENCK0.localdomain> Co-authored-by: Ashok Kumar M <35134347+marks0351@users.noreply.github.com> Co-authored-by: Pawan Kumar <pawankumar@Pawans-MacBook-Pro.local>
2021-09-21 07:55:56 +00:00
{props.actionElement && (
<StyledActionText type={TextType.P1}>
&nbsp;{props.actionElement}
</StyledActionText>
)}
{props.variant === Variant.danger && props.showDebugButton ? (
<StyledDebugButton
className="t--toast-debug-button"
source={"TOAST"}
/>
2021-04-23 13:50:55 +00:00
) : null}
</ToastTextWrapper>
</FlexContainer>
<div className="undo-section">
{props.onUndo || props.dispatchableAction ? (
<Text
onClick={() => {
if (props.dispatchableAction) {
dispatch(props.dispatchableAction);
props.undoAction && props.undoAction();
} else {
props.undoAction && props.undoAction();
}
}}
type={TextType.H6}
>
UNDO
</Text>
) : null}
</div>
2020-11-24 07:01:37 +00:00
</ToastBody>
);
}
2020-11-24 07:01:37 +00:00
export const Toaster = {
show: (config: ToastProps) => {
if (typeof config.text !== "string") {
console.error("Toast message needs to be a string");
return;
}
if (config.variant && !Object.values(Variant).includes(config.variant)) {
console.error(
"Toast type needs to be a one of " + Object.values(Variant).join(", "),
);
return;
}
2020-12-23 14:18:46 +00:00
// Stringified JSON is a long, but valid key :shrug:
const toastId = JSON.stringify(config);
toast(
2020-11-24 07:01:37 +00:00
<ToastComponent
undoAction={() => {
toast.dismiss(toastId);
config.onUndo && config.onUndo();
}}
{...config}
/>,
{
2020-12-23 14:18:46 +00:00
toastId: toastId,
pauseOnHover: !config.dispatchableAction && !config.hideProgressBar,
pauseOnFocusLoss: !config.dispatchableAction && !config.hideProgressBar,
2020-12-23 14:18:46 +00:00
autoClose: false,
closeOnClick: true,
2020-11-24 07:01:37 +00:00
hideProgressBar: config.hideProgressBar,
},
);
2020-12-23 14:18:46 +00:00
// Update autoclose everytime to keep resetting the timer.
toast.update(toastId, {
autoClose: config.duration || 5000,
});
2020-11-24 07:01:37 +00:00
},
clear: () => toast.dismiss(),
};