1
1
import { useState } from "react" ;
2
- import type { UseMutationResult } from "@tanstack/react-query" ;
2
+ import type { UseMutationResult , UseQueryResult } from "@tanstack/react-query" ;
3
3
import Button from "@mui/material/Button" ;
4
4
import Box from "@mui/material/Box" ;
5
5
import CircularProgress from "@mui/material/CircularProgress" ;
@@ -11,15 +11,14 @@ import Typography from "@mui/material/Typography";
11
11
import Tooltip from '@mui/material/Tooltip' ;
12
12
13
13
import CodeBlock from "../CodeBlock" ;
14
+ import type { Run as RunResponse } from "../../lib/paddles.d" ;
14
15
import { KillRunPayload } from "../../lib/teuthologyAPI.d" ;
15
- import { useSession } from "../../lib/teuthologyAPI" ;
16
+ import { useSession , useRunKill } from "../../lib/teuthologyAPI" ;
16
17
import Alert from "../Alert" ;
17
18
18
19
19
20
type KillButtonProps = {
20
- mutation : UseMutationResult ;
21
- payload : KillRunPayload ;
22
- disabled ?: boolean ;
21
+ query : UseQueryResult < RunResponse > ;
23
22
} ;
24
23
25
24
type KillButtonDialogProps = {
@@ -29,13 +28,21 @@ type KillButtonDialogProps = {
29
28
handleClose : ( ) => void ;
30
29
} ;
31
30
32
- export default function KillButton ( props : KillButtonProps ) {
31
+ export default function KillButton ( { query : runQuery } : KillButtonProps ) {
32
+ const killMutation = useRunKill ( ) ;
33
33
const [ open , setOpen ] = useState ( false ) ;
34
- const mutation : UseMutationResult = props . mutation ;
35
34
const sessionQuery = useSession ( ) ;
35
+ const data : RunResponse | undefined = runQuery . data ;
36
+ const run_owner = data ?. jobs [ 0 ] . owner || "" ;
37
+ const killPayload : KillRunPayload = {
38
+ "--run" : data ?. name || "" ,
39
+ "--owner" : run_owner ,
40
+ "--machine-type" : data ?. machine_type || "" ,
41
+ "--preserve-queue" : true ,
42
+ }
36
43
const loggedUser = sessionQuery . data ?. session ?. username ;
37
44
const isUserAdmin = sessionQuery . data ?. session ?. isUserAdmin ;
38
- const owner = props . payload [ "--owner" ] . toLowerCase ( )
45
+ const owner = killPayload [ "--owner" ] . toLowerCase ( )
39
46
const isOwner = ( loggedUser ?. toLowerCase ( ) == owner ) || ( `scheduled_${ loggedUser ?. toLowerCase ( ) } @teuthology` == owner )
40
47
const isButtonDisabled = ( ! isOwner && ! isUserAdmin )
41
48
@@ -53,11 +60,14 @@ export default function KillButton(props: KillButtonProps) {
53
60
} ;
54
61
55
62
const refreshAndtoggle = ( ) => {
63
+ if ( open && ! killMutation . isIdle ) { // on closing confirmation dialog after kill-run
64
+ runQuery . refetch ( ) ;
65
+ }
56
66
toggleDialog ( ) ;
57
- mutation . reset ( ) ;
67
+ killMutation . reset ( ) ;
58
68
}
59
69
60
- if ( props . disabled || ! ( sessionQuery . data ?. session ?. username ) ) {
70
+ if ( ( data ?. status . includes ( "finished" ) ) || ! ( sessionQuery . data ?. session ?. username ) ) {
61
71
// run finished or user logged out
62
72
return null ;
63
73
}
@@ -81,14 +91,14 @@ export default function KillButton(props: KillButtonProps) {
81
91
</ span >
82
92
</ Tooltip >
83
93
< KillButtonDialog
84
- mutation = { mutation }
85
- payload = { props . payload }
94
+ mutation = { killMutation }
95
+ payload = { killPayload }
86
96
open = { open }
87
- handleClose = { toggleDialog }
97
+ handleClose = { refreshAndtoggle }
88
98
/>
89
99
</ div >
90
- { ( mutation . isError ) ? < Alert severity = "error" message = "Unable to kill run" /> : null }
91
- { ( mutation . isSuccess ) ? < Alert severity = "success" message = { `Run killed successfully! \n` } /> : null }
100
+ { ( killMutation . isError ) ? < Alert severity = "error" message = "Unable to kill run" /> : null }
101
+ { ( killMutation . isSuccess ) ? < Alert severity = "success" message = { `Run killed successfully! \n` } /> : null }
92
102
</ div >
93
103
) ;
94
104
} ;
0 commit comments