PromucFlow_constructor/app/client/src/components/ads/Toast.tsx
rahulramesha 6cefc6dbea
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 13:25:56 +05:30

221 lines
6.1 KiB
TypeScript

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";
import { Colors } from "constants/Colors";
import DebugButton from "components/editorComponents/Debugger/DebugCTA";
type ToastProps = ToastOptions &
CommonComponentProps & {
text: string;
actionElement?: JSX.Element;
variant?: Variant;
duration?: number;
onUndo?: () => void;
dispatchableAction?: { type: ReduxActionType; payload: any };
showDebugButton?: boolean;
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;
font-family: ${(props) => props.theme.fonts.text};
margin-bottom: ${(props) => props.theme.spaces[4]}px;
}
.Toastify__toast-container--top-right {
top: 8em;
}
`;
export function StyledToastContainer(props: ToastOptions) {
return (
<WrappedToastContainer>
<ToastContainer {...props} />
</WrappedToastContainer>
);
}
const ToastBody = styled.div<{
variant?: Variant;
isUndo?: boolean;
dispatchableAction?: { type: ReduxActionType; payload: any };
}>`
width: 264px;
background: ${(props) => props.theme.colors.toast.bg};
padding: ${(props) => props.theme.spaces[4]}px
${(props) => props.theme.spaces[5]}px;
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;
overflow-wrap: anywhere;
div > .${Classes.ICON} {
cursor: auto;
margin-right: ${(props) => props.theme.spaces[3]}px;
margin-top: ${(props) => props.theme.spaces[1] / 2}px;
svg {
path {
fill: ${(props) =>
props.variant === Variant.warning
? props.theme.colors.toast.warningColor
: props.variant === Variant.danger
? "#FFFFFF"
: "#9F9F9F"};
}
rect {
${(props) =>
props.variant === Variant.danger
? `fill: ${props.theme.colors.toast.dangerColor}`
: null};
}
}
}
.${Classes.TEXT} {
color: ${(props) => props.theme.colors.toast.textColor};
}
${(props) =>
props.isUndo || props.dispatchableAction
? `
.undo-section .${Classes.TEXT} {
cursor: pointer;
margin-left: ${props.theme.spaces[3]}px;
color: ${props.theme.colors.toast.undo};
line-height: 18px;
font-weight: 600;
white-space: nowrap
}
`
: null}
`;
const FlexContainer = styled.div`
display: flex;
align-items: flex-start;
flex: 1;
`;
const ToastTextWrapper = styled.div`
flex: 1;
`;
const StyledDebugButton = styled(DebugButton)`
margin-left: auto;
`;
const StyledActionText = styled(Text)`
color: ${(props) => props.theme.colors.toast.undoRedoColor} !important;
`;
function ToastComponent(props: ToastProps & { undoAction?: () => void }) {
const dispatch = useDispatch();
return (
<ToastBody
className="t--toast-action"
dispatchableAction={props.dispatchableAction}
isUndo={!!props.onUndo}
variant={props.variant || Variant.info}
>
<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>
<Text type={TextType.P1}>{props.text}</Text>
{props.actionElement && (
<StyledActionText type={TextType.P1}>
&nbsp;{props.actionElement}
</StyledActionText>
)}
{props.variant === Variant.danger && props.showDebugButton ? (
<StyledDebugButton
className="t--toast-debug-button"
source={"TOAST"}
/>
) : 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>
</ToastBody>
);
}
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;
}
// Stringified JSON is a long, but valid key :shrug:
const toastId = JSON.stringify(config);
toast(
<ToastComponent
undoAction={() => {
toast.dismiss(toastId);
config.onUndo && config.onUndo();
}}
{...config}
/>,
{
toastId: toastId,
pauseOnHover: !config.dispatchableAction && !config.hideProgressBar,
pauseOnFocusLoss: !config.dispatchableAction && !config.hideProgressBar,
autoClose: false,
closeOnClick: true,
hideProgressBar: config.hideProgressBar,
},
);
// Update autoclose everytime to keep resetting the timer.
toast.update(toastId, {
autoClose: config.duration || 5000,
});
},
clear: () => toast.dismiss(),
};