"use client"; import { useState } from "react"; import { toast } from "sonner"; import { Check, Eye, Loader2, Lock, Shield } from "lucide-react"; import { Button } from "@/components/ui/button"; import { useI18n, type MessageKey } from "@/i18n/i18n-context"; import { useAttachPolicy } from "@/hooks/use-attach-policy"; import { useDetachPolicy } from "@/hooks/use-detach-policy"; import type { RamPolicy, PermissionPreset } from "@/types/api-key"; const OTS_SYSTEM_POLICIES = [ "AliyunOTSFullAccess", "AliyunOTSReadOnlyAccess", "AliyunOTSWriteOnlyAccess", ] as const; interface PresetDef { key: PermissionPreset; labelKey: MessageKey; descriptionKey: MessageKey; icon: typeof Eye; attach: string[]; } const PRESETS: PresetDef[] = [ { key: "readonly", labelKey: "apiKeys.presets.readonlyLabel", descriptionKey: "apiKeys.presets.readonlyDesc", icon: Eye, attach: ["AliyunOTSReadOnlyAccess"], }, { key: "full", labelKey: "apiKeys.presets.fullLabel", descriptionKey: "apiKeys.presets.fullDesc", icon: Shield, attach: ["AliyunOTSFullAccess"], }, ]; function detectActivePreset( policies: RamPolicy[] | undefined, ): PermissionPreset | null { if (!policies) return null; const otsPolicies = new Set( policies .filter((p) => OTS_SYSTEM_POLICIES.includes( p.policyName as (typeof OTS_SYSTEM_POLICIES)[number], ), ) .map((p) => p.policyName), ); if (otsPolicies.has("AliyunOTSFullAccess")) return "full"; if ( otsPolicies.has("AliyunOTSReadOnlyAccess") && !otsPolicies.has("AliyunOTSWriteOnlyAccess") ) return "readonly"; return null; } interface PermissionPresetsProps { serviceCredentialId: string; userName: string; policies: RamPolicy[] | undefined; } export function PermissionPresets({ serviceCredentialId, userName, policies, }: PermissionPresetsProps) { const { t } = useI18n(); const attachMutation = useAttachPolicy(); const detachMutation = useDetachPolicy(); const [applying, setApplying] = useState( null, ); const activePreset = detectActivePreset(policies); async function applyPreset(preset: PresetDef) { if (activePreset === preset.key) { await cancelPreset(); return; } setApplying(preset.key); try { const currentOtsPolicies = (policies ?? []).filter((p) => OTS_SYSTEM_POLICIES.includes( p.policyName as (typeof OTS_SYSTEM_POLICIES)[number], ), ); for (const p of currentOtsPolicies) { if (!preset.attach.includes(p.policyName)) { await detachMutation.mutateAsync({ serviceCredentialId, userName, policyName: p.policyName, policyType: p.policyType, }); } } const alreadyAttached = new Set( currentOtsPolicies.map((p) => p.policyName), ); for (const policyName of preset.attach) { if (!alreadyAttached.has(policyName)) { await attachMutation.mutateAsync({ serviceCredentialId, userName, policyName, policyType: "System", }); } } toast.success( t("apiKeys.presets.applySuccess", { preset: t(preset.labelKey) }), ); } catch (err) { toast.error( err instanceof Error ? err.message : t("apiKeys.presets.applyFailed"), ); } finally { setApplying(null); } } async function cancelPreset() { setApplying("cancel"); try { const currentOtsPolicies = (policies ?? []).filter((p) => OTS_SYSTEM_POLICIES.includes( p.policyName as (typeof OTS_SYSTEM_POLICIES)[number], ), ); for (const p of currentOtsPolicies) { await detachMutation.mutateAsync({ serviceCredentialId, userName, policyName: p.policyName, policyType: p.policyType, }); } toast.success(t("apiKeys.presets.cancelSuccess")); } catch (err) { toast.error( err instanceof Error ? err.message : t("apiKeys.presets.cancelFailed"), ); } finally { setApplying(null); } } return (
{PRESETS.map((preset) => { const isActive = activePreset === preset.key; const isApplying = applying === preset.key; const isCanceling = applying === "cancel" && isActive; const Icon = preset.icon; return ( ); })}
); }