Skip to content

Conversation

@WA11AX
Copy link
Owner

@WA11AX WA11AX commented Aug 13, 2025

Summary

  • type WebSocket messages with Tournament and strict event union
  • track reconnection attempts via useRef
  • deduplicate and type sendMessage in WebSocket context

Testing

  • npm run type-check
  • npm run lint:check
  • npm test

https://chatgpt.com/codex/tasks/task_e_689d085d8ccc8327bc09bfbcfab8927a

Copilot AI review requested due to automatic review settings August 13, 2025 22:58
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors WebSocket and Telegram services from simple utility modules into React context providers with improved type safety. The refactoring extracts service logic from lib/ into services/ directory and provides proper TypeScript definitions for WebSocket messages and tournament data.

  • Converts WebSocket and Telegram utilities to React context providers for better state management
  • Improves TypeScript type safety with strict WebSocketMessage union types using Tournament schema
  • Centralizes reconnection logic using useRef for tracking attempts

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
client/src/services/websocket.tsx New WebSocket service with context provider and improved type definitions
client/src/services/telegram.tsx New Telegram service with context provider and error handling improvements
client/src/pages/tournaments.tsx Updates to use new WebSocket service context
client/src/pages/tournament-detail.tsx Updates to use new Telegram service context
client/src/lib/websocket.ts Replaced with re-export from services
client/src/lib/telegram.ts Replaced with re-export from services
client/src/App.tsx Wraps app with new service providers
client/README.md Documents new services architecture

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

const reconnectTimeoutRef = useRef<number>();
const websocketCallbacks = useRef<((message: any) => void)[]>([]);

const addCallback = (callback: (message: any) => void) => {
Copy link

Copilot AI Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The callback parameter type any weakens type safety. Consider creating a specific type for internal callback messages that includes both WebSocketMessage and internal connection state messages like { type: 'connected' | 'disconnected' | 'error' | 'message', data?: WebSocketMessage, error?: string }.

Suggested change
const addCallback = (callback: (message: any) => void) => {
// Union type for all possible callback messages
export type WebSocketCallbackMessage =
| WebSocketMessage
| { type: 'connected' }
| { type: 'disconnected' }
| { type: 'error'; error: string }
| { type: 'message'; data: WebSocketMessage };
function useWebSocket(onMessage?: (message: WebSocketMessage) => void) {
const [isConnected, setIsConnected] = useState(false);
const wsRef = useRef<WebSocket | null>(null);
const reconnectTimeoutRef = useRef<number>();
const websocketCallbacks = useRef<((message: WebSocketCallbackMessage) => void)[]>([]);
const addCallback = (callback: (message: WebSocketCallbackMessage) => void) => {

Copilot uses AI. Check for mistakes.
const reconnectTimeoutRef = useRef<number>();
const websocketCallbacks = useRef<((message: any) => void)[]>([]);

const addCallback = (callback: (message: any) => void) => {
Copy link

Copilot AI Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The callback parameter type any should be replaced with a more specific type that matches the internal message structure used in the websocket callbacks.

Suggested change
const addCallback = (callback: (message: any) => void) => {
// Extend WebSocketMessage with internal messages for callbacks
type InternalWebSocketMessage =
| WebSocketMessage
| { type: 'connected' }
| { type: 'error'; error: string };
function useWebSocket(onMessage?: (message: WebSocketMessage) => void) {
const [isConnected, setIsConnected] = useState(false);
const wsRef = useRef<WebSocket | null>(null);
const reconnectTimeoutRef = useRef<number>();
const websocketCallbacks = useRef<((message: InternalWebSocketMessage) => void)[]>([]);
const addCallback = (callback: (message: InternalWebSocketMessage) => void) => {

Copilot uses AI. Check for mistakes.
interface WebSocketContextValue {
isConnected: boolean;
sendMessage: (message: WebSocketMessage) => void;
addCallback: (callback: (message: any) => void) => () => void;
Copy link

Copilot AI Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The addCallback interface uses any type which reduces type safety. This should match the callback type used in the implementation for consistency.

Suggested change
addCallback: (callback: (message: any) => void) => () => void;
addCallback: (callback: (message: WebSocketMessage) => void) => () => void;

Copilot uses AI. Check for mistakes.
}
});
useEffect(() => {
return addCallback((message: WebSocketMessage) => {
Copy link

Copilot AI Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type mismatch: addCallback expects (message: any) => void but receives (message: WebSocketMessage) => void. The callback should handle the internal message structure that includes connection state messages, not just WebSocketMessage.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants