Skip to content

Commit 9006d0c

Browse files
committed
Fix result-ai content scrolling
1 parent 6a8b761 commit 9006d0c

4 files changed

Lines changed: 43 additions & 10 deletions

File tree

app/api/openai/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { OpenAI } from "openai";
22
import { OpenAIStream, StreamingTextResponse } from "ai";
33
import { NextResponse } from "next/server";
44
import { kv } from "@vercel/kv";
5-
import { history, KV_DATA } from "@/lib/constant";
5+
import { History, KV_DATA } from "@/lib/constant";
66
import { getLocaleTime } from "@/lib/utils";
77
import { apiKeyPool } from "@/lib/pool";
88

@@ -25,7 +25,7 @@ export async function POST(req: Request) {
2525
const guaChange = splitList[2];
2626

2727
if (process.env.KV_REST_API_URL) {
28-
kv.lpush<history>(KV_DATA, {
28+
kv.lpush<History>(KV_DATA, {
2929
prompt: prompt,
3030
gua: guaMark,
3131
change: guaChange,

app/history/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from "react";
22
import clsx from "clsx";
33
import { kv } from "@vercel/kv";
4-
import { history, KV_DATA } from "@/lib/constant";
4+
import { History, KV_DATA } from "@/lib/constant";
55

66
const tdStyle = "border py-2 px-3 sm:px-4";
77

@@ -13,7 +13,7 @@ async function Page() {
1313
</div>
1414
);
1515
}
16-
const data = await kv.lrange<history>(KV_DATA, 0, 100);
16+
const data = await kv.lrange<History>(KV_DATA, 0, 100);
1717
return (
1818
<div className="mx-auto w-[88%] max-w-2xl py-6 sm:py-10">
1919
<table className="w-full table-fixed shadow">

components/result-ai.tsx

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useRef } from "react";
1+
import React, { useEffect, useRef, useState } from "react";
22
import { Button } from "@/components/ui/button";
33
import { RotateCw } from "lucide-react";
44

@@ -13,11 +13,43 @@ function ResultAI({
1313
onCompletion: () => void;
1414
error: Error | undefined;
1515
}) {
16-
const resultRef = useRef<any>(null);
16+
const scrollRef = useRef<HTMLElement>(null);
17+
const [autoScroll, setAutoScroll] = useState(false);
1718

1819
useEffect(() => {
19-
resultRef.current.scrollTop = resultRef.current.scrollHeight;
20-
}, [completion, isLoading]);
20+
setAutoScroll(isLoading);
21+
}, [isLoading]);
22+
23+
useEffect(() => {
24+
if (!autoScroll) {
25+
return;
26+
}
27+
scrollTo();
28+
});
29+
30+
function scrollTo() {
31+
requestAnimationFrame(() => {
32+
if (
33+
!scrollRef.current ||
34+
scrollRef.current.scrollHeight ===
35+
scrollRef.current.clientHeight + scrollRef.current.scrollTop
36+
) {
37+
return;
38+
}
39+
scrollRef.current.scrollTo(0, scrollRef.current.scrollHeight);
40+
});
41+
}
42+
43+
function onScroll(e: HTMLElement) {
44+
if (!isLoading) {
45+
return;
46+
}
47+
const hitBottom = e.scrollTop + e.clientHeight >= e.scrollHeight - 15;
48+
if (hitBottom === autoScroll) {
49+
return;
50+
}
51+
setAutoScroll(hitBottom);
52+
}
2153

2254
return (
2355
<div className="h-0 w-full flex-1 sm:max-w-md md:max-w-2xl">
@@ -30,7 +62,8 @@ function ResultAI({
3062
</div>
3163
)}
3264
<article
33-
ref={resultRef}
65+
ref={scrollRef}
66+
onScroll={(e) => onScroll(e.currentTarget)}
3467
className="max-h-full overflow-auto whitespace-break-spaces rounded-md border p-3 shadow dark:border-0 dark:bg-secondary/90 dark:shadow-none sm:p-5"
3568
>
3669
{error ? (

lib/constant.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export const KV_DATA = "divination";
22

3-
export interface history {
3+
export interface History {
44
date: string;
55
prompt: string;
66
gua: string;

0 commit comments

Comments
 (0)