-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.tsx
68 lines (60 loc) · 1.67 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* eslint-disable max-len */
import Head from "next/head";
import { useEffect, useRef, useState } from "react";
import { StreamingText } from "../components/StreamingText";
import { useTextBuffer } from "../hooks";
export default function Home() {
const nameRef = useRef<HTMLInputElement>(null);
const [data, setData] = useState({ name: "John" });
const { buffer, done, error, refresh, cancel } = useTextBuffer({
url: "/api/chat",
throttle: 100,
options: {
method: "POST",
},
data,
});
// eslint-disable-next-line no-console
console.log({ buffer, done, error });
useEffect(
() => {
const name = nameRef.current?.value;
if (name) setData((data) => ({ ...data, name }));
},
[]
);
const setName = () => {
const name = nameRef?.current?.value;
if (name) {
setData((data) => ({ ...data, name }));
refresh();
}
};
return (
<>
<Head>
<title>OpenAI Completion Stream</title>
</Head>
<main>
<div>
<label>Name</label>
<input
ref={nameRef}
defaultValue={data.name}
placeholder="Type a name here..."
/>
</div>
<div className="w-full max-w-md p-4 rounded-lg border-solid border-2 border-gray-400">
{/* <StreamingTextURL url="/api/demo" fade={600} throttle={100} data={data} /> */}
<StreamingText buffer={buffer} />
</div>
<div className="flex flex-row">
<button className="w-full" onClick={setName}>
Refresh
</button>
<button className="w-full" onClick={cancel}>Cancel</button>
</div>
</main>
</>
);
}