Skip to content

fix(site): disable auto fields when they are disabled in the template settings #10022

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 4 commits into from
Oct 4, 2023
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
120 changes: 65 additions & 55 deletions site/src/components/Pill/Pill.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type FC, type ReactNode, useMemo } from "react";
import { type FC, type ReactNode, useMemo, forwardRef } from "react";
import { css, type Interpolation, type Theme, useTheme } from "@emotion/react";
import { colors } from "theme/colors";

Expand Down Expand Up @@ -44,61 +44,71 @@ const themeStyles =
};
};

export const Pill: FC<PillProps> = (props) => {
const { lightBorder, icon, text = null, type = "neutral", ...attrs } = props;
const theme = useTheme();
export const Pill: FC<PillProps> = forwardRef<HTMLDivElement, PillProps>(
(props, ref) => {
const {
lightBorder,
icon,
text = null,
type = "neutral",
...attrs
} = props;
const theme = useTheme();

const typeStyles = useMemo(() => {
if (type in themeOverrides) {
return themeOverrides[type as keyof typeof themeOverrides](lightBorder);
}
return themeStyles(type, lightBorder);
}, [type, lightBorder]);
const typeStyles = useMemo(() => {
Copy link
Member

Choose a reason for hiding this comment

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

Does this need to be memoized? I guess it just feels like the logic wouldn't have much overhead, and it's only being used within the same component

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 don't think so, I guess it is only using memo as a way to wrap/encapsulate it into a function but I'm just guessing.

if (type in themeOverrides) {
return themeOverrides[type as keyof typeof themeOverrides](lightBorder);
}
return themeStyles(type, lightBorder);
}, [type, lightBorder]);

return (
<div
css={[
{
display: "inline-flex",
alignItems: "center",
borderWidth: 1,
borderStyle: "solid",
borderRadius: 99999,
fontSize: 12,
color: "#FFF",
height: theme.spacing(3),
paddingLeft: icon ? theme.spacing(0.75) : theme.spacing(1.5),
paddingRight: theme.spacing(1.5),
whiteSpace: "nowrap",
fontWeight: 400,
},
typeStyles,
]}
role="status"
{...attrs}
>
{icon && (
<div
css={css`
margin-right: ${theme.spacing(0.5)};
width: ${theme.spacing(1.75)};
height: ${theme.spacing(1.75)};
line-height: 0;
display: flex;
align-items: center;
justify-content: center;

& > img,
& > svg {
return (
<div
ref={ref}
Copy link
Member

Choose a reason for hiding this comment

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

I am probably missing something obvious, but what does passing in ref here do?

Copy link
Member

@Parkreiner Parkreiner Oct 4, 2023

Choose a reason for hiding this comment

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

The ref gives any parent component the ability to manipulate the the div element created by Pill.

So for example:

function Parent () {
  const divRef = useRef<HTMLDivElement>(null);
  return <Pill ref={divRef} {...otherProps} />
}

With this setup, the parent would be able to manipulate the real HTML div element that Pill creates from inside useEffect and event handlers, even though the div exists in a separate component

Copy link
Member

Choose a reason for hiding this comment

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

But judging from this PR, I think it's a preemptive change to make the component more reusable. I don't see any of the <Pill /> instances in the rest of the code using it

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, it is used by the Tooltip component that wraps the Pill

Copy link
Member

Choose a reason for hiding this comment

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

Ohhhhh I see. I see it in the docs. Unfortunate that it is not reflected in the code! I guess you just have to know how MUI tooltip works. Thanks for the explanations!

css={[
{
cursor: "default",
display: "inline-flex",
alignItems: "center",
borderWidth: 1,
borderStyle: "solid",
borderRadius: 99999,
fontSize: 12,
color: "#FFF",
height: theme.spacing(3),
paddingLeft: icon ? theme.spacing(0.75) : theme.spacing(1.5),
paddingRight: theme.spacing(1.5),
whiteSpace: "nowrap",
fontWeight: 400,
},
typeStyles,
]}
role="status"
{...attrs}
>
{icon && (
<div
css={css`
margin-right: ${theme.spacing(0.5)};
width: ${theme.spacing(1.75)};
height: ${theme.spacing(1.75)};
}
`}
>
{icon}
</div>
)}
{text}
</div>
);
};
line-height: 0;
display: flex;
align-items: center;
justify-content: center;

& > img,
& > svg {
width: ${theme.spacing(1.75)};
height: ${theme.spacing(1.75)};
}
`}
>
{icon}
</div>
)}
{text}
</div>
);
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ dayjs.extend(timezone);
const meta: Meta<typeof WorkspaceScheduleForm> = {
title: "components/WorkspaceScheduleForm",
component: WorkspaceScheduleForm,
args: {
enableAutoStart: true,
enableAutoStop: true,
},
};

export default meta;
Expand All @@ -38,6 +42,8 @@ export const AllDisabled: Story = {
autostopEnabled: false,
ttl: emptyTTL,
},
enableAutoStart: false,
enableAutoStop: false,
},
};

