|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { Button, buttonVariants } from "@/components/ui/button"; |
| 4 | +import { |
| 5 | + Dialog, |
| 6 | + DialogClose, |
| 7 | + DialogContent, |
| 8 | + DialogDescription, |
| 9 | + DialogFooter, |
| 10 | + DialogHeader, |
| 11 | + DialogTitle, |
| 12 | + DialogTrigger, |
| 13 | +} from "@/components/ui/dialog"; |
| 14 | +import { DropdownMenuItem } from "@/components/ui/dropdown-menu"; |
| 15 | +import { useMutation } from "@apollo/client/react"; |
| 16 | +import { Copy, Key, X } from "lucide-react"; |
| 17 | +import { useState, useTransition } from "react"; |
| 18 | +import { toast } from "sonner"; |
| 19 | +import revokeSpecificToken from "../_actions/revoke-token"; |
| 20 | +import { USER_IMPERSONATE_MUTATION } from "./mutation"; |
| 21 | + |
| 22 | +export function ImpersonateUserDropdownTrigger({ |
| 23 | + userId, |
| 24 | + userName, |
| 25 | +}: { |
| 26 | + userId: string; |
| 27 | + userName?: string; |
| 28 | +}) { |
| 29 | + const [open, setOpen] = useState(false); |
| 30 | + |
| 31 | + return ( |
| 32 | + <Dialog open={open} onOpenChange={setOpen}> |
| 33 | + <DialogTrigger asChild> |
| 34 | + <DropdownMenuItem |
| 35 | + onClick={(e) => { |
| 36 | + e.preventDefault(); |
| 37 | + setOpen(true); |
| 38 | + }} |
| 39 | + > |
| 40 | + 取得代理憑證 |
| 41 | + </DropdownMenuItem> |
| 42 | + </DialogTrigger> |
| 43 | + |
| 44 | + <ImpersonateUserDialogContent |
| 45 | + userId={userId} |
| 46 | + userName={userName} |
| 47 | + onCompleted={() => setOpen(false)} |
| 48 | + /> |
| 49 | + </Dialog> |
| 50 | + ); |
| 51 | +} |
| 52 | + |
| 53 | +export function ImpersonateUserButtonTrigger({ |
| 54 | + userId, |
| 55 | + userName, |
| 56 | +}: { |
| 57 | + userId: string; |
| 58 | + userName?: string; |
| 59 | +}) { |
| 60 | + const [open, setOpen] = useState(false); |
| 61 | + |
| 62 | + return ( |
| 63 | + <Dialog open={open} onOpenChange={setOpen}> |
| 64 | + <DialogTrigger className={buttonVariants({ variant: "outline" })}> |
| 65 | + <Key className="h-4 w-4" /> |
| 66 | + 取得代理憑證 |
| 67 | + </DialogTrigger> |
| 68 | + |
| 69 | + <ImpersonateUserDialogContent |
| 70 | + userId={userId} |
| 71 | + userName={userName} |
| 72 | + onCompleted={() => setOpen(false)} |
| 73 | + /> |
| 74 | + </Dialog> |
| 75 | + ); |
| 76 | +} |
| 77 | + |
| 78 | +function ImpersonateUserDialogContent({ |
| 79 | + userId, |
| 80 | + userName, |
| 81 | + onCompleted, |
| 82 | +}: { |
| 83 | + userId: string; |
| 84 | + userName?: string; |
| 85 | + onCompleted: () => void; |
| 86 | +}) { |
| 87 | + const [token, setToken] = useState<string | null>(null); |
| 88 | + const [isPending, startTransition] = useTransition(); |
| 89 | + |
| 90 | + const [impersonateUser, { loading }] = useMutation(USER_IMPERSONATE_MUTATION, { |
| 91 | + onError(error) { |
| 92 | + toast.error("無法取得代理操作憑證", { |
| 93 | + description: error.message, |
| 94 | + }); |
| 95 | + }, |
| 96 | + |
| 97 | + onCompleted(data) { |
| 98 | + setToken(data.impersonateUser); |
| 99 | + toast.success("已產生代理操作憑證"); |
| 100 | + }, |
| 101 | + }); |
| 102 | + |
| 103 | + const handleImpersonate = () => { |
| 104 | + impersonateUser({ |
| 105 | + variables: { |
| 106 | + userID: userId, |
| 107 | + }, |
| 108 | + }); |
| 109 | + }; |
| 110 | + |
| 111 | + const handleCopy = async () => { |
| 112 | + if (!token) return; |
| 113 | + |
| 114 | + try { |
| 115 | + await navigator.clipboard.writeText(token); |
| 116 | + toast.success("已將憑證複製到剪貼簿"); |
| 117 | + } catch (error) { |
| 118 | + toast.error("複製憑證失敗", { |
| 119 | + description: error instanceof Error ? error.message : undefined, |
| 120 | + }); |
| 121 | + } |
| 122 | + }; |
| 123 | + |
| 124 | + const handleRevoke = () => { |
| 125 | + if (!token) return; |
| 126 | + |
| 127 | + startTransition(async () => { |
| 128 | + try { |
| 129 | + await revokeSpecificToken(token); |
| 130 | + toast.success("已撤銷憑證"); |
| 131 | + setToken(null); |
| 132 | + onCompleted(); |
| 133 | + } catch (error) { |
| 134 | + toast.error("撤銷憑證失敗", { |
| 135 | + description: error instanceof Error ? error.message : undefined, |
| 136 | + }); |
| 137 | + } |
| 138 | + }); |
| 139 | + }; |
| 140 | + |
| 141 | + return ( |
| 142 | + <DialogContent className="sm:max-w-md"> |
| 143 | + <DialogHeader> |
| 144 | + <DialogTitle>取得代理憑證</DialogTitle> |
| 145 | + <DialogDescription> |
| 146 | + 產生代理憑證,以在 API 層面代理使用者執行動作。代理憑證有效期為 8 小時。 |
| 147 | + </DialogDescription> |
| 148 | + </DialogHeader> |
| 149 | + |
| 150 | + <div className="space-y-4"> |
| 151 | + {!token |
| 152 | + ? ( |
| 153 | + <div className="py-4 text-center"> |
| 154 | + <p className="mb-4 text-sm text-muted-foreground"> |
| 155 | + 點選下方按鈕以產生代理憑證 |
| 156 | + </p> |
| 157 | + <Button |
| 158 | + onClick={handleImpersonate} |
| 159 | + disabled={loading} |
| 160 | + className="w-full" |
| 161 | + > |
| 162 | + <Key className="mr-2 h-4 w-4" /> |
| 163 | + {loading ? "產生中……" : "產生代理憑證"} |
| 164 | + </Button> |
| 165 | + </div> |
| 166 | + ) |
| 167 | + : ( |
| 168 | + <div className="space-y-3"> |
| 169 | + <div className="rounded-md bg-muted p-3"> |
| 170 | + <p className="mb-2 text-xs font-medium text-muted-foreground"> |
| 171 | + 代理憑證 |
| 172 | + </p> |
| 173 | + <code className="font-mono text-sm break-all"> |
| 174 | + {token} |
| 175 | + </code> |
| 176 | + </div> |
| 177 | + |
| 178 | + <div className="flex gap-2"> |
| 179 | + <Button |
| 180 | + onClick={handleCopy} |
| 181 | + variant="outline" |
| 182 | + className="flex-1" |
| 183 | + > |
| 184 | + <Copy className="mr-2 h-4 w-4" /> |
| 185 | + 複製 |
| 186 | + </Button> |
| 187 | + <Button |
| 188 | + onClick={handleRevoke} |
| 189 | + variant="destructive" |
| 190 | + disabled={isPending} |
| 191 | + className="flex-1" |
| 192 | + > |
| 193 | + <X className="mr-2 h-4 w-4" /> |
| 194 | + {isPending ? "撤銷中……" : "撤銷"} |
| 195 | + </Button> |
| 196 | + </div> |
| 197 | + </div> |
| 198 | + )} |
| 199 | + </div> |
| 200 | + |
| 201 | + <DialogFooter> |
| 202 | + <DialogClose asChild> |
| 203 | + <Button variant="ghost">關閉</Button> |
| 204 | + </DialogClose> |
| 205 | + </DialogFooter> |
| 206 | + </DialogContent> |
| 207 | + ); |
| 208 | +} |
0 commit comments