import { ConnectionStatusChange } from '../../core/connection.js'; import { RoomStatusChange } from '../../core/room-status.js'; import { ChatStatusResponse } from '../types/chat-status-response.js'; /** * The parameters for the {@link useRoom} hook. */ export interface UseRoomParams { /** * Callback for when the room status changes. The listener is removed when * the component unmounts. * @param change The change object. * @example * ```tsx * useRoom({ * onStatusChange: (change) => { * console.log(`Room status changed from ${change.previous} to ${change.current}`); * }, * }); *``` */ onStatusChange?: (change: RoomStatusChange) => void; /** * Callback for when the connection status changes. The listener is removed * when the component unmounts. * @param change The change object. * @example * ```tsx * useRoom({ * onConnectionStatusChange: (change) => { * console.log(`Connection changed from ${change.previous} to ${change.current}`); * }, * }); * ``` */ onConnectionStatusChange?: (change: ConnectionStatusChange) => void; } /** * The return type for the {@link useRoom} hook. */ export interface UseRoomResponse extends ChatStatusResponse { /** * The unique identifier of the room. */ readonly roomName: string; /** * Shortcut to {@link Room.attach}. The {@link ChatRoomProvider} will handle attaching and detaching * automatically, so this is only needed for manual control. * * This is a stable reference and will not be changed between renders for the same room. * @example * ```tsx * const { attach, roomStatus } = useRoom(); * * const handleManualAttach = async () => { * try { * await attach(); * console.log('Room attached successfully'); * } catch (error) { * console.error('Failed to attach room:', error); * } * }; * ``` */ attach: () => Promise; /** * Shortcut to {@link Room.detach}. The {@link ChatRoomProvider} will handle attaching and detaching * automatically, so this is only needed for manual control. * * This is a stable reference and will not be changed between renders for the same room. * @example * ```tsx * const { detach, roomStatus } = useRoom(); * * const handleManualDetach = async () => { * try { * await detach(); * console.log('Room detached successfully'); * } catch (error) { * console.error('Failed to detach room:', error); * } * }; * ``` */ detach: () => Promise; } /** * React hook that provides access to room information and basic room operations. * * Typically, you won't need to call `attach()` or `detach()` manually since the * {@link ChatRoomProvider} handles room lifecycle automatically. * * **Note**: This hook must be used within a {@link ChatRoomProvider} component tree. * @param params - Optional parameters for status change callbacks * @returns A {@link UseRoomResponse} containing room data and operations * @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 from 'react'; * import { ChatClient } from '@ably/chat'; * import { * ChatClientProvider, * ChatRoomProvider, * useRoom * } from '@ably/chat/react'; * * // Component that displays room information * const RoomInfo = () => { * const { roomName, roomStatus, connectionStatus } = useRoom({ * onStatusChange: (change) => { * console.log(`Room status changed from ${change.previous} to ${change.current}`); * }, * onConnectionStatusChange: (change) => { * console.log(`Connection changed from ${change.previous} to ${change.current}`); * } * }); * * return ( *
*
Room: {roomName}
*
Status: {roomStatus}
*
Connection: {connectionStatus}
*
* ); * }; * * const chatClient: ChatClient; // existing ChatClient instance * * // App component with providers * const App = () => { * return ( * * * * * * ); * }; * * export default App; * ``` */ export declare const useRoom: (params?: UseRoomParams) => UseRoomResponse;