code.pablomurad.com

mymusics

Retro web player for random tracks from the Myspace Dragon Hoard

src/components/EmbedSnippet.tsx raw
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
import { useCallback, useMemo, useState } from "react";
import { PUBLIC_SITE_URL } from "../config/siteUrl";
import { buildEmbedSearchParams } from "../lib/embedParams";
import { EMBED_FONT_IDS, EMBED_PRESET_IDS, type EmbedFontId, type EmbedPresetId } from "../lib/embedTheme";

function buildIframeSnippet(opts: {
  autoplay: boolean;
  compact: boolean;
  startId: string;
  showBrand: boolean;
  preset: EmbedPresetId;
  accent: string;
  useAccent: boolean;
  radius: string;
  font: EmbedFontId;
}): string {
  const radiusNum = opts.radius.trim() ? Number.parseInt(opts.radius, 10) : undefined;
  const qs = buildEmbedSearchParams({
    autoplay: opts.autoplay,
    theme: opts.compact ? "compact" : "default",
    startId: opts.startId.trim() || null,
    showBrand: opts.showBrand,
    preset: opts.preset,
    accent: opts.useAccent ? opts.accent : undefined,
    radius: Number.isFinite(radiusNum) ? radiusNum : undefined,
    font: opts.font,
  });
  const src = `${PUBLIC_SITE_URL}/embed${qs}`;
  return `<iframe
  src="${src}"
  title="MyMusics"
  width="100%"
  height="540"
  style="max-width:380px;border:0;border-radius:12px"
  loading="lazy"
  allow="autoplay"
></iframe>`;
}

type Props = {
  className?: string;
};

export function EmbedSnippet({ className }: Props = {}) {
  const [copied, setCopied] = useState(false);
  const [autoplay, setAutoplay] = useState(true);
  const [compact, setCompact] = useState(false);
  const [showBrand, setShowBrand] = useState(true);
  const [startId, setStartId] = useState("");
  const [preset, setPreset] = useState<EmbedPresetId>("default");
  const [useAccent, setUseAccent] = useState(false);
  const [accent, setAccent] = useState("#e64a19");
  const [radius, setRadius] = useState("");
  const [font, setFont] = useState<EmbedFontId>("sans");

  const embedOptions = useMemo(
    () => ({
      autoplay,
      compact,
      startId,
      showBrand,
      preset,
      accent,
      useAccent,
      radius,
      font,
    }),
    [autoplay, compact, startId, showBrand, preset, accent, useAccent, radius, font],
  );

  const code = useMemo(() => buildIframeSnippet(embedOptions), [embedOptions]);

  const previewSrc = useMemo(() => {
    const radiusNum = radius.trim() ? Number.parseInt(radius, 10) : undefined;
    const qs = buildEmbedSearchParams({
      autoplay: false,
      theme: compact ? "compact" : "default",
      startId: startId.trim() || null,
      showBrand,
      preset,
      accent: useAccent ? accent : undefined,
      radius: Number.isFinite(radiusNum) ? radiusNum : undefined,
      font,
    });
    return `${PUBLIC_SITE_URL}/embed${qs}`;
  }, [compact, startId, showBrand, preset, accent, useAccent, radius, font]);

  const copy = useCallback(async () => {
    try {
      await navigator.clipboard.writeText(code);
      setCopied(true);
      window.setTimeout(() => setCopied(false), 2000);
    } catch {
      try {
        const ta = document.createElement("textarea");
        ta.value = code;
        ta.setAttribute("readonly", "");
        ta.style.position = "absolute";
        ta.style.left = "-9999px";
        document.body.appendChild(ta);
        ta.select();
        document.execCommand("copy");
        document.body.removeChild(ta);
        setCopied(true);
        window.setTimeout(() => setCopied(false), 2000);
      } catch {
        setCopied(false);
      }
    }
  }, [code]);

  return (
    <section
      className={["embed-snippet", "card", className].filter(Boolean).join(" ")}
      aria-label="Embed this player"
    >
      <h2 className="embed-snippet-title">Embed on your site</h2>
      <p className="embed-snippet-lead muted">
        Paste the HTML below on your page. Customize colors with presets and query params, or update
        the theme at runtime via <code>postMessage</code>. See{" "}
        <code>docs/EMBED-CUSTOMIZATION.md</code> for the full guide.
      </p>

      <div className="embed-snippet-options">
        <label className="check">
          <input type="checkbox" checked={autoplay} onChange={(e) => setAutoplay(e.target.checked)} />
          Autoplay / auto-advance
        </label>
        <label className="check">
          <input type="checkbox" checked={compact} onChange={(e) => setCompact(e.target.checked)} />
          Compact layout
        </label>
        <label className="check">
          <input type="checkbox" checked={showBrand} onChange={(e) => setShowBrand(e.target.checked)} />
          Show MyMusics logo
        </label>

        <div className="embed-snippet-theme-row">
          <label className="embed-snippet-field">
            <span>Preset</span>
            <select
              value={preset}
              onChange={(e) => setPreset(e.target.value as EmbedPresetId)}
              aria-label="Color preset"
            >
              {EMBED_PRESET_IDS.map((id) => (
                <option key={id} value={id}>
                  {id}
                </option>
              ))}
            </select>
          </label>

          <label className="embed-snippet-field">
            <span>Font</span>
            <select
              value={font}
              onChange={(e) => setFont(e.target.value as EmbedFontId)}
              aria-label="Font stack"
            >
              {EMBED_FONT_IDS.map((id) => (
                <option key={id} value={id}>
                  {id}
                </option>
              ))}
            </select>
          </label>

          <label className="embed-snippet-field">
            <span>Radius (px)</span>
            <input
              type="number"
              min={0}
              max={24}
              placeholder="default"
              value={radius}
              onChange={(e) => setRadius(e.target.value)}
              aria-label="Border radius in pixels"
            />
          </label>

          <label className="check embed-snippet-field">
            <span>Custom accent</span>
            <span className="embed-snippet-accent-row">
              <input
                type="checkbox"
                checked={useAccent}
                onChange={(e) => setUseAccent(e.target.checked)}
                aria-label="Use custom accent color"
              />
              <input
                type="color"
                value={accent}
                disabled={!useAccent}
                onChange={(e) => setAccent(e.target.value)}
                aria-label="Accent color"
              />
            </span>
          </label>
        </div>

        <label className="embed-snippet-start">
          <span>Start track id (optional)</span>
          <input
            type="text"
            value={startId}
            onChange={(e) => setStartId(e.target.value)}
            placeholder="e.g. 12345"
            spellCheck={false}
          />
        </label>
      </div>

      <div className="embed-snippet-preview-wrap">
        <iframe
          className="embed-snippet-preview"
          src={previewSrc}
          title="MyMusics embed preview"
          loading="lazy"
          allow="autoplay"
        />
      </div>

      <textarea className="embed-snippet-code" readOnly rows={8} value={code} spellCheck={false} />
      <button type="button" className="btn primary embed-snippet-copy" onClick={() => void copy()}>
        {copied ? "Copied!" : "Copy code"}
      </button>
      <p className="embed-snippet-lead muted">
        oEmbed:{" "}
        <code>
          {PUBLIC_SITE_URL}/api/oembed?url=
          {encodeURIComponent(`${PUBLIC_SITE_URL}/embed${buildEmbedSearchParams({ preset, accent: useAccent ? accent : undefined, font })}`)}
        </code>
      </p>
    </section>
  );
}