fix(frontend): Admin dashboard crash + add test jobs toggle (#248)

## Summary

- Fixes React error #310 that was crashing the admin dashboard at
`/admin`
- Adds "Show Test Jobs" toggle in user menu for admins to view E2E test
data on `/app`
- Adds ESLint to CI to prevent similar bugs in the future

## Root Cause

PR #239 added `useAdminSettings()` hook **after** conditional returns in
`admin/layout.tsx`, violating React's Rules of Hooks. The hook was only
called on some renders, causing React to crash with "Rendered more hooks
than during the previous render".

## Changes

### Bug Fix
- `frontend/app/admin/layout.tsx` - Move `useAdminSettings()` before
conditional returns

### Feature: Test Jobs Toggle
- `frontend/components/auth/AuthStatus.tsx` - Add "Show Test Jobs"
toggle in admin section of user dropdown
- `frontend/app/app/page.tsx` - Pass `exclude_test` param based on admin
preference
- `frontend/lib/api.ts` - Add `exclude_test` param to `api.listJobs()`

### CI: ESLint for Hooks
- `frontend/eslint.config.mjs` - New ESLint config with
`react-hooks/rules-of-hooks: error`
- `frontend/package.json` - Add ESLint dependencies
- `.github/workflows/ci.yml` - Add `frontend-lint` job to CI gate

## Test plan

- [x] Build passes locally
- [x] ESLint passes (no hooks violations)
- [x] 68 unit tests pass
- [ ] Verify admin dashboard loads at
https://gen.nomadkaraoke.com/admin/
- [ ] Verify "Show Test Jobs" toggle appears in user menu for admins
- [ ] Verify toggling it shows/hides test jobs on /app page

<!-- @coderabbitai ignore -->

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Andrew Beveridge ☄️andClaude Opus 4.5 authored and GitHub committed 2026-01-08 17:13:27 -05:00
1 parent 23e28489f6
commit addd0e1ce5
10 files changed
+3777 -14

No files matched your search

+26 -1
View File
@@ -67,6 +67,29 @@ jobs:
# FRONTEND TESTS (PRs only - main skips tests)
# ============================================
frontend-lint:
name: "Frontend - Lint (Rules of Hooks)"
runs-on: ubuntu-latest
needs: changes
if: github.ref != 'refs/heads/main' && needs.changes.outputs.frontend == 'true'
steps:
- uses: actions/checkout@v4
- name: Setup Node.js with caching
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
cache-dependency-path: frontend/package-lock.json
- name: Install dependencies
working-directory: frontend
run: npm ci --legacy-peer-deps
- name: Run ESLint
working-directory: frontend
run: npm run lint
frontend-unit-tests:
name: "Frontend - Unit Tests"
runs-on: ubuntu-latest
@@ -655,12 +678,14 @@ jobs:
runs-on: ubuntu-latest
# Note: frontend-e2e-full is optional (requires 'e2e' label) so not included here
# frontend-e2e-smoke IS required - fast smoke tests catch critical regressions
needs: [frontend-unit-tests, frontend-e2e-smoke, frontend-build, package-unit-tests, package-integration-tests, backend-unit-tests, backend-emulator-tests, backend-lint]
# frontend-lint IS required - catches React hooks violations that cause runtime crashes
needs: [frontend-lint, frontend-unit-tests, frontend-e2e-smoke, frontend-build, package-unit-tests, package-integration-tests, backend-unit-tests, backend-emulator-tests, backend-lint]
if: github.ref != 'refs/heads/main' && always()
steps:
- name: Check results
run: |
results=(
"${{ needs.frontend-lint.result }}"
"${{ needs.frontend-unit-tests.result }}"
"${{ needs.frontend-e2e-smoke.result }}"
"${{ needs.frontend-build.result }}"
+3 -2
View File
@@ -90,6 +90,9 @@ export default function AdminLayout({
const router = useRouter()
const { user } = useAuth()
const [isChecking, setIsChecking] = useState(true)
// IMPORTANT: All hooks must be called before any conditional returns
// to satisfy React's Rules of Hooks
const { showTestData, setShowTestData } = useAdminSettings()
useEffect(() => {
const token = getAccessToken()
@@ -127,8 +130,6 @@ export default function AdminLayout({
)
}
const { showTestData, setShowTestData } = useAdminSettings()
return (
<SidebarProvider>
<AdminSidebar />
+11 -2
View File
@@ -4,6 +4,7 @@ import { useState, useEffect, useCallback } from "react"
import { useRouter } from "next/navigation"
import { api, Job, getAccessToken } from "@/lib/api"
import { useAuth } from "@/lib/auth"
import { useAdminSettings } from "@/lib/admin-settings"
import { useJobNotifications, useVisibilityRefresh } from "@/hooks/use-notifications"
import { Button } from "@/components/ui/button"
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
@@ -30,6 +31,10 @@ export default function AppPage() {
const [isAuthenticated, setIsAuthenticated] = useState<boolean | null>(null)
const { isDarkMode, toggleTheme, mounted } = useTheme()
const { user, fetchUser } = useAuth()
const { showTestData } = useAdminSettings()
// Check if user is admin (for exclude_test parameter)
const isAdmin = user?.role === "admin" || user?.email?.endsWith("@nomadkaraoke.com")
// Memoize loadJobs for use with visibility refresh
const loadJobs = useCallback(async () => {
@@ -39,7 +44,11 @@ export default function AppPage() {
return
}
try {
const data = await api.listJobs({ limit: 20 })
// Only admins can filter test jobs, and by default we hide them
const data = await api.listJobs({
limit: 20,
exclude_test: isAdmin ? !showTestData : undefined
})
setJobs(data)
} catch (err: any) {
// Don't log auth errors - user just needs to authenticate
@@ -50,7 +59,7 @@ export default function AppPage() {
setIsLoadingJobs(false)
setIsInitialLoad(false)
}
}, [])
}, [isAdmin, showTestData])
// Enable notifications for job status changes (sound + title animation)
useJobNotifications(jobs)
@@ -384,7 +384,7 @@ export function AudioSearchDialog({ jobId, open, onClose, onSelect }: AudioSearc
<span className="truncate">{formatMetadata(result)}</span>
)}
{result.target_file && (
<span className="font-mono truncate">"{result.target_file}"</span>
<span className="font-mono truncate">&quot;{result.target_file}&quot;</span>
)}
</div>
)}
+16 -1
View File
@@ -1,7 +1,7 @@
"use client"
import { useState, useEffect } from "react"
import { User, LogOut, CreditCard, Coins, KeyRound, Shield } from "lucide-react"
import { User, LogOut, CreditCard, Coins, KeyRound, Shield, FlaskConical } from "lucide-react"
import Link from "next/link"
import { Button } from "@/components/ui/button"
import {
@@ -12,7 +12,9 @@ import {
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
import { Switch } from "@/components/ui/switch"
import { useAuth } from "@/lib/auth"
import { useAdminSettings } from "@/lib/admin-settings"
import { AuthDialog } from "./AuthDialog"
interface AuthStatusProps {
@@ -21,6 +23,7 @@ interface AuthStatusProps {
export function AuthStatus({ onAuthChange }: AuthStatusProps) {
const { user, logout } = useAuth()
const { showTestData, setShowTestData } = useAdminSettings()
const [showAuthDialog, setShowAuthDialog] = useState(false)
const [mounted, setMounted] = useState(false)
@@ -107,6 +110,18 @@ export function AuthStatus({ onAuthChange }: AuthStatusProps) {
<span>Admin Dashboard</span>
</Link>
</DropdownMenuItem>
<DropdownMenuItem
onSelect={(e) => e.preventDefault()}
className="text-muted-foreground focus:text-foreground focus:bg-secondary"
>
<FlaskConical className="w-4 h-4 mr-2" />
<span className="flex-1">Show Test Jobs</span>
<Switch
checked={showTestData}
onCheckedChange={setShowTestData}
className="ml-2"
/>
</DropdownMenuItem>
</>
)}
<DropdownMenuSeparator className="bg-border" />
+1 -1
View File
@@ -345,7 +345,7 @@ export function OutputLinks({ jobId }: OutputLinksProps) {
<DialogHeader>
<DialogTitle>Send Completion Email</DialogTitle>
<DialogDescription>
Send the job completion message to an email address. The email will be CC'd to gen@nomadkaraoke.com.
Send the job completion message to an email address. The email will be CC&apos;d to gen@nomadkaraoke.com.
</DialogDescription>
</DialogHeader>
<div className="grid gap-4 py-4">
+33
View File
@@ -0,0 +1,33 @@
import { FlatCompat } from "@eslint/eslintrc";
import { dirname } from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const compat = new FlatCompat({
baseDirectory: __dirname,
});
const eslintConfig = [
...compat.extends("next/core-web-vitals"),
{
rules: {
// Enforce Rules of Hooks - this catches hooks called after conditional returns
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "warn",
},
},
{
ignores: [
".next/**",
"node_modules/**",
"out/**",
"coverage/**",
"test-results/**",
"playwright-report/**",
],
},
];
export default eslintConfig;
+3 -2
View File
@@ -200,11 +200,12 @@ export const api = {
/**
* List all jobs
*/
async listJobs(params?: { status?: string; limit?: number }): Promise<Job[]> {
async listJobs(params?: { status?: string; limit?: number; exclude_test?: boolean }): Promise<Job[]> {
const searchParams = new URLSearchParams();
if (params?.status) searchParams.set('status', params.status);
if (params?.limit) searchParams.set('limit', String(params.limit));
if (params?.exclude_test !== undefined) searchParams.set('exclude_test', String(params.exclude_test));
const url = `${API_BASE_URL}/api/jobs${searchParams.toString() ? '?' + searchParams.toString() : ''}`;
const response = await fetch(url, {
headers: getAuthHeaders()
+3677 -2
View File
File diff suppressed because it is too large. Load diff
+6 -2
View File
@@ -1,6 +1,6 @@
{
"name": "karaoke-gen-frontend",
"version": "0.82.1",
"version": "0.83.0",
"private": true,
"scripts": {
"build": "next build",
@@ -81,8 +81,8 @@
"zustand": "latest"
},
"devDependencies": {
"@eslint/eslintrc": "^3",
"@playwright/test": "^1.57.0",
"playwright": "^1.57.0",
"@tailwindcss/postcss": "^4.1.9",
"@testing-library/jest-dom": "^6.9.1",
"@testing-library/react": "^16.3.1",
@@ -92,8 +92,12 @@
"@types/react": "^19",
"@types/react-dom": "^19",
"concurrently": "^9.2.1",
"eslint": "^9",
"eslint-config-next": "^15",
"eslint-plugin-react-hooks": "^5",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"playwright": "^1.57.0",
"postcss": "^8.5",
"tailwindcss": "^4.1.9",
"ts-node": "^10.9.2",