Skip to content

refactor(site): Refactor workspace schedule page #7069

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 12 commits into from
Apr 11, 2023
Merged
Prev Previous commit
Next Next commit
Improve data loading
  • Loading branch information
BrunoQuaresma committed Apr 10, 2023
commit bda21b275e664c4643372ef6702cf948fc839590
23 changes: 7 additions & 16 deletions site/src/pages/WorkspaceSchedulePage/WorkspaceSchedulePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { PageHeader, PageHeaderTitle } from "components/PageHeader/PageHeader"
import dayjs from "dayjs"
import { scheduleToAutostart } from "pages/WorkspaceSchedulePage/schedule"
import { ttlMsToAutostop } from "pages/WorkspaceSchedulePage/ttl"
import { useEffect, FC } from "react"
import { useWorkspaceSettingsContext } from "pages/WorkspaceSettingsPage/WorkspaceSettingsLayout"
import { FC } from "react"
import { Helmet } from "react-helmet-async"
import { useTranslation } from "react-i18next"
import { Navigate, useNavigate, useParams } from "react-router-dom"
Expand Down Expand Up @@ -44,25 +45,18 @@ export const WorkspaceSchedulePage: FC = () => {
const navigate = useNavigate()
const username = firstOrItem(usernameQueryParam, null)
const workspaceName = firstOrItem(workspaceQueryParam, null)
const [scheduleState, scheduleSend] = useMachine(workspaceSchedule)
const { workspace } = useWorkspaceSettingsContext()
const [scheduleState, scheduleSend] = useMachine(workspaceSchedule, {
context: { workspace },
})
const {
checkPermissionsError,
submitScheduleError,
getWorkspaceError,
getTemplateError,
permissions,
workspace,
template,
} = scheduleState.context

// Get workspace on mount and whenever the args for getting a workspace change.
// scheduleSend should not change.
useEffect(() => {
username &&
workspaceName &&
scheduleSend({ type: "GET_WORKSPACE", username, workspaceName })
}, [username, workspaceName, scheduleSend])

if (!username || !workspaceName) {
return <Navigate to="/workspaces" />
}
Expand All @@ -83,10 +77,7 @@ export const WorkspaceSchedulePage: FC = () => {
{scheduleState.matches("error") && (
<AlertBanner
severity="error"
error={getWorkspaceError || checkPermissionsError || getTemplateError}
retry={() =>
scheduleSend({ type: "GET_WORKSPACE", username, workspaceName })
}
error={checkPermissionsError || getTemplateError}
/>
)}
{permissions && !permissions.updateWorkspace && (
Expand Down
10 changes: 4 additions & 6 deletions site/src/pages/WorkspaceSettingsPage/WorkspaceSettingsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useTranslation } from "react-i18next"
import { useNavigate, useParams } from "react-router-dom"
import { pageTitle } from "util/page"
import { useUpdateWorkspaceSettings, useWorkspaceSettings } from "./data"
import { useWorkspaceSettingsContext } from "./WorkspaceSettingsLayout"
import { WorkspaceSettingsPageView } from "./WorkspaceSettingsPageView"

const WorkspaceSettingsPage = () => {
Expand All @@ -13,13 +14,10 @@ const WorkspaceSettingsPage = () => {
username: string
workspace: string
}
const {
data: settings,
error,
isLoading,
} = useWorkspaceSettings(username, workspaceName)
const { workspace } = useWorkspaceSettingsContext()
const { data: settings, error, isLoading } = useWorkspaceSettings(workspace)
const navigate = useNavigate()
const updateSettings = useUpdateWorkspaceSettings(settings?.workspace.id, {
const updateSettings = useUpdateWorkspaceSettings(workspace.id, {
onSuccess: ({ name }) => {
navigate(`/@${username}/${name}`)
},
Expand Down
12 changes: 5 additions & 7 deletions site/src/pages/WorkspaceSettingsPage/data.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { useMutation, useQuery } from "@tanstack/react-query"
import {
getWorkspaceByOwnerAndName,
getWorkspaceBuildParameters,
getTemplateVersionRichParameters,
patchWorkspace,
postWorkspaceBuild,
} from "api/api"
import { WorkspaceBuildParameter } from "api/typesGenerated"
import { Workspace, WorkspaceBuildParameter } from "api/typesGenerated"

const getWorkspaceSettings = async (owner: string, name: string) => {
const workspace = await getWorkspaceByOwnerAndName(owner, name)
const getWorkspaceSettings = async (workspace: Workspace) => {
const latestBuild = workspace.latest_build
const [templateVersionRichParameters, buildParameters] = await Promise.all([
getTemplateVersionRichParameters(latestBuild.template_version_id),
Expand All @@ -22,10 +20,10 @@ const getWorkspaceSettings = async (owner: string, name: string) => {
}
}

export const useWorkspaceSettings = (owner: string, workspace: string) => {
export const useWorkspaceSettings = (workspace: Workspace) => {
return useQuery({
queryKey: ["workspaceSettings", owner, workspace],
queryFn: () => getWorkspaceSettings(owner, workspace),
queryKey: ["workspaceSettings", workspace.id],
queryFn: () => getWorkspaceSettings(workspace),
})
}

Expand Down
53 changes: 4 additions & 49 deletions site/src/xServices/workspaceSchedule/workspaceScheduleXService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface WorkspaceScheduleContext {
* re-fetch the workspace to ensure we're up-to-date. As a result, this
* machine is partially influenced by workspaceXService.
*/
workspace?: TypesGen.Workspace
workspace: TypesGen.Workspace
template?: TypesGen.Template
getTemplateError?: Error | unknown
permissions?: Permissions
Expand All @@ -41,7 +41,6 @@ const permissionsToCheck = (workspace: TypesGen.Workspace) => ({
})

export type WorkspaceScheduleEvent =
| { type: "GET_WORKSPACE"; username: string; workspaceName: string }
| {
type: "SUBMIT_SCHEDULE"
autostart: TypesGen.UpdateWorkspaceAutostartRequest
Expand All @@ -63,38 +62,13 @@ export const workspaceSchedule =
context: {} as WorkspaceScheduleContext,
events: {} as WorkspaceScheduleEvent,
services: {} as {
getWorkspace: {
data: TypesGen.Workspace
}
getTemplate: {
data: TypesGen.Template
}
},
},
initial: "idle",
on: {
GET_WORKSPACE: "gettingWorkspace",
},
initial: "gettingPermissions",
states: {
idle: {
tags: "loading",
},
gettingWorkspace: {
entry: ["clearGetWorkspaceError", "clearContext"],
invoke: {
src: "getWorkspace",
id: "getWorkspace",
onDone: {
target: "gettingPermissions",
actions: ["assignWorkspace"],
},
onError: {
target: "error",
actions: ["assignGetWorkspaceError"],
},
},
tags: "loading",
},
gettingPermissions: {
entry: "clearGetPermissionsError",
invoke: {
Expand Down Expand Up @@ -167,9 +141,7 @@ export const workspaceSchedule =
},
},
error: {
on: {
GET_WORKSPACE: "gettingWorkspace",
},
type: "final",
},
done: {
type: "final",
Expand All @@ -184,12 +156,6 @@ export const workspaceSchedule =
assignSubmissionError: assign({
submitScheduleError: (_, event) => event.data,
}),
assignWorkspace: assign({
workspace: (_, event) => event.data,
}),
assignGetWorkspaceError: assign({
getWorkspaceError: (_, event) => event.data,
}),
assignPermissions: assign({
// Setting event.data as Permissions to be more stricted. So we know
// what permissions we asked for.
Expand All @@ -213,12 +179,7 @@ export const workspaceSchedule =
clearGetPermissionsError: assign({
checkPermissionsError: (_) => undefined,
}),
clearContext: () => {
assign({ workspace: undefined, permissions: undefined })
},
clearGetWorkspaceError: (context) => {
assign({ ...context, getWorkspaceError: undefined })
},

// action instead of service because we fire and forget so that the
// user can return to the workspace page to see the restart
restartWorkspace: (context) => {
Expand All @@ -232,12 +193,6 @@ export const workspaceSchedule =
},

services: {
getWorkspace: async (_, event) => {
return await API.getWorkspaceByOwnerAndName(
event.username,
event.workspaceName,
)
},
getTemplate: async (context) => {
if (context.workspace) {
return await API.getTemplate(context.workspace.template_id)
Expand Down