2024-08-06 14:52:22 +00:00
|
|
|
import { templateIdUrl } from "ee/RouteBuilder";
|
2023-06-15 05:26:42 +00:00
|
|
|
import {
|
|
|
|
|
FORK_THIS_TEMPLATE,
|
2023-11-25 02:29:35 +00:00
|
|
|
FORK_THIS_TEMPLATE_BUILDING_BLOCK,
|
2023-06-15 05:26:42 +00:00
|
|
|
GO_BACK,
|
|
|
|
|
createMessage,
|
2024-08-06 14:52:22 +00:00
|
|
|
} from "ee/constants/messages";
|
2024-08-09 14:20:29 +00:00
|
|
|
import { Button, Link, Text } from "@appsmith/ads";
|
2023-06-15 05:26:42 +00:00
|
|
|
import { useQuery } from "pages/Editor/utils";
|
|
|
|
|
import React from "react";
|
|
|
|
|
import { useSelector } from "react-redux";
|
|
|
|
|
import {
|
|
|
|
|
getActiveTemplateSelector,
|
|
|
|
|
getForkableWorkspaces,
|
2024-02-13 07:08:08 +00:00
|
|
|
isImportingTemplateSelector,
|
2023-11-09 04:19:02 +00:00
|
|
|
isImportingTemplateToAppSelector,
|
2023-06-15 05:26:42 +00:00
|
|
|
} from "selectors/templatesSelectors";
|
|
|
|
|
import styled from "styled-components";
|
|
|
|
|
import history from "utils/history";
|
|
|
|
|
import ForkTemplate from "./ForkTemplate";
|
2023-11-25 02:29:35 +00:00
|
|
|
import { TEMPLATE_BUILDING_BLOCKS_FILTER_FUNCTION_VALUE } from "./constants";
|
2023-06-15 05:26:42 +00:00
|
|
|
|
|
|
|
|
const HeaderWrapper = styled.div`
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
justify-content: space-between;
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const Title = styled(Text)`
|
|
|
|
|
display: inline-block;
|
|
|
|
|
color: var(--ads-v2-color-fg-emphasis-plus);
|
|
|
|
|
`;
|
|
|
|
|
|
2023-10-11 07:35:24 +00:00
|
|
|
interface Props {
|
2024-02-13 07:08:08 +00:00
|
|
|
handleBackPress?: () => void;
|
2023-06-15 05:26:42 +00:00
|
|
|
templateId: string;
|
2023-11-09 04:19:02 +00:00
|
|
|
onClickUseTemplate?: (id: string) => void;
|
|
|
|
|
showBack: boolean;
|
2023-10-11 07:35:24 +00:00
|
|
|
}
|
2023-06-15 05:26:42 +00:00
|
|
|
const SHOW_FORK_MODAL_PARAM = "showForkTemplateModal";
|
|
|
|
|
|
2023-11-09 04:19:02 +00:00
|
|
|
function TemplateViewHeader({
|
2024-02-13 07:08:08 +00:00
|
|
|
handleBackPress,
|
2023-11-09 04:19:02 +00:00
|
|
|
onClickUseTemplate,
|
|
|
|
|
showBack,
|
|
|
|
|
templateId,
|
|
|
|
|
}: Props) {
|
2023-06-15 05:26:42 +00:00
|
|
|
const currentTemplate = useSelector(getActiveTemplateSelector);
|
|
|
|
|
const query = useQuery();
|
|
|
|
|
const workspaceList = useSelector(getForkableWorkspaces);
|
2023-11-09 04:19:02 +00:00
|
|
|
const isImportingTemplateToApp = useSelector(
|
|
|
|
|
isImportingTemplateToAppSelector,
|
|
|
|
|
);
|
2024-02-13 07:08:08 +00:00
|
|
|
const isImportingTemplate = useSelector(isImportingTemplateSelector);
|
2023-06-15 05:26:42 +00:00
|
|
|
const goBack = () => {
|
2024-02-13 07:08:08 +00:00
|
|
|
if (handleBackPress) {
|
|
|
|
|
handleBackPress();
|
|
|
|
|
} else {
|
|
|
|
|
history.goBack();
|
|
|
|
|
}
|
2023-06-15 05:26:42 +00:00
|
|
|
};
|
|
|
|
|
const onForkModalClose = () => {
|
|
|
|
|
history.replace(`${templateIdUrl({ id: templateId })}`);
|
|
|
|
|
};
|
|
|
|
|
const onForkButtonTrigger = () => {
|
2023-11-09 04:19:02 +00:00
|
|
|
if (onClickUseTemplate) {
|
|
|
|
|
onClickUseTemplate(templateId);
|
|
|
|
|
} else {
|
|
|
|
|
history.replace(
|
|
|
|
|
`${templateIdUrl({ id: templateId })}?${SHOW_FORK_MODAL_PARAM}=true`,
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-06-15 05:26:42 +00:00
|
|
|
};
|
2023-11-25 02:29:35 +00:00
|
|
|
|
|
|
|
|
const FORK_BUTTON_TEXT = currentTemplate?.functions.includes(
|
|
|
|
|
TEMPLATE_BUILDING_BLOCKS_FILTER_FUNCTION_VALUE,
|
|
|
|
|
)
|
|
|
|
|
? FORK_THIS_TEMPLATE_BUILDING_BLOCK
|
|
|
|
|
: FORK_THIS_TEMPLATE;
|
|
|
|
|
|
2023-06-15 05:26:42 +00:00
|
|
|
return (
|
|
|
|
|
<HeaderWrapper>
|
2023-11-09 04:19:02 +00:00
|
|
|
{showBack && (
|
|
|
|
|
<Link
|
|
|
|
|
data-testid="t--template-view-goback"
|
|
|
|
|
onClick={goBack}
|
|
|
|
|
startIcon="arrow-left-line"
|
|
|
|
|
>
|
|
|
|
|
{createMessage(GO_BACK)}
|
|
|
|
|
</Link>
|
|
|
|
|
)}
|
2023-06-15 05:26:42 +00:00
|
|
|
<Title kind="heading-l" renderAs="h1">
|
|
|
|
|
{currentTemplate?.title}
|
|
|
|
|
</Title>
|
|
|
|
|
<section>
|
|
|
|
|
{!!workspaceList.length && (
|
|
|
|
|
<ForkTemplate
|
|
|
|
|
onClose={onForkModalClose}
|
|
|
|
|
showForkModal={!!query.get(SHOW_FORK_MODAL_PARAM)}
|
|
|
|
|
templateId={templateId}
|
|
|
|
|
>
|
|
|
|
|
<Button
|
|
|
|
|
className="template-fork-button"
|
|
|
|
|
data-testid="template-fork-button"
|
2024-02-13 07:08:08 +00:00
|
|
|
isLoading={
|
|
|
|
|
onClickUseTemplate &&
|
|
|
|
|
(isImportingTemplateToApp || isImportingTemplate)
|
|
|
|
|
}
|
2023-06-15 05:26:42 +00:00
|
|
|
onClick={onForkButtonTrigger}
|
|
|
|
|
size="md"
|
|
|
|
|
startIcon="fork-2"
|
|
|
|
|
>
|
2023-11-25 02:29:35 +00:00
|
|
|
{createMessage(FORK_BUTTON_TEXT)}
|
2023-06-15 05:26:42 +00:00
|
|
|
</Button>
|
|
|
|
|
</ForkTemplate>
|
|
|
|
|
)}
|
|
|
|
|
</section>
|
|
|
|
|
</HeaderWrapper>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default TemplateViewHeader;
|