fix(frontend): Fix critical type safety issues from code review

Address all Critical and High Priority issues identified in frontend code review report:

Critical Issues Fixed:
- Created unified logger utility (lib/utils/logger.ts) to replace all console.log statements
- Consolidated User type definitions - removed duplicate from authStore, using single source from types/user.ts
- Eliminated 'any' types in API client - added proper generic types with AxiosRequestConfig
- Fixed SignalR ConnectionManager - replaced 'any' with generic types <T>
- Created API error types (lib/types/errors.ts) with ApiError and getErrorMessage helper
- Fixed IssueCard component - removed all type assertions, created discriminated union types for Kanban items
- Added React.memo to IssueCard for performance optimization
- Added proper ARIA labels and accessibility attributes to IssueCard

High Priority Issues Fixed:
- Fixed hardcoded user ID in CreateProjectDialog - now uses actual user from authStore
- Added useCallback to CreateProjectDialog onSubmit handler
- Fixed error handlers in use-epics.ts - replaced 'any' with ApiError type
- Updated all error handling to use logger and getErrorMessage

Type Safety Improvements:
- Created KanbanItem discriminated union (KanbanEpic | KanbanStory | KanbanTask) with proper type guards
- Added 'never' types to prevent invalid property access
- Fixed User interface to include all required fields (createdAt, updatedAt)
- Maintained backward compatibility with LegacyKanbanBoard for existing code

Files Changed:
- lib/utils/logger.ts - New centralized logging utility
- lib/types/errors.ts - New API error types and helpers
- types/user.ts - Consolidated User type with TenantRole
- types/kanban.ts - New discriminated union types for type-safe Kanban items
- components/features/kanban/IssueCard.tsx - Type-safe with React.memo
- components/features/projects/CreateProjectDialog.tsx - Fixed hardcoded user ID, added useCallback
- lib/api/client.ts - Eliminated 'any', added proper generics
- lib/signalr/ConnectionManager.ts - Replaced console.log, added generics
- lib/hooks/use-epics.ts - Fixed error handler types
- stores/authStore.ts - Removed duplicate User type
- lib/hooks/useAuth.ts - Added createdAt field to User

TypeScript compilation:  All type checks passing (0 errors)

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Yaojia Wang
2025-11-05 19:11:48 +01:00
parent 90e3d2416c
commit ea67d90880
21 changed files with 1459 additions and 120 deletions

View File

@@ -1,5 +1,96 @@
import { Task, TaskStatus } from './project';
/**
* Kanban-specific types with discriminated unions
* Ensures type safety for Epic, Story, and Task cards
*/
import { WorkItemStatus, WorkItemPriority } from './project';
// Base Kanban item interface with common properties
interface BaseKanbanItem {
id: string;
projectId: string;
status: WorkItemStatus;
priority: WorkItemPriority;
description?: string;
estimatedHours?: number;
actualHours?: number;
assigneeId?: string;
tenantId: string;
createdAt: string;
updatedAt: string;
}
// Epic as Kanban item - discriminated by 'type' field
export interface KanbanEpic extends BaseKanbanItem {
type: 'Epic';
name: string; // Epic uses 'name' instead of 'title'
createdBy: string;
childCount?: number; // Number of stories in this epic
epicId?: never; // Epic doesn't have epicId
storyId?: never; // Epic doesn't have storyId
title?: never; // Epic uses 'name', not 'title'
}
// Story as Kanban item - discriminated by 'type' field
export interface KanbanStory extends BaseKanbanItem {
type: 'Story';
title: string; // Story uses 'title'
epicId: string; // Story always has epicId
childCount?: number; // Number of tasks in this story
storyId?: never; // Story doesn't have storyId
name?: never; // Story uses 'title', not 'name'
}
// Task as Kanban item - discriminated by 'type' field
export interface KanbanTask extends BaseKanbanItem {
type: 'Task';
title: string; // Task uses 'title'
storyId: string; // Task always has storyId
epicId?: never; // Task doesn't have epicId (only through story)
childCount?: never; // Task doesn't have children
name?: never; // Task uses 'title', not 'name'
}
// Discriminated union type for Kanban items
// TypeScript can narrow the type based on the 'type' field
export type KanbanItem = KanbanEpic | KanbanStory | KanbanTask;
// Type guards for runtime type checking
export function isKanbanEpic(item: KanbanItem): item is KanbanEpic {
return item.type === 'Epic';
}
export function isKanbanStory(item: KanbanItem): item is KanbanStory {
return item.type === 'Story';
}
export function isKanbanTask(item: KanbanItem): item is KanbanTask {
return item.type === 'Task';
}
// Helper to get display title regardless of type
export function getKanbanItemTitle(item: KanbanItem): string {
if (isKanbanEpic(item)) {
return item.name;
}
return item.title;
}
// Kanban column type
export interface KanbanColumn {
id: WorkItemStatus;
title: string;
items: KanbanItem[];
}
// Kanban board type
export interface KanbanBoard {
projectId: string;
projectName: string;
columns: KanbanColumn[];
}
// ==================== Legacy Types (for backward compatibility) ====================
export interface TaskCard {
id: string;
title: string;
@@ -10,14 +101,16 @@ export interface TaskCard {
actualHours?: number;
}
export interface KanbanColumn {
status: TaskStatus;
// Legacy KanbanColumn type for backward compatibility
export interface LegacyKanbanColumn {
status: string;
title: string;
tasks: TaskCard[];
}
export interface KanbanBoard {
// Legacy KanbanBoard type
export interface LegacyKanbanBoard {
projectId: string;
projectName: string;
columns: KanbanColumn[];
columns: LegacyKanbanColumn[];
}