Skip to content

chore(site): move ssh keys management to react-query #9625

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions site/src/api/queries/sshKeys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import * as API from "api/api";

const getUserSSHKeyQueryKey = (userId: string) => [userId, "sshKey"];

export const useUserSSHKey = (userId: string) => {
return useQuery({
queryKey: getUserSSHKeyQueryKey(userId),
queryFn: () => API.getUserSSHKey(userId),
});
};

export const useRegenerateUserSSHKey = (
userId: string,
onSuccess: () => void,
) => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: () => API.regenerateUserSSHKey(userId),
onSuccess: (newKey) => {
queryClient.setQueryData(getUserSSHKeyQueryKey(userId), newKey);
onSuccess();
},
});
};
42 changes: 21 additions & 21 deletions site/src/pages/UserSettingsPage/SSHKeysPage/SSHKeysPage.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useMachine } from "@xstate/react";
import { PropsWithChildren, FC } from "react";
import { sshKeyMachine } from "xServices/sshKey/sshKeyXService";
import { PropsWithChildren, FC, useState } from "react";
import { ConfirmDialog } from "../../../components/Dialogs/ConfirmDialog/ConfirmDialog";
import { Section } from "../../../components/SettingsLayout/Section";
import { SSHKeysPageView } from "./SSHKeysPageView";
import { useRegenerateUserSSHKey, useUserSSHKey } from "api/queries/sshKeys";
import { displaySuccess } from "components/GlobalSnackbar/utils";

export const Language = {
title: "SSH keys",
Expand All @@ -15,40 +15,40 @@ export const Language = {
};

export const SSHKeysPage: FC<PropsWithChildren<unknown>> = () => {
const [sshState, sshSend] = useMachine(sshKeyMachine);
const isLoading = sshState.matches("gettingSSHKey");
const hasLoaded = sshState.matches("loaded");
const { getSSHKeyError, regenerateSSHKeyError, sshKey } = sshState.context;

const onRegenerateClick = () => {
sshSend({ type: "REGENERATE_SSH_KEY" });
};
const [isConfirmingRegeneration, setIsConfirmingRegeneration] =
useState(false);
const userSSHKeyQuery = useUserSSHKey("me");
const regenerateUserSSHKeyMutation = useRegenerateUserSSHKey("me", () => {
displaySuccess("SSH Key regenerated successfully.");
setIsConfirmingRegeneration(false);
});

return (
<>
<Section title={Language.title}>
<SSHKeysPageView
isLoading={isLoading}
hasLoaded={hasLoaded}
getSSHKeyError={getSSHKeyError}
regenerateSSHKeyError={regenerateSSHKeyError}
sshKey={sshKey}
onRegenerateClick={onRegenerateClick}
isLoading={userSSHKeyQuery.isLoading}
getSSHKeyError={userSSHKeyQuery.error}
regenerateSSHKeyError={regenerateUserSSHKeyMutation.error}
sshKey={userSSHKeyQuery.data}
onRegenerateClick={() => {
setIsConfirmingRegeneration(true);
}}
/>
</Section>

<ConfirmDialog
type="delete"
hideCancel={false}
open={sshState.matches("confirmSSHKeyRegenerate")}
confirmLoading={sshState.matches("regeneratingSSHKey")}
open={isConfirmingRegeneration}
confirmLoading={regenerateUserSSHKeyMutation.isLoading}
title={Language.regenerateDialogTitle}
confirmText={Language.confirmLabel}
onConfirm={() => {
sshSend({ type: "CONFIRM_REGENERATE_SSH_KEY" });
regenerateUserSSHKeyMutation.mutate();
}}
onClose={() => {
sshSend({ type: "CANCEL_REGENERATE_SSH_KEY" });
setIsConfirmingRegeneration(false);
}}
description={<>{Language.regenerateDialogMessage}</>}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const meta: Meta<typeof SSHKeysPageView> = {
component: SSHKeysPageView,
args: {
isLoading: false,
hasLoaded: true,
sshKey: {
user_id: "test-user-id",
created_at: "2022-07-28T07:45:50.795918897Z",
Expand All @@ -30,7 +29,6 @@ export const Loading: Story = {

export const WithGetSSHKeyError: Story = {
args: {
hasLoaded: false,
getSSHKeyError: mockApiError({
message: "Failed to get SSH key",
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export const Language = {

export interface SSHKeysPageViewProps {
isLoading: boolean;
hasLoaded: boolean;
getSSHKeyError?: unknown;
regenerateSSHKeyError?: unknown;
sshKey?: GitSSHKey;
Expand All @@ -25,7 +24,6 @@ export const SSHKeysPageView: FC<
React.PropsWithChildren<SSHKeysPageViewProps>
> = ({
isLoading,
hasLoaded,
getSSHKeyError,
regenerateSSHKeyError,
sshKey,
Expand All @@ -49,7 +47,7 @@ export const SSHKeysPageView: FC<
{Boolean(regenerateSSHKeyError) && (
<ErrorAlert error={regenerateSSHKeyError} dismissible />
)}
{hasLoaded && sshKey && (
{sshKey && (
<>
<p className={styles.description}>
The following public key is used to authenticate Git in workspaces.
Expand Down
120 changes: 0 additions & 120 deletions site/src/xServices/sshKey/sshKeyXService.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bye bye!!

This file was deleted.