analytica-frontend-lib
Version:
Repositório público dos componentes utilizados nas plataformas da Analytica Ensino
112 lines • 3.21 kB
TypeScript
import type { ChatMessage, ChatParticipant } from '../types/chat';
/**
* WebSocket connection states
*/
export declare const WS_STATES: {
readonly CONNECTING: 0;
readonly OPEN: 1;
readonly CLOSING: 2;
readonly CLOSED: 3;
};
/**
* Options for the useChat hook
*/
export interface UseChatOptions {
/** WebSocket URL (e.g., wss://api.example.com/chat/ws) */
wsUrl: string;
/** JWT authentication token */
token: string;
/** Chat room ID */
roomId: string;
/** Current user's userInstitutionId */
userId: string;
/** Callback when connection is established */
onConnect?: () => void;
/** Callback when connection is closed */
onDisconnect?: () => void;
/** Callback when an error occurs */
onError?: (error: Error) => void;
/** Auto-reconnect on disconnect (default: true) */
autoReconnect?: boolean;
/** Reconnect interval in ms (default: 3000) */
reconnectInterval?: number;
/** Max reconnect attempts (default: 5) */
maxReconnectAttempts?: number;
}
/**
* Return type for the useChat hook
*/
export interface UseChatReturn {
/** Whether WebSocket is connected */
isConnected: boolean;
/** List of chat messages */
messages: ChatMessage[];
/** List of room participants */
participants: ChatParticipant[];
/** Send a text message */
sendMessage: (content: string) => void;
/** Leave the chat room */
leave: () => void;
/** Current error state */
error: Error | null;
/** Reconnect to the WebSocket */
reconnect: () => void;
/** Current user ID for identifying own messages */
currentUserId: string;
}
/**
* Hook for managing WebSocket connection to a chat room
*
* Handles:
* - WebSocket connection lifecycle
* - Message sending/receiving
* - Participant tracking
* - Auto-reconnection
*
* @param options - Hook configuration options
* @returns Chat state and actions
*
* @example
* ```tsx
* const {
* isConnected,
* messages,
* participants,
* sendMessage,
* error
* } = useChat({
* wsUrl: 'wss://api.example.com/chat/ws',
* token: authToken,
* roomId: 'room-123',
* userId: 'user-456',
* onConnect: () => console.log('Connected!'),
* onError: (err) => console.error('Error:', err),
* });
*
* const handleSend = () => {
* sendMessage('Hello, world!');
* };
* ```
*/
export declare function useChat({ wsUrl, token, roomId, userId, onConnect, onDisconnect, onError, autoReconnect, reconnectInterval, maxReconnectAttempts, }: UseChatOptions): UseChatReturn;
/**
* Factory function to create a pre-configured useChat hook
*
* @param baseWsUrl - Base WebSocket URL
* @returns Factory function that creates the hook with room-specific options
*
* @example
* ```tsx
* // In your app setup
* const createChatHook = createUseChat('wss://api.example.com/chat/ws');
*
* // In component
* const { messages, sendMessage } = createChatHook({
* token: authToken,
* roomId: 'room-123',
* userId: 'user-456',
* });
* ```
*/
export declare function createUseChat(baseWsUrl: string): (options: Omit<UseChatOptions, "wsUrl">) => UseChatReturn;
//# sourceMappingURL=useChat.d.ts.map