2022-08-03 07:02:49 +00:00
|
|
|
import React, { useCallback } from "react";
|
2021-05-20 12:03:08 +00:00
|
|
|
import { useDispatch, useSelector } from "react-redux";
|
2024-08-09 14:20:29 +00:00
|
|
|
import { Tooltip, ToggleButton } from "@appsmith/ads";
|
2021-05-20 12:03:08 +00:00
|
|
|
|
2024-08-06 14:52:22 +00:00
|
|
|
import type { AppState } from "ee/reducers";
|
2021-08-06 09:17:56 +00:00
|
|
|
import { APP_MODE } from "entities/App";
|
2021-06-17 07:37:27 +00:00
|
|
|
|
2024-08-06 14:52:22 +00:00
|
|
|
import { getAppMode } from "ee/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
|
|
|
|
2024-08-06 14:52:22 +00:00
|
|
|
import { createMessage, EDITOR_HEADER } from "ee/constants/messages";
|
2024-03-11 04:32:19 +00:00
|
|
|
import { altText } from "../../utils/helpers";
|
2023-01-09 14:22:23 +00:00
|
|
|
|
|
|
|
|
function ToggleModeButton() {
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
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
|
|
|
|
2024-01-24 11:01:21 +00:00
|
|
|
if (isViewMode) return null;
|
2021-06-17 07:37:27 +00:00
|
|
|
|
|
|
|
|
return (
|
2023-05-19 18:37:06 +00:00
|
|
|
<Tooltip
|
2021-06-17 07:37:27 +00:00
|
|
|
content={
|
|
|
|
|
<>
|
2023-01-09 14:22:23 +00:00
|
|
|
{createMessage(EDITOR_HEADER.previewTooltip.text)}
|
2023-05-19 18:37:06 +00:00
|
|
|
<span style={{ marginLeft: 20 }}>
|
2024-03-11 04:32:19 +00:00
|
|
|
{`${altText()} ${createMessage(
|
|
|
|
|
EDITOR_HEADER.previewTooltip.shortcut,
|
|
|
|
|
)}`}
|
2023-01-09 14:22:23 +00:00
|
|
|
</span>
|
2021-06-17 07:37:27 +00:00
|
|
|
</>
|
|
|
|
|
}
|
2023-05-19 18:37:06 +00:00
|
|
|
isDisabled={appMode !== APP_MODE.EDIT}
|
|
|
|
|
placement="bottom"
|
2021-06-17 07:37:27 +00:00
|
|
|
>
|
2023-08-31 09:14:50 +00:00
|
|
|
<ToggleButton
|
2023-05-19 18:37:06 +00:00
|
|
|
data-testid={`${isPreviewMode ? "preview" : "edit"}-mode`}
|
2023-08-31 09:14:50 +00:00
|
|
|
icon="play-line"
|
|
|
|
|
isSelected={isPreviewMode}
|
2023-01-09 14:22:23 +00:00
|
|
|
onClick={onClickPreviewModeButton}
|
2023-05-19 18:37:06 +00:00
|
|
|
size="md"
|
2023-08-31 09:14:50 +00:00
|
|
|
/>
|
2023-05-19 18:37:06 +00:00
|
|
|
</Tooltip>
|
2021-06-17 07:37:27 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-03 07:02:49 +00:00
|
|
|
export default ToggleModeButton;
|