'use client'; import { IconButton } from '@/components/ui/button'; import { useAsyncFn } from '@/hooks/tools'; import { cn } from '@/lib/utils'; import { useStreamChatStore } from '@/app/(main)/chat/[id]/stream-chat'; import { useState, useRef, useEffect } from 'react'; const AuthHeightTextarea = ( props: React.ComponentProps<'textarea'> & { maxHeight?: number; onSend?: (text: string) => void; } ) => { const { maxHeight = 200, className, value, onChange, onSend, ...restProps } = props; const textareaRef = useRef(null); // 调整高度的函数 const adjustHeight = () => { const textarea = textareaRef.current; if (!textarea) return; // 先重置高度为 0,这样才能获取真实的 scrollHeight textarea.style.height = '0px'; // 获取内容实际需要的高度 const scrollHeight = textarea.scrollHeight; // 计算新高度:取 scrollHeight 和 maxHeight 的较小值 const newHeight = Math.min(scrollHeight, maxHeight); textarea.style.height = `${newHeight}px`; // 如果内容超过最大高度,显示滚动条 textarea.style.overflowY = scrollHeight > maxHeight ? 'auto' : 'hidden'; }; const handleKeyDown = (e: React.KeyboardEvent) => { // Enter 发送,Shift+Enter 换行 if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); // 阻止默认的换行行为 onSend?.(value as string); } }; // 监听内容变化,自动调整高度 useEffect(() => { adjustHeight(); }, [value, maxHeight]); return (