import { useState, type ReactNode, type SyntheticEvent } from "react"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/components/ui/alert-dialog"; import { cn } from "@/lib/utils"; export type FeatureGateLabels = { title: string; description: string; cancel: string; confirm: string; }; type FeatureGateProps = { enabled: boolean; labels: FeatureGateLabels; onEnable: () => void | Promise; className?: string; children: ReactNode; }; /** 功能关闭时拦截子区域交互,提示是否开启总开关 */ export function FeatureGate({ enabled, labels, onEnable, className, children, }: FeatureGateProps) { const [open, setOpen] = useState(false); const intercept = (e: SyntheticEvent) => { if (enabled) return; e.preventDefault(); e.stopPropagation(); setOpen(true); }; const handleConfirm = () => { void Promise.resolve(onEnable()).then(() => setOpen(false)); }; return ( <>
{children}
{labels.title} {labels.description} {labels.cancel} {labels.confirm} ); }