Spaces:
Sleeping
Sleeping
File size: 4,879 Bytes
d97b8f9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | import React from 'react';
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert';
import { Button } from '@/components/ui/button';
import { Card, CardContent } from '@/components/ui/card';
import { AlertTriangle, RefreshCw, XCircle, AlertCircle } from 'lucide-react';
interface ErrorDisplayProps {
error: Error | string | null;
title?: string;
onRetry?: () => void;
variant?: 'alert' | 'card' | 'inline';
className?: string;
}
/**
* Reusable error display component with retry action
*/
export const ErrorDisplay: React.FC<ErrorDisplayProps> = ({
error,
title = 'Error',
onRetry,
variant = 'alert',
className = '',
}) => {
if (!error) return null;
const errorMessage = typeof error === 'string' ? error : error.message;
const ErrorContent = () => (
<>
<div className="flex items-start gap-3">
<AlertTriangle className="h-5 w-5 mt-0.5" />
<div className="flex-1">
<AlertTitle className="mb-2">{title}</AlertTitle>
<AlertDescription>
<p className="text-sm mb-3">{errorMessage}</p>
{onRetry && (
<Button
variant="outline"
size="sm"
onClick={onRetry}
className="flex items-center gap-2"
>
<RefreshCw className="h-4 w-4" />
Try Again
</Button>
)}
</AlertDescription>
</div>
</div>
</>
);
if (variant === 'card') {
return (
<Card className={className}>
<CardContent className="pt-6">
<Alert variant="destructive">
<ErrorContent />
</Alert>
</CardContent>
</Card>
);
}
if (variant === 'inline') {
return (
<div className={`bg-destructive/15 border border-destructive/20 rounded-md p-4 ${className}`}>
<div className="flex items-start gap-3">
<XCircle className="h-5 w-5 text-destructive mt-0.5" />
<div className="flex-1">
<p className="text-sm text-destructive font-medium mb-2">{title}</p>
<p className="text-sm text-destructive/90 mb-3">{errorMessage}</p>
{onRetry && (
<Button
variant="outline"
size="sm"
onClick={onRetry}
className="flex items-center gap-2 border-destructive/30 hover:bg-destructive/10"
>
<RefreshCw className="h-4 w-4" />
Try Again
</Button>
)}
</div>
</div>
</div>
);
}
return (
<Alert variant="destructive" className={className}>
<ErrorContent />
</Alert>
);
};
interface FormErrorDisplayProps {
errors: Record<string, string>;
className?: string;
}
/**
* Display form validation errors
*/
export const FormErrorDisplay: React.FC<FormErrorDisplayProps> = ({
errors,
className = '',
}) => {
const errorEntries = Object.entries(errors);
if (errorEntries.length === 0) return null;
return (
<div
className={`bg-destructive/15 border border-destructive/20 rounded-md p-4 ${className}`}
role="alert"
aria-live="polite"
>
<div className="flex items-start gap-3">
<AlertCircle className="h-5 w-5 text-destructive mt-0.5 flex-shrink-0" />
<div className="flex-1">
<p className="text-sm text-destructive font-medium mb-2">
Please fix the following errors:
</p>
<ul className="list-disc list-inside space-y-1">
{errorEntries.map(([field, message]) => (
<li key={field} className="text-sm text-destructive/90">
{message}
</li>
))}
</ul>
</div>
</div>
</div>
);
};
interface EmptyStateProps {
icon?: React.ReactNode;
title: string;
description?: string;
action?: {
label: string;
onClick: () => void;
};
className?: string;
}
/**
* Empty state display component
*/
export const EmptyState: React.FC<EmptyStateProps> = ({
icon,
title,
description,
action,
className = '',
}) => {
return (
<div className={`flex flex-col items-center justify-center py-12 text-center ${className}`}>
{icon && <div className="mb-4 text-muted-foreground">{icon}</div>}
<h3 className="text-lg font-semibold mb-2">{title}</h3>
{description && <p className="text-sm text-muted-foreground mb-4 max-w-md">{description}</p>}
{action && (
<Button onClick={action.onClick} variant="outline">
{action.label}
</Button>
)}
</div>
);
};
export default ErrorDisplay;
|