UNPKG

react-use-chat

Version:

一个用于构建对话式引导流程的 React Hook,支持单选、多选和嵌套分支处理

131 lines (126 loc) 5.28 kB
import React from 'react'; /** * 对话系统类型定义 */ interface DialogAnswer { answer_id: string; answer_text: string; next_node_id: string | null; plan_trigger: string | null; execution_order?: number; mutually_exclusive?: boolean; } type AnswerType = "single_select" | "multi_select" | "auto_complete"; interface DialogNode { node_id: string; question_text: string; answer_type: AnswerType; answers: DialogAnswer[]; default_next_node_id?: string | null; default_plan_trigger?: string | null; execute_by_config_order?: boolean; is_branch_end?: boolean; } interface HistoryItem { nodeId: string; question: string; answer: string | null; answerId: string; multiSelected?: string[]; type?: 'message' | 'learning_plan'; planData?: string[]; } interface SelectionBranch { answer_id: string; next_node_id: string; plan_trigger: string | null; } interface UseDialogOptions { /** 初始节点 ID,如果不提供则使用数据源的第一个节点 */ initialNodeId?: string; /** 自定义节点查找函数 */ findNodeById?: (nodeId: string) => DialogNode | null; /** 自动完成节点的延迟时间(毫秒) */ autoCompleteDelay?: number; } interface UseDialogReturn { /** 当前显示的对话节点 */ currentNode: DialogNode | null; /** 用户的对话历史记录 */ history: HistoryItem[]; /** 收集到的学习计划触发器列表 */ learningPlan: string[]; /** 在多选模式下,当前用户勾选的答案ID列表 */ selectedAnswers: string[]; /** 管理嵌套多选分支的栈 */ multiSelectBranchStack: SelectionBranch[][]; /** 对应 multiSelectBranchStack 中每个层级当前已处理到的分支索引的栈 */ currentBranchIndices: number[]; /** 在对话流程结束前,临时收集的所有计划触发器 */ pendingPlanTriggers: string[]; /** 处理单选答案选择 */ handleSelection: (answerId: string) => void; /** 处理多选答案勾选/取消勾选 */ handleMultiSelect: (answerId: string) => void; /** 用户在多选模式下确认选择后调用 */ confirmSelections: () => void; /** 重置整个对话状态到初始状态 */ resetDialog: () => void; } declare const SCROLL_TO_BOTTOM_EVENT = "scrollToBottom"; /** * @hook useDialog * @description 用于构建对话式引导流程的 React Hook,支持单选、多选和嵌套分支处理 * @param dialogData 对话节点数据数组 * @param options 配置选项 * @returns 对话状态和操作方法 */ declare function useDialog$1(dialogData: DialogNode[], options?: UseDialogOptions): UseDialogReturn; /** * @interface DialogContextType * @description 对话上下文的状态和操作方法定义 * @property {DialogNode | null} currentNode - 当前显示的对话节点 * @property {HistoryItem[]} history - 用户的对话历史记录 * @property {string[]} learningPlan - 收集到的学习计划触发器列表 * @property {string[]} selectedAnswers - 在多选模式下,当前用户勾选的答案ID列表 * @property {SelectionBranch[][]} multiSelectBranchStack - 管理嵌套多选分支的栈,每个元素是当前层级的待处理分支数组 * @property {number[]} currentBranchIndices - 对应 multiSelectBranchStack 中每个层级当前已处理到的分支索引的栈 * @property {string[]} pendingPlanTriggers - 在对话流程结束前,临时收集的所有计划触发器 * @property {(answerId: string) => void} handleSelection - 处理单选答案选择的函数 * @property {(answerId: string) => void} handleMultiSelect - 处理多选答案勾选/取消勾选的函数 * @property {() => void} confirmSelections - 用户在多选模式下确认选择后调用的函数 * @property {() => void} resetDialog - 重置整个对话状态到初始状态的函数 */ interface DialogContextType { currentNode: DialogNode | null; history: HistoryItem[]; learningPlan: string[]; selectedAnswers: string[]; multiSelectBranchStack: SelectionBranch[][]; currentBranchIndices: number[]; pendingPlanTriggers: string[]; handleSelection: (answerId: string) => void; handleMultiSelect: (answerId: string) => void; confirmSelections: () => void; resetDialog: () => void; } /** * @hook useDialog * @description 自定义 Hook,用于方便地访问对话上下文。 * @returns {DialogContextType} 对话上下文对象 */ declare const useDialog: () => DialogContextType; /** * @component DialogProvider * @description 对话状态管理和逻辑处理的核心组件。 * 使用 React Context API 向其子组件提供对话状态和操作方法。 * @param {object} props - 组件属性 * @param {React.ReactNode} props.children - 子组件 * @param {DialogNode[]} props.dialogData - 对话节点数据 */ declare const DialogProvider: React.FC<{ children: React.ReactNode; dialogData: DialogNode[]; }>; export { DialogProvider, SCROLL_TO_BOTTOM_EVENT, useDialog$1 as useDialog, useDialog as useDialogContext }; export type { AnswerType, DialogAnswer, DialogContextType, DialogNode, HistoryItem, SelectionBranch, UseDialogOptions, UseDialogReturn };