Replace all console.log/warn/error statements with unified logger utility. Changes: - Replaced console in lib/hooks/use-stories.ts - Replaced console in lib/signalr/SignalRContext.tsx - Replaced console in lib/hooks/useProjectHub.ts - Replaced console in lib/hooks/use-tasks.ts - Replaced console in lib/hooks/useNotificationHub.ts - Replaced console in lib/hooks/use-projects.ts - Replaced console in app/(dashboard)/projects/[id]/kanban/page.tsx Logger respects NODE_ENV (debug disabled in production). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
218 lines
6.6 KiB
TypeScript
218 lines
6.6 KiB
TypeScript
'use client';
|
|
|
|
import { useEffect, useState, useCallback, useRef } from 'react';
|
|
import { SignalRConnectionManager } from '@/lib/signalr/ConnectionManager';
|
|
import { SIGNALR_CONFIG } from '@/lib/signalr/config';
|
|
import { useAuthStore } from '@/stores/authStore';
|
|
import type { ProjectHubEventCallbacks } from '@/lib/signalr/types';
|
|
import { logger } from '@/lib/utils/logger';
|
|
|
|
// Re-export for backward compatibility
|
|
interface UseProjectHubOptions extends ProjectHubEventCallbacks {}
|
|
|
|
export function useProjectHub(projectId?: string, options?: UseProjectHubOptions) {
|
|
const isAuthenticated = useAuthStore((state) => state.isAuthenticated);
|
|
const [connectionState, setConnectionState] = useState<
|
|
'disconnected' | 'connecting' | 'connected' | 'reconnecting'
|
|
>('disconnected');
|
|
const managerRef = useRef<SignalRConnectionManager | null>(null);
|
|
|
|
useEffect(() => {
|
|
if (!isAuthenticated) return;
|
|
|
|
const manager = new SignalRConnectionManager(
|
|
SIGNALR_CONFIG.HUB_URLS.PROJECT
|
|
);
|
|
managerRef.current = manager;
|
|
|
|
const unsubscribe = manager.onStateChange(setConnectionState);
|
|
|
|
// ============================================
|
|
// PROJECT EVENTS (3)
|
|
// ============================================
|
|
manager.on('ProjectCreated', (data: any) => {
|
|
logger.debug('[ProjectHub] Project created:', data);
|
|
options?.onProjectCreated?.(data);
|
|
});
|
|
|
|
manager.on('ProjectUpdated', (data: any) => {
|
|
logger.debug('[ProjectHub] Project updated:', data);
|
|
options?.onProjectUpdated?.(data);
|
|
});
|
|
|
|
manager.on('ProjectArchived', (data: any) => {
|
|
logger.debug('[ProjectHub] Project archived:', data);
|
|
options?.onProjectArchived?.(data);
|
|
});
|
|
|
|
// ============================================
|
|
// EPIC EVENTS (3)
|
|
// ============================================
|
|
manager.on('EpicCreated', (data: any) => {
|
|
logger.debug('[ProjectHub] Epic created:', data);
|
|
options?.onEpicCreated?.(data);
|
|
});
|
|
|
|
manager.on('EpicUpdated', (data: any) => {
|
|
logger.debug('[ProjectHub] Epic updated:', data);
|
|
options?.onEpicUpdated?.(data);
|
|
});
|
|
|
|
manager.on('EpicDeleted', (data: any) => {
|
|
logger.debug('[ProjectHub] Epic deleted:', data);
|
|
options?.onEpicDeleted?.(data);
|
|
});
|
|
|
|
// ============================================
|
|
// STORY EVENTS (3)
|
|
// ============================================
|
|
manager.on('StoryCreated', (data: any) => {
|
|
logger.debug('[ProjectHub] Story created:', data);
|
|
options?.onStoryCreated?.(data);
|
|
});
|
|
|
|
manager.on('StoryUpdated', (data: any) => {
|
|
logger.debug('[ProjectHub] Story updated:', data);
|
|
options?.onStoryUpdated?.(data);
|
|
});
|
|
|
|
manager.on('StoryDeleted', (data: any) => {
|
|
logger.debug('[ProjectHub] Story deleted:', data);
|
|
options?.onStoryDeleted?.(data);
|
|
});
|
|
|
|
// ============================================
|
|
// TASK EVENTS (4)
|
|
// ============================================
|
|
manager.on('TaskCreated', (data: any) => {
|
|
logger.debug('[ProjectHub] Task created:', data);
|
|
options?.onTaskCreated?.(data);
|
|
});
|
|
|
|
manager.on('TaskUpdated', (data: any) => {
|
|
logger.debug('[ProjectHub] Task updated:', data);
|
|
options?.onTaskUpdated?.(data);
|
|
});
|
|
|
|
manager.on('TaskDeleted', (data: any) => {
|
|
logger.debug('[ProjectHub] Task deleted:', data);
|
|
options?.onTaskDeleted?.(data);
|
|
});
|
|
|
|
manager.on('TaskAssigned', (data: any) => {
|
|
logger.debug('[ProjectHub] Task assigned:', data);
|
|
options?.onTaskAssigned?.(data);
|
|
});
|
|
|
|
// ============================================
|
|
// LEGACY ISSUE EVENTS (Backward Compatibility)
|
|
// ============================================
|
|
manager.on('IssueCreated', (data: any) => {
|
|
logger.debug('[ProjectHub] Issue created:', data);
|
|
options?.onIssueCreated?.(data);
|
|
});
|
|
|
|
manager.on('IssueUpdated', (data: any) => {
|
|
logger.debug('[ProjectHub] Issue updated:', data);
|
|
options?.onIssueUpdated?.(data);
|
|
});
|
|
|
|
manager.on('IssueDeleted', (data: any) => {
|
|
logger.debug('[ProjectHub] Issue deleted:', data);
|
|
options?.onIssueDeleted?.(data);
|
|
});
|
|
|
|
manager.on('IssueStatusChanged', (data: any) => {
|
|
logger.debug('[ProjectHub] Issue status changed:', data);
|
|
options?.onIssueStatusChanged?.(data);
|
|
});
|
|
|
|
// ============================================
|
|
// USER COLLABORATION EVENTS
|
|
// ============================================
|
|
manager.on('UserJoinedProject', (data: any) => {
|
|
logger.debug('[ProjectHub] User joined:', data);
|
|
options?.onUserJoinedProject?.(data);
|
|
});
|
|
|
|
manager.on('UserLeftProject', (data: any) => {
|
|
logger.debug('[ProjectHub] User left:', data);
|
|
options?.onUserLeftProject?.(data);
|
|
});
|
|
|
|
manager.on('TypingIndicator', (data: any) => {
|
|
logger.debug('[ProjectHub] Typing indicator:', data);
|
|
options?.onTypingIndicator?.(data);
|
|
});
|
|
|
|
manager.start();
|
|
|
|
return () => {
|
|
unsubscribe();
|
|
manager.stop();
|
|
};
|
|
}, [isAuthenticated, options]);
|
|
|
|
// 加入项目房间
|
|
const joinProject = useCallback(async (projectId: string) => {
|
|
if (!managerRef.current) return;
|
|
|
|
try {
|
|
await managerRef.current.invoke('JoinProject', projectId);
|
|
logger.debug(`[ProjectHub] Joined project ${projectId}`);
|
|
} catch (error) {
|
|
logger.error('[ProjectHub] Error joining project:', error);
|
|
}
|
|
}, []);
|
|
|
|
// 离开项目房间
|
|
const leaveProject = useCallback(async (projectId: string) => {
|
|
if (!managerRef.current) return;
|
|
|
|
try {
|
|
await managerRef.current.invoke('LeaveProject', projectId);
|
|
logger.debug(`[ProjectHub] Left project ${projectId}`);
|
|
} catch (error) {
|
|
logger.error('[ProjectHub] Error leaving project:', error);
|
|
}
|
|
}, []);
|
|
|
|
// 发送正在输入指示器
|
|
const sendTypingIndicator = useCallback(
|
|
async (projectId: string, issueId: string, isTyping: boolean) => {
|
|
if (!managerRef.current) return;
|
|
|
|
try {
|
|
await managerRef.current.invoke(
|
|
'SendTypingIndicator',
|
|
projectId,
|
|
issueId,
|
|
isTyping
|
|
);
|
|
} catch (error) {
|
|
logger.error('[ProjectHub] Error sending typing indicator:', error);
|
|
}
|
|
},
|
|
[]
|
|
);
|
|
|
|
// 当 projectId 变化时自动加入/离开
|
|
useEffect(() => {
|
|
if (connectionState === 'connected' && projectId) {
|
|
joinProject(projectId);
|
|
|
|
return () => {
|
|
leaveProject(projectId);
|
|
};
|
|
}
|
|
}, [connectionState, projectId, joinProject, leaveProject]);
|
|
|
|
return {
|
|
connectionState,
|
|
joinProject,
|
|
leaveProject,
|
|
sendTypingIndicator,
|
|
isConnected: connectionState === 'connected',
|
|
};
|
|
}
|