-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpage.tsx
More file actions
106 lines (94 loc) · 3.12 KB
/
page.tsx
File metadata and controls
106 lines (94 loc) · 3.12 KB
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
"use client";
import React, { useState, useEffect, FormEvent } from 'react';
import { useSession, signIn, signOut } from "next-auth/react";
import markdownit from 'markdown-it';
const md = markdownit();
function LoginStatus() {
const { data: session } = useSession();
if (session && session.user) {
return (
<div className="login-status">
Signed in as {session.user.name} <br />
<button onClick={() => signOut()}>Sign out</button>
</div>
);
}
return (
<div className="login-status">
Not signed in <br />
<button onClick={() => signIn("twitter")}>Sign in with Twitter</button>
</div>
);
}
export default function Home() {
const { data: session } = useSession();
const [markdown, setMarkdown] = useState('');
const [isStreaming, setIsStreaming] = useState(false);
const [highlightsInput, setHighlightsInput] = useState('');
const handleClick = async (e: FormEvent) => {
e.preventDefault();
setMarkdown('');
setIsStreaming(true);
const params = new URLSearchParams();
if (highlightsInput.trim()) {
params.set('highlights', highlightsInput);
}
const url = params.toString()
? `/api/twitter?${params.toString()}`
: `/api/twitter`;
const eventSource = new EventSource(url);
eventSource.onmessage = (event) => {
const data = JSON.parse(event.data);
setMarkdown((prev) => prev + data.chunk);
};
eventSource.onerror = (error) => {
console.error('EventSource failed:', error);
eventSource.close();
setIsStreaming(false);
};
eventSource.addEventListener('done', () => {
eventSource.close();
setIsStreaming(false);
});
};
const handleMarkdownChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
setMarkdown(e.target.value);
};
return (
<main className="main">
<h1>Newsletter Generator</h1>
<LoginStatus />
{session && session.user && session.user.name && (
<div>
<div className="highlights-input">
<h3>Highlights and CTAs (optional)</h3>
<textarea
value={highlightsInput}
onChange={(e) => setHighlightsInput(e.target.value)}
disabled={isStreaming}
placeholder="Paste important URLs or context to highlight at the very top of the newsletter."
/>
</div>
<button onClick={handleClick} disabled={isStreaming}>
{isStreaming ? 'Streaming...' : 'Generate newsletter'}
</button>
<div className="markdown-container">
<div className="markdown-edit">
<h3>Raw Markdown</h3>
<textarea
value={markdown}
onChange={handleMarkdownChange}
disabled={isStreaming}
placeholder="Markdown will appear here. You can edit it after generation."
/>
</div>
<div className="markdown-preview">
<h3>Rendered Markdown</h3>
<div dangerouslySetInnerHTML={{ __html: md.render(markdown) }} />
</div>
</div>
</div>
)}
</main>
);
}