import { useParams, useSearchParams } from "next/navigation"; import { useEffect, useMemo, useRef } from "react"; import { usePromptInputController } from "@/components/ai-elements/prompt-input"; import { useI18n } from "@/core/i18n/hooks"; /** * 根据 URL 参数判断当前是否处于特定 Chat 模式(如 iframe 技能模式), * 并根据模式自动填入默认 prompt 值。 */ export function useSpecificChatMode() { const { t } = useI18n(); const { thread_id: threadIdFromPath } = useParams<{ thread_id: string }>(); const searchParams = useSearchParams(); const promptInputController = usePromptInputController(); const inputInitialValue = useMemo(() => { if (threadIdFromPath !== "new" || searchParams.get("mode") !== "skill") { return undefined; } return t.inputBox.createSkillPrompt; }, [threadIdFromPath, searchParams, t.inputBox.createSkillPrompt]); const lastInitialValueRef = useRef(undefined); const setInputRef = useRef(promptInputController.textInput.setInput); setInputRef.current = promptInputController.textInput.setInput; useEffect(() => { if ( inputInitialValue && inputInitialValue !== lastInitialValueRef.current ) { lastInitialValueRef.current = inputInitialValue; setTimeout(() => { setInputRef.current(inputInitialValue); const textarea = document.querySelector("textarea"); if (textarea) { textarea.focus(); textarea.selectionStart = textarea.value.length; textarea.selectionEnd = textarea.value.length; } }, 100); } }, [inputInitialValue]); }