@@ -2,6 +2,9 @@ import { type FC, useRef, useState } from "react";
2
2
import { Link as RouterLink , useNavigate } from "react-router-dom" ;
3
3
import { useDeleteTemplate } from "./deleteTemplate" ;
4
4
5
+ import { useQuery } from "react-query" ;
6
+ import { workspacesByQuery } from "api/queries/workspaces" ;
7
+ import { ConfirmDialog } from "components/Dialogs/ConfirmDialog/ConfirmDialog" ;
5
8
import {
6
9
AuthorizationResponse ,
7
10
Template ,
@@ -33,7 +36,6 @@ type TemplateMenuProps = {
33
36
templateName : string ;
34
37
templateVersion : string ;
35
38
onDelete : ( ) => void ;
36
- onMenuOpen ?: ( ) => void ;
37
39
} ;
38
40
39
41
const TemplateMenu : FC < TemplateMenuProps > = ( {
@@ -120,8 +122,16 @@ export const TemplatePageHeader: FC<TemplatePageHeaderProps> = ({
120
122
permissions,
121
123
onDeleteTemplate,
122
124
} ) => {
125
+ const navigate = useNavigate ( ) ;
126
+ const deletionState = useDeleteTemplate ( template , onDeleteTemplate ) ;
127
+
128
+ const queryText = `template:${ template . name } ` ;
129
+ const workspaceCountQuery = useQuery ( {
130
+ ...workspacesByQuery ( queryText ) ,
131
+ select : ( res ) => res . count ,
132
+ } ) ;
133
+
123
134
const hasIcon = template . icon && template . icon !== "" ;
124
- const deleteTemplate = useDeleteTemplate ( template , onDeleteTemplate ) ;
125
135
126
136
return (
127
137
< Margins >
@@ -141,7 +151,7 @@ export const TemplatePageHeader: FC<TemplatePageHeaderProps> = ({
141
151
< TemplateMenu
142
152
templateVersion = { activeVersion . name }
143
153
templateName = { template . name }
144
- onDelete = { deleteTemplate . openDeleteConfirmation }
154
+ onDelete = { deletionState . openDeleteConfirmation }
145
155
/>
146
156
) }
147
157
</ >
@@ -170,14 +180,54 @@ export const TemplatePageHeader: FC<TemplatePageHeaderProps> = ({
170
180
</ Stack >
171
181
</ PageHeader >
172
182
173
- < DeleteDialog
174
- isOpen = { deleteTemplate . isDeleteDialogOpen }
175
- confirmLoading = { deleteTemplate . state . status === "deleting" }
176
- onConfirm = { deleteTemplate . confirmDelete }
177
- onCancel = { deleteTemplate . cancelDeleteConfirmation }
178
- entity = "template"
179
- name = { template . name }
180
- />
183
+ { workspaceCountQuery . status === "success" &&
184
+ workspaceCountQuery . data === 0 ? (
185
+ < DeleteDialog
186
+ isOpen = { deletionState . isDeleteDialogOpen }
187
+ onConfirm = { deletionState . confirmDelete }
188
+ onCancel = { deletionState . cancelDeleteConfirmation }
189
+ entity = "template"
190
+ name = { template . name }
191
+ />
192
+ ) : (
193
+ < ConfirmDialog
194
+ type = "info"
195
+ title = "Unable to delete"
196
+ hideCancel = { false }
197
+ open = { deletionState . isDeleteDialogOpen }
198
+ onClose = { deletionState . cancelDeleteConfirmation }
199
+ confirmText = "See workspaces"
200
+ onConfirm = { ( ) => {
201
+ navigate ( {
202
+ pathname : "/workspaces" ,
203
+ search : new URLSearchParams ( { filter : queryText } ) . toString ( ) ,
204
+ } ) ;
205
+ } }
206
+ description = {
207
+ < >
208
+ { workspaceCountQuery . isSuccess && (
209
+ < >
210
+ This template is used by{ " " }
211
+ < strong >
212
+ { workspaceCountQuery . data } workspace
213
+ { workspaceCountQuery . data === 1 ? "" : "s" }
214
+ </ strong >
215
+ . Please delete all related workspaces before deleting this
216
+ template.
217
+ </ >
218
+ ) }
219
+
220
+ { workspaceCountQuery . isLoading && (
221
+ < > Loading information about workspaces used by this template.</ >
222
+ ) }
223
+
224
+ { workspaceCountQuery . isError && (
225
+ < > Unable to determine workspaces used by this template.</ >
226
+ ) }
227
+ </ >
228
+ }
229
+ />
230
+ ) }
181
231
</ Margins >
182
232
) ;
183
233
} ;
0 commit comments