import React, { useEffect, useRef, useState } from "react"; import { Button } from "@/components/ui/button"; import { RotateCw } from "lucide-react"; import Markdown from "react-markdown"; function ResultAI({ completion, isLoading, onCompletion, error, }: { completion: string; isLoading: boolean; onCompletion: () => void; error: string; }) { const scrollRef = useRef(null); const [autoScroll, setAutoScroll] = useState(false); useEffect(() => { setAutoScroll(isLoading); }, [isLoading]); useEffect(() => { if (!autoScroll) { return; } scrollTo(); }); function scrollTo() { requestAnimationFrame(() => { if ( !scrollRef.current || scrollRef.current.scrollHeight === scrollRef.current.clientHeight + scrollRef.current.scrollTop ) { return; } scrollRef.current.scrollTo(0, scrollRef.current.scrollHeight); }); } function onScroll(e: HTMLElement) { if (!isLoading) { return; } const hitBottom = e.scrollTop + e.clientHeight >= e.scrollHeight - 15; if (hitBottom === autoScroll) { return; } setAutoScroll(hitBottom); } return (
{isLoading && (
AI 分析中...
)}
onScroll(e.currentTarget)} className="max-h-full overflow-auto rounded-md border p-3 shadow dark:border-0 dark:bg-secondary/90 dark:shadow-none sm:p-5" > {error ? (
ಠ_ಠ 请求出错了!
{error}
) : ( {completion} )} {!isLoading && ( )}
); } export default ResultAI;