All files / src/components/admin/monitoring ErrorDetailsTab.tsx

83.17% Statements 257/309
58.33% Branches 14/24
75% Functions 6/8
83.17% Lines 257/309

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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 3101x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 45x 45x 45x 45x 45x 1x 65x 65x 65x 65x 65x 1x 21x 21x 21x 1x 1x 1x 1x 1x 1x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 1x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 1x 1x 1x 1x 1x 22x 22x 22x 22x 1x 1x 1x 1x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x   22x 22x 22x 22x 22x 22x 22x 22x 22x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 10x 10x 10x 10x 10x 10x 10x 10x 21x 21x 21x 10x 10x 10x 10x 10x 10x 10x 10x 21x 21x 22x 22x 1x 1x 1x 22x 22x 22x 22x 22x 22x 1x 1x 1x 1x 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 1x 1x 1x 1x 1x                                                                                                 1x 1x  
'use client';
 
import { formatDistanceToNow, format } from 'date-fns';
import { PrioritySelector } from './PrioritySelector';
import { Input } from '@/components/ui';
import type { ErrorPriority, Prisma } from '@prisma/client';
 
type JsonValue = Prisma.JsonValue;
 
/**
 * Error statistic data for display
 */
interface ErrorStat {
  id: number;
  fingerprint: string;
  category: string;
  message: string;
  count: number;
  lastSeen: Date | string;
  firstSeen: Date | string;
  status: string;
  resolvedAt: Date | string | null;
  resolvedBy: number | null;
  assignedTo: number | null;
  assignedAt: Date | string | null;
  priority: ErrorPriority | string;
  snoozedUntil: Date | string | null;
  ignoredReason: string | null;
}
 
/**
 * Recent error log occurrence
 */
interface RecentLog {
  id: number;
  level: string;
  message: string;
  stack: string | null;
  url: string | null;
  metadata: JsonValue;
  createdAt: Date | string;
}
 
export interface ErrorDetailsTabProps {
  /** Error statistic data */
  error: ErrorStat;
  /** Recent occurrences of this error */
  recentLogs: RecentLog[];
  /** Callback when error is updated */
  onUpdate: (updates: Partial<{
    status: string;
    assignedTo: number | null;
    priority: ErrorPriority;
    snoozedUntil: Date | null;
  }>) => void;
  /** Whether updates are in progress */
  isUpdating?: boolean;
  /** Available team members for assignment (optional) */
  teamMembers?: Array<{ id: number; email: string; name?: string }>;
}
 
function formatDate(date: Date | string | null | undefined): string {
  if (!date) return '-';
  const d = typeof date === 'string' ? new Date(date) : date;
  return format(d, 'PPpp');
}
 
function formatRelativeTime(date: Date | string | null | undefined): string {
  if (!date) return '-';
  const d = typeof date === 'string' ? new Date(date) : date;
  return formatDistanceToNow(d, { addSuffix: true });
}
 
function isJsonObject(value: JsonValue): value is Prisma.JsonObject {
  return typeof value === 'object' && value !== null && !Array.isArray(value);
}
 
/**
 * ErrorDetailsTab Component
 *
 * Displays detailed error information and quick action controls.
 */