Expand All @@ -49,6 +55,7 @@ export const Autostart: Story = {
autostopEnabled: false,
ttl: emptyTTL,
},
enableAutoStop: false,
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import { ChangeEvent, FC } from "react";
import * as Yup from "yup";
import { getFormHelpers } from "utils/formUtils";
import { timeZones } from "utils/timeZones";
import { Pill } from "components/Pill/Pill";
import Tooltip from "@mui/material/Tooltip";

// REMARK: some plugins depend on utc, so it's listed first. Otherwise they're
// sorted alphabetically.
Expand Down Expand Up @@ -76,6 +78,8 @@ export interface WorkspaceScheduleFormProps {
submitScheduleError?: unknown;
initialValues: WorkspaceScheduleFormValues;
isLoading: boolean;
enableAutoStop: boolean;
enableAutoStart: boolean;
onCancel: () => void;
onSubmit: (values: WorkspaceScheduleFormValues) => void;
// for storybook
Expand Down Expand Up @@ -193,6 +197,8 @@ export const WorkspaceScheduleForm: FC<
onSubmit,
initialTouched,
defaultTTL,
enableAutoStop,
enableAutoStart,
}) => {
const styles = useStyles();

Expand Down Expand Up @@ -284,12 +290,25 @@ export const WorkspaceScheduleForm: FC<
<HorizontalForm onSubmit={form.handleSubmit}>
<FormSection
title="Autostart"
description="Select the time and days of week on which you want the workspace starting automatically."
description={
<>
<div css={{ marginBottom: 16 }}>
Select the time and days of week on which you want the workspace
starting automatically.
</div>
{!enableAutoStart && (
<Tooltip title="This option can be enabled in the template settings">
<Pill text="Disabled" />
</Tooltip>
)}
</>
}
>
<FormFields>
<FormControlLabel
control={
<Switch
disabled={!enableAutoStart}
name="autostartEnabled"
checked={form.values.autostartEnabled}
onChange={handleToggleAutostart}
Expand Down Expand Up @@ -352,7 +371,21 @@ export const WorkspaceScheduleForm: FC<

<FormSection
title="Autostop"
description="Set how many hours should elapse after a workspace is started before it automatically shuts down. If workspace connection activity is detected, the autostop timer will be bumped by this value."
description={
<>
<div css={{ marginBottom: 16 }}>
Set how many hours should elapse after a workspace is started
before it automatically shuts down. If workspace connection
activity is detected, the autostop timer will be bumped by this
value.
</div>
{!enableAutoStop && (
<Tooltip title="This option can be enabled in the template settings">
<Pill text="Disabled" />
</Tooltip>
)}
</>
}
>
<FormFields>
<FormControlLabel
Expand All @@ -361,6 +394,7 @@ export const WorkspaceScheduleForm: FC<
name="autostopEnabled"
checked={form.values.autostopEnabled}
onChange={handleToggleAutostop}
disabled={!enableAutoStop}
/>
}
label={Language.stopSwitch}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ export const WorkspaceSchedulePage: FC = () => {
(scheduleState.matches("presentForm") ||
scheduleState.matches("submittingSchedule")) && (
<WorkspaceScheduleForm
enableAutoStart={template.allow_user_autostart}
enableAutoStop={template.allow_user_autostop}
submitScheduleError={submitScheduleError}
initialValues={{
...getAutostart(workspace),
Expand Down
4 changes: 2 additions & 2 deletions site/src/testHelpers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,8 @@ export const MockTemplate: TypesGen.Template = {
failure_ttl_ms: 0,
time_til_dormant_ms: 0,
time_til_dormant_autodelete_ms: 0,
allow_user_autostart: false,
allow_user_autostop: false,
allow_user_autostart: true,
allow_user_autostop: true,
};

export const MockTemplateVersionFiles: TemplateVersionFiles = {
Expand Down