// eslint-disable-next-line @typescript-eslint/no-unused-vars import * as Ably from '@ably/pubsub-core'; import { useEffect, useState } from 'react'; // eslint-disable-next-line @typescript-eslint/no-unused-vars import { type ChatClient } from '../../core/chat-client.js'; import { ConnectionStatus, ConnectionStatusChange } from '../../core/connection.js'; import { useChatClientContext } from './internal/use-chat-client-context.js'; import { useLogger } from './internal/use-logger.js'; /** * The response from the {@link useChatClient} hook. */ export interface UseChatClientResponse { /** * The current clientId, if known. * * **Important** When using an Ably key for authentication, this value is determined immediately. If using a token, * the clientId is not known until the client has successfully connected to and authenticated with * the server. Use the `chatClient.connection.status` to check the connection status. */ readonly clientId?: string; } /** * React hook to access the chat client provided by the current {@link ChatClientProvider}. * * This hook automatically tracks the clientId and updates when connection state changes, * ensuring the most current client ID is always available. The client ID may change * when the underlying Ably Realtime client reconnects with different authentication. * * **Note**: This hook must be used within a {@link ChatClientProvider} component tree. * @returns A {@link UseChatClientResponse} containing the current client ID * @throws An {@link Ably.ErrorInfo} When used outside of a {@link ChatClientProvider} * @example * ```tsx * import * as Ably from '@ably/pubsub-device'; * import React from 'react'; * import { ChatClient } from '@ably/chat'; * import { ChatClientProvider, useChatClient } from '@ably/chat/react'; * * // Component that displays current user information * const UserInfo = () => { * const { clientId } = useChatClient(); * return (
Connected as: {clientId}
); * }; * * const chatClient: ChatClient; // existing ChatClient instance * * // App component with provider * const App = () => { * return ( *