mirror of
https://github.com/bitbaum/botsmann.git
synced 2026-12-04 09:19:41 +01:00
Re-enables the three React-Compiler-era rules deferred in #114 (set-state-in-effect, error-boundaries, immutability) — all now ON — and resolves every violation with a real fix or a justified, documented exception. error-boundaries (12 → 0, real fix): - app/blog/[slug]/page.tsx wrapped its whole render in try/catch that only re-threw. Removed it: notFound()/render errors now propagate to Next's not-found.tsx / error.tsx as intended (the catch also mislogged notFound()). immutability (5 → 0, real fix): - The auth pages assigned window.location.href during render. Moved each redirect into a useEffect (above the early returns for rules-of-hooks), preserving the full-page navigation while keeping render pure. signin, signup, forgot-password, verify-email (×2). set-state-in-effect (13 → 0, justified inline disables): - Every case is a legitimate effect use — external-system sync (Supabase auth, API loads), browser-only hydration (localStorage, client-mount), or seeding editable state from async-loaded data. Forcing them off-effect would add complexity or risk behaviour changes to auth/data code, so each carries an eslint-disable-next-line with a specific reason. The rule stays ON to catch genuinely-avoidable derived-state-in-effect in future code. Verified: eslint . (0 problems), prettier, tsc --noEmit, next build, jest 227 pass. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
139 lines
3.7 KiB
TypeScript
139 lines
3.7 KiB
TypeScript
/**
|
|
* Dashboard Stats Hook
|
|
*
|
|
* Handles fetching and managing dashboard statistics.
|
|
* Centralizes data loading logic for reusability and testing.
|
|
*/
|
|
|
|
import { useState, useEffect, useCallback } from 'react';
|
|
import { DOCUMENT_STATUS } from '@/lib/constants';
|
|
import type { Document } from '@/types/document';
|
|
import type { UsageStats } from '@/types/conversation';
|
|
|
|
export interface CustomBot {
|
|
id: string;
|
|
slug: string;
|
|
title: string;
|
|
description: string | null;
|
|
emoji: string;
|
|
accent_color: string;
|
|
is_public: boolean;
|
|
is_published: boolean;
|
|
knowledge_count: number;
|
|
created_at: string;
|
|
updated_at: string;
|
|
}
|
|
|
|
export interface DashboardStats {
|
|
documentsTotal: number;
|
|
documentsReady: number;
|
|
documentsPending: number;
|
|
botsTotal: number;
|
|
botsPublished: number;
|
|
conversationsTotal: number;
|
|
messagesTotal: number;
|
|
}
|
|
|
|
const INITIAL_STATS: DashboardStats = {
|
|
documentsTotal: 0,
|
|
documentsReady: 0,
|
|
documentsPending: 0,
|
|
botsTotal: 0,
|
|
botsPublished: 0,
|
|
conversationsTotal: 0,
|
|
messagesTotal: 0,
|
|
};
|
|
|
|
interface UseDashboardStatsResult {
|
|
stats: DashboardStats;
|
|
documents: Document[];
|
|
bots: CustomBot[];
|
|
loading: boolean;
|
|
error: string | null;
|
|
refresh: () => Promise<void>;
|
|
isEmptyState: boolean;
|
|
}
|
|
|
|
export function useDashboardStats(userId: string | undefined): UseDashboardStatsResult {
|
|
const [documents, setDocuments] = useState<Document[]>([]);
|
|
const [bots, setBots] = useState<CustomBot[]>([]);
|
|
const [stats, setStats] = useState<DashboardStats>(INITIAL_STATS);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
const loadData = useCallback(async () => {
|
|
if (!userId) return;
|
|
|
|
setLoading(true);
|
|
setError(null);
|
|
|
|
try {
|
|
const [docsResponse, botsResponse, statsResponse] = await Promise.all([
|
|
fetch('/api/documents'),
|
|
fetch('/api/custom-bots'),
|
|
fetch('/api/stats'),
|
|
]);
|
|
|
|
const docsData = await docsResponse.json();
|
|
const botsData = await botsResponse.json();
|
|
const statsData = await statsResponse.json();
|
|
|
|
if (docsData.success) {
|
|
const docs = docsData.documents || [];
|
|
setDocuments(docs);
|
|
setStats((prev) => ({
|
|
...prev,
|
|
documentsTotal: docs.length,
|
|
documentsReady: docs.filter((d: Document) => d.status === DOCUMENT_STATUS.READY).length,
|
|
documentsPending: docs.filter((d: Document) => d.status === DOCUMENT_STATUS.PENDING)
|
|
.length,
|
|
}));
|
|
}
|
|
|
|
if (botsData.success) {
|
|
const botsList = botsData.data?.bots || botsData.bots || [];
|
|
setBots(botsList);
|
|
setStats((prev) => ({
|
|
...prev,
|
|
botsTotal: botsList.length,
|
|
botsPublished: botsList.filter((b: CustomBot) => b.is_published).length,
|
|
}));
|
|
}
|
|
|
|
if (statsData.success && statsData.data?.stats) {
|
|
const apiStats: UsageStats = statsData.data.stats;
|
|
setStats((prev) => ({
|
|
...prev,
|
|
conversationsTotal: apiStats.total_conversations,
|
|
messagesTotal: apiStats.total_messages,
|
|
}));
|
|
}
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : 'Failed to load dashboard data');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}, [userId]);
|
|
|
|
useEffect(() => {
|
|
// eslint-disable-next-line react-hooks/set-state-in-effect -- loads dashboard stats from the API on mount
|
|
loadData();
|
|
}, [loadData]);
|
|
|
|
const isEmptyState =
|
|
!loading &&
|
|
stats.documentsTotal === 0 &&
|
|
stats.conversationsTotal === 0 &&
|
|
stats.botsTotal === 0;
|
|
|
|
return {
|
|
stats,
|
|
documents,
|
|
bots,
|
|
loading,
|
|
error,
|
|
refresh: loadData,
|
|
isEmptyState,
|
|
};
|
|
}
|