Skip to content

feat: show template versions #3003

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 2 commits into from
Jul 15, 2022
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
9 changes: 9 additions & 0 deletions site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ export const getTemplateVersionResources = async (
return response.data
}

export const getTemplateVersions = async (
templateId: string,
): Promise<TypesGen.TemplateVersion[]> => {
const response = await axios.get<TypesGen.TemplateVersion[]>(
`/api/v2/templates/${templateId}/versions`,
)
return response.data
}

export const getWorkspace = async (
workspaceId: string,
params?: TypesGen.WorkspaceOptions,
Expand Down
27 changes: 27 additions & 0 deletions site/src/components/VersionsTable/VersionsTable.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ComponentMeta, Story } from "@storybook/react"
import { MockTemplateVersion } from "../../testHelpers/entities"
import { VersionsTable, VersionsTableProps } from "./VersionsTable"

export default {
title: "components/VersionsTable",
component: VersionsTable,
} as ComponentMeta<typeof VersionsTable>

const Template: Story<VersionsTableProps> = (args) => <VersionsTable {...args} />

export const Example = Template.bind({})
Example.args = {
versions: [
MockTemplateVersion,
{
...MockTemplateVersion,
name: "test-template-version-2",
created_at: "2022-05-18T18:39:01.382927298Z",
},
],
}

export const Empty = Template.bind({})
Empty.args = {
versions: [],
}
74 changes: 74 additions & 0 deletions site/src/components/VersionsTable/VersionsTable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import Box from "@material-ui/core/Box"
import { Theme } from "@material-ui/core/styles"
import Table from "@material-ui/core/Table"
import TableBody from "@material-ui/core/TableBody"
import TableCell from "@material-ui/core/TableCell"
import TableHead from "@material-ui/core/TableHead"
import TableRow from "@material-ui/core/TableRow"
import useTheme from "@material-ui/styles/useTheme"
import { FC } from "react"
import * as TypesGen from "../../api/typesGenerated"
import { EmptyState } from "../EmptyState/EmptyState"
import { TableLoader } from "../TableLoader/TableLoader"

export const Language = {
emptyMessage: "No versions found",
nameLabel: "Version name",
createdAtLabel: "Created at",
createdByLabel: "Created by",
}

export interface VersionsTableProps {
versions?: TypesGen.TemplateVersion[]
}

export const VersionsTable: FC<VersionsTableProps> = ({ versions }) => {
const isLoading = !versions
const theme: Theme = useTheme()

return (
<Table data-testid="versions-table">
<TableHead>
<TableRow>
<TableCell width="30%">{Language.nameLabel}</TableCell>
<TableCell width="30%">{Language.createdAtLabel}</TableCell>
<TableCell width="40%">{Language.createdByLabel}</TableCell>
</TableRow>
</TableHead>
<TableBody>
{isLoading && <TableLoader />}
{versions &&
versions
.slice()
.reverse()
.map((version) => {
return (
<TableRow key={version.id} data-testid={`version-${version.id}`}>
<TableCell>{version.name}</TableCell>
<TableCell>
<span style={{ color: theme.palette.text.secondary }}>
{new Date(version.created_at).toLocaleString()}
</span>
</TableCell>
<TableCell>
<span style={{ color: theme.palette.text.secondary }}>
{version.created_by_name}
</span>
</TableCell>
</TableRow>
)
})}

{versions && versions.length === 0 && (
<TableRow>
<TableCell colSpan={999}>
<Box p={4}>
<EmptyState message={Language.emptyMessage} />
</Box>
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
)
}
2 changes: 2 additions & 0 deletions site/src/pages/TemplatePage/TemplatePage.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { screen } from "@testing-library/react"
import {
MockTemplate,
MockTemplateVersion,
MockWorkspaceResource,
renderWithAuth,
} from "../../testHelpers/renderHelpers"
Expand All @@ -15,5 +16,6 @@ describe("TemplatePage", () => {
await screen.findByText(MockTemplate.name)
screen.getByTestId("markdown")
screen.getByText(MockWorkspaceResource.name)
screen.getByTestId(`version-${MockTemplateVersion.id}`)
})
})
4 changes: 3 additions & 1 deletion site/src/pages/TemplatePage/TemplatePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export const TemplatePage: FC = () => {
organizationId,
},
})
const { template, activeTemplateVersion, templateResources } = templateState.context
const { template, activeTemplateVersion, templateResources, templateVersions } =
templateState.context
const isLoading = !template || !activeTemplateVersion || !templateResources

