2023-10-04 09:42:02 +02:00
|
|
|
import MenuBookIcon from "@mui/icons-material/MenuBook";
|
|
|
|
import Codebox from "../Codebox/Codebox";
|
2022-05-12 15:34:04 +02:00
|
|
|
import {
|
|
|
|
Collapse,
|
|
|
|
IconButton,
|
|
|
|
useMediaQuery,
|
|
|
|
Tooltip,
|
|
|
|
Divider,
|
2023-01-05 15:23:40 +01:00
|
|
|
styled,
|
2023-10-04 09:42:02 +02:00
|
|
|
} from "@mui/material";
|
|
|
|
import { FileCopy, Info } from "@mui/icons-material";
|
|
|
|
import { ConditionallyRender } from "component/common/ConditionallyRender/ConditionallyRender";
|
|
|
|
import Loader from "../Loader/Loader";
|
|
|
|
import copy from "copy-to-clipboard";
|
|
|
|
import useToast from "hooks/useToast";
|
|
|
|
import React, { ReactNode, useState } from "react";
|
|
|
|
import { ReactComponent as MobileGuidanceBG } from "assets/img/mobileGuidanceBg.svg";
|
|
|
|
import { formTemplateSidebarWidth } from "./FormTemplate.styles";
|
|
|
|
import { relative } from "themes/themeStyles";
|
2022-01-14 15:50:02 +01:00
|
|
|
|
|
|
|
interface ICreateProps {
|
2023-09-07 12:27:46 +02:00
|
|
|
title?: ReactNode;
|
2022-01-14 15:50:02 +01:00
|
|
|
description: string;
|
|
|
|
documentationLink: string;
|
2022-04-25 09:36:23 +02:00
|
|
|
documentationLinkLabel: string;
|
2022-01-14 15:50:02 +01:00
|
|
|
loading?: boolean;
|
2022-03-09 14:59:24 +01:00
|
|
|
modal?: boolean;
|
2023-07-11 08:47:38 +02:00
|
|
|
disablePadding?: boolean;
|
2023-10-04 09:42:02 +02:00
|
|
|
compactPadding?: boolean;
|
|
|
|
showDescription?: boolean;
|
|
|
|
showLink?: boolean;
|
2023-09-07 10:51:50 +02:00
|
|
|
formatApiCode?: () => string;
|
2023-09-07 12:27:46 +02:00
|
|
|
footer?: ReactNode;
|
2023-10-04 09:42:02 +02:00
|
|
|
compact?: boolean;
|
2022-01-14 15:50:02 +01:00
|
|
|
}
|
|
|
|
|
2023-10-04 09:42:02 +02:00
|
|
|
const StyledContainer = styled("section", {
|
|
|
|
shouldForwardProp: (prop) =>
|
|
|
|
!["modal", "compact"].includes(prop.toString()),
|
|
|
|
})<{ modal?: boolean; compact?: boolean }>(({ theme, modal, compact }) => ({
|
|
|
|
minHeight: modal ? "100vh" : compact ? 0 : "80vh",
|
2023-01-05 15:23:40 +01:00
|
|
|
borderRadius: modal ? 0 : theme.spacing(2),
|
2023-10-04 09:42:02 +02:00
|
|
|
width: "100%",
|
|
|
|
display: "flex",
|
|
|
|
margin: "0 auto",
|
|
|
|
overflow: modal ? "unset" : "hidden",
|
2023-01-05 15:23:40 +01:00
|
|
|
[theme.breakpoints.down(1100)]: {
|
2023-10-04 09:42:02 +02:00
|
|
|
flexDirection: "column",
|
2023-01-05 15:23:40 +01:00
|
|
|
minHeight: 0,
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
|
2023-10-04 09:42:02 +02:00
|
|
|
const StyledRelativeDiv = styled("div")(({ theme }) => relative);
|
2023-01-05 15:23:40 +01:00
|
|
|
|
2023-10-04 09:42:02 +02:00
|
|
|
const StyledMain = styled("div")(({ theme }) => ({
|
|
|
|
display: "flex",
|
|
|
|
flexDirection: "column",
|
2023-09-07 12:27:46 +02:00
|
|
|
flexGrow: 1,
|
|
|
|
flexShrink: 1,
|
2023-10-04 09:42:02 +02:00
|
|
|
width: "100%",
|
2023-09-07 12:27:46 +02:00
|
|
|
[theme.breakpoints.down(1100)]: {
|
2023-10-04 09:42:02 +02:00
|
|
|
width: "100%",
|
2023-09-07 12:27:46 +02:00
|
|
|
},
|
|
|
|
}));
|
|
|
|
|
2023-10-04 09:42:02 +02:00
|
|
|
const StyledFormContent = styled("div", {
|
|
|
|
shouldForwardProp: (prop) => {
|
|
|
|
return !["disablePadding", "compactPadding"].includes(prop.toString());
|
2023-01-05 15:23:40 +01:00
|
|
|
},
|
2023-10-04 09:42:02 +02:00
|
|
|
})<{ disablePadding?: boolean; compactPadding?: boolean }>(
|
|
|
|
({ theme, disablePadding, compactPadding }) => ({
|
|
|
|
backgroundColor: theme.palette.background.paper,
|
|
|
|
display: "flex",
|
|
|
|
flexDirection: "column",
|
|
|
|
flexGrow: 1,
|
|
|
|
padding: disablePadding
|
|
|
|
? 0
|
|
|
|
: compactPadding
|
|
|
|
? theme.spacing(4)
|
|
|
|
: theme.spacing(6),
|
|
|
|
[theme.breakpoints.down("lg")]: {
|
|
|
|
padding: disablePadding ? 0 : theme.spacing(4),
|
|
|
|
},
|
|
|
|
[theme.breakpoints.down(1100)]: {
|
|
|
|
width: "100%",
|
|
|
|
},
|
|
|
|
[theme.breakpoints.down(500)]: {
|
|
|
|
padding: disablePadding ? 0 : theme.spacing(4, 2),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
2023-01-05 15:23:40 +01:00
|
|
|
|
2023-10-04 09:42:02 +02:00
|
|
|
const StyledFooter = styled("div")(({ theme }) => ({
|
2023-09-07 12:27:46 +02:00
|
|
|
backgroundColor: theme.palette.background.paper,
|
|
|
|
padding: theme.spacing(4, 6),
|
2023-10-04 09:42:02 +02:00
|
|
|
[theme.breakpoints.down("lg")]: {
|
2023-09-07 12:27:46 +02:00
|
|
|
padding: theme.spacing(4),
|
|
|
|
},
|
|
|
|
[theme.breakpoints.down(500)]: {
|
|
|
|
padding: theme.spacing(4, 2),
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
|
2023-10-04 09:42:02 +02:00
|
|
|
const StyledTitle = styled("h1")(({ theme }) => ({
|
2023-01-05 15:23:40 +01:00
|
|
|
marginBottom: theme.fontSizes.mainHeader,
|
2023-10-04 09:42:02 +02:00
|
|
|
fontWeight: "normal",
|
2023-01-05 15:23:40 +01:00
|
|
|
}));
|
|
|
|
|
|
|
|
const StyledSidebarDivider = styled(Divider)(({ theme }) => ({
|
|
|
|
opacity: 0.3,
|
|
|
|
marginBottom: theme.spacing(0.5),
|
|
|
|
}));
|
|
|
|
|
2023-10-04 09:42:02 +02:00
|
|
|
const StyledSubtitle = styled("h2")(({ theme }) => ({
|
2023-03-06 11:58:36 +01:00
|
|
|
color: theme.palette.common.white,
|
2023-01-05 15:23:40 +01:00
|
|
|
marginBottom: theme.spacing(2),
|
2023-10-04 09:42:02 +02:00
|
|
|
display: "flex",
|
|
|
|
justifyContent: "space-between",
|
|
|
|
alignItems: "center",
|
2023-01-05 15:23:40 +01:00
|
|
|
fontWeight: theme.fontWeight.bold,
|
|
|
|
fontSize: theme.fontSizes.bodySize,
|
|
|
|
}));
|
|
|
|
|
|
|
|
const StyledIcon = styled(FileCopy)(({ theme }) => ({
|
2023-03-06 11:58:36 +01:00
|
|
|
fill: theme.palette.primary.contrastText,
|
2023-01-05 15:23:40 +01:00
|
|
|
}));
|
|
|
|
|
2023-10-04 09:42:02 +02:00
|
|
|
const StyledMobileGuidanceContainer = styled("div")(() => ({
|
2023-01-05 15:23:40 +01:00
|
|
|
zIndex: 1,
|
2023-10-04 09:42:02 +02:00
|
|
|
position: "absolute",
|
2023-01-05 15:23:40 +01:00
|
|
|
right: -3,
|
|
|
|
top: -3,
|
|
|
|
}));
|
|
|
|
|
|
|
|
const StyledMobileGuidanceBackground = styled(MobileGuidanceBG)(() => ({
|
2023-10-04 09:42:02 +02:00
|
|
|
width: "75px",
|
|
|
|
height: "75px",
|
2023-01-05 15:23:40 +01:00
|
|
|
}));
|
|
|
|
|
|
|
|
const StyledMobileGuidanceButton = styled(IconButton)(() => ({
|
2023-10-04 09:42:02 +02:00
|
|
|
position: "absolute",
|
2023-01-05 15:23:40 +01:00
|
|
|
zIndex: 400,
|
|
|
|
right: 0,
|
|
|
|
}));
|
|
|
|
|
|
|
|
const StyledInfoIcon = styled(Info)(({ theme }) => ({
|
2023-03-06 11:58:36 +01:00
|
|
|
fill: theme.palette.primary.contrastText,
|
2023-01-05 15:23:40 +01:00
|
|
|
}));
|
|
|
|
|
2023-10-04 09:42:02 +02:00
|
|
|
const StyledSidebar = styled("aside")(({ theme }) => ({
|
2023-03-06 11:58:36 +01:00
|
|
|
backgroundColor: theme.palette.background.sidebar,
|
2023-01-05 15:23:40 +01:00
|
|
|
padding: theme.spacing(4),
|
|
|
|
flexGrow: 0,
|
|
|
|
flexShrink: 0,
|
|
|
|
width: formTemplateSidebarWidth,
|
|
|
|
[theme.breakpoints.down(1100)]: {
|
2023-10-04 09:42:02 +02:00
|
|
|
width: "100%",
|
|
|
|
color: "red",
|
2023-01-05 15:23:40 +01:00
|
|
|
},
|
|
|
|
[theme.breakpoints.down(500)]: {
|
|
|
|
padding: theme.spacing(4, 2),
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
|
2023-10-04 09:42:02 +02:00
|
|
|
const StyledDescription = styled("p")(({ theme }) => ({
|
2023-03-06 11:58:36 +01:00
|
|
|
color: theme.palette.common.white,
|
2023-01-05 15:23:40 +01:00
|
|
|
zIndex: 1,
|
2023-10-04 09:42:02 +02:00
|
|
|
position: "relative",
|
2023-01-05 15:23:40 +01:00
|
|
|
}));
|
|
|
|
|
2023-10-04 09:42:02 +02:00
|
|
|
const StyledLinkContainer = styled("div")(({ theme }) => ({
|
2023-01-05 15:23:40 +01:00
|
|
|
margin: theme.spacing(3, 0),
|
2023-10-04 09:42:02 +02:00
|
|
|
display: "flex",
|
|
|
|
alignItems: "center",
|
2023-01-05 15:23:40 +01:00
|
|
|
}));
|
|
|
|
|
|
|
|
const StyledLinkIcon = styled(MenuBookIcon)(({ theme }) => ({
|
|
|
|
marginRight: theme.spacing(1),
|
2023-03-06 11:58:36 +01:00
|
|
|
color: theme.palette.primary.contrastText,
|
2023-01-05 15:23:40 +01:00
|
|
|
}));
|
|
|
|
|
2023-10-04 09:42:02 +02:00
|
|
|
const StyledDocumentationLink = styled("a")(({ theme }) => ({
|
2023-03-06 11:58:36 +01:00
|
|
|
color: theme.palette.primary.contrastText,
|
2023-10-04 09:42:02 +02:00
|
|
|
display: "block",
|
|
|
|
"&:hover": {
|
|
|
|
textDecoration: "none",
|
2023-01-05 15:23:40 +01:00
|
|
|
},
|
|
|
|
}));
|
|
|
|
|
2022-01-14 15:50:02 +01:00
|
|
|
const FormTemplate: React.FC<ICreateProps> = ({
|
|
|
|
title,
|
|
|
|
description,
|
|
|
|
children,
|
|
|
|
documentationLink,
|
2022-03-29 09:30:57 +02:00
|
|
|
documentationLinkLabel,
|
2022-01-14 15:50:02 +01:00
|
|
|
loading,
|
2022-03-09 14:59:24 +01:00
|
|
|
modal,
|
2022-01-14 15:50:02 +01:00
|
|
|
formatApiCode,
|
2023-07-11 08:47:38 +02:00
|
|
|
disablePadding,
|
2023-10-04 09:42:02 +02:00
|
|
|
compactPadding = false,
|
|
|
|
showDescription = true,
|
|
|
|
showLink = true,
|
2023-09-07 12:27:46 +02:00
|
|
|
footer,
|
2023-10-04 09:42:02 +02:00
|
|
|
compact,
|
2022-01-14 15:50:02 +01:00
|
|
|
}) => {
|
|
|
|
const { setToastData } = useToast();
|
2022-03-29 09:30:57 +02:00
|
|
|
const smallScreen = useMediaQuery(`(max-width:${1099}px)`);
|
2022-01-14 15:50:02 +01:00
|
|
|
const copyCommand = () => {
|
2023-09-07 10:51:50 +02:00
|
|
|
if (formatApiCode !== undefined) {
|
|
|
|
if (copy(formatApiCode())) {
|
|
|
|
setToastData({
|
2023-10-04 09:42:02 +02:00
|
|
|
title: "Successfully copied the command",
|
|
|
|
text: "The command should now be automatically copied to your clipboard",
|
2023-09-07 10:51:50 +02:00
|
|
|
autoHideDuration: 6000,
|
2023-10-04 09:42:02 +02:00
|
|
|
type: "success",
|
2023-09-07 10:51:50 +02:00
|
|
|
show: true,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
setToastData({
|
2023-10-04 09:42:02 +02:00
|
|
|
title: "Could not copy the command",
|
|
|
|
text: "Sorry, but we could not copy the command.",
|
2023-09-07 10:51:50 +02:00
|
|
|
autoHideDuration: 6000,
|
2023-10-04 09:42:02 +02:00
|
|
|
type: "error",
|
2023-09-07 10:51:50 +02:00
|
|
|
show: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-10-04 09:42:02 +02:00
|
|
|
const renderApiInfo = (apiDisabled: boolean, dividerDisabled = false) => {
|
2023-09-07 10:51:50 +02:00
|
|
|
if (!apiDisabled) {
|
|
|
|
return (
|
|
|
|
<>
|
2023-10-04 09:42:02 +02:00
|
|
|
<ConditionallyRender
|
|
|
|
condition={!dividerDisabled}
|
|
|
|
show={<StyledSidebarDivider />}
|
|
|
|
/>
|
2023-09-07 10:51:50 +02:00
|
|
|
<StyledSubtitle>
|
2023-10-04 09:42:02 +02:00
|
|
|
API Command{" "}
|
|
|
|
<Tooltip title="Copy command" arrow>
|
|
|
|
<IconButton onClick={copyCommand} size="large">
|
2023-09-07 10:51:50 +02:00
|
|
|
<StyledIcon />
|
|
|
|
</IconButton>
|
|
|
|
</Tooltip>
|
|
|
|
</StyledSubtitle>
|
2023-10-04 09:42:02 +02:00
|
|
|
<Codebox text={formatApiCode!()} />{" "}
|
2023-09-07 10:51:50 +02:00
|
|
|
</>
|
|
|
|
);
|
2022-01-14 15:50:02 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2023-10-04 09:42:02 +02:00
|
|
|
<StyledContainer modal={modal} compact={compact}>
|
2022-03-14 13:14:26 +01:00
|
|
|
<ConditionallyRender
|
|
|
|
condition={smallScreen}
|
|
|
|
show={
|
2023-01-05 15:23:40 +01:00
|
|
|
<StyledRelativeDiv>
|
2022-03-14 13:14:26 +01:00
|
|
|
<MobileGuidance
|
|
|
|
description={description}
|
|
|
|
documentationLink={documentationLink}
|
2022-03-29 09:30:57 +02:00
|
|
|
documentationLinkLabel={documentationLinkLabel}
|
2022-03-14 13:14:26 +01:00
|
|
|
/>
|
2023-01-05 15:23:40 +01:00
|
|
|
</StyledRelativeDiv>
|
2022-03-14 13:14:26 +01:00
|
|
|
}
|
|
|
|
/>
|
2023-09-07 12:27:46 +02:00
|
|
|
<StyledMain>
|
2023-10-04 09:42:02 +02:00
|
|
|
<StyledFormContent
|
|
|
|
disablePadding={disablePadding}
|
|
|
|
compactPadding={compactPadding}
|
|
|
|
>
|
2023-09-07 12:27:46 +02:00
|
|
|
<ConditionallyRender
|
|
|
|
condition={loading || false}
|
|
|
|
show={<Loader />}
|
|
|
|
elseShow={
|
|
|
|
<>
|
|
|
|
<ConditionallyRender
|
|
|
|
condition={title !== undefined}
|
|
|
|
show={<StyledTitle>{title}</StyledTitle>}
|
|
|
|
/>
|
|
|
|
{children}
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</StyledFormContent>
|
2022-01-14 15:50:02 +01:00
|
|
|
<ConditionallyRender
|
2023-09-07 12:27:46 +02:00
|
|
|
condition={footer !== undefined}
|
|
|
|
show={() => (
|
2022-03-14 13:14:26 +01:00
|
|
|
<>
|
2023-09-07 12:27:46 +02:00
|
|
|
<Divider />
|
|
|
|
<StyledFooter>{footer}</StyledFooter>
|
2022-03-14 13:14:26 +01:00
|
|
|
</>
|
2023-09-07 12:27:46 +02:00
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</StyledMain>
|
2022-03-14 13:14:26 +01:00
|
|
|
<ConditionallyRender
|
|
|
|
condition={!smallScreen}
|
|
|
|
show={
|
|
|
|
<Guidance
|
|
|
|
description={description}
|
|
|
|
documentationLink={documentationLink}
|
2022-03-29 09:30:57 +02:00
|
|
|
documentationLinkLabel={documentationLinkLabel}
|
2023-10-04 09:42:02 +02:00
|
|
|
showDescription={showDescription}
|
|
|
|
showLink={showLink}
|
2022-03-14 13:14:26 +01:00
|
|
|
>
|
2023-10-04 09:42:02 +02:00
|
|
|
{renderApiInfo(
|
|
|
|
formatApiCode === undefined,
|
|
|
|
!(showDescription || showLink)
|
|
|
|
)}
|
2022-03-14 13:14:26 +01:00
|
|
|
</Guidance>
|
|
|
|
}
|
|
|
|
/>
|
2023-01-05 15:23:40 +01:00
|
|
|
</StyledContainer>
|
2022-01-14 15:50:02 +01:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-03-14 13:14:26 +01:00
|
|
|
interface IMobileGuidance {
|
|
|
|
description: string;
|
|
|
|
documentationLink: string;
|
2022-03-29 09:30:57 +02:00
|
|
|
documentationLinkLabel?: string;
|
2022-03-14 13:14:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const MobileGuidance = ({
|
|
|
|
description,
|
|
|
|
documentationLink,
|
2022-03-29 09:30:57 +02:00
|
|
|
documentationLinkLabel,
|
2022-03-14 13:14:26 +01:00
|
|
|
}: IMobileGuidance) => {
|
|
|
|
const [open, setOpen] = useState(false);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2023-01-05 15:23:40 +01:00
|
|
|
<StyledMobileGuidanceContainer>
|
|
|
|
<StyledMobileGuidanceBackground />
|
|
|
|
</StyledMobileGuidanceContainer>
|
2023-10-04 09:42:02 +02:00
|
|
|
<Tooltip title="Toggle help" arrow>
|
2023-01-05 15:23:40 +01:00
|
|
|
<StyledMobileGuidanceButton
|
2023-10-02 14:25:46 +02:00
|
|
|
onClick={() => setOpen((prev) => !prev)}
|
2023-10-04 09:42:02 +02:00
|
|
|
size="large"
|
2022-04-21 08:26:49 +02:00
|
|
|
>
|
2023-01-05 15:23:40 +01:00
|
|
|
<StyledInfoIcon />
|
|
|
|
</StyledMobileGuidanceButton>
|
2022-04-21 08:26:49 +02:00
|
|
|
</Tooltip>
|
2022-03-14 13:14:26 +01:00
|
|
|
<Collapse in={open} timeout={500}>
|
|
|
|
<Guidance
|
|
|
|
description={description}
|
|
|
|
documentationLink={documentationLink}
|
2022-03-29 09:30:57 +02:00
|
|
|
documentationLinkLabel={documentationLinkLabel}
|
2022-03-14 13:14:26 +01:00
|
|
|
/>
|
|
|
|
</Collapse>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
interface IGuidanceProps {
|
|
|
|
description: string;
|
|
|
|
documentationLink: string;
|
2022-03-29 09:30:57 +02:00
|
|
|
documentationLinkLabel?: string;
|
2023-10-04 09:42:02 +02:00
|
|
|
showDescription?: boolean;
|
|
|
|
showLink?: boolean;
|
2022-03-14 13:14:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const Guidance: React.FC<IGuidanceProps> = ({
|
|
|
|
description,
|
|
|
|
children,
|
|
|
|
documentationLink,
|
2023-10-04 09:42:02 +02:00
|
|
|
documentationLinkLabel = "Learn more",
|
|
|
|
showDescription = true,
|
|
|
|
showLink = true,
|
2022-03-14 13:14:26 +01:00
|
|
|
}) => {
|
|
|
|
return (
|
2023-01-05 15:23:40 +01:00
|
|
|
<StyledSidebar>
|
2023-10-04 09:42:02 +02:00
|
|
|
<ConditionallyRender
|
|
|
|
condition={showDescription}
|
|
|
|
show={<StyledDescription>{description}</StyledDescription>}
|
|
|
|
/>
|
2022-03-14 13:14:26 +01:00
|
|
|
|
2023-10-04 09:42:02 +02:00
|
|
|
<ConditionallyRender
|
|
|
|
condition={showLink}
|
|
|
|
show={
|
|
|
|
<StyledLinkContainer>
|
|
|
|
<StyledLinkIcon />
|
|
|
|
<StyledDocumentationLink
|
|
|
|
href={documentationLink}
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
target="_blank"
|
|
|
|
>
|
|
|
|
{documentationLinkLabel}
|
|
|
|
</StyledDocumentationLink>
|
|
|
|
</StyledLinkContainer>
|
|
|
|
}
|
|
|
|
/>
|
2022-03-14 13:14:26 +01:00
|
|
|
|
|
|
|
{children}
|
2023-01-05 15:23:40 +01:00
|
|
|
</StyledSidebar>
|
2022-03-14 13:14:26 +01:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-01-14 15:50:02 +01:00
|
|
|
export default FormTemplate;
|