-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost-chat-container.tsx
163 lines (153 loc) · 5.8 KB
/
post-chat-container.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
"use client";
import { useChat } from "ai/react";
import { Bot, CircleUser, Share } from "lucide-react";
import Link from "next/link";
import type { FormEvent } from "react";
import { Controller, useForm, type SubmitHandler } from "react-hook-form";
import { toast } from "sonner";
import { PostTweetWrapper } from "./post-tweet-wrapper";
import { Button } from "./ui/button";
import {
Card,
CardContent,
CardDescription,
CardFooter,
CardHeader,
CardTitle,
} from "./ui/card";
import { Checkbox } from "./ui/checkbox";
import { Label } from "./ui/label";
import { ScrollArea } from "./ui/scroll-area";
import { Textarea } from "./ui/textarea";
type PostGenerateFormInput = {
prompt: string;
hashtags: boolean;
premium: boolean;
};
export const PostChatContainer = () => {
const {
register,
handleSubmit,
control,
} = useForm<PostGenerateFormInput>({
defaultValues: {
prompt: "",
hashtags: false,
premium: false,
},
});
const { messages, append, handleSubmit: handleSubmitOpenAi } = useChat();
const handlePostGenerate: SubmitHandler<PostGenerateFormInput> = (
data: PostGenerateFormInput,
e: FormEvent<HTMLFormElement>,
) => {
const promptBase = "Create a detailed post for twitter/X with the following content:";
const prompt = `${promptBase} ${data.prompt} ${data.hashtags ? ", add hashtags related to the content " : "do not insert hashtags"} ${!data.premium ? "maximun characters 250 " : "250 characters or more, focus on content and mininum of hashtags"}`;
append({
role: "user",
content: prompt,
});
handleSubmitOpenAi(e)
};
return (
<div className="flex items-center justify-center">
<Card className="w-full h-full">
<CardHeader>
<CardTitle>Post Generate</CardTitle>
<CardDescription>Create our post with Chat Bot</CardDescription>
</CardHeader>
<CardContent>
<ScrollArea className="h-[500px]">
{messages.map((message) => (
<div className="flex flex-row items-start justify-start gap-2 mt-4" key={message.id}>
{message.role === "user" && (
<>
<CircleUser className="size-6" />
<div className="flex flex-col items-start gap-2 max-w-3xl">
<p className="text-md text-muted-foreground">{message.content}</p>
</div>
</>
)}
{message.role === "assistant" && (
<>
<Bot className="size-6 text-purple-400" />
<div className="flex flex-col items-start gap-2">
<PostTweetWrapper>
<p className="text-md text-muted-foreground">{message.content}</p>
</PostTweetWrapper>
<Link href={"https://x.com/compose/post"} target="_blank">
<Button className="flex items-center gap-2" onClick={() => {
navigator.clipboard.writeText(message.content)
toast.success("Copied to clipboard")
}}>
<Share className="size-4" /> Copy and create a post!
</Button>
</Link>
</div>
</>
)}
</div>
))}
</ScrollArea>
</CardContent>
<CardFooter>
<form
className="flex flex-col w-full"
onSubmit={handleSubmit(handlePostGenerate)}
>
<div className="flex flex-col gap-4">
<Card className="w-full p-4 flex flex-col gap-4">
<Textarea
{...register("prompt")}
className="resize-none border-none focus-within:outline-none focus:outline-none focus-visible:ring-0 focus-within:border-none focus:ring-0 focus-visible:ring-offset-0"
placeholder="What's the subject of your post?"
/>
<Button className="self-end" type="submit">Generate Post</Button>
</Card>
<div className="flex gap-2 items-center">
<Controller
control={control}
render={({ field }) => (
<div className="flex items-center space-x-2">
<Checkbox
checked={field.value}
onCheckedChange={(value) => field.onChange(value)}
id="hashtag"
/>
<Label
htmlFor="hashtag"
className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
>
Do you want Hashtags?
</Label>
</div>
)}
name="hashtags"
/>
<Controller
control={control}
render={({ field }) => (
<div className="flex items-center space-x-2">
<Checkbox
checked={field.value}
onCheckedChange={(value) => field.onChange(value)}
id="premium"
/>
<Label
htmlFor="premium"
className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
>
Premium account?
</Label>
</div>
)}
name="premium"
/>
</div>
</div>
</form>
</CardFooter>
</Card>
</div>
);
};