2022-08-03 07:02:49 +00:00
|
|
|
import React, { useCallback } from "react";
|
2021-05-20 12:03:08 +00:00
|
|
|
import styled from "styled-components";
|
|
|
|
|
import { useDispatch, useSelector } from "react-redux";
|
2023-01-09 14:22:23 +00:00
|
|
|
import {
|
|
|
|
|
Button,
|
|
|
|
|
Category,
|
|
|
|
|
IconPositions,
|
|
|
|
|
Size,
|
|
|
|
|
TooltipComponent,
|
2023-01-23 03:50:47 +00:00
|
|
|
} from "design-system-old";
|
2021-05-20 12:03:08 +00:00
|
|
|
|
2022-08-24 12:16:32 +00:00
|
|
|
import { AppState } from "@appsmith/reducers";
|
2021-08-06 09:17:56 +00:00
|
|
|
import { APP_MODE } from "entities/App";
|
2021-06-17 07:37:27 +00:00
|
|
|
|
2022-07-26 12:34:54 +00:00
|
|
|
import { getAppMode } from "selectors/applicationSelectors";
|
2023-01-09 14:22:23 +00:00
|
|
|
import { setPreviewModeInitAction } from "actions/editorActions";
|
2022-08-03 07:02:49 +00:00
|
|
|
import { previewModeSelector } from "selectors/editorSelectors";
|
2021-07-02 06:04:36 +00:00
|
|
|
|
2022-01-25 13:56:52 +00:00
|
|
|
import { isExploringSelector } from "selectors/onboardingSelectors";
|
2023-01-09 14:22:23 +00:00
|
|
|
import { Colors } from "constants/Colors";
|
|
|
|
|
import { createMessage, EDITOR_HEADER } from "ce/constants/messages";
|
|
|
|
|
|
|
|
|
|
const StyledButton = styled(Button)<{ active: boolean }>`
|
|
|
|
|
${(props) =>
|
|
|
|
|
props.active &&
|
|
|
|
|
`
|
|
|
|
|
background-color: ${Colors.GREY_200};
|
|
|
|
|
border: 1.2px solid transparent;
|
|
|
|
|
`}
|
|
|
|
|
padding: 0 ${(props) => props.theme.spaces[2]}px;
|
|
|
|
|
color: ${Colors.GREY_900};
|
2021-05-20 12:03:08 +00:00
|
|
|
height: ${(props) => props.theme.smallHeaderHeight};
|
|
|
|
|
|
2023-01-09 14:22:23 +00:00
|
|
|
svg {
|
|
|
|
|
height: 18px;
|
|
|
|
|
width: 18px;
|
2021-07-05 07:12:02 +00:00
|
|
|
}
|
2023-01-09 14:22:23 +00:00
|
|
|
`;
|
2021-07-05 07:12:02 +00:00
|
|
|
|
2023-01-09 14:22:23 +00:00
|
|
|
function ToggleModeButton() {
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
const isExploring = useSelector(isExploringSelector);
|
|
|
|
|
const isPreviewMode = useSelector(previewModeSelector);
|
|
|
|
|
const appMode = useSelector(getAppMode);
|
2021-07-05 07:12:02 +00:00
|
|
|
|
2023-01-09 14:22:23 +00:00
|
|
|
const mode = useSelector((state: AppState) => state.entities.app.mode);
|
|
|
|
|
const isViewMode = mode === APP_MODE.PUBLISHED;
|
2021-05-20 12:03:08 +00:00
|
|
|
|
2023-01-09 14:22:23 +00:00
|
|
|
const onClickPreviewModeButton = useCallback(() => {
|
|
|
|
|
dispatch(setPreviewModeInitAction(!isPreviewMode));
|
|
|
|
|
}, [dispatch, setPreviewModeInitAction, isPreviewMode]);
|
2021-05-20 12:03:08 +00:00
|
|
|
|
2023-01-09 14:22:23 +00:00
|
|
|
if (isExploring || isViewMode) return null;
|
2021-06-17 07:37:27 +00:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<TooltipComponent
|
|
|
|
|
content={
|
|
|
|
|
<>
|
2023-01-09 14:22:23 +00:00
|
|
|
{createMessage(EDITOR_HEADER.previewTooltip.text)}
|
|
|
|
|
<span style={{ color: "#fff", marginLeft: 20 }}>
|
|
|
|
|
{createMessage(EDITOR_HEADER.previewTooltip.shortcut)}
|
|
|
|
|
</span>
|
2021-06-17 07:37:27 +00:00
|
|
|
</>
|
|
|
|
|
}
|
2023-01-09 14:22:23 +00:00
|
|
|
disabled={appMode !== APP_MODE.EDIT}
|
2021-06-17 07:37:27 +00:00
|
|
|
hoverOpenDelay={1000}
|
2022-06-24 14:23:02 +00:00
|
|
|
position="bottom"
|
2021-06-17 07:37:27 +00:00
|
|
|
>
|
2023-01-09 14:22:23 +00:00
|
|
|
<StyledButton
|
|
|
|
|
active={isPreviewMode}
|
|
|
|
|
category={Category.tertiary}
|
|
|
|
|
data-cy={`${isPreviewMode ? "preview" : "edit"}-mode`}
|
|
|
|
|
icon={"play-circle-line"}
|
|
|
|
|
iconPosition={IconPositions.left}
|
|
|
|
|
onClick={onClickPreviewModeButton}
|
|
|
|
|
size={Size.medium}
|
|
|
|
|
tag={"button"}
|
|
|
|
|
text={createMessage(EDITOR_HEADER.previewTooltip.text).toUpperCase()}
|
|
|
|
|
/>
|
2021-06-17 07:37:27 +00:00
|
|
|
</TooltipComponent>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-03 07:02:49 +00:00
|
|
|
export default ToggleModeButton;
|