1
- import { useMachine } from "@xstate/react" ;
2
- import { PropsWithChildren , FC } from "react" ;
3
- import { sshKeyMachine } from "xServices/sshKey/sshKeyXService" ;
1
+ import { PropsWithChildren , FC , useState } from "react" ;
4
2
import { ConfirmDialog } from "../../../components/Dialogs/ConfirmDialog/ConfirmDialog" ;
5
3
import { Section } from "../../../components/SettingsLayout/Section" ;
6
4
import { SSHKeysPageView } from "./SSHKeysPageView" ;
5
+ import { regenerateUserSSHKey , userSSHKey } from "api/queries/sshKeys" ;
6
+ import { displaySuccess } from "components/GlobalSnackbar/utils" ;
7
+ import { useMutation , useQuery , useQueryClient } from "@tanstack/react-query" ;
7
8
8
9
export const Language = {
9
10
title : "SSH keys" ,
@@ -15,40 +16,47 @@ export const Language = {
15
16
} ;
16
17
17
18
export const SSHKeysPage : FC < PropsWithChildren < unknown > > = ( ) => {
18
- const [ sshState , sshSend ] = useMachine ( sshKeyMachine ) ;
19
- const isLoading = sshState . matches ( "gettingSSHKey" ) ;
20
- const hasLoaded = sshState . matches ( "loaded" ) ;
21
- const { getSSHKeyError, regenerateSSHKeyError, sshKey } = sshState . context ;
22
-
23
- const onRegenerateClick = ( ) => {
24
- sshSend ( { type : "REGENERATE_SSH_KEY" } ) ;
25
- } ;
19
+ const [ isConfirmingRegeneration , setIsConfirmingRegeneration ] =
20
+ useState ( false ) ;
21
+ const queryClient = useQueryClient ( ) ;
22
+ const userSSHKeyQuery = useQuery ( userSSHKey ( "me" ) ) ;
23
+ const regenerateSSHKeyMutationOptions = regenerateUserSSHKey (
24
+ "me" ,
25
+ queryClient ,
26
+ ) ;
27
+ const regenerateSSHKeyMutation = useMutation ( {
28
+ ...regenerateSSHKeyMutationOptions ,
29
+ onSuccess : ( newKey ) => {
30
+ regenerateSSHKeyMutationOptions . onSuccess ( newKey ) ;
31
+ displaySuccess ( "SSH Key regenerated successfully." ) ;
32
+ setIsConfirmingRegeneration ( false ) ;
33
+ } ,
34
+ } ) ;
26
35
27
36
return (
28
37
< >
29
38
< Section title = { Language . title } >
30
39
< SSHKeysPageView
31
- isLoading = { isLoading }
32
- hasLoaded = { hasLoaded }
33
- getSSHKeyError = { getSSHKeyError }
34
- regenerateSSHKeyError = { regenerateSSHKeyError }
35
- sshKey = { sshKey }
36
- onRegenerateClick = { onRegenerateClick }
40
+ isLoading = { userSSHKeyQuery . isLoading }
41
+ getSSHKeyError = { userSSHKeyQuery . error }
42
+ regenerateSSHKeyError = { regenerateSSHKeyMutation . error }
43
+ sshKey = { userSSHKeyQuery . data }
44
+ onRegenerateClick = { ( ) => {
45
+ setIsConfirmingRegeneration ( true ) ;
46
+ } }
37
47
/>
38
48
</ Section >
39
49
40
50
< ConfirmDialog
41
51
type = "delete"
42
52
hideCancel = { false }
43
- open = { sshState . matches ( "confirmSSHKeyRegenerate" ) }
44
- confirmLoading = { sshState . matches ( "regeneratingSSHKey" ) }
53
+ open = { isConfirmingRegeneration }
54
+ confirmLoading = { regenerateSSHKeyMutation . isLoading }
45
55
title = { Language . regenerateDialogTitle }
46
56
confirmText = { Language . confirmLabel }
47
- onConfirm = { ( ) => {
48
- sshSend ( { type : "CONFIRM_REGENERATE_SSH_KEY" } ) ;
49
- } }
57
+ onConfirm = { regenerateSSHKeyMutation . mutate }
50
58
onClose = { ( ) => {
51
- sshSend ( { type : "CANCEL_REGENERATE_SSH_KEY" } ) ;
59
+ setIsConfirmingRegeneration ( false ) ;
52
60
} }
53
61
description = { < > { Language . regenerateDialogMessage } </ > }
54
62
/>
0 commit comments