Skip to content

refactor(site): improve parameters field #11802

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

Merged
merged 5 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const createTemplateVersionParameter = (
name: "first_parameter",
description: "This is first parameter.",
type: "string",
mutable: false,
mutable: true,
default_value: "default string",
icon: "/icon/folder.svg",
options: [],
Expand Down Expand Up @@ -47,6 +47,46 @@ export const Basic: Story = {
},
};

export const Optional: Story = {
args: {
value: "initial-value",
id: "project_name",
parameter: createTemplateVersionParameter({
required: false,
name: "project_name",
description:
"Customize the name of a Google Cloud project that will be created!",
}),
},
};

export const Immutable: Story = {
args: {
value: "initial-value",
id: "project_name",
parameter: createTemplateVersionParameter({
mutable: false,
name: "project_name",
description:
"Customize the name of a Google Cloud project that will be created!",
}),
},
};

export const WithError: Story = {
args: {
id: "number_parameter",
parameter: createTemplateVersionParameter({
name: "number_parameter",
type: "number",
description: "Numeric parameter",
default_value: "",
}),
error: true,
helperText: "Number must be greater than 5",
},
};

export const NumberType: Story = {
args: {
value: "4",
Expand Down
36 changes: 33 additions & 3 deletions site/src/components/RichParameterInput/RichParameterInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { MemoizedMarkdown } from "components/Markdown/Markdown";
import { Stack } from "components/Stack/Stack";
import { MultiTextField } from "./MultiTextField";
import { ExternalImage } from "components/ExternalImage/ExternalImage";
import { Pill } from "components/Pill/Pill";
import ErrorOutline from "@mui/icons-material/ErrorOutline";

const isBoolean = (parameter: TemplateVersionParameter) => {
return parameter.type === "bool";
Expand All @@ -31,7 +33,11 @@ const styles = {
labelPrimary: (theme) => ({
fontSize: 16,
color: theme.palette.text.primary,
fontWeight: 600,
fontWeight: 500,
display: "flex",
alignItems: "center",
flexWrap: "wrap",
gap: 8,

"& p": {
margin: 0,
Expand All @@ -42,6 +48,11 @@ const styles = {
fontSize: 14,
},
}),
optionalLabel: (theme) => ({
fontSize: 14,
color: theme.palette.text.disabled,
fontWeight: 500,
}),
textField: {
".small & .MuiInputBase-root": {
height: 36,
Expand Down Expand Up @@ -102,6 +113,25 @@ const ParameterLabel: FC<ParameterLabelProps> = ({ parameter }) => {
? parameter.display_name
: parameter.name;

const labelPrimary = (
<span css={styles.labelPrimary}>
{displayName}

{!parameter.required && (
<Tooltip title="If no value is specified, the system will default to the value set by the administrator.">
<span css={styles.optionalLabel}>(optional)</span>
</Tooltip>
)}
{!parameter.mutable && (
<Tooltip title="This value cannot be modified after the workspace has been created.">
<Pill type="warning" icon={<ErrorOutline />}>
Immutable
</Pill>
</Tooltip>
)}
</span>
);

return (
<label htmlFor={parameter.name}>
<Stack direction="row" alignItems="center">
Expand All @@ -117,13 +147,13 @@ const ParameterLabel: FC<ParameterLabelProps> = ({ parameter }) => {

{hasDescription ? (
<Stack spacing={0}>
<span css={styles.labelPrimary}>{displayName}</span>
{labelPrimary}
<MemoizedMarkdown css={styles.labelCaption}>
{parameter.description}
</MemoizedMarkdown>
</Stack>
) : (
<span css={styles.labelPrimary}>{displayName}</span>
labelPrimary
)}
</Stack>
</label>
Expand Down
45 changes: 5 additions & 40 deletions site/src/components/TemplateParameters/TemplateParameters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ export type TemplateParametersSectionProps = {
) => Omit<RichParameterInputProps, "parameter" | "index">;
} & Pick<ComponentProps<typeof FormSection>, "classes">;

export const MutableTemplateParametersSection: FC<
TemplateParametersSectionProps
> = ({ templateParameters, getInputProps, ...formSectionProps }) => {
export const TemplateParametersSection: FC<TemplateParametersSectionProps> = ({
templateParameters,
getInputProps,
...formSectionProps
}) => {
const hasMutableParameters =
templateParameters.filter((p) => p.mutable).length > 0;

Expand Down Expand Up @@ -45,40 +47,3 @@ export const MutableTemplateParametersSection: FC<
</>
);
};

export const ImmutableTemplateParametersSection: FC<
TemplateParametersSectionProps
> = ({ templateParameters, getInputProps, ...formSectionProps }) => {
const hasImmutableParameters =
templateParameters.filter((p) => !p.mutable).length > 0;

return (
<>
{hasImmutableParameters && (
<FormSection
{...formSectionProps}
title="Immutable parameters"
description={
<>
These settings <strong>cannot be changed</strong> after creating
the workspace.
</>
}
>
<FormFields>
{templateParameters.map(
(parameter, index) =>
!parameter.mutable && (
<RichParameterInput
{...getInputProps(parameter, index)}
key={parameter.name}
parameter={parameter}
/>
),
)}
</FormFields>
</FormSection>
)}
</>
);
};
101 changes: 36 additions & 65 deletions site/src/pages/CreateWorkspacePage/CreateWorkspacePageView.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { css } from "@emotion/css";
import { useTheme, type Interpolation, type Theme } from "@emotion/react";
import { type Interpolation, type Theme } from "@emotion/react";
import TextField from "@mui/material/TextField";
import type * as TypesGen from "api/typesGenerated";
import { UserAutocomplete } from "components/UserAutocomplete/UserAutocomplete";
Expand All @@ -21,10 +20,6 @@ import {
getInitialRichParameterValues,
useValidationSchemaForRichParameters,
} from "utils/richParameters";
import {
ImmutableTemplateParametersSection,
MutableTemplateParametersSection,
} from "components/TemplateParameters/TemplateParameters";
import { ErrorAlert } from "components/Alert/ErrorAlert";
import { Stack } from "components/Stack/Stack";
import {
Expand All @@ -44,6 +39,7 @@ import {
PageHeaderSubtitle,
} from "components/PageHeader/PageHeader";
import { Pill } from "components/Pill/Pill";
import { RichParameterInput } from "components/RichParameterInput/RichParameterInput";

export const Language = {
duplicationWarning:
Expand Down Expand Up @@ -90,7 +86,6 @@ export const CreateWorkspacePageView: FC<CreateWorkspacePageViewProps> = ({
onSubmit,
onCancel,
}) => {
const theme = useTheme();
const [owner, setOwner] = useState(defaultOwner);
const [searchParams] = useSearchParams();
const disabledParamsList = searchParams?.get("disable_params")?.split(",");
Expand Down Expand Up @@ -222,65 +217,41 @@ export const CreateWorkspacePageView: FC<CreateWorkspacePageViewProps> = ({
</FormFields>
</FormSection>

{parameters && (
<>
<MutableTemplateParametersSection
templateParameters={parameters}
getInputProps={(parameter, index) => {
return {
...getFieldHelpers(
"rich_parameter_values[" + index + "].value",
),
onChange: async (value) => {
await form.setFieldValue(
"rich_parameter_values." + index,
{
name: parameter.name,
value: value,
},
);
},
disabled:
disabledParamsList?.includes(
parameter.name.toLowerCase().replace(/ /g, "_"),
) || creatingWorkspace,
};
}}
/>
<ImmutableTemplateParametersSection
templateParameters={parameters}
classes={{
root: css`
border: 1px solid ${theme.palette.warning.light};
border-radius: 8px;
background-color: ${theme.palette.background.paper};
padding: 80px;
margin-left: -80px;
margin-right: -80px;
`,
}}
getInputProps={(parameter, index) => {
return {
...getFieldHelpers(
"rich_parameter_values[" + index + "].value",
),
onChange: async (value) => {
await form.setFieldValue(
"rich_parameter_values." + index,
{
{parameters.length > 0 && (
<FormSection
title="Parameters"
description="These are the settings used by your template. Please note that immutable parameters cannot be modified once the workspace is created."
>
{/*
Opted not to use FormFields in order to increase spacing.
This decision was made because rich parameter inputs are more visually dense than standard text fields.
*/}
<div css={{ display: "flex", flexDirection: "column", gap: 36 }}>
{parameters.map((parameter, index) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: I might lift this function outside of the JSX.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which function? The map?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the callback that the map takes

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is quite normal in react codebases but any specific concern?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like it lowers general readability because its a longer callback that defines multiple variables, but no strong concerns!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see! But in this case, we would have to pass 3+ arguments to the function so I'm not sure if extracting it to a new function or component would improve that much but I can understand the reasoning.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, good point

const parameterField = `rich_parameter_values.${index}`;
const parameterInputName = `${parameterField}.value`;
const isDisabled =
disabledParamsList?.includes(
parameter.name.toLowerCase().replace(/ /g, "_"),
) || creatingWorkspace;

return (
<RichParameterInput
{...getFieldHelpers(parameterInputName)}
onChange={async (value) => {
await form.setFieldValue(parameterField, {
name: parameter.name,
value: value,
},
);
},
disabled:
disabledParamsList?.includes(
parameter.name.toLowerCase().replace(/ /g, "_"),
) || creatingWorkspace,
};
}}
/>
</>
value,
});
}}
key={parameter.name}
parameter={parameter}
disabled={isDisabled}
/>
);
})}
</div>
</FormSection>
)}

<FormFooter
Expand Down
Loading