/
App.tsx
163 lines (143 loc) · 4.67 KB
/
App.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
import Lottie from "lottie-react";
import React, { useEffect, useState } from "react";
import { Player } from "react-simple-player";
import "./App.css";
import loader from "./loader.json";
import NewArticleModal from "./Modal";
type ProgressState = "idle" | "extractingContent" | "generatingAudio" | "done";
function useSynth(articleUrl: string) {
const [progress, setProgress] = useState<ProgressState>("idle");
const [articleContent, setArticleContent] = useState<{
content: string;
title: string;
}>();
const [audioUrl, setAudioUrl] = useState<string>("");
useEffect(() => {
if (articleUrl === "") return;
const ws = new WebSocket("wss://api.easy-reader.inflow-it-labs.tk");
ws.onopen = () => {
setProgress("extractingContent");
ws.send(
JSON.stringify({
action: "processUrl",
articleUrl,
})
);
ws.onmessage = async (event) => {
const result = JSON.parse(event.data);
if (result.type === "contentExtracted") {
const contentResponse = await fetch(result.contentUrl);
const content = await contentResponse.json();
setArticleContent(content);
setProgress("generatingAudio");
}
if (result.type === "audioGenerated") {
const audioUrl = result.audioUrl;
setAudioUrl(audioUrl);
setProgress("done");
}
};
};
return () => {
ws.close();
};
}, [articleUrl]);
return {
progress,
articleContent,
audioUrl,
};
}
type ArticleContentProps = {
articleContent: { title: string; content: string };
audioUrl: string;
progress: ProgressState;
};
const ArticleContent = ({
articleContent,
audioUrl,
progress,
}: ArticleContentProps) => {
return (
<>
{articleContent && (
<>
<h1 className="pt-5">{articleContent.title}</h1>
{progress === "generatingAudio" && (
<div className="flex items-center font-light">
<Lottie animationData={loader} loop style={{ width: 60 }} />
Generating Audio
</div>
)}
{progress === "done" && audioUrl && (
<div className="my-5">
<Player src={audioUrl} height={40} />
</div>
)}
<div
dangerouslySetInnerHTML={{ __html: articleContent.content }}
></div>
</>
)}
</>
);
};
function App() {
const [open, setOpen] = useState(false);
const [articleUrl, setArticleUrl] = useState("");
const { articleContent, audioUrl, progress } = useSynth(articleUrl);
return (
<div>
<div className="flex justify-center items-center mt-10">
<div className="bg-white w-full rounded-2xl max-w-5xl shadow-xl article z-10">
{progress === "idle" && (
<div className="flex flex-col justify-center items-center h-96 font-light">
<img className="w-20 h-20" src="/newspaper.png" alt="logo" />
<button
onClick={() => setOpen(!open)}
type="button"
className="z-20 inline-flex items-center mt-3 px-6 py-3 border border-transparent text-base font-medium rounded-full shadow-sm text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
>
Get started here
</button>
</div>
)}
{progress === "extractingContent" && (
<div className="flex flex-col justify-center items-center h-96 font-light">
<Lottie animationData={loader} loop style={{ width: 100 }} />
Extracting content
</div>
)}
{articleContent && (
<ArticleContent
articleContent={articleContent}
audioUrl={audioUrl}
progress={progress!}
/>
)}
</div>
</div>
<div className="fixed h-96 bg-indigo-500 w-full top-0"></div>
{progress !== "idle" && (
<button
onClick={() => setOpen(!open)}
type="button"
className="fixed z-20 bottom-6 right-6 inline-flex items-center px-6 py-3 border border-transparent text-base font-medium rounded-full shadow-sm text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
>
+ Listen to a new article
</button>
)}
<NewArticleModal
open={open}
onClose={() => {
setOpen(false);
}}
onSelectArticle={(url) => {
setOpen(false);
setArticleUrl(url);
}}
/>
</div>
);
}
export default App;