|
1 |
| -import { TemplateVersion, type Workspace } from "api/typesGenerated"; |
2 |
| -import { type FC, useMemo, useState } from "react"; |
| 1 | +import type { Workspace } from "api/typesGenerated"; |
| 2 | +import { type FC, ReactElement, ReactNode, useState } from "react"; |
3 | 3 | import { Dialog, DialogContent } from "components/Dialog/Dialog";
|
4 | 4 | import { Button } from "components/Button/Button";
|
5 | 5 | import { useQueries } from "react-query";
|
6 | 6 | import { templateVersion } from "api/queries/templates";
|
| 7 | +import { Loader } from "components/Loader/Loader"; |
| 8 | +import { ErrorAlert } from "components/Alert/ErrorAlert"; |
| 9 | +import { Avatar } from "components/Avatar/Avatar"; |
7 | 10 |
|
8 | 11 | /**
|
9 | 12 | * @todo Need to decide if we should include the template display name here, or
|
@@ -41,96 +44,218 @@ function groupWorkspacesByTemplateVersionId(
|
41 | 44 | return [...grouped.values()];
|
42 | 45 | }
|
43 | 46 |
|
44 |
| -type WorkspaceDeltaEntry = Readonly<{}>; |
45 |
| -type WorkspaceDeltas = Map<string, WorkspaceDeltaEntry | null>; |
| 47 | +type Separation = Readonly<{ |
| 48 | + dormant: readonly Workspace[]; |
| 49 | + noUpdateNeeded: readonly Workspace[]; |
| 50 | + readyToUpdate: readonly Workspace[]; |
| 51 | +}>; |
46 | 52 |
|
47 |
| -function separateWorkspacesByDormancy( |
48 |
| - workspaces: readonly Workspace[], |
49 |
| -): readonly [dormant: readonly Workspace[], active: readonly Workspace[]] { |
| 53 | +function separateWorkspaces(workspaces: readonly Workspace[]): Separation { |
| 54 | + const noUpdateNeeded: Workspace[] = []; |
50 | 55 | const dormant: Workspace[] = [];
|
51 |
| - const active: Workspace[] = []; |
| 56 | + const readyToUpdate: Workspace[] = []; |
52 | 57 |
|
53 | 58 | for (const ws of workspaces) {
|
54 |
| - // If a workspace doesn't have any pending updates whatsoever, we can |
55 |
| - // safely skip processing it |
56 | 59 | if (!ws.outdated) {
|
| 60 | + noUpdateNeeded.push(ws); |
57 | 61 | continue;
|
58 | 62 | }
|
59 |
| - if (ws.dormant_at) { |
| 63 | + if (ws.dormant_at !== null) { |
60 | 64 | dormant.push(ws);
|
61 |
| - } else { |
62 |
| - active.push(ws); |
| 65 | + continue; |
63 | 66 | }
|
| 67 | + readyToUpdate.push(ws); |
64 | 68 | }
|
65 | 69 |
|
66 |
| - return [dormant, active]; |
| 70 | + return { dormant, noUpdateNeeded, readyToUpdate }; |
67 | 71 | }
|
68 | 72 |
|
69 |
| -type BatchUpdateModalFormProps = Readonly<{ |
| 73 | +type WorkspacePanelProps = Readonly<{ |
| 74 | + workspaceName: string; |
| 75 | + workspaceIconUrl: string; |
| 76 | + label?: ReactNode; |
| 77 | + adornment?: ReactNode; |
| 78 | +}>; |
| 79 | + |
| 80 | +const ReviewPanel: FC<WorkspacePanelProps> = ({ |
| 81 | + workspaceName, |
| 82 | + label, |
| 83 | + workspaceIconUrl, |
| 84 | +}) => { |
| 85 | + return ( |
| 86 | + <div className="rounded-md px-4 py-2 border border-solid border-content-secondary/50 text-sm"> |
| 87 | + <div className="flex flex-row flex-wrap grow items-center gap-2"> |
| 88 | + <Avatar size="sm" variant="icon" src={workspaceIconUrl} /> |
| 89 | + {workspaceName} |
| 90 | + </div> |
| 91 | + </div> |
| 92 | + ); |
| 93 | +}; |
| 94 | + |
| 95 | +type ReviewFormProps = Readonly<{ |
70 | 96 | workspacesToUpdate: readonly Workspace[];
|
71 |
| - onClose: () => void; |
| 97 | + onCancel: () => void; |
72 | 98 | onSubmit: () => void;
|
73 | 99 | }>;
|
74 | 100 |
|
75 |
| -export const BatchUpdateModalForm: FC<BatchUpdateModalFormProps> = ({ |
| 101 | +const ReviewForm: FC<ReviewFormProps> = ({ |
76 | 102 | workspacesToUpdate,
|
77 |
| - onClose, |
| 103 | + onCancel, |
78 | 104 | onSubmit,
|
79 | 105 | }) => {
|
80 | 106 | // We need to take a local snapshot of the workspaces that existed on mount
|
81 | 107 | // because workspaces are such a mutable resource, and there's a chance that
|
82 | 108 | // they can be changed by another user + be subject to a query invalidation
|
83 |
| - // while the form is open. We need to cross-reference these with the latest |
84 |
| - // workspaces from props so that we can display any changes in the UI |
| 109 | + // while the form is open |
85 | 110 | const [cachedWorkspaces, setCachedWorkspaces] = useState(workspacesToUpdate);
|
86 | 111 | // Dormant workspaces can't be activated without activating them first. For
|
87 | 112 | // now, we'll only show the user that some workspaces can't be updated, and
|
88 | 113 | // then skip over them for all other update logic
|
89 |
| - const [dormant, active] = separateWorkspacesByDormancy(cachedWorkspaces); |
| 114 | + const { dormant, noUpdateNeeded, readyToUpdate } = |
| 115 | + separateWorkspaces(cachedWorkspaces); |
90 | 116 |
|
91 | 117 | // The workspaces don't have all necessary data by themselves, so we need to
|
92 | 118 | // fetch the unique template versions, and massage the results
|
93 |
| - const groups = groupWorkspacesByTemplateVersionId(active); |
| 119 | + const groups = groupWorkspacesByTemplateVersionId(readyToUpdate); |
94 | 120 | const templateVersionQueries = useQueries({
|
95 | 121 | queries: groups.map((g) => templateVersion(g.templateVersionId)),
|
96 | 122 | });
|
97 | 123 | // React Query persists previous errors even if a query is no longer in the
|
98 |
| - // error state, so we need to explicitly check the isError property |
| 124 | + // error state, so we need to explicitly check the isError property to see |
| 125 | + // if any of the queries actively have an error |
99 | 126 | const error = templateVersionQueries.find((q) => q.isError)?.error;
|
100 | 127 | const merged = templateVersionQueries.every((q) => q.isSuccess)
|
101 | 128 | ? templateVersionQueries.map((q) => q.data)
|
102 | 129 | : undefined;
|
103 | 130 |
|
104 | 131 | // Also need to tease apart workspaces that are actively running, because
|
105 | 132 | // there's a whole set of warnings we need to issue about them
|
106 |
| - const running = active.filter((a) => a.latest_build.status === "running"); |
107 |
| - const workspacesChangedWhileOpen = workspacesToUpdate !== cachedWorkspaces; |
| 133 | + const running = readyToUpdate.filter( |
| 134 | + (ws) => ws.latest_build.status === "running", |
| 135 | + ); |
108 | 136 |
|
109 |
| - const deltas = useMemo<WorkspaceDeltas>(() => new Map(), []); |
| 137 | + const workspacesChangedWhileOpen = workspacesToUpdate !== cachedWorkspaces; |
| 138 | + const updateIsReady = error !== undefined && readyToUpdate.length > 0; |
110 | 139 |
|
111 | 140 | return (
|
112 |
| - <Dialog> |
113 |
| - <DialogContent> |
114 |
| - <form |
115 |
| - className="max-w-lg px-4" |
116 |
| - onSubmit={(e) => { |
117 |
| - e.preventDefault(); |
118 |
| - console.log("Blah"); |
119 |
| - onSubmit(); |
120 |
| - }} |
| 141 | + <form |
| 142 | + className="overflow-y-auto max-h-[90vh]" |
| 143 | + onSubmit={(e) => { |
| 144 | + e.preventDefault(); |
| 145 | + onSubmit(); |
| 146 | + }} |
| 147 | + > |
| 148 | + <div className="flex flex-row justify-between items-center pb-6"> |
| 149 | + <h3 className="text-2xl font-semibold m-0 leading-tight"> |
| 150 | + Review update |
| 151 | + </h3> |
| 152 | + |
| 153 | + <Button |
| 154 | + variant="outline" |
| 155 | + disabled={!workspacesChangedWhileOpen} |
| 156 | + onClick={() => setCachedWorkspaces(workspacesToUpdate)} |
121 | 157 | >
|
122 |
| - <div className="flex flex-row justify-between"> |
123 |
| - <h2 className="text-xl font-semibold m-0 leading-tight"> |
124 |
| - Review updates |
125 |
| - </h2> |
126 |
| - <Button |
127 |
| - disabled={workspacesChangedWhileOpen} |
128 |
| - onClick={() => setCachedWorkspaces(workspacesToUpdate)} |
129 |
| - > |
130 |
| - Refresh |
131 |
| - </Button> |
| 158 | + Refresh list |
| 159 | + </Button> |
| 160 | + </div> |
| 161 | + |
| 162 | + {error !== undefined && <ErrorAlert error={error} />} |
| 163 | + |
| 164 | + {noUpdateNeeded.length > 0 && ( |
| 165 | + <section className="border-0 border-t border-solid border-t-content-secondary/25 py-4"> |
| 166 | + <div className="max-w-prose"> |
| 167 | + <h4 className="m-0">Updated workspaces</h4> |
| 168 | + <p className="m-0 text-sm leading-snug text-content-secondary"> |
| 169 | + These workspaces are fully up to date and will be skipped during |
| 170 | + the update. |
| 171 | + </p> |
132 | 172 | </div>
|
133 |
| - </form> |
| 173 | + |
| 174 | + <ul className="list-none p-0"> |
| 175 | + {noUpdateNeeded.map((ws) => ( |
| 176 | + <li key={ws.id}> |
| 177 | + <ReviewPanel |
| 178 | + workspaceName={ws.name} |
| 179 | + workspaceIconUrl={ws.template_icon} |
| 180 | + /> |
| 181 | + </li> |
| 182 | + ))} |
| 183 | + </ul> |
| 184 | + </section> |
| 185 | + )} |
| 186 | + |
| 187 | + {dormant.length > 0 && ( |
| 188 | + <section className="border-0 border-t border-solid border-t-content-secondary/25 py-4"> |
| 189 | + <div className="max-w-prose"> |
| 190 | + <h4 className="m-0">Dormant workspaces</h4> |
| 191 | + <p className="m-0 text-sm leading-snug text-content-secondary"> |
| 192 | + Dormant workspaces cannot be updated without first activating the |
| 193 | + workspace. They will be skipped during the batch update. |
| 194 | + </p> |
| 195 | + </div> |
| 196 | + |
| 197 | + <ul className="list-none p-0"> |
| 198 | + {dormant.map((ws) => ( |
| 199 | + <li key={ws.id}> |
| 200 | + <ReviewPanel |
| 201 | + workspaceName={ws.name} |
| 202 | + workspaceIconUrl={ws.template_icon} |
| 203 | + /> |
| 204 | + </li> |
| 205 | + ))} |
| 206 | + </ul> |
| 207 | + </section> |
| 208 | + )} |
| 209 | + |
| 210 | + <div className="flex flex-row flex-wrap justify-end gap-4"> |
| 211 | + <Button variant="outline" onClick={onCancel}> |
| 212 | + Cancel |
| 213 | + </Button> |
| 214 | + <Button variant="default" type="submit" disabled={!updateIsReady}> |
| 215 | + Update |
| 216 | + </Button> |
| 217 | + </div> |
| 218 | + </form> |
| 219 | + ); |
| 220 | +}; |
| 221 | + |
| 222 | +type BatchUpdateModalFormProps = Readonly<{ |
| 223 | + workspacesToUpdate: readonly Workspace[]; |
| 224 | + open: boolean; |
| 225 | + loading: boolean; |
| 226 | + onClose: () => void; |
| 227 | + onSubmit: () => void; |
| 228 | +}>; |
| 229 | + |
| 230 | +export const BatchUpdateModalForm: FC<BatchUpdateModalFormProps> = ({ |
| 231 | + open, |
| 232 | + loading, |
| 233 | + workspacesToUpdate, |
| 234 | + onClose, |
| 235 | + onSubmit, |
| 236 | +}) => { |
| 237 | + return ( |
| 238 | + <Dialog |
| 239 | + open={open} |
| 240 | + onOpenChange={() => { |
| 241 | + if (open) { |
| 242 | + onClose(); |
| 243 | + } |
| 244 | + }} |
| 245 | + > |
| 246 | + <DialogContent className="max-w-screen-md"> |
| 247 | + {loading ? ( |
| 248 | + <Loader /> |
| 249 | + ) : ( |
| 250 | + <ReviewForm |
| 251 | + workspacesToUpdate={workspacesToUpdate} |
| 252 | + onCancel={onClose} |
| 253 | + onSubmit={() => { |
| 254 | + onSubmit(); |
| 255 | + onClose(); |
| 256 | + }} |
| 257 | + /> |
| 258 | + )} |
134 | 259 | </DialogContent>
|
135 | 260 | </Dialog>
|
136 | 261 | );
|
|
0 commit comments