import { Socket } from 'phoenix'; import { ChannelOptions } from './socket'; import { UseRealtimePropsReturn } from './useRealtimeProps'; /** * Built-in update strategies for prop arrays */ export type UpdateStrategy = AppendStrategy | PrependStrategy | RemoveStrategy | UpdateStrategy_ | UpsertStrategy | ReplaceStrategy | ReloadStrategy; /** * Append item to end of array */ export interface AppendStrategy { strategy: 'append'; transform: (event: TEvent) => TItem; } /** * Prepend item to start of array */ export interface PrependStrategy { strategy: 'prepend'; transform: (event: TEvent) => TItem; } /** * Remove items matching predicate */ export interface RemoveStrategy { strategy: 'remove'; match: (item: TItem, event: TEvent) => boolean; } /** * Update item in place by key */ export interface UpdateStrategy_ { strategy: 'update'; key: keyof TItem; transform: (event: TEvent) => TItem; } /** * Update if exists, append if not */ export interface UpsertStrategy { strategy: 'upsert'; key: keyof TItem; transform: (event: TEvent) => TItem; } /** * Replace entire prop value */ export interface ReplaceStrategy { strategy: 'replace'; transform: (event: TEvent) => unknown; } /** * Reload prop(s) from server */ export interface ReloadStrategy { strategy: 'reload'; only?: string[]; } /** * Declarative event configuration */ export interface DeclarativeEventConfig { prop: keyof TProps; strategy: UpdateStrategy['strategy']; transform?: (event: TEvent) => TItem; match?: (item: TItem, event: TEvent) => boolean; key?: keyof TItem; only?: string[]; } /** * Custom event handler with helpers */ export type CustomEventHandler = (event: TEvent, helpers: { props: TProps; setProp: UseRealtimePropsReturn['setProp']; setProps: UseRealtimePropsReturn['setProps']; reload: UseRealtimePropsReturn['reload']; }) => void; /** * Event configuration - either declarative or custom handler */ export type EventConfig, TEvent> = DeclarativeEventConfig | CustomEventHandler; /** * Map of event names to their configurations */ export type EventConfigs, TEvents extends Record> = { [K in keyof TEvents]?: EventConfig; }; /** * Return type for useChannelProps hook */ export interface UseChannelPropsReturn> extends UseRealtimePropsReturn { } /** * React hook combining Phoenix Channel subscription with Inertia prop updates * * Provides a declarative way to map channel events to prop updates using * built-in strategies (append, remove, update, etc.) or custom handlers. * * @template TProps - Type for page props (from nb_ts) * @template TEvents - Type for channel events * @param socket - Phoenix Socket instance * @param topic - Channel topic (e.g., "chat:123") * @param configs - Map of event names to configurations * @param options - Channel options (params, callbacks, enabled) * @returns Object with props and update methods * * @example * // Declarative strategies * const { props } = useChannelProps( * socket, * `chat:${roomId}`, * { * // Append new message to array * message_created: { * prop: 'messages', * strategy: 'append', * transform: e => e.message * }, * * // Remove message from array * message_deleted: { * prop: 'messages', * strategy: 'remove', * match: (msg, event) => msg.id === event.id * }, * * // Update message in place * message_edited: { * prop: 'messages', * strategy: 'update', * key: 'id', * transform: e => e.message * }, * * // Upsert (update or append) * user_status: { * prop: 'users', * strategy: 'upsert', * key: 'id', * transform: e => e.user * }, * * // Replace entire prop * room_updated: { * prop: 'room', * strategy: 'replace', * transform: e => e.room * }, * * // Reload from server * major_change: { * prop: 'messages', * strategy: 'reload', * only: ['messages', 'room'] * } * } * ); * * @example * // Custom handlers * const { props } = useChannelProps( * socket, * `chat:${roomId}`, * { * // Declarative for simple cases * message_created: { * prop: 'messages', * strategy: 'append', * transform: e => e.message * }, * * // Custom handler for complex logic * presence_changed: (event, { props, setProp, reload }) => { * if (event.userCount > 100) { * // Too many users, fall back to polling * reload({ only: ['messages'] }); * } else { * setProp('onlineUsers', event.users); * } * } * } * ); */ export declare function useChannelProps = Record, TEvents extends Record = Record>(socket: Socket | null, topic: string, configs: EventConfigs, options?: ChannelOptions): UseChannelPropsReturn; export default useChannelProps; //# sourceMappingURL=useChannelProps.d.ts.map