Forum: the link module, with YouTube previews
- services/forum/src/links: recognises the links in a post and shows a preview for the ones a provider knows; for now YouTube (watch, shorts, live, embed and youtu.be links, with the start time). Title and channel come from YouTube's oEmbed answer, the thumbnail is cut to 16:9 and kept in our own storage, so reading a thread loads nothing from YouTube. Made when a post is written or edited; without it the link stays text. - The card: on a computer a click plays the video in place (youtube-nocookie.com); on a phone it opens the YouTube app (Android: an intent with the web page as fallback; iOS: the link the app takes over), and "Lejátszás itt" plays it in place. - The privacy notice (HU, EN) says what YouTube sees when one plays it. - "Pörgős most" is now "Élénk beszélgetések"; the new thread form lost its note about text and emoji. - docs: the Turnstile captcha is on the list before going live. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
"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>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user