-
Notifications
You must be signed in to change notification settings - Fork 482
/
Copy pathindex.tsx
184 lines (172 loc) · 6.34 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { AnimatePresence, motion } from "framer-motion";
import type { NextPage } from "next";
import Head from "next/head";
import Image from "next/image";
import { useState } from "react";
import { toast, Toaster } from "react-hot-toast";
import DropDown from "../components/DropDown";
import Footer from "../components/Footer";
import Github from "../components/GitHub";
import Header from "../components/Header";
import LoadingDots from "../components/LoadingDots";
import ResizablePanel from "../components/ResizablePanel";
import { type VibeType } from "../utils/prompt";
const Home: NextPage = () => {
const [loading, setLoading] = useState(false);
const [bio, setBio] = useState("");
const [vibe, setVibe] = useState<VibeType>("Professional");
const [generatedBios, setGeneratedBios] = useState<String>("");
console.log("Streamed response: ", generatedBios);
const generateBio = async (e: any) => {
e.preventDefault();
setGeneratedBios("");
setLoading(true);
const response = await fetch("/api/generate", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
bio,
vibe,
}),
});
console.log("Edge function returned.");
if (!response.ok) {
throw new Error(response.statusText);
}
// This data is a ReadableStream
const data = response.body;
if (!data) {
return;
}
const reader = data.getReader();
const decoder = new TextDecoder();
let done = false;
while (!done) {
const { value, done: doneReading } = await reader.read();
done = doneReading;
const chunkValue = decoder.decode(value);
setGeneratedBios((prev) => prev + chunkValue);
}
setLoading(false);
};
return (
<div className="flex max-w-5xl mx-auto flex-col items-center justify-center py-2 min-h-screen">
<Head>
<title>Twitter Generator</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<Header />
<main className="flex flex-1 w-full flex-col items-center justify-center text-center px-4 mt-12 sm:mt-20">
<a
className="flex max-w-fit items-center justify-center space-x-2 rounded-full border border-gray-300 bg-white px-4 py-2 text-sm text-gray-600 shadow-md transition-colors hover:bg-gray-100 mb-5"
href="https://github.com/Nutlope/twitterbio"
target="_blank"
rel="noopener noreferrer"
>
<Github />
<p>Star on GitHub</p>
</a>
<h1 className="sm:text-6xl text-4xl max-w-2xl font-bold text-slate-900">
Generate your next Twitter bio in seconds
</h1>
<p className="text-slate-500 mt-5">18,167 bios generated so far.</p>
<div className="max-w-xl">
<div className="flex mt-10 items-center space-x-3">
<Image
src="/1-black.png"
width={30}
height={30}
alt="1 icon"
className="mb-5 sm:mb-0"
/>
<p className="text-left font-medium">
Copy your current bio{" "}
<span className="text-slate-500">
(or write a few sentences about yourself)
</span>
.
</p>
</div>
<textarea
value={bio}
onChange={(e) => setBio(e.target.value)}
rows={4}
className="w-full rounded-md border-gray-300 shadow-sm focus:border-black focus:ring-black my-5"
placeholder={
"e.g. Senior Developer Advocate @vercel. Tweeting about web development, AI, and React / Next.js. Writing nutlope.substack.com."
}
/>
<div className="flex mb-5 items-center space-x-3">
<Image src="/2-black.png" width={30} height={30} alt="1 icon" />
<p className="text-left font-medium">Select your vibe.</p>
</div>
<div className="block">
<DropDown vibe={vibe} setVibe={(newVibe) => setVibe(newVibe)} />
</div>
{!loading && (
<button
className="bg-black rounded-xl text-white font-medium px-4 py-2 sm:mt-10 mt-8 hover:bg-black/80 w-full"
onClick={(e) => generateBio(e)}
>
Generate your bio →
</button>
)}
{loading && (
<button
className="bg-black rounded-xl text-white font-medium px-4 py-2 sm:mt-10 mt-8 hover:bg-black/80 w-full"
disabled
>
<LoadingDots color="white" style="large" />
</button>
)}
</div>
<Toaster
position="top-center"
reverseOrder={false}
toastOptions={{ duration: 2000 }}
/>
<hr className="h-px bg-gray-700 border-1 dark:bg-gray-700" />
<ResizablePanel>
<AnimatePresence mode="wait">
<motion.div className="space-y-10 my-10">
{generatedBios && (
<>
<div>
<h2 className="sm:text-4xl text-3xl font-bold text-slate-900 mx-auto">
Your generated bios
</h2>
</div>
<div className="space-y-8 flex flex-col items-center justify-center max-w-xl mx-auto">
{generatedBios
.substring(generatedBios.indexOf("1") + 3)
.split("2.")
.map((generatedBio) => {
return (
<div
className="bg-white rounded-xl shadow-md p-4 hover:bg-gray-100 transition cursor-copy border"
onClick={() => {
navigator.clipboard.writeText(generatedBio);
toast("Bio copied to clipboard", {
icon: "✂️",
});
}}
key={generatedBio}
>
<p>{generatedBio}</p>
</div>
);
})}
</div>
</>
)}
</motion.div>
</AnimatePresence>
</ResizablePanel>
</main>
<Footer />
</div>
);
};
export default Home;