Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5ca8c42

Browse files
chore(site): move ssh keys management to react-query (#9625)
1 parent6db89b0 commit5ca8c42

File tree

5 files changed

+57
-148
lines changed

5 files changed

+57
-148
lines changed

‎site/src/api/queries/sshKeys.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import{QueryClient}from"@tanstack/react-query";
2+
import*asAPIfrom"api/api";
3+
import{GitSSHKey}from"api/typesGenerated";
4+
5+
constgetUserSSHKeyQueryKey=(userId:string)=>[userId,"sshKey"];
6+
7+
exportconstuserSSHKey=(userId:string)=>{
8+
return{
9+
queryKey:getUserSSHKeyQueryKey(userId),
10+
queryFn:()=>API.getUserSSHKey(userId),
11+
};
12+
};
13+
14+
exportconstregenerateUserSSHKey=(
15+
userId:string,
16+
queryClient:QueryClient,
17+
)=>{
18+
return{
19+
mutationFn:()=>API.regenerateUserSSHKey(userId),
20+
onSuccess:(newKey:GitSSHKey)=>{
21+
queryClient.setQueryData(getUserSSHKeyQueryKey(userId),newKey);
22+
},
23+
};
24+
};

‎site/src/pages/UserSettingsPage/SSHKeysPage/SSHKeysPage.tsx‎

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
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";
42
import{ConfirmDialog}from"../../../components/Dialogs/ConfirmDialog/ConfirmDialog";
53
import{Section}from"../../../components/SettingsLayout/Section";
64
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";
78

89
exportconstLanguage={
910
title:"SSH keys",
@@ -15,40 +16,47 @@ export const Language = {
1516
};
1617

1718
exportconstSSHKeysPage:FC<PropsWithChildren<unknown>>=()=>{
18-
const[sshState,sshSend]=useMachine(sshKeyMachine);
19-
constisLoading=sshState.matches("gettingSSHKey");
20-
consthasLoaded=sshState.matches("loaded");
21-
const{ getSSHKeyError, regenerateSSHKeyError, sshKey}=sshState.context;
22-
23-
constonRegenerateClick=()=>{
24-
sshSend({type:"REGENERATE_SSH_KEY"});
25-
};
19+
const[isConfirmingRegeneration,setIsConfirmingRegeneration]=
20+
useState(false);
21+
constqueryClient=useQueryClient();
22+
constuserSSHKeyQuery=useQuery(userSSHKey("me"));
23+
constregenerateSSHKeyMutationOptions=regenerateUserSSHKey(
24+
"me",
25+
queryClient,
26+
);
27+
constregenerateSSHKeyMutation=useMutation({
28+
...regenerateSSHKeyMutationOptions,
29+
onSuccess:(newKey)=>{
30+
regenerateSSHKeyMutationOptions.onSuccess(newKey);
31+
displaySuccess("SSH Key regenerated successfully.");
32+
setIsConfirmingRegeneration(false);
33+
},
34+
});
2635

2736
return(
2837
<>
2938
<Sectiontitle={Language.title}>
3039
<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+
}}
3747
/>
3848
</Section>
3949

4050
<ConfirmDialog
4151
type="delete"
4252
hideCancel={false}
43-
open={sshState.matches("confirmSSHKeyRegenerate")}
44-
confirmLoading={sshState.matches("regeneratingSSHKey")}
53+
open={isConfirmingRegeneration}
54+
confirmLoading={regenerateSSHKeyMutation.isLoading}
4555
title={Language.regenerateDialogTitle}
4656
confirmText={Language.confirmLabel}
47-
onConfirm={()=>{
48-
sshSend({type:"CONFIRM_REGENERATE_SSH_KEY"});
49-
}}
57+
onConfirm={regenerateSSHKeyMutation.mutate}
5058
onClose={()=>{
51-
sshSend({type:"CANCEL_REGENERATE_SSH_KEY"});
59+
setIsConfirmingRegeneration(false);
5260
}}
5361
description={<>{Language.regenerateDialogMessage}</>}
5462
/>

‎site/src/pages/UserSettingsPage/SSHKeysPage/SSHKeysPageView.stories.tsx‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const meta: Meta<typeof SSHKeysPageView> = {
77
component:SSHKeysPageView,
88
args:{
99
isLoading:false,
10-
hasLoaded:true,
1110
sshKey:{
1211
user_id:"test-user-id",
1312
created_at:"2022-07-28T07:45:50.795918897Z",
@@ -30,7 +29,7 @@ export const Loading: Story = {
3029

3130
exportconstWithGetSSHKeyError:Story={
3231
args:{
33-
hasLoaded:false,
32+
sshKey:undefined,
3433
getSSHKeyError:mockApiError({
3534
message:"Failed to get SSH key",
3635
}),

‎site/src/pages/UserSettingsPage/SSHKeysPage/SSHKeysPageView.tsx‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export const Language = {
1414

1515
exportinterfaceSSHKeysPageViewProps{
1616
isLoading:boolean;
17-
hasLoaded:boolean;
1817
getSSHKeyError?:unknown;
1918
regenerateSSHKeyError?:unknown;
2019
sshKey?:GitSSHKey;
@@ -25,7 +24,6 @@ export const SSHKeysPageView: FC<
2524
React.PropsWithChildren<SSHKeysPageViewProps>
2625
>=({
2726
isLoading,
28-
hasLoaded,
2927
getSSHKeyError,
3028
regenerateSSHKeyError,
3129
sshKey,
@@ -49,7 +47,7 @@ export const SSHKeysPageView: FC<
4947
{Boolean(regenerateSSHKeyError)&&(
5048
<ErrorAlerterror={regenerateSSHKeyError}dismissible/>
5149
)}
52-
{hasLoaded&&sshKey&&(
50+
{sshKey&&(
5351
<>
5452
<pclassName={styles.description}>
5553
The following public key is used to authenticate Git in workspaces.

‎site/src/xServices/sshKey/sshKeyXService.ts‎

Lines changed: 0 additions & 120 deletions
This file was deleted.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp