All files / src/features/dashboard/components/shared dashboard-state-renderer.tsx

100% Statements 18/18
100% Branches 22/22
100% Functions 4/4
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                      4x   38x                         4x                   4x   12x                                                                               4x               4x   8x                                     4x                                 4x                           41x 34x     7x 3x                   4x 2x                 2x       4x  
import { CardTitle } from '@/components/card';
import { Text } from '@/components/text';
import { Heading } from '@/components/heading';
import type { ReactNode } from 'react';
import { memo } from 'react';
 
interface LoadingStateProps {
  message?: string;
  className?: string;
}
 
export const LoadingState = memo<LoadingStateProps>(
  ({ message = 'Loading data...', className = '' }) => {
    return (
      <div className={`w-full flex items-center justify-center ${className}`}>
        <div className="text-center">
          <div className="animate-spin rounded-full h-6 w-6 border-b-2 border-primary mx-auto mb-2"></div>
          <Text size="sm" variant="muted">
            {message}
          </Text>
        </div>
      </div>
    );
  }
);
 
LoadingState.displayName = 'LoadingState';
 
interface ErrorStateProps {
  title?: string;
  error: string;
  onRetry?: () => void;
  className?: string;
  showTitle?: boolean;
}
 
export const ErrorState = memo<ErrorStateProps>(
  ({ title = 'Error', error, onRetry, className, showTitle = true }) => {
    return (
      <>
        {showTitle && title && (
          <CardTitle className="pt-6 pl-6">{title}</CardTitle>
        )}
        <div className={`p-6 flex items-center justify-center ${className}`}>
          <div className="text-center max-w-md">
            <div className="mb-3">
              <svg
                className="w-12 h-12 text-destructive mx-auto"
                fill="none"
                stroke="currentColor"
                viewBox="0 0 24 24"
              >
                <path
                  strokeLinecap="round"
                  strokeLinejoin="round"
                  strokeWidth={2}
                  d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.864-.833-2.634 0L4.098 16.5c-.77.833.192 2.5 1.732 2.5z"
                />
              </svg>
            </div>
            <Text size="sm" variant="destructive" className="mb-3">
              {error}
            </Text>
            {onRetry && (
              <button
                onClick={onRetry}
                className="px-3 py-1.5 bg-primary text-primary-foreground text-sm rounded hover:bg-primary/90 transition-colors"
              >
                Retry
              </button>
            )}
          </div>
        </div>
      </>
    );
  }
);
 
ErrorState.displayName = 'ErrorState';
 
interface EmptyStateProps {
  title?: string;
  message: string;
  className?: string;
}
 
export const EmptyState = memo<EmptyStateProps>(
  ({ title, message, className = 'h-64' }) => {
    return (
      <div className="bg-card rounded-lg border border-border shadow-sm">
        {title && (
          <div className="p-6 border-b border-border">
            <Heading as="h3" size="h4">
              {title}
            </Heading>
          </div>
        )}
        <div className={`flex items-center justify-center ${className}`}>
          <div className="text-center">
            <Text variant="muted">{message}</Text>
          </div>
        </div>
      </div>
    );
  }
);
 
EmptyState.displayName = 'EmptyState';
 
interface DashboardStateRendererProps<T> {
  loading: boolean;
  error: string | null;
  data: T;
  isEmpty?: (data: T) => boolean;
  onRetry?: () => void;
  loadingTitle?: string;
  loadingMessage?: string;
  errorTitle?: string;
  emptyTitle?: string;
  emptyMessage?: string;
  className?: string;
  children: (data: T) => ReactNode;
}
 
export const DashboardStateRenderer = memo(
  <T,>({
    loading,
    error,
    data,
    isEmpty,
    onRetry,
    loadingMessage,
    errorTitle,
    emptyTitle,
    emptyMessage,
    className,
    children,
  }: DashboardStateRendererProps<T>) => {
    if (loading) {
      return <LoadingState message={loadingMessage} className={className} />;
    }
 
    if (error) {
      return (
        <ErrorState
          title={errorTitle}
          error={error}
          onRetry={onRetry}
          className={className}
        />
      );
    }
 
    if (isEmpty && isEmpty(data)) {
      return (
        <EmptyState
          title={emptyTitle}
          message={emptyMessage || 'No data available'}
          className={className}
        />
      );
    }
 
    return <>{children(data)}</>;
  }
);
 
DashboardStateRenderer.displayName = 'DashboardStateRenderer';