|
| 1 | +import { GetKeysResponse } from "@/app/api/keys/keys-schema"; |
| 2 | +import { useState } from "react"; |
| 3 | +import { Button } from "../ui/button"; |
| 4 | +import { Trash, Clipboard, Eye, EyeOff } from "lucide-react"; |
| 5 | +import KeyDeleteConfirm from "./key-delete-confirm"; |
| 6 | +import { toast } from "sonner"; |
| 7 | + |
| 8 | +type Props = { |
| 9 | + keysData: { |
| 10 | + data: GetKeysResponse[]; |
| 11 | + total: number; |
| 12 | + } | undefined; |
| 13 | +}; |
| 14 | + |
| 15 | +const Keys = ({ keysData }: Props) => { |
| 16 | + const [openDelete, setOpenDelete] = useState(false); |
| 17 | + const [selectedKey, setSelectedKey] = useState<GetKeysResponse | null>(null); |
| 18 | + const [visibleKeys, setVisibleKeys] = useState<{ [key: number]: boolean }>({}); |
| 19 | + |
| 20 | + const handleDeleteCancel = () => { |
| 21 | + setOpenDelete(false); |
| 22 | + setSelectedKey(null); |
| 23 | + }; |
| 24 | + |
| 25 | + const handleDeleteClick = (selectedKey: GetKeysResponse) => { |
| 26 | + setSelectedKey(selectedKey); |
| 27 | + setOpenDelete(true); |
| 28 | + }; |
| 29 | + |
| 30 | + const handleCopy = (key: string) => { |
| 31 | + navigator.clipboard.writeText(key); |
| 32 | + toast("Copied to clipboard!"); |
| 33 | + }; |
| 34 | + |
| 35 | + const toggleVisibility = (index: number) => { |
| 36 | + setVisibleKeys((prev) => ({ ...prev, [index]: !prev[index] })); |
| 37 | + }; |
| 38 | + |
| 39 | + return ( |
| 40 | + <> |
| 41 | + <div className="space-y-4 mt-2 overflow-y-auto max-h-[400px] p-2"> |
| 42 | + <div> |
| 43 | + Found <span className="font-bold">{keysData?.total}</span> record |
| 44 | + {keysData && keysData?.total > 1 ? "s." : "."} |
| 45 | + </div> |
| 46 | + <div className="space-y-3 pr-2"> |
| 47 | + {keysData?.data.map((record, index) => ( |
| 48 | + <div key={index} className="border-l-4 border-gray-600 pl-4 pr-2 py-1 flex items-center justify-between"> |
| 49 | + <div className="flex flex-col"> |
| 50 | + <span className="font-medium text-gray-700">{record.name}</span> |
| 51 | + <div className="flex items-center gap-2"> |
| 52 | + <span className="text-sm text-gray-500 truncate max-w-[250px]"> |
| 53 | + {visibleKeys[index] ? record.key : `${record.key.substring(0,4)} * * * * * * * * *`} |
| 54 | + </span> |
| 55 | + <Button |
| 56 | + size="icon" |
| 57 | + variant="ghost" |
| 58 | + onClick={() => toggleVisibility(index)} |
| 59 | + className="cursor-pointer hover:bg-gray-200 text-gray-600 hover:text-gray-800" |
| 60 | + > |
| 61 | + {visibleKeys[index] ? <EyeOff size={16} /> : <Eye size={16} />} |
| 62 | + </Button> |
| 63 | + </div> |
| 64 | + </div> |
| 65 | + <div className="flex items-center gap-2"> |
| 66 | + <Button |
| 67 | + size="icon" |
| 68 | + variant="ghost" |
| 69 | + onClick={() => handleCopy(record.key)} |
| 70 | + className="cursor-pointer hover:bg-blue-100 text-blue-500 hover:text-blue-600" |
| 71 | + > |
| 72 | + <Clipboard size={16} /> |
| 73 | + </Button> |
| 74 | + <Button |
| 75 | + size="icon" |
| 76 | + variant="ghost" |
| 77 | + onClick={() => handleDeleteClick(record)} |
| 78 | + className="cursor-pointer hover:bg-red-100 text-red-400 hover:text-red-500" |
| 79 | + > |
| 80 | + <Trash size={16} /> |
| 81 | + </Button> |
| 82 | + </div> |
| 83 | + </div> |
| 84 | + ))} |
| 85 | + </div> |
| 86 | + </div> |
| 87 | + <KeyDeleteConfirm |
| 88 | + open={openDelete} |
| 89 | + onCancel={handleDeleteCancel} |
| 90 | + selectedKey={selectedKey} |
| 91 | + /> |
| 92 | + </> |
| 93 | + ); |
| 94 | +}; |
| 95 | + |
| 96 | +export default Keys; |
0 commit comments