import { TypingListener, TypingMember } from '../../core/typing.js'; import { ChatStatusResponse } from '../types/chat-status-response.js'; import { Listenable } from '../types/listenable.js'; import { StatusParams } from '../types/status-params.js'; /** * The parameters for the {@link useTyping} hook. */ export interface TypingParams extends StatusParams, Listenable { /** * A listener that will be called whenever a typing event is sent to the room. * The listener is removed when the component unmounts. * @example * ```tsx * useTyping({ * listener: (typingEvent) => { * console.log('Typing event:', Array.from(typingEvent.currentlyTyping)); * } * }); * ``` */ listener?: TypingListener; } export interface UseTypingResponse extends ChatStatusResponse { /** * A shortcut to the {@link Typing.keystroke} method. * * Sends a typing started event to notify other users that the current user is typing. * * Events are throttled according to the `heartbeatThrottleMs` room option to prevent * excessive network traffic. If called within the throttle interval, the operation * becomes a no-op. Multiple rapid calls are serialized to maintain consistency. * * **Note**: * - The connection must be in the `connected` state. * - Calls to `keystroke()` and `stop()` are serialized and resolve in order. * - The most recent operation always determines the final typing state. * - The room must be attached to send typing events, typically the {@link ChatRoomProvider} handles this automatically. * * This is a stable reference and will not be changed between renders for the same room. * @returns Promise that resolves when the typing event has been sent, or rejects with: * - {@link chat-js!ErrorCode.Disconnected | Disconnected} if not connected * - {@link chat-js!ErrorCode.OperationSerializationFailed | OperationSerializationFailed} if mutex acquisition fails * - {@link Ably.ErrorInfo} if the operation fails to send the event * @example * ```tsx * const { keystroke } = useTyping(); * * const handleKeyPress = async () => { * try { * await keystroke(); * console.log('Typing indicator sent'); * } catch (error) { * console.error('Failed to send keystroke:', error); * } * }; * ``` */ readonly keystroke: () => Promise; /** * A shortcut to the {@link Typing.stop} method. * * Sends a typing stopped event to notify other users that the current user has stopped typing. * * If the user is not currently typing, this operation is a no-op. Multiple rapid calls * are serialized to maintain consistency, with the most recent operation determining * the final state. * * **Note**: * - The connection must be in the `connected` state. * - Calls to `keystroke()` and `stop()` are serialized and resolve in order. * - The room must be attached to send typing events, typically the {@link ChatRoomProvider} handles this automatically. * * This is a stable reference and will not be changed between renders for the same room. * @returns Promise that resolves when the stop event has been sent, or rejects with: * - {@link chat-js!ErrorCode.Disconnected | Disconnected} if not connected * - {@link chat-js!ErrorCode.OperationSerializationFailed | OperationSerializationFailed} if mutex acquisition fails * - {@link Ably.ErrorInfo} if the operation fails to send the event * @example * ```tsx * const { stop } = useTyping(); * * const handleStopTyping = async () => { * try { * await stop(); * console.log('Stopped typing indicator'); * } catch (error) { * console.error('Failed to stop typing:', error); * } * }; * ``` */ readonly stop: () => Promise; /** * A state value representing the set of client IDs that are currently typing in the room. * It automatically updates based on typing events received from the room. * @deprecated Use {@link UseTypingResponse.currentTypers | currentTypers} instead, which includes metadata such as user claims. */ readonly currentlyTyping: Set; /** * A state value representing the set of users currently typing in the room, with associated metadata. * It automatically updates based on typing events received from the room. */ readonly currentTypers: TypingMember[]; } /** * * React hook that provides typing indicator functionality for chat rooms. * * The hook automatically tracks the set of users currently typing and provides * this as state that updates in real-time as users start and stop typing. * * **Note**: * - This hook must be used within a {@link ChatRoomProvider} component tree. * - The `Room` must be attached to send and receive typing indicators, typically the {@link ChatRoomProvider} handles this automatically. * @param params - Optional parameters for event listeners and room status callbacks * @returns A {@link UseTypingResponse} containing typing methods and current state * @throws An {@link Ably.ErrorInfo} with {@link chat-js!ErrorCode.ReactHookMustBeUsedWithinProvider | ReactHookMustBeUsedWithinProvider} When used outside of a {@link ChatRoomProvider} * @example Basic usage * ```tsx * import React, { useState } from 'react'; * import { ChatClient, TypingSetEvent } from '@ably/chat'; * import { * ChatClientProvider, * ChatRoomProvider, * useTyping * } from '@ably/chat/react'; * * // Component that handles typing indicators * const TypingIndicator = () => { * const { keystroke, stop, currentlyTyping } = useTyping({ * listener: (typingEvent: TypingSetEvent) => { * console.log('Currently typing users:', Array.from(typingEvent.currentlyTyping)); * }, * }); * * const handleInputChange = async (e: React.ChangeEvent) => { * const value = e.target.value; * if (value.length > 0) { * try { * await keystroke(); * console.log('Started typing'); * } catch (error) { * console.error('Failed to send keystroke:', error); * } * } else { * try { * await stop(); * console.log('Stopped typing'); * } catch (error) { * console.error('Failed to stop typing:', error); * } * } * }; * * return ( *
* *
Currently typing: {Array.from(currentlyTyping).join(', ')}
*
* ); * }; * * const chatClient: ChatClient; // existing ChatClient instance * * const App = () => { * return ( * * * * * * ); * }; * * export default App; * ``` */ export declare const useTyping: (params?: TypingParams) => UseTypingResponse;