"use client"; import { useState } from "react"; import { toast } from "sonner"; import { Loader2 } from "lucide-react"; import { Switch } from "@/components/ui/switch"; import { useI18n } from "@/i18n/i18n-context"; import { useAttachPolicy } from "@/hooks/use-attach-policy"; import { useDetachPolicy } from "@/hooks/use-detach-policy"; import type { RamPolicy } from "@/types/api-key"; const BEARER_POLICY_NAME = "ts-ots-bearer-token-access"; interface BearerTokenToggleProps { serviceCredentialId: string; userName: string; policies: RamPolicy[] | undefined; } export function BearerTokenToggle({ serviceCredentialId, userName, policies, }: BearerTokenToggleProps) { const { t } = useI18n(); const attachMutation = useAttachPolicy(); const detachMutation = useDetachPolicy(); const [pending, setPending] = useState(false); const isAttached = policies?.some((p) => p.policyName === BEARER_POLICY_NAME) ?? false; async function handleToggle(next: boolean) { setPending(true); try { if (next) { await attachMutation.mutateAsync({ serviceCredentialId, userName, policyName: BEARER_POLICY_NAME, policyType: "Custom", }); toast.success(t("apiKeys.bearer.enableSuccess")); } else { await detachMutation.mutateAsync({ serviceCredentialId, userName, policyName: BEARER_POLICY_NAME, policyType: "Custom", }); toast.success(t("apiKeys.bearer.disableSuccess")); } } catch (err) { toast.error( err instanceof Error ? err.message : t("apiKeys.bearer.operationFailed"), ); } finally { setPending(false); } } return (