Download frontend/src/components/admin/admin-user-toggle.tsx from new-arc/vector: direct link, hf CLI and curl.
- Browser
- Download file 1.76 kB
-
https://huggingface.co/spaces/new-arc/vector/resolve/main/frontend/src/components/admin/admin-user-toggle.tsx
- Command line
-
hf download hf://spaces/new-arc/vector/frontend/src/components/admin/admin-user-toggle.tsx
-
curl -L -o admin-user-toggle.tsx https://huggingface.co/spaces/new-arc/vector/resolve/main/frontend/src/components/admin/admin-user-toggle.tsx
1.76 kB
| "use client"; | |
| import { useState } from "react"; | |
| import { useRouter } from "next/navigation"; | |
| import { useTranslations } from "next-intl"; | |
| import { Button } from "@/components/ui/button"; | |
| interface AdminUserToggleProps { | |
| userId: string; | |
| isAdmin: boolean; | |
| isCurrentUser: boolean; | |
| } | |
| export function AdminUserToggle({ userId, isAdmin, isCurrentUser }: AdminUserToggleProps) { | |
| const t = useTranslations("admin"); | |
| const [isLoading, setIsLoading] = useState(false); | |
| const [error, setError] = useState<string | null>(null); | |
| const router = useRouter(); | |
| const handleToggle = async () => { | |
| if (isCurrentUser && isAdmin) { | |
| return; | |
| } | |
| setError(null); | |
| setIsLoading(true); | |
| try { | |
| const response = await fetch(`/api/admin/users/${userId}/admin`, { | |
| method: "PATCH", | |
| headers: { | |
| "Content-Type": "application/json", | |
| }, | |
| body: JSON.stringify({ is_admin: !isAdmin }), | |
| }); | |
| if (!response.ok) { | |
| const data = await response.json().catch(() => ({})); | |
| throw new Error(data.error || t("users.updateFailed")); | |
| } | |
| router.refresh(); | |
| } catch (toggleError) { | |
| setError(toggleError instanceof Error ? toggleError.message : t("users.updateFailed")); | |
| } finally { | |
| setIsLoading(false); | |
| } | |
| }; | |
| return ( | |
| <div className="flex items-center justify-end gap-2"> | |
| <Button | |
| variant={isAdmin ? "outline" : "default"} | |
| size="sm" | |
| onClick={handleToggle} | |
| disabled={isLoading || (isCurrentUser && isAdmin)} | |
| > | |
| {isLoading ? t("savingDots") : isAdmin ? t("users.removeAdmin") : t("users.makeAdmin")} | |
| </Button> | |
| {error && <span className="text-xs text-red-600">{error}</span>} | |
| </div> | |
| ); | |
| } | |