The frontend code in Clutch is separated into two distinct pieces; the packages that power the platform and the out of the box workflows that utilize these packages.
The goal for developing the core Clutch code was to make it as easy as possible for those with little to no domain knowledge or experience. To do this we leverage Lerna and Yarn workspaces to run commands against all core packages and installed workflows.
The packages within Clutch are separated by functionality, with the intent being that each package provides value to workflows while also not being required.
An interactive playground of our component library is available at storybook.clutch.sh.
The Core package consists of various reusable components that are shared between workflows and/or other Clutch packages. This can span from things like the application provider, which renders the Clutch app, and resolver component (see below) to something as simple as a centralized button component.
It’s important to note that all components which reside in this package should be developed in a reusable manner and should have a wide application. Workflow-specific code should not be contributed to the core package.
The data-layout package provides both layouts for workflows to store data within and a manager for those layouts.
Data layouts follow a pattern outlined below.
- Define a layout object, assiging each layout to a key. These keys will be how you reference the layouts throughout the workflow.
Layouts can be completely empty as showing with
resourceData above but they can also override the default values for a layout to implement custom behavior.
In the example above
terminationData defines values for both
hydrator. Let's dig into these keys more.
depsis powerful in that it allows layouts to specify their dependency on other layouts. If a layout is accessed before its dependencies have data, an error will be displayed to the user. This is a failsafe way to ensure that users provide required data. In the example above,
terminationDatahas a dependency on
resourceDatacontaining some data.
hydratoris a callback fired when a layout is first used by a workflow with all of the defined
depsdata as arguments. This is particularly useful when data is needed from the user before dispatching a request. In the example above the
terminationDatalayout posts to
/v1/aws/ec2/terminateInstancewith data from it's dependency
resourceDataand analyzes the response for possible errors before returning the result.
- Use data layout(s) in a workflow.
This allows the workflow to access any data the layout contains from either a prior step or as a result of the hydrator. As mentioned in the prior step, if this is the first time the
terminationData layout has been used, this will invoke the hydrator callback.
The Tools package is meant to be used exclusively in development and contains dependencies and configuration files for tasks such as linting, testing, etc.
The Wizard package works hand-in-hand with the data-layout package, leveraging it for workflow progression and state while providing its steps with the ability to easily access data layouts.
There are two pieces to effectively utilizing the wizard component, a
Wizard and a
WizardStep components where the Wizard has one or more WizardStep children.
In the above example you can see that the
WizardStep utilizes a data layout's
isLoading props to display helpful feedback to the user.
To move between wizard steps the
@clutch-sh/core package exposes hooks for moving forward and backward. This allows workflows to take additional action before submitting a step or disable backward movement altogether.
Workflows are essentially a combination of the packages listed above with configration values for routing and display purposes. Bear in mind that while, at the time of writing this, the out of the box workflows are all linear and serve a single purpose not all workflows need to follow that design. A workflow could be anything from a dashboard of information to a single form for submission.
The most important piece of a workflow, even before the implementation is the configuration. A workflows configuration allows it to be registered on the Clutch app, allows for users to override default values to suit their specific needs, and provides ownership details for others.
Let's break down the example above.
Every workflow package has a single exported callback function that is used by Clutch to register it. This callback function must return an object with the following:
developer: the name and contact url for the party developing this workflow package. This is used if a workflow fails to load with an error message giving users the proper channel to report failures to.
path: the top level routing path for this collection of workflows.
group: the group to nest these workflows under within the navigation drawer.
displayName: The display name for these workflows. This is used throughout the Clutch application in various spots.
routes: A collection of the workflows and their metadata contained within this package.
Each route is a key/value pair where the key is used by others to register this workflow on their Clutch application. The keys of a route are similar to a package with a few new ones:
path: the path of the component relative to the top level routing path.
component: the component to render for this route.
description: a short description of what this component does.
trending: this controls if a workflow is shown on the landing page of Clutch. It is generally advised to omit this and override it's value in the application config.
props: this is a list of keys that users must specify in
clutch.config.jswhen adding this workflow. If these are not present in the config the workflow will not be loaded.
The purpose and structure of the Resolver component is explained in the Architecture & Components section, if you haven’t had a chance to read that yet this would be a good chance to as its content will be helpful in understanding the usage.
Using the Resolver component in your workflow should be mostly seamless requiring only a few properties that are specific to your workflow. To help with our explanation let’s look at the following example:
Notice that the resolver specified only three properties: a type, search limit, and a callback on resolution.
type: this property denotes the type of resource the resolver will prompt the user for and pass back to the onResolve callback. For example,
clutch.aws.ec2.v1.Instance. In the above example this value is being passed in as a prop from its parent which has it injected from the application configuration.
searchLimit: allows developers to specify a threshold for the number of resources to search for. This is particularly useful when searching across multiple environments for a small number of resources as the server will respond as soon as this threshold is satisfied. This also denotes the expected number of environments to search, allowing the resolver component to warn when searches fail in specific environments.
onResolve: when the resolver completes a lookup and casts the response to a specified protobuf object it will need a way to pass the results back to a workflow. This property allows users to specify a callback that takes the results and use them in the appropriate way for that workflow.