import { PresenceData } from '../../core/presence.js'; import { ChatStatusResponse } from '../types/chat-status-response.js'; import { StatusParams } from '../types/status-params.js'; import * as Ably from '@ably/pubsub-core'; /** * The options for the {@link usePresence} hook. */ export interface UsePresenceParams extends StatusParams { /** * The initial data to enter the room with when auto-entering (autoEnterLeave=true). Any JSON serializable data can be provided. * This data is only used for the initial auto-enter when the component mounts. Changes to this value * after the first render are ignored. To update presence data after the initial enter, use the * `update` or `enter` methods returned by the hook. * @example * ```tsx * // This will cause the hook to auto-enter presence with the provided data * // autoEnterLeave is implicitly true * const { presence, update } = usePresence({ * initialData: { status: 'online', lastSeen: Date.now() } * }); * * // Subsequent data updates must be done via calls to enter/update * await update({status: 'away'}); * ``` * @defaultValue undefined */ initialData?: PresenceData; /** * Controls whether the hook should automatically enter presence when the component mounts and the room * becomes attached, and automatically leave presence when the component unmounts. * * Also controls whether the hook will automatically re-enter presence if the room is detached and then re-attached. * * **Important** If {@link UsePresenceResponse.leave} is called, then the hook will NOT auto-enter. To re-enable * auto-enter behavior, you must call {@link UsePresenceResponse.enter} or {@link UsePresenceResponse.update}. * When set to false, you have full manual control over entering and leaving presence. * * Defaults to true if not provided. * @defaultValue true */ autoEnterLeave?: boolean; } export interface UsePresenceResponse extends ChatStatusResponse { /** * A shortcut to the {@link Presence.update} method. * * Updates the presence data for the current user in the chat room. * Emits an 'update' event to all subscribers. If the user is not already present, they will be entered automatically. * * **Note**: * - The room must be attached before calling this method. * - This method uses PUT-like semantics - the entire presence data is replaced with the new value. * * This is a stable reference and will not be changed between renders for the same room. * * **Important** When called, if {@link UsePresenceParams.autoEnterLeave} is set to true, the hook will attempt to * auto-enter presence automatically when conditions are met. * @param data - JSON-serializable data to replace the user's current presence data * @returns Promise that resolves when the presence data has been updated, * or rejects with {@link chat-js!ErrorCode.RoomInInvalidState | RoomInInvalidState} if the room is not attached */ readonly update: (data?: PresenceData) => Promise; /** * A shortcut to the {@link Presence.enter} method, which can be used to manually enter presence when * `autoEnterLeave` is false, or to explicitly re-enter presence with new data. * * Enters the current user into the chat room presence set. * Emits an 'enter' event to all presence subscribers. Multiple calls will emit additional `update` events if the * user is already present. * * **Note**: The room must be attached before calling this method. * * This is a stable reference and will not be changed between renders for the same room. * * **Important** When called, if {@link UsePresenceParams.autoEnterLeave} is set to true, the hook will attempt to * auto-enter presence automatically when conditions are met. * @param data - Optional JSON-serializable data to associate with the user's presence * @returns Promise that resolves when the user has successfully entered, * or rejects with {@link chat-js!ErrorCode.RoomInInvalidState | RoomInInvalidState} if the room is not attached * @example * ```tsx * // Manual control over presence with conditional logic * const { enter, leave } = usePresence({ autoEnterLeave: false }); * * useEffect(() => { * if (effectCondition) { * enter({ status: 'active' }); * } * * return () => { * if (effectCondition) { * leave(); * } * }; * }, [effectCondition, enter, leave]); * ``` */ readonly enter: (data?: PresenceData) => Promise; /** * A shortcut to the {@link Presence.leave} method. * * Removes the current user from the chat room presence set. * Emits a 'leave' event to all subscribers. If the user is not present, this is a no-op. * * **Note**: The room must be attached before calling this method. * * This is a stable reference and will not be changed between renders for the same room. * * **Important** When called, this will prevent the hook from automatically re-entering presence, even when `autoEnterLeave` is true. * * This is useful for manually controlling when presence is left. * @param data - Optional final presence data to include with the leave event * @returns Promise that resolves when the user has left the presence set, * or rejects with {@link chat-js!ErrorCode.RoomInInvalidState | RoomInInvalidState} if the room is not attached * @example * ```tsx * // Manual control over presence with conditional logic * const { enter, leave } = usePresence({ autoEnterLeave: false }); * * useEffect(() => { * if (effectCondition) { * enter({ status: 'active' }); * } * * return () => { * if (effectCondition) { * leave(); * } * }; * }, [effectCondition, enter, leave]); * ``` * @example * ```tsx * // Enter presence automatically with some initial data * const { leave, enter } = usePresence({ initialData: { status: 'online' } }); * * // Leave presence explicitly, disabling auto re-entry * await leave(); * * // Re-enter presence again, re-enabling auto-entry if selected in the hook * await enter({ status: 'online again' }) * ``` */ readonly leave: (data?: PresenceData) => Promise; /** * The current presence state of this client. */ readonly myPresenceState: { /** * Indicates if the user is currently present in the room. */ present: boolean; /** * Indicates if an error occurred while trying to enter or leave presence. */ error?: Ably.ErrorInfo; }; } /** * React hook that manages user presence in a chat room. * * This hook provides comprehensive presence functionality with both automatic and manual * control modes. It handles entering/leaving presence, updating presence data, and * tracking the current presence state with automatic recovery after disconnections. * * By default (`autoEnterLeave: true`), the hook automatically enters presence when the * component mounts and the room is attached, and leaves when unmounting. It also handles * automatic re-entry after room detachment/reattachment cycles. * * With `autoEnterLeave: false`, you have full manual control over presence lifecycle * using the returned `enter` and `leave` methods. * * **Important**: When using `autoEnterLeave`, avoid multiple instances of this hook within * the same ChatClientProvider, as they share the same underlying presence instance. For * multiple components updating presence data, either: * 1. Set `autoEnterLeave: false` and manage state manually * 2. Manage presence state at a higher level (e.g., context provider) * * **Note**: This hook must be used within a {@link ChatRoomProvider} component tree. * **Note**: Room must be attached and connection active for presence operations, typically the {@link ChatRoomProvider} handles this automatically. * @param params - Optional parameters for initial data, auto-enter/leave behavior, and status callbacks * @returns A {@link UsePresenceResponse} containing presence methods and current state * @example * ```tsx * // Example with full manual control (no auto-enter/leave) * const ManualPresenceComponent = () => { * const { enter, leave, update, myPresenceState } = usePresence({ * autoEnterLeave: false, * initialData: { status: 'available' } * }); * * const handleJoin = () => enter({ status: 'online' }); * const handleLeave = () => leave(); * const handleUpdateStatus = () => update({ status: 'busy' }); * * return ( *
* * * *
Present: {myPresenceState.present}
*
* ); * }; * ``` * @example * ```tsx * // Example with auto-enter but taking manual control via leave. * // This pattern is useful if you have multiple components in your app updating presence data. * const MixedControlComponent = () => { * const { leave, update, myPresenceState } = usePresence({ * initialData: { status: 'online' } * }); * * const handleGoOffline = () => { * // Calling leave() prevents auto re-entry until enter() or update() is called * leave({ status: 'offline' }); * }; * * const handleUpdatePresence = () => { * // Calling update() re-enables auto-enter behavior * update({ status: 'back online' }); * }; * * return ( *
* * *
Present: {myPresenceState.present}
*
* ); * }; * ``` * @example * ```tsx * // Example with manual mount/unmount behavior using enter/leave explicitly * const ManualMountComponent = () => { * const { enter, leave, myPresenceState } = usePresence({ * autoEnterLeave: false, * initialData: { status: 'ready' } * }); * * // Manual mount behavior - enter presence when component mounts * useEffect(() => { * enter({ status: 'active' }); * * // Manual unmount behavior - leave presence when component unmounts * return () => { * leave({ status: 'disconnecting' }); * }; * }, [enter, leave]); * * return
Present: {myPresenceState.present}
; * }; * ``` */ export declare const usePresence: (params?: UsePresenceParams) => UsePresenceResponse;