|
| 1 | +import type { ReactNode } from "react"; |
| 2 | +import { LoaderCircle } from "lucide-react"; |
| 3 | + |
| 4 | +import { cn } from "@/lib/utils"; |
| 5 | + |
| 6 | +export type NodeStatus = "loading" | "success" | "error" | "initial"; |
| 7 | + |
| 8 | +export type NodeStatusVariant = "overlay" | "border"; |
| 9 | + |
| 10 | +export type NodeStatusIndicatorProps = { |
| 11 | + status?: NodeStatus; |
| 12 | + variant?: NodeStatusVariant; |
| 13 | + children: ReactNode; |
| 14 | + className?: string; |
| 15 | +}; |
| 16 | + |
| 17 | +export const SpinnerLoadingIndicator = ({ |
| 18 | + children, |
| 19 | +}: { |
| 20 | + children: ReactNode; |
| 21 | +}) => { |
| 22 | + return ( |
| 23 | + <div className="relative"> |
| 24 | + <StatusBorder className="border-blue-700/40">{children}</StatusBorder> |
| 25 | + |
| 26 | + <div className="bg-background/50 absolute inset-0 z-50 rounded-[9px] backdrop-blur-xs" /> |
| 27 | + <div className="absolute inset-0 z-50"> |
| 28 | + <span className="absolute top-[calc(50%-1.25rem)] left-[calc(50%-1.25rem)] inline-block h-10 w-10 animate-ping rounded-full bg-blue-700/20" /> |
| 29 | + |
| 30 | + <LoaderCircle className="absolute top-[calc(50%-0.75rem)] left-[calc(50%-0.75rem)] size-6 animate-spin text-blue-700" /> |
| 31 | + </div> |
| 32 | + </div> |
| 33 | + ); |
| 34 | +}; |
| 35 | + |
| 36 | +export const BorderLoadingIndicator = ({ |
| 37 | + children, |
| 38 | + className, |
| 39 | +}: { |
| 40 | + className?: string; |
| 41 | + children: ReactNode; |
| 42 | +}) => { |
| 43 | + return ( |
| 44 | + <> |
| 45 | + <div className="absolute -top-[2px] -left-[2px] h-[calc(100%+4px)] w-[calc(100%+4px)]"> |
| 46 | + <style> |
| 47 | + {` |
| 48 | + @keyframes spin { |
| 49 | + from { transform: translate(-50%, -50%) rotate(0deg); } |
| 50 | + to { transform: translate(-50%, -50%) rotate(360deg); } |
| 51 | + } |
| 52 | + .spinner { |
| 53 | + animation: spin 2s linear infinite; |
| 54 | + position: absolute; |
| 55 | + left: 50%; |
| 56 | + top: 50%; |
| 57 | + width: 140%; |
| 58 | + aspect-ratio: 1; |
| 59 | + transform-origin: center; |
| 60 | + } |
| 61 | + `} |
| 62 | + </style> |
| 63 | + <div |
| 64 | + className={cn( |
| 65 | + "absolute inset-0 overflow-hidden rounded-sm", |
| 66 | + className, |
| 67 | + )} |
| 68 | + > |
| 69 | + <div className="spinner rounded-full bg-[conic-gradient(from_0deg_at_50%_50%,rgba(42,67,233,0.5)_0deg,rgba(42,138,246,0)_360deg)]" /> |
| 70 | + </div> |
| 71 | + </div> |
| 72 | + {children} |
| 73 | + </> |
| 74 | + ); |
| 75 | +}; |
| 76 | + |
| 77 | +const StatusBorder = ({ |
| 78 | + children, |
| 79 | + className, |
| 80 | +}: { |
| 81 | + children: ReactNode; |
| 82 | + className?: string; |
| 83 | +}) => { |
| 84 | + return ( |
| 85 | + <> |
| 86 | + <div |
| 87 | + className={cn( |
| 88 | + "absolute -top-[2px] -left-[2px] h-[calc(100%+4px)] w-[calc(100%+4px)] rounded-md border-3", |
| 89 | + className, |
| 90 | + )} |
| 91 | + /> |
| 92 | + {children} |
| 93 | + </> |
| 94 | + ); |
| 95 | +}; |
| 96 | + |
| 97 | +export const NodeStatusIndicator = ({ |
| 98 | + status, |
| 99 | + variant = "border", |
| 100 | + children, |
| 101 | + className, |
| 102 | +}: NodeStatusIndicatorProps) => { |
| 103 | + switch (status) { |
| 104 | + case "loading": |
| 105 | + switch (variant) { |
| 106 | + case "overlay": |
| 107 | + return <SpinnerLoadingIndicator>{children}</SpinnerLoadingIndicator>; |
| 108 | + case "border": |
| 109 | + return ( |
| 110 | + <BorderLoadingIndicator className={className}> |
| 111 | + {children} |
| 112 | + </BorderLoadingIndicator> |
| 113 | + ); |
| 114 | + default: |
| 115 | + return <>{children}</>; |
| 116 | + } |
| 117 | + case "success": |
| 118 | + return ( |
| 119 | + <StatusBorder className={cn("border-green-700/50", className)}> |
| 120 | + {children} |
| 121 | + </StatusBorder> |
| 122 | + ); |
| 123 | + case "error": |
| 124 | + return ( |
| 125 | + <StatusBorder className={cn("border-red-700/50", className)}> |
| 126 | + {children} |
| 127 | + </StatusBorder> |
| 128 | + ); |
| 129 | + default: |
| 130 | + return <>{children}</>; |
| 131 | + } |
| 132 | +}; |
0 commit comments