MicProtocol / apps /web /src /components /ui /Alert.tsx
Benaah's picture
Upload folder using huggingface_hub
fb12607 verified
Raw History Blame Contribute Delete
10.6 kB
"use client";
import React, { useState, useRef, useEffect } from "react";
import {
AlertCircle,
AlertTriangle,
CheckCircle2,
Info,
X,
ChevronDown,
ChevronUp,
} from "lucide-react";
export type AlertTone = "neutral" | "info" | "success" | "warning" | "danger" | "error";
export type AlertVariant = "subtle" | "outline" | "solid" | "glass";
export interface AlertProps {
title?: React.ReactNode;
description?: React.ReactNode;
children?: React.ReactNode;
tone?: AlertTone;
variant?: AlertVariant;
/** Pass custom icon, or false to suppress the icon */
icon?: React.ReactNode | false;
/** Whether the alert can be dismissed by the user */
dismissible?: boolean;
/** Callback fired after the dismiss animation completes */
onDismiss?: () => void;
/** Action slot (e.g. Retry button, Upgrade link) */
action?: React.ReactNode;
/** Technical details or stack trace for collapsible drawer */
details?: React.ReactNode | string;
className?: string;
style?: React.CSSProperties;
}
const TONE_CONFIGS: Record<
string,
{
icon: typeof AlertCircle;
color: string;
bgSubtle: string;
borderSubtle: string;
glow: string;
}
> = {
danger: {
icon: AlertCircle,
color: "var(--color-error, #EF4444)",
bgSubtle: "rgba(239, 68, 68, 0.08)",
borderSubtle: "rgba(239, 68, 68, 0.25)",
glow: "0 0 20px -4px rgba(239, 68, 68, 0.18)",
},
error: {
icon: AlertCircle,
color: "var(--color-error, #EF4444)",
bgSubtle: "rgba(239, 68, 68, 0.08)",
borderSubtle: "rgba(239, 68, 68, 0.25)",
glow: "0 0 20px -4px rgba(239, 68, 68, 0.18)",
},
warning: {
icon: AlertTriangle,
color: "var(--color-warning, #F59E0B)",
bgSubtle: "rgba(245, 158, 11, 0.08)",
borderSubtle: "rgba(245, 158, 11, 0.25)",
glow: "0 0 20px -4px rgba(245, 158, 11, 0.18)",
},
info: {
icon: Info,
color: "var(--color-info, #3B82F6)",
bgSubtle: "rgba(59, 130, 246, 0.08)",
borderSubtle: "rgba(59, 130, 246, 0.25)",
glow: "0 0 20px -4px rgba(59, 130, 246, 0.18)",
},
success: {
icon: CheckCircle2,
color: "var(--color-success, #10B981)",
bgSubtle: "rgba(16, 185, 129, 0.08)",
borderSubtle: "rgba(16, 185, 129, 0.25)",
glow: "0 0 20px -4px rgba(16, 185, 129, 0.18)",
},
neutral: {
icon: Info,
color: "var(--text-secondary, #A7A3B4)",
bgSubtle: "var(--bg-elevated, #17151F)",
borderSubtle: "var(--border-color, #272430)",
glow: "none",
},
};
/**
* EasyUI-inspired Alert component.
* Features smooth spring-like entrance, collapse dismissal, tone-aware styling,
* action slot, and expandable diagnostics.
*/
export function Alert({
title,
description,
children,
tone = "neutral",
variant = "subtle",
icon,
dismissible = false,
onDismiss,
action,
details,
className = "",
style,
}: AlertProps) {
const [isDismissing, setIsDismissing] = useState(false);
const [isDismissed, setIsDismissed] = useState(false);
const [showDetails, setShowDetails] = useState(false);
const alertRef = useRef<HTMLDivElement>(null);
const normalizedTone = tone === "error" ? "danger" : tone;
const config = TONE_CONFIGS[normalizedTone] || TONE_CONFIGS.neutral;
const IconComponent = config.icon;
const handleDismiss = () => {
setIsDismissing(true);
setTimeout(() => {
setIsDismissed(true);
onDismiss?.();
}, 280);
};
if (isDismissed) {
return null;
}
const isUrgent = normalizedTone === "danger" || normalizedTone === "warning";
let bg = config.bgSubtle;
let borderColor = config.borderSubtle;
let boxShadow = config.glow;
if (variant === "outline") {
bg = "transparent";
} else if (variant === "solid") {
bg = config.color;
borderColor = config.color;
} else if (variant === "glass") {
bg = "rgba(18, 16, 22, 0.65)";
boxShadow = `${config.glow}, inset 0 1px 0 rgba(255, 255, 255, 0.08)`;
}
return (
<div
ref={alertRef}
role={isUrgent ? "alert" : "status"}
aria-live={isUrgent ? "assertive" : "polite"}
className={`alert-component ${className}`}
style={{
position: "relative",
display: "flex",
flexDirection: "column",
background: bg,
backdropFilter: variant === "glass" ? "blur(12px)" : undefined,
WebkitBackdropFilter: variant === "glass" ? "blur(12px)" : undefined,
border: `1px solid ${borderColor}`,
borderRadius: "var(--radius-md, 4px)",
boxShadow,
padding: "var(--space-3, 0.75rem) var(--space-4, 1rem)",
overflow: "hidden",
transition: "all 0.28s cubic-bezier(0.16, 1, 0.3, 1)",
opacity: isDismissing ? 0 : 1,
transform: isDismissing ? "scale(0.97) translateY(-4px)" : "scale(1) translateY(0)",
maxHeight: isDismissing ? 0 : 600,
...style,
}}
>
{/* Top accent glow line */}
<div
style={{
position: "absolute",
top: 0,
left: 0,
right: 0,
height: 2,
background: `linear-gradient(90deg, transparent, ${config.color}, transparent)`,
opacity: 0.8,
}}
/>
<div
style={{
display: "flex",
alignItems: "flex-start",
gap: "var(--space-3, 0.75rem)",
width: "100%",
}}
>
{/* Tone Icon */}
{icon !== false && (
<div
style={{
flexShrink: 0,
display: "flex",
alignItems: "center",
justifyContent: "center",
width: 26,
height: 26,
borderRadius: "var(--radius-sm, 2px)",
background: `color-mix(in srgb, ${config.color} 15%, transparent)`,
color: config.color,
marginTop: 1,
}}
>
{icon ? icon : <IconComponent size={16} strokeWidth={2.2} />}
</div>
)}
{/* Content */}
<div style={{ flex: 1, minWidth: 0 }}>
{title && (
<div
style={{
fontFamily: "var(--font-sans, inherit)",
fontWeight: 600,
fontSize: "var(--text-sm, 0.875rem)",
color: "var(--text-primary, #F6F4F8)",
marginBottom: description || children ? "var(--space-1, 0.25rem)" : 0,
display: "flex",
alignItems: "center",
gap: "var(--space-2, 0.5rem)",
}}
>
{title}
</div>
)}
{description && (
<div
style={{
fontSize: "var(--text-sm, 0.875rem)",
color: "var(--text-secondary, #A7A3B4)",
lineHeight: 1.5,
}}
>
{description}
</div>
)}
{children && (
<div
style={{
fontSize: "var(--text-sm, 0.875rem)",
color: "var(--text-secondary, #A7A3B4)",
marginTop: title || description ? "var(--space-1, 0.25rem)" : 0,
lineHeight: 1.5,
}}
>
{children}
</div>
)}
{/* Action Slot & Expandable Details Button */}
{(action || details) && (
<div
style={{
display: "flex",
flexWrap: "wrap",
alignItems: "center",
gap: "var(--space-3, 0.75rem)",
marginTop: "var(--space-2, 0.5rem)",
}}
>
{action}
{details && (
<button
type="button"
onClick={() => setShowDetails(!showDetails)}
style={{
background: "transparent",
border: "none",
color: "var(--text-tertiary, #6D697C)",
fontSize: "var(--text-xs, 0.75rem)",
cursor: "pointer",
display: "inline-flex",
alignItems: "center",
gap: 4,
padding: 0,
textDecoration: "underline",
textUnderlineOffset: 3,
}}
>
{showDetails ? (
<>
Hide technical details <ChevronUp size={12} />
</>
) : (
<>
Show technical details <ChevronDown size={12} />
</>
)}
</button>
)}
</div>
)}
</div>
{/* Dismiss Button */}
{dismissible && (
<button
type="button"
onClick={handleDismiss}
aria-label="Dismiss alert"
style={{
flexShrink: 0,
background: "transparent",
border: "none",
color: "var(--text-tertiary, #6D697C)",
cursor: "pointer",
padding: 4,
borderRadius: "var(--radius-sm, 2px)",
display: "flex",
alignItems: "center",
justifyContent: "center",
transition: "color 0.15s ease",
}}
onMouseEnter={(e) => {
e.currentTarget.style.color = "var(--text-primary, #F6F4F8)";
}}
onMouseLeave={(e) => {
e.currentTarget.style.color = "var(--text-tertiary, #6D697C)";
}}
>
<X size={15} />
</button>
)}
</div>
{/* Expandable Technical Details Drawer */}
{details && showDetails && (
<div
style={{
marginTop: "var(--space-3, 0.75rem)",
padding: "var(--space-2, 0.5rem) var(--space-3, 0.75rem)",
background: "rgba(0, 0, 0, 0.35)",
border: "1px solid var(--border-color-subtle, #1C1924)",
borderRadius: "var(--radius-sm, 2px)",
fontFamily: "var(--font-mono, monospace)",
fontSize: "var(--text-xs, 0.75rem)",
color: "var(--text-secondary, #A7A3B4)",
whiteSpace: "pre-wrap",
wordBreak: "break-all",
maxHeight: 180,
overflowY: "auto",
}}
>
{typeof details === "string" ? details : JSON.stringify(details, null, 2)}
</div>
)}
</div>
);
}