-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathApp.jsx
286 lines (247 loc) · 8.46 KB
/
App.jsx
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import { useState, useEffect, useRef, lazy, Suspense } from "react";
import CardStyleContext from "@contexts/CardStyleContext";
import routes from "@/js/api/routes";
import { objectEmpty } from "@utils";
import toast, { Toaster } from "react-hot-toast";
import { QueryClient, QueryClientProvider } from "react-query";
import { useWindowSize } from "@uidotdev/usehooks";
import {
formatLyrics,
getLang,
bestContrast,
getContrast,
download,
} from "./utils";
import Searchbar from "@components/searchbar/Searchbar";
const SongPreview = lazy(() => import("@components/SongPreview"));
import SidePanel from "@components/SidePanel";
import LyricsViewer from "@components/LyricsViewer";
import LyricsCard from "@components/lyrics-card/LyricsCard";
import SizeMenu from "@compUtils/SizeMenu";
import PageLogo from "@compUtils/PageLogo";
import OptionsPanel from "@components/OptionsPanel";
const LyricsModal = lazy(() => import("@components/LyricsModal"));
import DownloadingOverlay from "@compUtils/DownloadingOverlay";
import ShareModal from "@components/ShareModal";
import useCardToImage from "@/hooks/useCardToImage";
import useCopyImageToClipboard from "@/hooks/useCopyImageToClipboard";
const defaultLyricsData = {
lang: "",
lyrics: [],
selectionCompleted: false,
status: 0,
};
function App() {
const [song, setSong] = useState({});
const [colors, setColors] = useState(null);
const [cardAspectRatio, setCardAspectRatio] = useState("1:1");
const [cardStyling, setCardStyling] = useState({
bold: false,
italic: false,
alignment: "left",
highlightColor: "#ffffff",
textColor: "#000000",
bannerBackground: "#f7f16c",
bannerForeground: "#000000",
});
const [shareModalOpen, setShareModalOpen] = useState(false);
const [downloading, setDownloading] = useState(false);
const cardRef = useRef(null);
// Setting a proper foreground color for the background color
useEffect(() => {
setCardStyling((prev) => {
return {
...prev,
bannerForeground: bestContrast(prev.bannerBackground, [
"#000000",
"#ffffff",
]),
textColor: bestContrast(prev.highlightColor, ["#000000", "#ffffff"]),
};
});
}, [cardStyling.bannerBackground, cardStyling.highlightColor]);
const [lyricsData, setLyricsData] = useState(defaultLyricsData);
const { id, image } = song;
const copyToClipboard = useCopyImageToClipboard();
// Whenever a song is selected, fetch lyrics and colors
useEffect(() => {
if (objectEmpty(song)) return;
setLyricsData(defaultLyricsData);
setColors(null);
// lyrics
const lyricsPromise = routes
.getLyrics(id)
.then((res) => {
const lang = getLang(res.data);
setLyricsData({
lang: lang,
lyrics: formatLyrics(res.data)
.split("\n")
.map((l) => [l, 0]),
selectionCompleted: false,
status: 1,
});
})
.catch(() => {
setLyricsData({
...defaultLyricsData,
status: -1,
});
});
//colors
const colorsPromise = routes
.getColors(image)
.then((res) => {
let { background_color, text_color } = res.data;
if (getContrast(background_color, text_color, true) <= 2)
text_color = bestContrast(background_color, ["#000000", "#ffffff"]);
setColors({
background_color: background_color,
text_color: text_color,
});
})
.catch(() => {
console.error("Couldn't extract colors from image");
});
toast.promise(Promise.all([lyricsPromise, colorsPromise]), {
loading: "Loading song...",
success: <p>Song loaded!</p>,
error: <p>Could load song. Please try again</p>,
});
}, [song]);
const handleResultSelected = (newSong) => setSong(newSong);
const handleLyricsSelectionChanged = (index) => {
setLyricsData((prev) => {
const newLyrics = prev.lyrics.map((l, i) => {
if (i == index) return [l[0], l[1] == 0 ? 1 : 0];
return l;
});
const selectedCount = newLyrics.filter((l) => l[1] == 1).length;
return {
...prev,
lyrics: newLyrics,
selectionCompleted: selectedCount == 4,
};
});
};
const downloadHandler = () => {
setDownloading(true);
const promise = useCardToImage(cardRef, 2).then((base64) =>
download(base64, `lyrics-card-${id ? id : "untitled"}.jpeg`)
);
// Showing a confirmation toast
toast
.promise(promise, {
loading: "Saving...",
success: <p>Card saved!</p>,
error: <p>Could not save. Please try again</p>,
})
.then(() => setDownloading(false))
.catch(() => setDownloading(false));
setShareModalOpen(false);
};
const copyToClipboardHandler = () => {
setDownloading(true);
const promise = useCardToImage(cardRef, 2, 0.7).then((base64) =>
copyToClipboard(base64)
);
toast
.promise(promise, {
loading: "Copying...",
success: <p>Copied to clipboard!</p>,
error: <p>Could not copy. Please try again</p>,
})
.then(() => setDownloading(false))
.catch((e) => {
setDownloading(false);
console.error(e);
});
};
return (
<QueryClientProvider client={new QueryClient()}>
<div
className={`${
downloading && "downloading"
} relative container max-w-[1920px] max-h-[1080px] mx-auto flex h-[100vh]`}
>
<Suspense>
{useWindowSize().width <= 1150 && (
<LyricsModal
song={song}
colors={colors}
lyricsData={lyricsData}
onLyricsSelectionChanged={handleLyricsSelectionChanged}
/>
)}
</Suspense>
<SidePanel onSizeChanged={setCardAspectRatio} />
<main className="grow grid grid-rows-[5rem_1fr] grid-cols-[1fr] lg:grid-cols-[1fr_36ch] p-5 gap-5">
<header className="relative lg:col-span-2 flex gap-4 sm:gap-8 items-center">
<PageLogo
className="block 2xl:hidden h-[60%] sm:h-[70%] self-center"
geniusColor="#272838"
/>
<Searchbar
className="grow"
onResultSelected={(id) => handleResultSelected(id)}
/>
</header>
<section className="row-start-2">
<CardStyleContext.Provider value={{ cardStyling, setCardStyling }}>
<OptionsPanel className="rounded-md mb-4 shadow-md" />
<SizeMenu
className="2xl:hidden flex items-center justify-center sm:justify-start gap-4 mb-4 px-2"
cardClassName="aspect-square w-[65px] md:w-[80px]"
showLabel={false}
onSizeChanged={setCardAspectRatio}
/>
<div className="relative">
<LyricsCard
ref={cardRef}
cardInfo={song}
lyricsData={lyricsData}
aspectRatio={cardAspectRatio}
onSave={() => setShareModalOpen(true)}
/>
<DownloadingOverlay
className="opacity-75 bg-gray-300 card-size show-when-download"
ratio={cardAspectRatio}
style={{ aspectRatio: cardAspectRatio.replace(":", "/") }}
/>
</div>
</CardStyleContext.Provider>
<ShareModal
open={shareModalOpen}
onClosing={() => setShareModalOpen(false)}
downloadHandler={downloadHandler}
copyHandler={copyToClipboardHandler}
/>
</section>
<aside className="row-start-2 col-start-2 hidden lg:grid grid-rows-[120px_1fr] border border-gray-400 rounded-md overflow-auto">
{!objectEmpty(song) && (
<Suspense>
<SongPreview
className="row-start-1"
song={song}
colors={colors}
/>
</Suspense>
)}
<LyricsViewer
className="grow"
style={{
gridRow: !objectEmpty(song) ? "2/3" : "1/3",
}}
id={id}
colors={colors}
lyricsData={lyricsData}
onSelectionChanged={handleLyricsSelectionChanged}
/>
</aside>
</main>
<Toaster position="bottom-center" reverseOrder={false} />
</div>
</QueryClientProvider>
);
}
export default App;