aboutsummaryrefslogtreecommitdiffstats
path: root/src/app/create/page.tsx
blob: 016c930ec13f90183ff0147863cecc66e62cff4f (plain) (blame)
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
"use client";
import { useMemo, useState } from "react";
import { MdLibraryMusic } from "react-icons/md";
import { FaCopy, FaCheck, FaExternalLinkAlt } from "react-icons/fa";
import { Root, Navbar, Logo, LogoIcon, NavLink } from "../styles/shared";
import {
  Content,
  Heading,
  Subheading,
  Form,
  FieldGroup,
  Label,
  Input,
  Divider,
  Row,
  GenerateButton,
  OutputSection,
  OutputLabel,
  CodeBox,
  CopyButton,
  OpenLink,
} from "./page.styles";

type CreateMode = "karaoke" | "typing";

interface KaraokePayload {
  lrc?: string;
  srv3?: string;
  file1?: string;
  file2?: string;
  offset?: number;
  offset2?: number;
}

interface TypingPayload {
  file1?: string;
  lrc?: string;
  offset?: number;
  title?: string;
  artist?: string;
  skip_backing?: boolean;
  difficulty?: number;
}

export default function CreatePage() {
  const [mode, setMode] = useState<CreateMode>("karaoke");

  // Karaoke fields
  const [lrc, setLrc] = useState("");
  const [srv3, setSrv3] = useState("");
  const [file1, setFile1] = useState("");
  const [file2, setFile2] = useState("");
  const [offset, setOffset] = useState("");
  const [offset2, setOffset2] = useState("");

  // Typing fields
  const [typingTitle, setTypingTitle] = useState("");
  const [typingArtist, setTypingArtist] = useState("");
  const [typingDifficulty, setTypingDifficulty] = useState("");
  const [skipBacking, setSkipBacking] = useState(true);

  const [code, setCode] = useState<string | null>(null);
  const [copiedCode, setCopiedCode] = useState(false);
  const [copiedUrl, setCopiedUrl] = useState(false);

  const resetCopyStates = () => {
    setCopiedCode(false);
    setCopiedUrl(false);
  };

  const generate = () => {
    if (mode === "karaoke") {
      const payload: KaraokePayload = {};
      if (lrc.trim()) payload.lrc = lrc.trim();
      if (srv3.trim()) payload.srv3 = srv3.trim();
      if (file1.trim()) payload.file1 = file1.trim();
      if (file2.trim()) payload.file2 = file2.trim();
      if (offset.trim() !== "") payload.offset = Number(offset);
      if (offset2.trim() !== "") payload.offset2 = Number(offset2);

      setCode(btoa(JSON.stringify(payload)));
      resetCopyStates();
      return;
    }

    const payload: TypingPayload = {};
    if (file1.trim()) payload.file1 = file1.trim();
    if (lrc.trim()) payload.lrc = lrc.trim();
    if (offset.trim() !== "") payload.offset = Number(offset);
    if (typingTitle.trim()) payload.title = typingTitle.trim();
    if (typingArtist.trim()) payload.artist = typingArtist.trim();
    payload.skip_backing = skipBacking;
    if (typingDifficulty.trim() !== "") payload.difficulty = Number(typingDifficulty);

    setCode(btoa(JSON.stringify(payload)));
    resetCopyStates();
  };

  const copy = (text: string, which: "code" | "url") => {
    navigator.clipboard.writeText(text);
    if (which === "code") {
      setCopiedCode(true);
      setTimeout(() => setCopiedCode(false), 2000);
    } else {
      setCopiedUrl(true);
      setTimeout(() => setCopiedUrl(false), 2000);
    }
  };

  const playerPath = mode === "typing" ? "/typing" : "/player";
  const shareUrl = useMemo(
    () =>
      code
        ? `${typeof window !== "undefined" ? window.location.origin : ""}${playerPath}?code=${code}`
        : "",
    [code, playerPath]
  );

  return (
    <Root>
      <Navbar>
        <Logo href="/">
          <LogoIcon>
            <MdLibraryMusic />
          </LogoIcon>
          LRC-Karaoke-Player
        </Logo>
        <NavLink href="/">← Back</NavLink>
      </Navbar>

      <Content>
        <Heading>Create a Code</Heading>
        <Subheading>
          Switch between Karaoke and Typing Game modes, then generate a shareable code for your session.
        </Subheading>

        <Form>
          <Row>
            <GenerateButton
              onClick={() => {
                setMode("karaoke");
                setCode(null);
                resetCopyStates();
              }}
              style={{
                backgroundColor: mode === "karaoke" ? "#1a1a1a" : "#e5e5e5",
                color: mode === "karaoke" ? "#fff" : "#1a1a1a",
              }}
            >
              MoekyunKaraoke
            </GenerateButton>
            <GenerateButton
              onClick={() => {
                setMode("typing");
                setCode(null);
                resetCopyStates();
              }}
              style={{
                backgroundColor: mode === "typing" ? "#1a1a1a" : "#e5e5e5",
                color: mode === "typing" ? "#fff" : "#1a1a1a",
              }}
            >
              LRC-Type
            </GenerateButton>
          </Row>

          <Divider />

          <FieldGroup>
            <Label>Media - The main audio that plays</Label>
            <Input
              type="url"
              placeholder="https://example.com/song.mp4"
              value={file1}
              onChange={(e) => setFile1(e.target.value)}
            />
          </FieldGroup>

          <FieldGroup>
            <Label>LRC Lyrics</Label>
            <Input
              type="url"
              placeholder="https://example.com/song.lrc"
              value={lrc}
              onChange={(e) => setLrc(e.target.value)}
            />
          </FieldGroup>

          <FieldGroup>
            <Label title="Offset in milliseconds. Increase this value if the main audio is ahead of the lyrics.">
              LRC Offset (ms)
            </Label>
            <Input
              type="number"
              placeholder="0"
              value={offset}
              onChange={(e) => setOffset(e.target.value)}
              step="25"
            />
          </FieldGroup>

          {mode === "karaoke" ? (
            <>
              <FieldGroup>
                <Label title="SRV3 is a YouTube-style timed text format used for subtitles. Provide a .srv3 URL to display timed subtitles in the player (optional).">
                  SRV3 Subtitles (Optional)
                </Label>
                <Input
                  type="url"
                  placeholder="https://example.com/song.srv3"
                  value={srv3}
                  onChange={(e) => setSrv3(e.target.value)}
                />
              </FieldGroup>

              <Divider />

              <FieldGroup>
                <Label>Backing Audio #2 (Optional)</Label>
                <Input
                  type="url"
                  placeholder="https://example.com/instrumental.mp3"
                  value={file2}
                  onChange={(e) => setFile2(e.target.value)}
                />
              </FieldGroup>

              <FieldGroup>
                <Label title="Offset in milliseconds. Increase this value if the main audio is ahead of the backing audio.">
                  Backing Audio #2 Offset (ms)
                </Label>
                <Input
                  type="number"
                  placeholder="0"
                  value={offset2}
                  onChange={(e) => setOffset2(e.target.value)}
                  step="25"
                />
              </FieldGroup>
            </>
          ) : (
            <>
              <Divider />
              <Row>
                <FieldGroup>
                  <Label>Title</Label>
                  <Input
                    type="text"
                    placeholder="Song Title"
                    value={typingTitle}
                    onChange={(e) => setTypingTitle(e.target.value)}
                  />
                </FieldGroup>
                <FieldGroup>
                  <Label>Artist</Label>
                  <Input
                    type="text"
                    placeholder="Artist Name"
                    value={typingArtist}
                    onChange={(e) => setTypingArtist(e.target.value)}
                  />
                </FieldGroup>
              </Row>

              <Row>
                <FieldGroup>
                  <Label title="When enabled, lyrics inside parentheses are treated as backing lyrics and skipped.">
                    Skip Backing
                  </Label>
                  <Input
                    type="checkbox"
                    checked={skipBacking}
                    onChange={(e) => setSkipBacking(e.target.checked)}
                    style={{ width: "18px", height: "18px", marginTop: "10px" }}
                  />
                </FieldGroup>

                <FieldGroup>
                  <Label>Difficulty (number)</Label>
                  <Input
                    type="number"
                    placeholder="1"
                    min="1"
                    step="1"
                    value={typingDifficulty}
                    onChange={(e) => setTypingDifficulty(e.target.value)}
                  />
                </FieldGroup>
              </Row>
            </>
          )}

          <GenerateButton onClick={generate}>Generate Code</GenerateButton>
        </Form>

        {code && (
          <OutputSection>
            <div>
              <OutputLabel>Code</OutputLabel>
              <CodeBox>
                {code}
                <CopyButton
                  $copied={copiedCode}
                  onClick={() => copy(code, "code")}
                  aria-label="Copy code"
                >
                  {copiedCode ? <FaCheck /> : <FaCopy />}
                </CopyButton>
              </CodeBox>
            </div>

            <div>
              <OutputLabel>Share URL</OutputLabel>
              <CodeBox>
                {shareUrl}
                <CopyButton
                  $copied={copiedUrl}
                  onClick={() => copy(shareUrl, "url")}
                  aria-label="Copy URL"
                >
                  {copiedUrl ? <FaCheck /> : <FaCopy />}
                </CopyButton>
              </CodeBox>
            </div>

            <OpenLink href={shareUrl} target="_blank" rel="noopener noreferrer">
              <FaExternalLinkAlt /> Open in {mode === "typing" ? "Typing Game" : "Player"}
            </OpenLink>
          </OutputSection>
        )}
      </Content>
    </Root>
  );
}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage