From 50d9fdfce7ec728b4698aeb1d71502868dc2cc1f Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Thu, 28 Sep 2023 15:11:36 -0500 Subject: [PATCH 1/4] chore: handle interfaces as "any" in typescript Use generated Deployment Option --- scripts/apitypings/main.go | 29 +++++-------------- site/src/api/api.ts | 13 +-------- site/src/api/typesGenerated.ts | 4 +-- .../DeploySettingsLayout/OptionsTable.tsx | 3 +- 4 files changed, 11 insertions(+), 38 deletions(-) diff --git a/scripts/apitypings/main.go b/scripts/apitypings/main.go index 3b5709cc01646..cb842d601fbfa 100644 --- a/scripts/apitypings/main.go +++ b/scripts/apitypings/main.go @@ -989,28 +989,13 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) { }, nil } - // Do support "Stringer" interfaces, they likely can get string - // marshaled. - for i := 0; i < intf.NumMethods(); i++ { - meth := intf.Method(i) - if meth.Name() == "String" { - return TypescriptType{ - ValueType: "string", - AboveTypeLine: indentedComment("actual value is an interface that implements 'String()'"), - Optional: false, - }, nil - } - } - - // All complex interfaces should be named. So if we get here, that means - // we are using anonymous interfaces. Which is just weird and not supported. - // Example: - // type Foo struct { - // Bar interface { - // Baz() string - // } - // } - return TypescriptType{}, xerrors.New("only empty interface types are supported") + // Interfaces are difficult to determine the JSON type, so just return + // an 'any'. + return TypescriptType{ + ValueType: "any", + AboveTypeLine: indentedComment("eslint-disable-next-line @typescript-eslint/no-explicit-any -- Golang interface, unable to resolve type."), + Optional: false, + }, nil case *types.TypeParam: _, ok := ty.Underlying().(*types.Interface) if !ok { diff --git a/site/src/api/api.ts b/site/src/api/api.ts index 657d0efc6765f..9c202c61f1bf8 100644 --- a/site/src/api/api.ts +++ b/site/src/api/api.ts @@ -1024,21 +1024,10 @@ export interface DeploymentGroup { readonly description: string; readonly children: DeploymentGroup[]; } -export interface DeploymentOption { - readonly name: string; - readonly description: string; - readonly flag: string; - readonly flag_shorthand: string; - readonly value: unknown; - readonly hidden: boolean; - readonly group?: DeploymentGroup; - readonly env?: string; - readonly yaml?: string; -} export type DeploymentConfig = { readonly config: TypesGen.DeploymentValues; - readonly options: DeploymentOption[]; + readonly options: TypesGen.ClibaseOption[]; }; export const getDeploymentConfig = async (): Promise => { diff --git a/site/src/api/typesGenerated.ts b/site/src/api/typesGenerated.ts index 639ba1e837b4d..a631dc3d79868 100644 --- a/site/src/api/typesGenerated.ts +++ b/site/src/api/typesGenerated.ts @@ -2106,8 +2106,8 @@ export interface ClibaseOption { readonly env?: string; readonly yaml?: string; readonly default?: string; - // actual value is an interface that implements 'String()' - readonly value?: string; + // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Golang interface, unable to resolve type. + readonly value?: any; readonly annotations?: ClibaseAnnotations; readonly group?: ClibaseGroup; readonly use_instead?: ClibaseOption[]; diff --git a/site/src/components/DeploySettingsLayout/OptionsTable.tsx b/site/src/components/DeploySettingsLayout/OptionsTable.tsx index 62b4008702c75..214b2d0b4be7f 100644 --- a/site/src/components/DeploySettingsLayout/OptionsTable.tsx +++ b/site/src/components/DeploySettingsLayout/OptionsTable.tsx @@ -14,11 +14,10 @@ import { } from "components/DeploySettingsLayout/Option"; import { FC } from "react"; import { optionValue } from "./optionValue"; -import { DeploymentOption } from "api/api"; import Box from "@mui/material/Box"; const OptionsTable: FC<{ - options: DeploymentOption[]; + options: ClibaseOption[]; }> = ({ options }) => { const styles = useStyles(); From 41f562b7688d3149573e04b3abfc230262056b39 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Thu, 28 Sep 2023 15:35:00 -0500 Subject: [PATCH 2/4] Use generated types --- site/src/api/api.ts | 9 --------- .../DeploySettingsLayout/OptionsTable.tsx | 1 + .../DeploySettingsLayout/optionValue.test.ts | 6 +++--- .../DeploySettingsLayout/optionValue.ts | 4 ++-- .../GeneralSettingsPageView.tsx | 5 ++--- .../SecuritySettingsPageView.tsx | 4 ++-- .../UserAuthSettingsPageView.tsx | 4 ++-- site/src/utils/deployOptions.ts | 15 ++++++++------- 8 files changed, 20 insertions(+), 28 deletions(-) diff --git a/site/src/api/api.ts b/site/src/api/api.ts index 9c202c61f1bf8..600903806ccef 100644 --- a/site/src/api/api.ts +++ b/site/src/api/api.ts @@ -1016,15 +1016,6 @@ export const getDeploymentSSHConfig = return response.data; }; -// The Deployment types are not generated on from the Go generator yet because -// it does not know how to generate OptionSet -export interface DeploymentGroup { - readonly name: string; - readonly parent?: DeploymentGroup; - readonly description: string; - readonly children: DeploymentGroup[]; -} - export type DeploymentConfig = { readonly config: TypesGen.DeploymentValues; readonly options: TypesGen.ClibaseOption[]; diff --git a/site/src/components/DeploySettingsLayout/OptionsTable.tsx b/site/src/components/DeploySettingsLayout/OptionsTable.tsx index 214b2d0b4be7f..d5a8cdad75b27 100644 --- a/site/src/components/DeploySettingsLayout/OptionsTable.tsx +++ b/site/src/components/DeploySettingsLayout/OptionsTable.tsx @@ -15,6 +15,7 @@ import { import { FC } from "react"; import { optionValue } from "./optionValue"; import Box from "@mui/material/Box"; +import { ClibaseOption } from "api/typesGenerated"; const OptionsTable: FC<{ options: ClibaseOption[]; diff --git a/site/src/components/DeploySettingsLayout/optionValue.test.ts b/site/src/components/DeploySettingsLayout/optionValue.test.ts index 10a50ef09e9a2..890fa6a72c638 100644 --- a/site/src/components/DeploySettingsLayout/optionValue.test.ts +++ b/site/src/components/DeploySettingsLayout/optionValue.test.ts @@ -1,7 +1,7 @@ -import { DeploymentOption } from "api/api"; import { optionValue } from "./optionValue"; +import { ClibaseOption } from "api/typesGenerated"; -const defaultOption: DeploymentOption = { +const defaultOption: ClibaseOption = { name: "", description: "", flag: "", @@ -12,7 +12,7 @@ const defaultOption: DeploymentOption = { describe("optionValue", () => { it.each<{ - option: DeploymentOption; + option: ClibaseOption; expected: unknown; }>([ { diff --git a/site/src/components/DeploySettingsLayout/optionValue.ts b/site/src/components/DeploySettingsLayout/optionValue.ts index 09bff5293e4a8..1976e52b4593b 100644 --- a/site/src/components/DeploySettingsLayout/optionValue.ts +++ b/site/src/components/DeploySettingsLayout/optionValue.ts @@ -1,8 +1,8 @@ -import { DeploymentOption } from "api/api"; +import { ClibaseOption } from "api/typesGenerated"; import { intervalToDuration, formatDuration } from "date-fns"; // optionValue is a helper function to format the value of a specific deployment options -export function optionValue(option: DeploymentOption) { +export function optionValue(option: ClibaseOption) { switch (option.name) { case "Max Token Lifetime": case "Session Duration": diff --git a/site/src/pages/DeploySettingsPage/GeneralSettingsPage/GeneralSettingsPageView.tsx b/site/src/pages/DeploySettingsPage/GeneralSettingsPage/GeneralSettingsPageView.tsx index aabdf9a98e53d..378d2d431b938 100644 --- a/site/src/pages/DeploySettingsPage/GeneralSettingsPage/GeneralSettingsPageView.tsx +++ b/site/src/pages/DeploySettingsPage/GeneralSettingsPage/GeneralSettingsPageView.tsx @@ -1,5 +1,5 @@ import Box from "@mui/material/Box"; -import { DAUsResponse } from "api/typesGenerated"; +import { ClibaseOption, DAUsResponse } from "api/typesGenerated"; import { ErrorAlert } from "components/Alert/ErrorAlert"; import { DAUChart, DAUTitle } from "components/DAUChart/DAUChart"; import { Header } from "components/DeploySettingsLayout/Header"; @@ -8,10 +8,9 @@ import { Stack } from "components/Stack/Stack"; import { ChartSection } from "./ChartSection"; import { useDeploymentOptions } from "utils/deployOptions"; import { docs } from "utils/docs"; -import { DeploymentOption } from "api/api"; export type GeneralSettingsPageViewProps = { - deploymentOptions: DeploymentOption[]; + deploymentOptions: ClibaseOption[]; deploymentDAUs?: DAUsResponse; deploymentDAUsError: unknown; }; diff --git a/site/src/pages/DeploySettingsPage/SecuritySettingsPage/SecuritySettingsPageView.tsx b/site/src/pages/DeploySettingsPage/SecuritySettingsPage/SecuritySettingsPageView.tsx index ed6adc2aa0eec..e2bb6508fb239 100644 --- a/site/src/pages/DeploySettingsPage/SecuritySettingsPage/SecuritySettingsPageView.tsx +++ b/site/src/pages/DeploySettingsPage/SecuritySettingsPage/SecuritySettingsPageView.tsx @@ -1,4 +1,4 @@ -import { DeploymentOption } from "api/api"; +import { ClibaseOption } from "api/typesGenerated"; import { Badges, DisabledBadge, @@ -15,7 +15,7 @@ import { import { docs } from "utils/docs"; export type SecuritySettingsPageViewProps = { - options: DeploymentOption[]; + options: ClibaseOption[]; featureAuditLogEnabled: boolean; featureBrowserOnlyEnabled: boolean; }; diff --git a/site/src/pages/DeploySettingsPage/UserAuthSettingsPage/UserAuthSettingsPageView.tsx b/site/src/pages/DeploySettingsPage/UserAuthSettingsPage/UserAuthSettingsPageView.tsx index 884bb1fee7ceb..9d329293eb9d7 100644 --- a/site/src/pages/DeploySettingsPage/UserAuthSettingsPage/UserAuthSettingsPageView.tsx +++ b/site/src/pages/DeploySettingsPage/UserAuthSettingsPage/UserAuthSettingsPageView.tsx @@ -1,4 +1,4 @@ -import { DeploymentOption } from "api/api"; +import { ClibaseOption } from "api/typesGenerated"; import { Badges, DisabledBadge, @@ -14,7 +14,7 @@ import { import { docs } from "utils/docs"; export type UserAuthSettingsPageViewProps = { - options: DeploymentOption[]; + options: ClibaseOption[]; }; export const UserAuthSettingsPageView = ({ diff --git a/site/src/utils/deployOptions.ts b/site/src/utils/deployOptions.ts index eb9bd8e7fbd22..819e6eef115dd 100644 --- a/site/src/utils/deployOptions.ts +++ b/site/src/utils/deployOptions.ts @@ -1,11 +1,12 @@ -import { DeploymentOption, DeploymentGroup } from "api/api"; +import { DeploymentGroup } from "api/api"; +import { ClibaseGroup, ClibaseOption } from "api/typesGenerated"; import { useMemo } from "react"; const deploymentOptions = ( - options: DeploymentOption[], + options: ClibaseOption[], ...names: string[] -): DeploymentOption[] => { - const found: DeploymentOption[] = []; +): ClibaseOption[] => { + const found: ClibaseOption[] = []; for (const name of names) { const option = options.find((o) => o.name === name); if (option) { @@ -18,14 +19,14 @@ const deploymentOptions = ( }; export const useDeploymentOptions = ( - options: DeploymentOption[], + options: ClibaseOption[], ...names: string[] -): DeploymentOption[] => { +): ClibaseOption[] => { return useMemo(() => deploymentOptions(options, ...names), [options, names]); }; export const deploymentGroupHasParent = ( - group: DeploymentGroup | undefined, + group: ClibaseGroup | undefined, parent: string, ): boolean => { if (!group) { From 9dc53612a3caca0e95599d3fcbc648d61207f92b Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Thu, 28 Sep 2023 15:44:14 -0500 Subject: [PATCH 3/4] Use generated group type --- .../NetworkSettingsPageView.stories.tsx | 5 ++--- .../SecuritySettingsPageView.stories.tsx | 11 +++++------ .../UserAuthSettingsPageView.stories.tsx | 8 +++----- site/src/utils/deployOptions.ts | 1 - 4 files changed, 10 insertions(+), 15 deletions(-) diff --git a/site/src/pages/DeploySettingsPage/NetworkSettingsPage/NetworkSettingsPageView.stories.tsx b/site/src/pages/DeploySettingsPage/NetworkSettingsPage/NetworkSettingsPageView.stories.tsx index 0cb90b6d048f3..42f3677b80f27 100644 --- a/site/src/pages/DeploySettingsPage/NetworkSettingsPage/NetworkSettingsPageView.stories.tsx +++ b/site/src/pages/DeploySettingsPage/NetworkSettingsPage/NetworkSettingsPageView.stories.tsx @@ -1,11 +1,10 @@ -import { DeploymentGroup } from "api/api"; +import { ClibaseGroup } from "api/typesGenerated"; import { NetworkSettingsPageView } from "./NetworkSettingsPageView"; import type { Meta, StoryObj } from "@storybook/react"; -const group: DeploymentGroup = { +const group: ClibaseGroup = { name: "Networking", description: "", - children: [] as DeploymentGroup[], }; const meta: Meta = { diff --git a/site/src/pages/DeploySettingsPage/SecuritySettingsPage/SecuritySettingsPageView.stories.tsx b/site/src/pages/DeploySettingsPage/SecuritySettingsPage/SecuritySettingsPageView.stories.tsx index 161276763dc6a..b081afdebee9e 100644 --- a/site/src/pages/DeploySettingsPage/SecuritySettingsPage/SecuritySettingsPageView.stories.tsx +++ b/site/src/pages/DeploySettingsPage/SecuritySettingsPage/SecuritySettingsPageView.stories.tsx @@ -1,11 +1,10 @@ -import { DeploymentGroup, DeploymentOption } from "api/api"; import { SecuritySettingsPageView } from "./SecuritySettingsPageView"; import type { Meta, StoryObj } from "@storybook/react"; +import { ClibaseGroup, ClibaseOption } from "api/typesGenerated"; -const group: DeploymentGroup = { +const group: ClibaseGroup = { name: "Networking", description: "", - children: [] as DeploymentGroup[], }; const meta: Meta = { @@ -64,15 +63,15 @@ export const NoTLS = { { name: "SSH Keygen Algorithm", value: "1234", - } as DeploymentOption, + } as ClibaseOption, { name: "Disable Owner Workspace Access", value: false, - } as DeploymentOption, + } as ClibaseOption, { name: "Secure Auth Cookie", value: "1234", - } as DeploymentOption, + } as ClibaseOption, ], }, }; diff --git a/site/src/pages/DeploySettingsPage/UserAuthSettingsPage/UserAuthSettingsPageView.stories.tsx b/site/src/pages/DeploySettingsPage/UserAuthSettingsPage/UserAuthSettingsPageView.stories.tsx index 744fa98d9105e..10f304fd7e116 100644 --- a/site/src/pages/DeploySettingsPage/UserAuthSettingsPage/UserAuthSettingsPageView.stories.tsx +++ b/site/src/pages/DeploySettingsPage/UserAuthSettingsPage/UserAuthSettingsPageView.stories.tsx @@ -1,17 +1,15 @@ -import { DeploymentGroup } from "api/api"; +import { ClibaseGroup } from "api/typesGenerated"; import { UserAuthSettingsPageView } from "./UserAuthSettingsPageView"; import type { Meta, StoryObj } from "@storybook/react"; -const oidcGroup: DeploymentGroup = { +const oidcGroup: ClibaseGroup = { name: "OIDC", description: "", - children: [] as DeploymentGroup[], }; -const ghGroup: DeploymentGroup = { +const ghGroup: ClibaseGroup = { name: "GitHub", description: "", - children: [] as DeploymentGroup[], }; const meta: Meta = { diff --git a/site/src/utils/deployOptions.ts b/site/src/utils/deployOptions.ts index 819e6eef115dd..44a0db64fb39a 100644 --- a/site/src/utils/deployOptions.ts +++ b/site/src/utils/deployOptions.ts @@ -1,4 +1,3 @@ -import { DeploymentGroup } from "api/api"; import { ClibaseGroup, ClibaseOption } from "api/typesGenerated"; import { useMemo } from "react"; From 385c411652185838173fb8be7064b7d38e61f3b1 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Thu, 28 Sep 2023 16:02:09 -0500 Subject: [PATCH 4/4] Fix type --- .../NetworkSettingsPage/NetworkSettingsPageView.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/site/src/pages/DeploySettingsPage/NetworkSettingsPage/NetworkSettingsPageView.tsx b/site/src/pages/DeploySettingsPage/NetworkSettingsPage/NetworkSettingsPageView.tsx index 9edcf69cbc550..3daa105967764 100644 --- a/site/src/pages/DeploySettingsPage/NetworkSettingsPage/NetworkSettingsPageView.tsx +++ b/site/src/pages/DeploySettingsPage/NetworkSettingsPage/NetworkSettingsPageView.tsx @@ -1,4 +1,4 @@ -import { DeploymentOption } from "api/api"; +import { ClibaseOption } from "api/typesGenerated"; import { Badges, EnabledBadge, @@ -14,7 +14,7 @@ import { import { docs } from "utils/docs"; export type NetworkSettingsPageViewProps = { - options: DeploymentOption[]; + options: ClibaseOption[]; }; export const NetworkSettingsPageView = ({