Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

frontend: Refactor workflows to use new layout #3159

Merged
merged 17 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions frontend/packages/core/src/AppProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,12 @@ const ClutchApp = ({
: workflow.displayName;

const workflowLayoutProps: LayoutProps = {
...route.layoutProps,
heading: route.layoutProps?.heading || heading,
workflow,
title: heading,
subtitle: route.description,
variant: route.layoutProps ? route.layoutProps.variant : null,
breadcrumbsOnly: route.layoutProps?.breadcrumbsOnly || false,
hideHeader: route.layoutProps?.hideHeader || false,
jecr marked this conversation as resolved.
Show resolved Hide resolved
};

const workflowRouteComponent = (
Expand Down
2 changes: 1 addition & 1 deletion frontend/packages/core/src/AppProvider/workflow.tsx
jecr marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export interface Route {
*/
featureFlag?: string;

layoutProps?: Omit<LayoutProps, "workflow">;
layoutProps?: Omit<LayoutProps, "workflow" | "title" | "subtitle">;
}

export interface ConfiguredRoute extends Route {
Expand Down
75 changes: 50 additions & 25 deletions frontend/packages/core/src/WorkflowLayout/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from "react";
import { matchPath } from "react-router";
import { matchPath, Params, useParams } from "react-router";
septum marked this conversation as resolved.
Show resolved Hide resolved
import type { Interpolation } from "@emotion/styled";
import type { CSSObject, Theme } from "@mui/material";
import { alpha } from "@mui/system";
septum marked this conversation as resolved.
Show resolved Hide resolved

import type { Workflow } from "../AppProvider/workflow";
import Breadcrumbs from "../Breadcrumbs";
Expand All @@ -10,12 +11,14 @@ import styled from "../styled";
import { Typography } from "../typography";
import { generateBreadcrumbsEntries } from "../utils";

export type LayoutVariant = "standard" | "wizard" | "custom";
export type LayoutVariant = "standard" | "wizard";

export type LayoutProps = {
workflow: Workflow;
variant?: LayoutVariant;
heading?: string | React.ReactElement;
variant: LayoutVariant | null;
septum marked this conversation as resolved.
Show resolved Hide resolved
title?: string | ((params: Params) => string);
subtitle?: string;
breadcrumbsOnly?: boolean;
hideHeader?: boolean;
};

Expand Down Expand Up @@ -43,8 +46,6 @@ const getContainerVariantStyles = (variant: LayoutVariant, theme: Theme) => {
padding: theme.spacing(theme.clutch.spacing.lg, theme.clutch.spacing.none),
margin: theme.spacing(theme.clutch.spacing.none, "auto"),
},
// No styles
custom: {},
};
return layoutVariantStylesMap[variant];
};
Expand All @@ -63,49 +64,73 @@ const PageHeader = styled("div")(({ $variant, theme }: StyledVariantComponentPro
width: "100%",
}));

const PageHeaderMainContainer = styled("div")({
const PageHeaderBreadcrumbsWrapper = styled("div")(({ theme }: { theme: Theme }) => ({
marginBottom: theme.spacing(theme.clutch.spacing.xs),
}));

const PageHeaderMainContainer = styled("div")(({ theme }: { theme: Theme }) => ({
display: "flex",
alignItems: "center",
height: "70px",
marginBottom: theme.spacing(theme.clutch.spacing.sm),
}));

const PageHeaderInformation = styled("div")({
display: "flex",
flexDirection: "column",
justifyContent: "space-evenly",
height: "100%",
});

const Heading = styled(Typography)({
const Title = styled(Typography)({
lineHeight: 1,
});

const Subtitle = styled(Typography)(({ theme }: { theme: Theme }) => ({
color: alpha(theme.colors.neutral[900], 0.45),
}));

const WorkflowLayout = ({
workflow,
variant = "standard",
heading = null,
variant,
title = null,
subtitle = null,
breadcrumbsOnly = false,
hideHeader = false,
children,
}: React.PropsWithChildren<LayoutProps>) => {
const params = useParams();
const location = useLocation();

if (variant === null) {
return <>{children}</>;
}

const workflowPaths = workflow.routes.map(({ path }) => `/${workflow.path}/${path}`);
const breadcrumbsEntries = generateBreadcrumbsEntries(
location,
(url: string) =>
`/${workflow.path}` !== url &&
!workflowPaths.includes(url) &&
!workflowPaths.find(path => !!matchPath({ path }, url))
url => !!workflowPaths.find(path => !!matchPath({ path }, url))
septum marked this conversation as resolved.
Show resolved Hide resolved
);

return (
<LayoutContainer $variant={variant}>
{!hideHeader && (
<PageHeader $variant={variant}>
<Breadcrumbs entries={breadcrumbsEntries} />
<PageHeaderMainContainer>
{heading && (
<>
{React.isValidElement(heading) ? (
heading
) : (
<Heading variant="h2">{heading}</Heading>
<PageHeaderBreadcrumbsWrapper>
<Breadcrumbs entries={breadcrumbsEntries} />
</PageHeaderBreadcrumbsWrapper>
{!breadcrumbsOnly && (title || subtitle) && (
<PageHeaderMainContainer>
<PageHeaderInformation>
{title && (
<Title variant="h2" textTransform="capitalize">
{typeof title === "function" ? title(params) : title}
</Title>
)}
</>
)}
</PageHeaderMainContainer>
{subtitle && <Subtitle variant="subtitle2">{subtitle}</Subtitle>}
</PageHeaderInformation>
</PageHeaderMainContainer>
)}
</PageHeader>
)}
{children}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import type { Location } from "history";
import type { BreadcrumbEntry } from "../Breadcrumbs";

const generateBreadcrumbsEntries = (location: Location, validateUrl: (url: string) => boolean) => {
const labels = location.pathname
const labels = decodeURIComponent(location.pathname)
.split("/")
.slice(1, location.pathname.endsWith("/") ? -1 : undefined);

const entries: Array<BreadcrumbEntry> = [{ label: "Home", url: "/" }].concat(
labels.map((label, index) => {
let url = `/${labels.slice(0, index + 1).join("/")}`;

if (validateUrl(url)) {
if (!validateUrl(url)) {
url = undefined;
}

Expand Down
1 change: 0 additions & 1 deletion frontend/packages/wizard/src/wizard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ const Header = styled(Grid)<{ $orientation: MuiStepperProps["orientation"] }>(

const Container = styled(MuiContainer)<{ $width: ContainerProps["width"] }>(
{
paddingBlock: "24px 32px",
height: "calc(100% - 56px)",
septum marked this conversation as resolved.
Show resolved Hide resolved
},
props => ({
Expand Down
5 changes: 2 additions & 3 deletions frontend/workflows/audit/src/audit-event.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";
import ReactJson from "react-json-view";
import type { clutch as IClutch } from "@clutch-sh/api";
import { client, ClutchError, Error, Loadable, Typography, useParams } from "@clutch-sh/core";
import { client, ClutchError, Error, Loadable, useParams } from "@clutch-sh/core";
import { Stack } from "@mui/material";

import type { WorkflowProps } from ".";
Expand Down Expand Up @@ -37,8 +37,7 @@ const AuditEvent: React.FC<WorkflowProps> = ({ heading }) => {
React.useEffect(() => fetch(), []);

return (
<Stack spacing={2} direction="column" style={{ padding: "32px" }}>
<Typography variant="h2">{heading}</Typography>
<Stack spacing={2} direction="column">
<Loadable isLoading={isLoading}>
{error && <Error subject={error} />}
<ReactJson
Expand Down
6 changes: 6 additions & 0 deletions frontend/workflows/audit/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@ const register = (): WorkflowConfiguration => {
displayName: "Logs",
description: "View audit log",
component: AuditLog,
layoutProps: {
variant: "standard",
},
septum marked this conversation as resolved.
Show resolved Hide resolved
},
event: {
path: "/event/:id",
displayName: "Event Details",
description: "View audit event",
component: AuditEvent,
hideNav: true,
layoutProps: {
variant: "standard",
},
},
},
};
Expand Down
5 changes: 1 addition & 4 deletions frontend/workflows/audit/src/logs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
IconButton,
styled,
Table,
Typography,
useSearchParams,
useTheme,
} from "@clutch-sh/core";
Expand All @@ -19,7 +18,6 @@ import { DateTimeRangeSelector, QUICK_TIME_OPTIONS } from "./date-selector";
import EventRows from "./event-row";

const RootContainer = styled(Stack)({
padding: "32px",
height: "100%",
});

Expand All @@ -39,7 +37,7 @@ const LoadingSpinner = styled(CircularProgress)(({ theme }: { theme: Theme }) =>
position: "absolute",
}));

const AuditLog: React.FC<AuditLogProps> = ({ heading, detailsPathPrefix, downloadPrefix }) => {
const AuditLog: React.FC<AuditLogProps> = ({ detailsPathPrefix, downloadPrefix }) => {
const [searchParams, setSearchParams] = useSearchParams();
const [now] = React.useState<Date>(new Date());
const [timeRangeKey, setTimeRangeKey] = React.useState<string>("");
Expand Down Expand Up @@ -74,7 +72,6 @@ const AuditLog: React.FC<AuditLogProps> = ({ heading, detailsPathPrefix, downloa
const genTimeRangeKey = () => `${startTime}-${endTime}-${new Date().toString()}`;
return (
<RootContainer spacing={2} direction="column">
<Typography variant="h2">{heading}</Typography>
<Stack direction="column" spacing={2}>
<Stack
direction={shrink ? "column" : "row"}
Expand Down
3 changes: 3 additions & 0 deletions frontend/workflows/dynamodb/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ const register = (): WorkflowConfiguration => {
displayName: "Update Capacity",
description: "Update the table or GSI provisioned capacity.",
requiredConfigProps: ["resolverType"],
layoutProps: {
variant: "wizard",
},
},
},
};
Expand Down
2 changes: 1 addition & 1 deletion frontend/workflows/dynamodb/src/update-capacity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ const UpdateCapacity: React.FC<WorkflowProps> = ({
};

return (
<Wizard dataLayout={dataLayout} heading={heading}>
<Wizard dataLayout={dataLayout}>
<TableIdentifier name="Lookup" resolverType={resolverType} notes={notes} />
<TableDetails name="Modify" enableOverride={enableOverride} notes={notes} />
<Confirm name="Results" />
Expand Down
9 changes: 9 additions & 0 deletions frontend/workflows/ec2/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,29 @@ const register = (): WorkflowConfiguration => {
description: "Terminate an EC2 instance.",
component: TerminateInstance,
requiredConfigProps: ["resolverType"],
layoutProps: {
variant: "wizard",
},
},
rebootInstance: {
path: "instance/reboot",
displayName: "Reboot Instance",
description: "Reboot an EC2 Instance",
component: RebootInstance,
requiredConfigProps: ["resolverType"],
layoutProps: {
variant: "wizard",
},
},
resizeAutoscalingGroup: {
path: "asg/resize",
displayName: "Resize Autoscaling Group",
description: "Resize an autoscaling group.",
component: ResizeAutoscalingGroup,
requiredConfigProps: ["resolverType"],
layoutProps: {
variant: "wizard",
},
},
},
};
Expand Down
2 changes: 1 addition & 1 deletion frontend/workflows/ec2/src/reboot-instance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ const RebootInstance: React.FC<WorkflowProps> = ({ heading, resolverType, notes
};

return (
<Wizard dataLayout={dataLayout} heading={heading}>
jecr marked this conversation as resolved.
Show resolved Hide resolved
<Wizard dataLayout={dataLayout}>
<InstanceIdentifier name="Lookup" resolverType={resolverType} />
<InstanceDetails name="Verify" />
<Confirm name="Result" notes={notes} />
Expand Down
2 changes: 1 addition & 1 deletion frontend/workflows/ec2/src/resize-asg.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ const ResizeAutoscalingGroup: React.FC<WorkflowProps> = ({ heading, resolverType
};

return (
<Wizard dataLayout={dataLayout} heading={heading}>
<Wizard dataLayout={dataLayout}>
<GroupIdentifier name="Lookup" resolverType={resolverType} />
<GroupDetails name="Modify" />
<Confirm name="Result" notes={notes} />
Expand Down
2 changes: 1 addition & 1 deletion frontend/workflows/ec2/src/terminate-instance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ const TerminateInstance: React.FC<WorkflowProps> = ({ heading, resolverType, not
};

return (
<Wizard dataLayout={dataLayout} heading={heading}>
<Wizard dataLayout={dataLayout}>
<InstanceIdentifier name="Lookup" resolverType={resolverType} />
<InstanceDetails name="Verify" />
<Confirm name="Result" notes={notes} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`renders correctly 1`] = `
<DocumentFragment>
<div
class="MuiContainer-root css-1s2y8jy-MuiContainer-root"
class="MuiContainer-root css-1so714y-MuiContainer-root"
>
<div
class="MuiGrid-root MuiGrid-container css-1yw6yql-MuiGrid-root"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`renders correctly 1`] = `
<DocumentFragment>
<div
class="MuiContainer-root css-1s2y8jy-MuiContainer-root"
class="MuiContainer-root css-1so714y-MuiContainer-root"
>
<div
class="MuiGrid-root MuiGrid-container css-1yw6yql-MuiGrid-root"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`renders correctly 1`] = `
<DocumentFragment>
<div
class="MuiContainer-root css-1s2y8jy-MuiContainer-root"
class="MuiContainer-root css-1so714y-MuiContainer-root"
>
<div
class="MuiGrid-root MuiGrid-container css-1yw6yql-MuiGrid-root"
Expand Down
3 changes: 3 additions & 0 deletions frontend/workflows/envoy/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ const register = (): WorkflowConfiguration => {
component: RemoteTriage,
displayName: "Remote Triage",
description: "Triage Envoy configurations.",
layoutProps: {
variant: "wizard",
},
},
},
};
Expand Down
2 changes: 1 addition & 1 deletion frontend/workflows/envoy/src/remote-triage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ const RemoteTriage: React.FC<WorkflowProps> = ({ heading }) => {
};

return (
<Wizard dataLayout={dataLayout} heading={heading}>
<Wizard dataLayout={dataLayout}>
<TriageIdentifier name="Lookup" host={hostParam} />
<TriageDetails name="Details" />
</Wizard>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`renders correctly 1`] = `
<DocumentFragment>
<div
class="MuiContainer-root css-1s2y8jy-MuiContainer-root"
class="MuiContainer-root css-1so714y-MuiContainer-root"
>
<div
class="MuiGrid-root MuiGrid-container css-1yw6yql-MuiGrid-root"
Expand Down
Loading
Loading