Skip to content

Commit

Permalink
feat: cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
Raju-kadel-27 committed Aug 7, 2024
1 parent 96cf8c6 commit 7371a75
Showing 1 changed file with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
import { api } from "@/trpc/react";
import { RiMore2Fill } from "@remixicon/react";
import { useRouter } from "next/navigation";
import { Fragment, useEffect, useRef, useState } from "react";
import { Fragment, useState } from "react";
import { toast } from "sonner";
import { useCopyToClipboard } from "usehooks-ts";

Expand All @@ -59,7 +59,7 @@ interface ApiKey {
lastUsed: Date | null;
}

function DeleteKey({ keyId, open, setOpen }: DeleteDialogProps) {
function DeleteKeyAlert({ keyId, open, setOpen }: DeleteDialogProps) {
const router = useRouter();

const deleteMutation = api.apiKey.delete.useMutation({
Expand Down Expand Up @@ -97,7 +97,7 @@ function DeleteKey({ keyId, open, setOpen }: DeleteDialogProps) {
);
}

function RotateKey({
function RotateKeyAlert({
keyId,
open,
setOpen,
Expand All @@ -122,6 +122,10 @@ function RotateKey({
console.error(error);
toast.error("An error occurred while creating the API key.");
},

onSettled: () => {
setLoading(false);
},
});
return (
<AlertDialog open={open} onOpenChange={setOpen}>
Expand Down Expand Up @@ -149,12 +153,11 @@ function RotateKey({
);
}

function KeyModal({ apiKey, open, setOpen }: KeyModalProps) {
function CopyKeyModal({ apiKey, open, setOpen }: KeyModalProps) {
const [_copied, copy] = useCopyToClipboard();

return (
<Modal
title="API key created"
title="API key rotated"
subtitle={
<Tldr
message="
Expand Down Expand Up @@ -191,20 +194,22 @@ function KeyModal({ apiKey, open, setOpen }: KeyModalProps) {

const ApiKeysTable = ({ keys }: { keys: ApiKey[] }) => {
const [loading, setLoading] = useState<boolean>(false);
const [openDeleteAlert, setOpenDeleteAlert] = useState<boolean>(false);
const [openRotateAlert, setOpenRotateAlert] = useState<boolean>(false);
const [copyApiKeyModal, setCopyApiKeyModal] = useState<boolean>(false);
const [openDeleteAlertDialog, setOpenDeleteAlertDialog] =
useState<boolean>(false);
const [openRotateAlertDialog, setOpenRotateAlertDialog] =
useState<boolean>(false);
const [copyApiKeyModal, showCopyApiKeyModal] = useState<boolean>(false);

const [apiKey, setApiKey] = useState<string>("");
const [selectedKey, setSelected] = useState<string>("");

const handleDeleteKey = (key: string) => {
setSelected(key);
setOpenDeleteAlert(true);
setOpenDeleteAlertDialog(true);
};
const handleRotateKey = (key: string) => {
setSelected(key);
setOpenRotateAlert(true);
setOpenRotateAlertDialog(true);
};

return (
Expand Down Expand Up @@ -281,23 +286,23 @@ const ApiKeysTable = ({ keys }: { keys: ApiKey[] }) => {
))}
</TableBody>
</Table>
<DeleteKey
open={openDeleteAlert}
setOpen={(val) => setOpenDeleteAlert(val)}
<DeleteKeyAlert
open={openDeleteAlertDialog}
setOpen={(val) => setOpenDeleteAlertDialog(val)}
keyId={selectedKey}
/>
<RotateKey
open={openRotateAlert}
setOpen={(val: boolean) => setOpenRotateAlert(val)}
setShowModal={setCopyApiKeyModal}
<RotateKeyAlert
open={openRotateAlertDialog}
setOpen={(val: boolean) => setOpenRotateAlertDialog(val)}
setShowModal={showCopyApiKeyModal}
setApiKey={setApiKey}
keyId={selectedKey}
setLoading={setLoading}
/>
<KeyModal
<CopyKeyModal
apiKey={apiKey}
open={copyApiKeyModal}
setOpen={setCopyApiKeyModal}
setOpen={showCopyApiKeyModal}
/>
</Card>
{loading && <Loading />}
Expand Down

0 comments on commit 7371a75

Please sign in to comment.