if (isLoading) {
Expand All @@ -43,6 +44,7 @@ export const TemplatePage: FC = () => {
template={template}
activeTemplateVersion={activeTemplateVersion}
templateResources={templateResources}
templateVersions={templateVersions}
/>
</>
)
Expand Down
2 changes: 2 additions & 0 deletions site/src/pages/TemplatePage/TemplatePageView.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Example.args = {
template: Mocks.MockTemplate,
activeTemplateVersion: Mocks.MockTemplateVersion,
templateResources: [Mocks.MockWorkspaceResource, Mocks.MockWorkspaceResource2],
templateVersions: [Mocks.MockTemplateVersion],
}

export const SmallViewport = Template.bind({})
Expand All @@ -33,6 +34,7 @@ You can add instructions here
\`\`\``,
},
templateResources: [Mocks.MockWorkspaceResource, Mocks.MockWorkspaceResource2],
templateVersions: [Mocks.MockTemplateVersion],
}
SmallViewport.parameters = {
chromatic: { viewports: [600] },
Expand Down
13 changes: 13 additions & 0 deletions site/src/pages/TemplatePage/TemplatePageView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,29 @@ import {
import { Stack } from "../../components/Stack/Stack"
import { TemplateResourcesTable } from "../../components/TemplateResourcesTable/TemplateResourcesTable"
import { TemplateStats } from "../../components/TemplateStats/TemplateStats"
import { VersionsTable } from "../../components/VersionsTable/VersionsTable"
import { WorkspaceSection } from "../../components/WorkspaceSection/WorkspaceSection"

const Language = {
createButton: "Create workspace",
noDescription: "",
readmeTitle: "README",
resourcesTitle: "Resources",
versionsTitle: "Version history",
}

export interface TemplatePageViewProps {
template: Template
activeTemplateVersion: TemplateVersion
templateResources: WorkspaceResource[]
templateVersions?: TemplateVersion[]
}

export const TemplatePageView: FC<TemplatePageViewProps> = ({
template,
activeTemplateVersion,
templateResources,
templateVersions,
}) => {
const styles = useStyles()
const readme = frontMatter(activeTemplateVersion.readme)
Expand Down Expand Up @@ -88,6 +92,12 @@ export const TemplatePageView: FC<TemplatePageViewProps> = ({
</ReactMarkdown>
</div>
</WorkspaceSection>
<WorkspaceSection
title={Language.versionsTitle}
contentsProps={{ className: styles.versionsTableContents }}
>
<VersionsTable versions={templateVersions} />
</WorkspaceSection>
</Stack>
</Margins>
)
Expand All @@ -111,5 +121,8 @@ export const useStyles = makeStyles((theme) => {
resourcesTableContents: {
margin: 0,
},
versionsTableContents: {
margin: 0,
},
}
})
5 changes: 3 additions & 2 deletions site/src/testHelpers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ export const MockRunningProvisionerJob: TypesGen.ProvisionerJob = {

export const MockTemplateVersion: TypesGen.TemplateVersion = {
id: "test-template-version",
created_at: "",
updated_at: "",
created_at: "2022-05-17T17:39:01.382927298Z",
updated_at: "2022-05-17T17:39:01.382927298Z",
template_id: "test-template",
job: MockProvisionerJob,
name: "test-version",
readme: `---
Expand Down
3 changes: 3 additions & 0 deletions site/src/testHelpers/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export const handlers = [
rest.get("/api/v2/templates/:templateId", async (req, res, ctx) => {
return res(ctx.status(200), ctx.json(M.MockTemplate))
}),
rest.get("/api/v2/templates/:templateId/versions", async (req, res, ctx) => {
return res(ctx.status(200), ctx.json([M.MockTemplateVersion]))
}),
rest.get("/api/v2/templateversions/:templateVersionId", async (req, res, ctx) => {
return res(ctx.status(200), ctx.json(M.MockTemplateVersion))
}),
Expand Down
36 changes: 35 additions & 1 deletion site/src/xServices/template/templateXService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { assign, createMachine } from "xstate"
import { getTemplateByName, getTemplateVersion, getTemplateVersionResources } from "../../api/api"
import {
getTemplateByName,
getTemplateVersion,
getTemplateVersionResources,
getTemplateVersions,
} from "../../api/api"
import { Template, TemplateVersion, WorkspaceResource } from "../../api/typesGenerated"

interface TemplateContext {
Expand All @@ -8,6 +13,7 @@ interface TemplateContext {
template?: Template
activeTemplateVersion?: TemplateVersion
templateResources?: WorkspaceResource[]
templateVersions?: TemplateVersion[]
}

export const templateMachine = createMachine(
Expand All @@ -24,6 +30,9 @@ export const templateMachine = createMachine(
getTemplateResources: {
data: WorkspaceResource[]
}
getTemplateVersions: {
data: TemplateVersion[]
}
},
},
tsTypes: {} as import("./templateXService.typegen").Typegen0,
Expand Down Expand Up @@ -72,6 +81,21 @@ export const templateMachine = createMachine(
success: { type: "final" },
},
},
templateVersions: {
initial: "gettingTemplateVersions",
states: {
gettingTemplateVersions: {
invoke: {
src: "getTemplateVersions",
onDone: {
actions: ["assignTemplateVersions"],
target: "success",
},
},
},
success: { type: "final" },
},
},
},
},
loaded: {},
Expand All @@ -94,6 +118,13 @@ export const templateMachine = createMachine(

return getTemplateVersionResources(ctx.template.active_version_id)
},
getTemplateVersions: (ctx) => {
if (!ctx.template) {
throw new Error("Template not loaded")
}

return getTemplateVersions(ctx.template.id)
},
},
actions: {
assignTemplate: assign({
Expand All @@ -105,6 +136,9 @@ export const templateMachine = createMachine(
assignTemplateResources: assign({
templateResources: (_, event) => event.data,
}),
assignTemplateVersions: assign({
templateVersions: (_, event) => event.data,
}),
},
},
)