112 lines
3.9 KiB
TypeScript
112 lines
3.9 KiB
TypeScript
"use client";
|
|||
|
|
|
||
|
|
import { useEffect, useState } from "react";
|
||
|
|
|
||
|
|
import type { LinkPreview } from "@/lib/forum";
|
||
|
|
|
||
|
|
type Device = "android" | "ios" | "desktop";
|
||
|
|
|
||
|
|
function detect(): Device {
|
||
|
|
const ua = navigator.userAgent;
|
||
|
|
if (/Android/i.test(ua)) return "android";
|
||
|
|
// iPadOS reports itself as a Mac with touch.
|
||
|
|
if (/iPhone|iPad|iPod/i.test(ua) || (/Macintosh/.test(ua) && navigator.maxTouchPoints > 1)) return "ios";
|
||
|
|
return "desktop";
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* A YouTube link under a post: our own copy of the thumbnail (nothing is
|
||
|
|
* loaded from YouTube until someone plays it). On a computer a click plays
|
||
|
|
* it here; on a phone it opens the YouTube app (Android: an intent with the
|
||
|
|
* web page as fallback; iOS: the link, which the app takes over), and
|
||
|
|
* "Lejátszás itt" plays it here.
|
||
|
|
*/
|
||
|
|
function YouTubeCard({ link }: { link: LinkPreview }) {
|
||
|
|
const [playing, setPlaying] = useState(false);
|
||
|
|
const [device, setDevice] = useState<Device>("desktop");
|
||
|
|
useEffect(() => setDevice(detect()), []);
|
||
|
|
const at = link.start ? `&t=${link.start}s` : "";
|
||
|
|
const watch = `https://www.youtube.com/watch?v=${link.id}${at}`;
|
||
|
|
const app =
|
||
|
|
device === "android"
|
||
|
|
? `intent://www.youtube.com/watch?v=${link.id}${at}#Intent;scheme=https;package=com.google.android.youtube;` +
|
||
|
|
`S.browser_fallback_url=${encodeURIComponent(watch)};end`
|
||
|
|
: watch;
|
||
|
|
const mobile = device !== "desktop";
|
||
|
|
|
||
|
|
if (playing) {
|
||
|
|
const start = link.start ? `&start=${link.start}` : "";
|
||
|
|
return (
|
||
|
|
<div className="flink flink-playing">
|
||
|
|
<div className="flink-frame">
|
||
|
|
<iframe
|
||
|
|
src={`https://www.youtube-nocookie.com/embed/${link.id}?autoplay=1&rel=0&playsinline=1${start}`}
|
||
|
|
title={link.title}
|
||
|
|
allow="autoplay; encrypted-media; picture-in-picture; fullscreen"
|
||
|
|
allowFullScreen
|
||
|
|
referrerPolicy="strict-origin-when-cross-origin"
|
||
|
|
/>
|
||
|
|
</div>
|
||
|
|
<div className="flink-meta">
|
||
|
|
<span className="flink-title">{link.title}</span>
|
||
|
|
<a href={watch} target="_blank" rel="noopener noreferrer" className="fp-link">
|
||
|
|
Megnyitás a YouTube-on
|
||
|
|
</a>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
}
|
||
|
|
return (
|
||
|
|
<div className="flink">
|
||
|
|
<a
|
||
|
|
className="flink-thumb"
|
||
|
|
href={mobile ? app : watch}
|
||
|
|
onClick={(e) => {
|
||
|
|
if (!mobile) {
|
||
|
|
e.preventDefault();
|
||
|
|
setPlaying(true);
|
||
|
|
}
|
||
|
|
}}
|
||
|
|
aria-label={mobile ? `Megnyitás a YouTube alkalmazásban: ${link.title}` : `Lejátszás: ${link.title}`}
|
||
|
|
>
|
||
|
|
{link.thumbnail ? (
|
||
|
|
// Our own copy of the thumbnail (already small).
|
||
|
|
<img src={link.thumbnail} alt="" width={480} height={270} loading="lazy" />
|
||
|
|
) : (
|
||
|
|
<span className="flink-noimg" />
|
||
|
|
)}
|
||
|
|
<span className="flink-play" aria-hidden="true">
|
||
|
|
<svg viewBox="0 0 24 24">
|
||
|
|
<path d="M8 5.5v13l11-6.5z" />
|
||
|
|
</svg>
|
||
|
|
</span>
|
||
|
|
</a>
|
||
|
|
<div className="flink-meta">
|
||
|
|
<span className="flink-title">{link.title}</span>
|
||
|
|
<span className="hint">
|
||
|
|
YouTube{link.author ? ` · ${link.author}` : ""}
|
||
|
|
{link.start ? ` · ${Math.floor(link.start / 60)}:${String(link.start % 60).padStart(2, "0")}-tól` : ""}
|
||
|
|
</span>
|
||
|
|
{mobile ? (
|
||
|
|
<button type="button" className="fp-link flink-here" onClick={() => setPlaying(true)}>
|
||
|
|
Lejátszás itt
|
||
|
|
</button>
|
||
|
|
) : (
|
||
|
|
<a href={watch} target="_blank" rel="noopener noreferrer" className="fp-link flink-here">
|
||
|
|
Megnyitás a YouTube-on
|
||
|
|
</a>
|
||
|
|
)}
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
/** The previews of the links in a post (the link module: YouTube for now). */
|
||
|
|
export function LinkPreviews({ links }: { links: LinkPreview[] }) {
|
||
|
|
return (
|
||
|
|
<div className="flinks">
|
||
|
|
{links.map((link) => (link.provider === "youtube" ? <YouTubeCard key={link.id} link={link} /> : null))}
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
}
|