export function ErrorDetailsTab({
  error,
  recentLogs,
  onUpdate,
  isUpdating = false,
  teamMembers = [],
}: ErrorDetailsTabProps) {
  return (
    <div className="space-y-6" data-testid="error-details-tab">
      {/* Quick Actions */}
      <div className="flex flex-wrap gap-4">
        <PrioritySelector
          label="Priority"
          value={error.priority}
          onChange={(priority) => onUpdate({ priority })}
          disabled={isUpdating}
          className="w-32"
        />
 
        {teamMembers.length > 0 && (
          <div className="w-48">
            <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
              Assigned To
            </label>
            <Input
              as="select"
              value={error.assignedTo?.toString() || ''}
              onChange={(e) =>
                onUpdate({
                  assignedTo: e.target.value ? parseInt(e.target.value, 10) : null,
                })
              }
              disabled={isUpdating}
              size="sm"
            >
              <option value="">Unassigned</option>
              {teamMembers.map((member) => (
                <option key={member.id} value={member.id}>
                  {member.name || member.email}
                </option>
              ))}
            </Input>
          </div>
        )}
      </div>
 
      {/* Error Info Grid */}
      <div className="grid grid-cols-2 md:grid-cols-4 gap-4">
        <InfoItem label="First Seen" value={formatRelativeTime(error.firstSeen)} title={formatDate(error.firstSeen)} />
        <InfoItem label="Last Seen" value={formatRelativeTime(error.lastSeen)} title={formatDate(error.lastSeen)} />
        <InfoItem label="Occurrences" value={error.count.toLocaleString()} />
        <InfoItem label="Category" value={error.category} />
      </div>
 
      {/* Snooze Info */}
      {error.snoozedUntil && (
        <div className="p-3 bg-purple-50 dark:bg-purple-900/30 rounded-lg">
          <p className="text-sm text-purple-700 dark:text-purple-300">
            Snoozed until {formatDate(error.snoozedUntil)}
          </p>
        </div>
      )}
 
      {/* Ignored Reason */}
      {error.ignoredReason && (
        <div className="p-3 bg-gray-50 dark:bg-gray-900/50 rounded-lg">
          <p className="text-sm font-medium text-gray-600 dark:text-gray-400">Ignored Reason:</p>
          <p className="text-sm text-gray-700 dark:text-gray-300">{error.ignoredReason}</p>
        </div>
      )}
 
      {/* Error Message */}
      <div>
        <h4 className="font-medium text-gray-900 dark:text-white mb-2">Error Message</h4>
        <div className="p-3 bg-gray-50 dark:bg-gray-900 rounded-lg">
          <p className="text-sm text-gray-700 dark:text-gray-300 break-words">{error.message}</p>
        </div>
      </div>
 
      {/* SQL Query Display - Prominent for DATABASE errors */}
      {error.category === 'DATABASE' && recentLogs.length > 0 && (
        <QueryDisplay logs={recentLogs} />
      )}
 
      {/* Recent Occurrences */}
      <div>
        <h4 className="font-medium text-gray-900 dark:text-white mb-3">
          Recent Occurrences ({recentLogs.length})
        </h4>
        <div className="space-y-2 max-h-64 overflow-y-auto">
          {recentLogs.map((log) => (
            <div
              key={log.id}
              className="p-3 bg-gray-50 dark:bg-gray-900 rounded-lg border border-gray-200 dark:border-gray-700"
            >
              <div className="flex items-center justify-between mb-2">
                <span className={`text-xs px-2 py-0.5 rounded ${
                  log.level === 'error'
                    ? 'bg-red-100 text-red-700 dark:bg-red-900 dark:text-red-300'
                    : 'bg-yellow-100 text-yellow-700 dark:bg-yellow-900 dark:text-yellow-300'
                }`}>
                  {log.level}
                </span>
                <span className="text-xs text-gray-500 dark:text-gray-400">
                  {formatRelativeTime(log.createdAt)}
                </span>
              </div>
 
              {log.url && (
                <p className="text-xs text-gray-500 dark:text-gray-400 mb-1 truncate" title={log.url}>
                  URL: {log.url}
                </p>
              )}
 
              {log.stack && (
                <details className="mt-2">
                  <summary className="text-xs text-blue-600 dark:text-blue-400 cursor-pointer">
                    Stack trace
                  </summary>
                  <pre className="mt-1 p-2 text-xs bg-gray-100 dark:bg-gray-800 rounded overflow-x-auto max-h-32 text-gray-700 dark:text-gray-300">
                    {log.stack}
                  </pre>
                </details>
              )}
 
              {isJsonObject(log.metadata) && Object.keys(log.metadata).length > 0 && (
                <details className="mt-2">
                  <summary className="text-xs text-blue-600 dark:text-blue-400 cursor-pointer">
                    Metadata
                  </summary>
                  <pre className="mt-1 p-2 text-xs bg-gray-100 dark:bg-gray-800 rounded overflow-x-auto text-gray-700 dark:text-gray-300">
                    {JSON.stringify(log.metadata, null, 2)}
                  </pre>
                </details>
              )}
            </div>
          ))}
          {recentLogs.length === 0 && (
            <p className="text-sm text-gray-500 dark:text-gray-400 text-center py-4">
              No recent occurrences
            </p>
          )}
        </div>
      </div>
    </div>
  );
}
 
/**
 * Info item helper component
 */
function InfoItem({
  label,
  value,
  title,
}: {
  label: string;
  value: string | number;
  title?: string;
}) {
  return (
    <div>
      <p className="text-xs text-gray-500 dark:text-gray-400">{label}</p>
      <p
        className="text-sm font-medium text-gray-900 dark:text-white"
        title={title}
      >
        {value}
      </p>
    </div>
  );
}
 
/**
 * Query display component for DATABASE errors
 * Extracts and displays SQL queries from error metadata
 */
function QueryDisplay({ logs }: { logs: RecentLog[] }) {
  // Find the most recent log with query metadata
  const logWithQuery = logs.find((log) => {
    if (!isJsonObject(log.metadata)) return false;
    return 'query' in log.metadata && typeof log.metadata.query === 'string';
  });

  if (!logWithQuery || !isJsonObject(logWithQuery.metadata)) {
    return null;
  }

  const query = logWithQuery.metadata.query as string;
  const params = logWithQuery.metadata.params as string | undefined;
  const duration = logWithQuery.metadata.duration as number | undefined;

  return (
    <div className="space-y-3">
      <div>
        <div className="flex items-center justify-between mb-2">
          <h4 className="font-medium text-gray-900 dark:text-white">SQL Query</h4>
          {duration && (
            <span className="text-xs px-2 py-1 bg-yellow-100 text-yellow-800 dark:bg-yellow-900/50 dark:text-yellow-300 rounded-full">
              {duration}ms
            </span>
          )}
        </div>
        <div className="p-3 bg-gray-900 dark:bg-gray-950 rounded-lg">
          <pre className="text-sm text-green-400 dark:text-green-300 whitespace-pre-wrap break-all font-mono overflow-x-auto max-h-48 overflow-y-auto">
            {query}
          </pre>
        </div>
      </div>

      {params && params !== '[]' && (
        <div>
          <h4 className="text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
            Query Parameters
          </h4>
          <div className="p-2 bg-gray-100 dark:bg-gray-800 rounded-lg">
            <pre className="text-xs text-gray-600 dark:text-gray-400 whitespace-pre-wrap break-all font-mono overflow-x-auto max-h-24 overflow-y-auto">
              {params}
            </pre>
          </div>
        </div>
      )}
    </div>
  );
}
 
export default ErrorDetailsTab;