Skip to content

Commit

Permalink
DEVPROD-8688: Add time zone selector to host uptime scheduler (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
sophstad authored Aug 6, 2024
1 parent 4a66d87 commit ddbea6b
Show file tree
Hide file tree
Showing 14 changed files with 240 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface Props {
myPublicKeys: MyPublicKeysQuery["myPublicKeys"];
noExpirationCheckboxTooltip: string;
permanentlyExempt: boolean;
timeZone?: string;
timeZone: string;
volumes: MyVolumesQuery["myVolumes"];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ describe("edit spawn host modal", () => {
hostId: "host_id",
myPublicKeys,
oldUserTags,
timeZone: "America/New_York",
}),
).toStrictEqual({
hostId: "host_id",
Expand Down Expand Up @@ -67,6 +66,9 @@ const formState = {
runContinuously: false,
},
},
details: {
timeZone: "America/New_York",
},
},
},
instanceType: "m4.xlarge",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ interface Props {
myPublicKeys: MyPublicKeysQuery["myPublicKeys"];
formData: FormState;
oldUserTags: { key: string; value: string }[];
timeZone: string;
}
export const formToGql = ({
formData,
hostId,
myPublicKeys,
oldUserTags,
timeZone,
}: Props): EditSpawnHostMutationVariables => {
const {
expirationDetails,
Expand Down Expand Up @@ -78,8 +76,6 @@ export const formToGql = ({
},
savePublicKey: !useExisting && savePublicKey,
sleepSchedule:
noExpiration && hostUptime
? getSleepSchedule(hostUptime, timeZone)
: null,
noExpiration && hostUptime ? getSleepSchedule(hostUptime) : null,
};
};
77 changes: 56 additions & 21 deletions apps/spruce/src/components/Spawn/getFormSchema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { add } from "date-fns";
import widgets from "components/SpruceForm/Widgets";
import { StyledLink } from "components/styles";
import { hostUptimeDocumentationUrl } from "constants/externalResources";
import { prettifyTimeZone } from "constants/fieldMaps";
import { abbreviateTimeZone, timeZones } from "constants/fieldMaps";
import { size } from "constants/tokens";
import { MyPublicKeysQuery } from "gql/generated/types";
import {
Expand All @@ -24,7 +24,7 @@ type HostUptimeProps = {
warnings: string[];
};
isEditModal: boolean;
timeZone?: string;
timeZone: string;
};

const getHostUptimeSchema = ({
Expand All @@ -38,7 +38,7 @@ const getHostUptimeSchema = ({
properties: {
useDefaultUptimeSchedule: {
type: "boolean" as "boolean",
title: "Use default host uptime schedule (Mon–Fri, 8am–8pm)",
title: `Use default host uptime schedule (Mon–Fri, 8am–8pm ${abbreviateTimeZone(timeZone)})`,
default: true,
},
sleepSchedule: {
Expand Down Expand Up @@ -97,7 +97,24 @@ const getHostUptimeSchema = ({
},
},
details: {
type: "null" as "null",
type: "object" as "object",
title: "",
properties: {
timeZone: {
type: "string",
title: "Time Zone",
default: timeZone,
oneOf: timeZones.map(({ str, value }) => ({
type: "string" as "string",
title: str,
enum: [value],
})),
},

uptimeHours: {
type: "null" as "null",
},
},
},
isBetaTester: {
type: "boolean" as "boolean",
Expand Down Expand Up @@ -169,16 +186,35 @@ const getHostUptimeSchema = ({
},
},
details: {
"ui:descriptionNode": (
<Details
// @ts-expect-error: FIXME. This comment was added by an automated script.
timeZone={timeZone}
// @ts-expect-error: FIXME. This comment was added by an automated script.
totalUptimeHours={hostUptimeWarnings?.enabledHoursCount}
/>
),
"ui:showLabel": false,
"ui:warnings": hostUptimeWarnings?.warnings,
"ui:elementWrapperCSS": css`
align-items: flex-end;
display: flex;
gap: ${size.xs};
flex-wrap: wrap;
> div {
width: 40%;
}
> [role="alert"] {
margin-top: 0;
width: 100%;
}
`,
timeZone: {
"ui:allowDeselect": false,
"ui:sizeVariant": "xsmall",
},
uptimeHours: {
"ui:descriptionNode": (
<Details
// @ts-expect-error: FIXME. This comment was added by an automated script.
totalUptimeHours={hostUptimeWarnings?.enabledHoursCount}
/>
),
"ui:showLabel": false,
"ui:warnings": hostUptimeWarnings?.warnings,
},
},
isBetaTester: {
"ui:widget": widgets.ToggleWidget,
Expand Down Expand Up @@ -210,19 +246,18 @@ const getHostUptimeSchema = ({
},
});

const Details: React.FC<{ timeZone: string; totalUptimeHours: number }> = ({
timeZone,
const Details: React.FC<{ totalUptimeHours: number }> = ({
totalUptimeHours,
}) => (
<DetailsDiv data-cy="host-uptime-details">
All times are displayed in{" "}
<Badge>{prettifyTimeZone.get(timeZone) ?? timeZone}</Badge>{" "}
{totalUptimeHours} host uptime hours per week
{" "}
{totalUptimeHours} host uptime hours per week
</DetailsDiv>
);

const DetailsDiv = styled.div`
margin-bottom: ${size.xs};
margin-bottom: 21px;
white-space: nowrap;
`;

type ExpirationProps = {
Expand All @@ -234,7 +269,7 @@ type ExpirationProps = {
isEditModal: boolean;
noExpirationCheckboxTooltip?: string;
permanentlyExempt?: boolean;
timeZone?: string;
timeZone: string;
};

export const getExpirationDetailsSchema = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ interface Props {
noExpirationCheckboxTooltip: string;
permanentlyExempt: boolean;
spawnTaskData?: SpawnTaskQuery["task"];
timeZone?: string;
timeZone: string;
useSetupScript?: boolean;
useProjectSetupScript?: boolean;
userAwsRegion?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ describe("spawn host modal", () => {
formData,
myPublicKeys,
spawnTaskData: null,
timeZone: "America/New_York",
}),
).toStrictEqual(mutationInput);
});
Expand All @@ -26,7 +25,6 @@ describe("spawn host modal", () => {
myPublicKeys,
spawnTaskData: null,
migrateVolumeId,
timeZone: "America/New_York",
}),
).toStrictEqual({
...mutationInput,
Expand Down Expand Up @@ -120,6 +118,7 @@ const data: Array<{ formData: FormState; mutationInput: SpawnHostInput }> = [
runContinuously: false,
},
},
details: { timeZone: "America/New_York" },
},
},
homeVolumeDetails: { selectExistingVolume: true, volumeSelect: "" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ interface Props {
myPublicKeys: MyPublicKeysQuery["myPublicKeys"];
spawnTaskData?: SpawnTaskQuery["task"];
migrateVolumeId?: string;
timeZone?: string;
}
export const formToGql = ({
formData,
isVirtualWorkStation,
migrateVolumeId,
myPublicKeys,
spawnTaskData,
timeZone,
}: Props): SpawnHostMutationVariables["spawnHostInput"] => {
const {
expirationDetails,
Expand All @@ -50,8 +48,7 @@ export const formToGql = ({
noExpiration: expirationDetails?.noExpiration,
sleepSchedule:
expirationDetails?.noExpiration && hostUptime
? // @ts-expect-error: FIXME. This comment was added by an automated script.
getSleepSchedule(hostUptime, timeZone)
? getSleepSchedule(hostUptime)
: null,
volumeId:
migrateVolumeId ||
Expand Down
Loading

0 comments on commit ddbea6b

Please sign in to comment.