All files / src/app/admin/dev-tools/lint page.tsx

0% Statements 0/239
100% Branches 0/0
0% Functions 0/1
0% Lines 0/239

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
'use client';

import { useState, useEffect, useCallback } from 'react';
import Link from 'next/link';
import type { LintResult, CommandStatusResponse } from '@/lib/dev-tools/types';
import { Icon } from '@/components/ui/icons';

export default function LintCheckerPage() {
  const [status, setStatus] = useState<CommandStatusResponse>({ status: 'idle' });
  const [isStarting, setIsStarting] = useState(false);
  const [expandedFiles, setExpandedFiles] = useState<Set<string>>(new Set());

  const fetchStatus = useCallback(async () => {
    try {
      const res = await fetch('/api/admin/dev-tools/lint');
      if (res.ok) {
        const result = await res.json();
        // Handle both new wrapped format and legacy format
        const data = result.data ?? result;
        setStatus(data);
      }
    } catch {
      // Ignore errors
    }
  }, []);

  // Fetch initial status
  useEffect(() => {
    fetchStatus();
  }, [fetchStatus]);

  // Poll for status updates when running
  useEffect(() => {
    if (status.status !== 'running') return;

    const interval = setInterval(fetchStatus, 2000);
    return () => clearInterval(interval);
  }, [status.status, fetchStatus]);

  const runLint = async (fix: boolean = false) => {
    setIsStarting(true);
    try {
      const res = await fetch('/api/admin/dev-tools/lint', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ fix })});
      if (res.ok) {
        setStatus({ status: 'running', startedAt: new Date().toISOString() });
      }
    } finally {
      setIsStarting(false);
    }
  };

  const stopLint = async () => {
    await fetch('/api/admin/dev-tools/lint', { method: 'DELETE' });
    fetchStatus();
  };

  const toggleFile = (filePath: string) => {
    const newExpanded = new Set(expandedFiles);
    if (newExpanded.has(filePath)) {
      newExpanded.delete(filePath);
    } else {
      newExpanded.add(filePath);
    }
    setExpandedFiles(newExpanded);
  };

  const lintResult = status.result as LintResult | undefined;

  return (
    <div className="max-w-[1170px] mx-auto px-4 sm:px-7.5 xl:px-0">
      {/* Breadcrumb */}
      <nav className="mb-4">
        <ol className="flex items-center gap-2 text-sm text-gray-600 dark:text-gray-400">
          <li><Link href="/admin/dev-tools" className="hover:text-blue">Developer Tools</Link></li>
          <li>/</li>
          <li className="text-dark dark:text-gray-100 font-medium">Lint Checker</li>
        </ol>
      </nav>

      <div className="mb-8">
        <h1 className="text-2xl font-bold text-dark dark:text-gray-100">Lint Checker</h1>
        <p className="text-gray-600 dark:text-gray-400 mt-1">
          Run ESLint and view code quality issues
        </p>
      </div>

      {/* Controls */}
      <div className="bg-white dark:bg-gray-800 rounded-lg shadow-sm border border-gray-200 dark:border-gray-700 p-6 mb-6">
        <h2 className="font-semibold text-lg text-dark dark:text-gray-100 mb-4">Run Lint</h2>
        <div className="flex gap-2">
          {status.status === 'running' ? (
            <button
              onClick={stopLint}
              className="px-4 py-2 bg-red-500 text-white rounded-md text-sm font-medium hover:bg-red-600 transition-colors"
            >
              Stop Lint
            </button>
          ) : (
            <>
              <button
                onClick={() => runLint(false)}
                disabled={isStarting}
                className="px-4 py-2 bg-blue text-white rounded-md text-sm font-medium hover:bg-blue/90 transition-colors disabled:opacity-50"
              >
                {isStarting ? 'Starting...' : 'Run Lint'}
              </button>
              <button
                onClick={() => runLint(true)}
                disabled={isStarting}
                className="px-4 py-2 bg-green-600 text-white rounded-md text-sm font-medium hover:bg-green-700 transition-colors disabled:opacity-50"
              >
                Run with Fix
              </button>
            </>
          )}
        </div>
      </div>

      {/* Status */}
      {status.status !== 'idle' && (
        <div className="bg-white dark:bg-gray-800 rounded-lg shadow-sm border border-gray-200 dark:border-gray-700 p-6 mb-6">
          <div className="flex items-center justify-between mb-4">
            <h2 className="font-semibold text-lg text-dark dark:text-gray-100">Results</h2>
            <span className={`px-3 py-1 rounded-full text-sm font-medium ${
              status.status === 'running' ? 'bg-blue-100 dark:bg-blue-900/30 text-blue-700 dark:text-blue-300' :
              status.status === 'completed' ? 'bg-green-100 dark:bg-green-900/30 text-green-700 dark:text-green-300' :
              'bg-red-100 dark:bg-red-900/30 text-red-700 dark:text-red-300'
            }`}>
              {status.status === 'running' ? 'Running...' :
               status.status === 'completed' ? 'No Issues' : 'Issues Found'}
            </span>
          </div>

          {/* Summary */}
          {lintResult && (
            <div className="grid grid-cols-2 md:grid-cols-4 gap-4 mb-6">
              <div className="bg-gray-50 dark:bg-gray-700 rounded-lg p-4">
                <div className="text-2xl font-bold text-red-600 dark:text-red-400">{lintResult.errorCount}</div>
                <div className="text-sm text-gray-600 dark:text-gray-400">Errors</div>
              </div>
              <div className="bg-gray-50 dark:bg-gray-700 rounded-lg p-4">
                <div className="text-2xl font-bold text-yellow-600 dark:text-yellow-400">{lintResult.warningCount}</div>
                <div className="text-sm text-gray-600 dark:text-gray-400">Warnings</div>
              </div>
              <div className="bg-gray-50 dark:bg-gray-700 rounded-lg p-4">
                <div className="text-2xl font-bold text-dark dark:text-gray-100">{lintResult.files.length}</div>
                <div className="text-sm text-gray-600 dark:text-gray-400">Files with Issues</div>
              </div>
              <div className="bg-gray-50 dark:bg-gray-700 rounded-lg p-4">
                <div className="text-2xl font-bold text-blue-600 dark:text-blue-400">
                  {lintResult.fixableErrorCount + lintResult.fixableWarningCount}
                </div>
                <div className="text-sm text-gray-600 dark:text-gray-400">Auto-fixable</div>
              </div>
            </div>
          )}

          {/* File List */}
          {lintResult && lintResult.files.length > 0 && (
            <div className="mb-6">
              <h3 className="font-medium text-dark dark:text-gray-100 mb-3">Issues by File</h3>
              <div className="space-y-2">
                {lintResult.files.map((file) => (
                  <div key={file.filePath} className="border border-gray-200 dark:border-gray-700 rounded-lg overflow-hidden">
                    <button
                      onClick={() => toggleFile(file.filePath)}
                      className="w-full px-4 py-3 bg-gray-50 dark:bg-gray-700 flex items-center justify-between hover:bg-gray-100 dark:hover:bg-gray-600 transition-colors"
                    >
                      <span className="font-mono text-sm text-dark dark:text-gray-100 truncate">
                        {file.filePath.replace(process.cwd?.() || '', '')}
                      </span>
                      <div className="flex items-center gap-3">
                        {file.errorCount > 0 && (
                          <span className="text-xs bg-red-100 dark:bg-red-900/30 text-red-700 dark:text-red-300 px-2 py-1 rounded">
                            {file.errorCount} errors
                          </span>
                        )}
                        {file.warningCount > 0 && (
                          <span className="text-xs bg-yellow-100 dark:bg-yellow-900/30 text-yellow-700 dark:text-yellow-300 px-2 py-1 rounded">
                            {file.warningCount} warnings
                          </span>
                        )}
                        <Icon
                          name="chevron-down"
                          size={20}
                          className={`text-gray-400 transition-transform ${
                            expandedFiles.has(file.filePath) ? 'rotate-180' : ''
                          }`}
                        />
                      </div>
                    </button>
                    {expandedFiles.has(file.filePath) && (
                      <div className="px-4 py-3 space-y-2 bg-white dark:bg-gray-800">
                        {file.messages.map((msg, idx) => (
                          <div
                            key={idx}
                            className={`flex items-start gap-3 text-sm ${
                              msg.severity === 2 ? 'text-red-700 dark:text-red-400' : 'text-yellow-700 dark:text-yellow-400'
                            }`}
                          >
                            <span className="font-mono text-gray-500 dark:text-gray-400 whitespace-nowrap">
                              {msg.line}:{msg.column}
                            </span>
                            <span className={`px-1.5 py-0.5 rounded text-xs ${
                              msg.severity === 2 ? 'bg-red-100 dark:bg-red-900/30' : 'bg-yellow-100 dark:bg-yellow-900/30'
                            }`}>
                              {msg.severity === 2 ? 'error' : 'warn'}
                            </span>
                            <span className="flex-1">{msg.message}</span>
                            {msg.ruleId && (
                              <span className="text-gray-500 dark:text-gray-400 font-mono text-xs">{msg.ruleId}</span>
                            )}
                          </div>
                        ))}
                      </div>
                    )}
                  </div>
                ))}
              </div>
            </div>
          )}

          {/* Raw Output */}
          {status.output && (
            <div>
              <h3 className="font-medium text-dark dark:text-gray-100 mb-3">Raw Output</h3>
              <pre className="bg-gray-900 text-gray-100 rounded-lg p-4 overflow-x-auto text-sm max-h-96 overflow-y-auto">
                {status.output}
              </pre>
            </div>
          )}
        </div>
      )}
    </div>
  );
}