/** * Renders a dump's page, with its preview, comments, related dumps and the * playlists holding it. * * @module */ import { useEffect, useState } from "react"; import { Link, useLocation, useNavigate, useParams } from "react-router"; import { t } from "@lingui/core/macro"; import { Trans } from "@lingui/react/macro"; import { dumpThumbnailUrl, dumpUrl } from "../utils/shared/urls.ts"; import { can } from "../utils/auth/permissions.ts"; import { AddToPlaylistModal } from "../components/playlist/AddToPlaylistModal.tsx"; import { API_URL } from "../config/api.ts"; import { VALIDATION } from "../../shared/validation.ts"; import type { Comment } from "../../shared/comment.ts"; import type { Dump, UpdateDumpRequest } from "../../shared/dump.ts"; import type { Playlist } from "../../shared/playlist.ts"; import type { PublicUser } from "../../shared/user.ts"; import type { Wire } from "../../shared/api.ts"; import { deserializeComment, deserializeDump, deserializePlaylist, deserializePublicUser, } from "../utils/shared/deserialize.ts"; import { friendlyFetchError, parseAPIResponse } from "../utils/shared/api.ts"; import { useAuth } from "../hooks/auth/useAuth.ts"; import { useCategories } from "../hooks/category/useCategories.ts"; import { useDocumentTitle } from "../hooks/shared/useDocumentTitle.ts"; import { relativeTime } from "../utils/shared/relativeTime.ts"; import { useHeightTransition } from "../hooks/shared/useHeightTransition.ts"; import { useWS } from "../hooks/ws/useWS.ts"; import { Avatar } from "../components/shared/Avatar.tsx"; import { RichContentCard } from "../components/dump/RichContentCard.tsx"; import { FilePreview } from "../components/dump/FilePreview.tsx"; import { VoteButton } from "../components/social/VoteButton.tsx"; import { DumpCardList } from "../components/dump/DumpCardList.tsx"; import { PlaylistCardList } from "../components/playlist/PlaylistCardList.tsx"; import { PageShell } from "../components/layout/PageShell.tsx"; import { PageError } from "../components/layout/PageError.tsx"; import { Markdown } from "../components/editor/Markdown.tsx"; import { CommentThread } from "../components/comment/CommentThread.tsx"; import { Tooltip } from "../components/shared/Tooltip.tsx"; type DumpState = | { status: "loading" } | { status: "error"; error: string } | { status: "loaded"; dump: Dump }; /** * Renders `/dumps/:selectedDump`, for anyone who may see the dump: a private * dump answers 404 to anyone but its poster. The dump comes from * `GET /api/dumps/:dumpId`, or from the navigation's state when `DumpEdit` or * a title edit here just saved it. Its comments, related dumps and playlists * come from their own endpoints, and `WSContext`'s events keep them current. * The poster and moderators can edit the title in place, and get a link to * `DumpEdit`. */ export function Dump(): React.JSX.Element { const { selectedDump } = useParams(); const location = useLocation(); const navigate = useNavigate(); // Taken once, since it stays in the history entry, where a reload or Back // would show that snapshot forever. It is cleared from there below. const [handedOver] = useState( () => (location.state as { dump?: Dump } | null)?.dump ?? null, ); const preloaded = handedOver && (handedOver.id === selectedDump || handedOver.slug === selectedDump) ? handedOver : null; useEffect(() => { if (!(location.state as { dump?: Dump } | null)?.dump) return; navigate(`${location.pathname}${location.search}${location.hash}`, { replace: true, state: null, }); }, [location, navigate]); const [dumpState, setDumpState] = useState( preloaded ? { status: "loaded", dump: preloaded } : { status: "loading" }, ); const [op, setOp] = useState(null); const [playlistModalOpen, setPlaylistModalOpen] = useState(false); useDocumentTitle( dumpState.status === "loaded" ? dumpState.dump.title : dumpState.status === "error" ? null : undefined, ); const [comments, setComments] = useState([]); const [relatedDumps, setRelatedDumps] = useState([]); const [collections, setCollections] = useState([]); const [titleEditing, setTitleEditing] = useState(false); const headerRightRef = useHeightTransition(titleEditing); const [titleDraft, setTitleDraft] = useState(""); const [titleSaving, setTitleSaving] = useState(false); const [titleError, setTitleError] = useState(null); const { user, mediaToken, authFetch } = useAuth(); const { byId } = useCategories(); const { voteCounts, myVotes, castVote, removeVote, lastDumpEvent, lastCommentEvent, lastPlaylistEvent, connectionEpoch, } = useWS(); useEffect(() => { if (!selectedDump) return; const controller = new AbortController(); if (preloaded) { fetch(`${API_URL}/api/users/by-id/${preloaded.userId}`, { signal: controller.signal, }) .then((r) => r.json()) .then((r) => r.success && setOp(deserializePublicUser(r.data))) .catch(() => {}); return () => controller.abort(); } (async () => { setDumpState({ status: "loading" }); setOp(null); try { const res = await authFetch(`${API_URL}/api/dumps/${selectedDump}`, { cache: "no-store", signal: controller.signal, }); const apiResponse = parseAPIResponse>(await res.json()); if (!apiResponse.success) { throw new Error(apiResponse.error.message); } const dump: Dump = deserializeDump(apiResponse.data); setDumpState({ status: "loaded", dump }); fetch(`${API_URL}/api/users/by-id/${dump.userId}`, { signal: controller.signal, }) .then((r) => r.json()) .then((r) => r.success && setOp(deserializePublicUser(r.data))) .catch(() => {}); } catch (err) { if ((err as Error).name === "AbortError") return; setDumpState({ status: "error", error: friendlyFetchError(err) }); } })(); return () => controller.abort(); }, [selectedDump, preloaded, authFetch]); useEffect(() => { if (!lastDumpEvent) return; // Mirrors the socket's edits, which only an effect can observe. // eslint-disable-next-line react-hooks/set-state-in-effect setDumpState((prev) => { if (prev.status !== "loaded" || prev.dump.id !== lastDumpEvent.id) { return prev; } return { status: "loaded", dump: lastDumpEvent }; }); }, [lastDumpEvent]); useEffect(() => { if (!selectedDump) return; const controller = new AbortController(); authFetch(`${API_URL}/api/dumps/${selectedDump}/comments`, { signal: controller.signal, }) .then((r) => r.json()) .then((body) => { if (body.success) { setComments((body.data as Wire[]).map(deserializeComment)); } }) .catch(() => {}); return () => controller.abort(); // connectionEpoch refetches on reconnect, for the comments missed while the // socket was down. }, [selectedDump, authFetch, connectionEpoch]); // The dumps that link to this one, or that it links to. useEffect(() => { if (!selectedDump) return; const controller = new AbortController(); authFetch(`${API_URL}/api/dumps/${selectedDump}/related`, { signal: controller.signal, }) .then((r) => r.json()) .then((body) => { if (body.success) { setRelatedDumps((body.data as Wire[]).map(deserializeDump)); } }) .catch(() => {}); return () => controller.abort(); }, [selectedDump, authFetch]); // The server leaves out the private playlists of others. useEffect(() => { if (!selectedDump) return; const controller = new AbortController(); authFetch(`${API_URL}/api/dumps/${selectedDump}/playlists`, { signal: controller.signal, }) .then((r) => r.json()) .then((body) => { if (body.success) { setCollections( (body.data as Wire[]).map(deserializePlaylist), ); } }) .catch(() => {}); return () => controller.abort(); // Any playlist event may change the list, and they are rare enough to // refetch on each. }, [selectedDump, authFetch, lastPlaylistEvent]); // A notification links to `#comment-`. Rerun when the comments arrive, // since the element doesn't exist before. useEffect(() => { if (!location.hash.startsWith("#comment-")) return; const id = location.hash.slice(1); const el = document.getElementById(id); if (!el) return; el.scrollIntoView({ behavior: "smooth", block: "start" }); el.classList.add("comment-node--highlight"); const timer = setTimeout( () => el.classList.remove("comment-node--highlight"), 2000, ); return () => clearTimeout(timer); }, [comments, location.hash]); const loadedDumpId = dumpState.status === "loaded" ? dumpState.dump.id : null; useEffect(() => { if ( !lastCommentEvent || !loadedDumpId || lastCommentEvent.dumpId !== loadedDumpId ) return; // A comment of this user's arrives both from the socket and from // CommentThread's callbacks below, hence the duplicate checks. if (lastCommentEvent.type === "created" && lastCommentEvent.comment) { // eslint-disable-next-line react-hooks/set-state-in-effect setComments((prev) => { if (prev.some((c) => c.id === lastCommentEvent.comment!.id)) { return prev; } return [...prev, lastCommentEvent.comment!]; }); } else if ( lastCommentEvent.type === "deleted" && lastCommentEvent.commentId ) { setComments((prev) => prev.map((c) => c.id === lastCommentEvent.commentId ? { ...c, deleted: true, body: "" } : c ) ); } else if ( lastCommentEvent.type === "updated" && lastCommentEvent.comment ) { setComments((prev) => prev.map((c) => c.id === lastCommentEvent.comment!.id ? lastCommentEvent.comment! : c ) ); } }, [lastCommentEvent, loadedDumpId]); if (dumpState.status === "loading") { return (

Loading dump…

); } if (dumpState.status === "error") { return ( } /> ); } const { dump } = dumpState; const canEdit = !!user && (dump.userId === user.id || can(user, "dump:moderate")); const dumpCategories = (dump.categoryIds ?? []) .map((id) => byId.get(id)) .filter((c) => c !== undefined); const startTitleEdit = () => { setTitleDraft(dump.title); setTitleError(null); setTitleEditing(true); }; const handleTitleSave = async () => { const trimmed = titleDraft.trim(); if (!trimmed || trimmed.length > VALIDATION.DUMP_TITLE_MAX) return; setTitleSaving(true); setTitleError(null); try { const res = await authFetch(`${API_URL}/api/dumps/${dump.id}`, { method: "PUT", body: JSON.stringify( { title: trimmed } satisfies UpdateDumpRequest, ), }); const apiResponse = parseAPIResponse>(await res.json()); if (!apiResponse.success) { setTitleError(apiResponse.error.message); return; } const updatedDump = deserializeDump(apiResponse.data); setTitleEditing(false); // The page stays mounted across the slug change below, and would keep // the old title. setDumpState({ status: "loaded", dump: updatedDump }); if (updatedDump.slug !== dump.slug) { navigate(dumpUrl(updatedDump), { replace: true, state: { dump: updatedDump }, }); } } catch (err) { setTitleError(friendlyFetchError(err)); } finally { setTitleSaving(false); } }; return (
{user && ( )}
{titleEditing ? (
setTitleDraft(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") { e.preventDefault(); handleTitleSave(); } else if (e.key === "Escape") { setTitleEditing(false); } }} maxLength={VALIDATION.DUMP_TITLE_MAX} disabled={titleSaving} autoFocus /> {titleError && (

{titleError}

)}
) : (
{ if (e.key === "Enter" || e.key === " ") { e.preventDefault(); startTitleEdit(); } } : undefined} >

{dump.title}

{canEdit && ( ✎ )}
)}
{op ? ( {op.username} ) : …} {dump.updatedAt && ( edited {relativeTime(dump.updatedAt)} )} {dump.isPrivate && ( private )}
{dumpCategories.length > 0 && (
{dumpCategories.map((category) => ( {category.name} ))}
)}
{dump.comment && ( {dump.comment} )}
{dump.kind === "file" ? : dump.richContent ? ( ) : ( {dump.url} )}
{canEdit && ( Edit )} ← Back to all dumps
{relatedDumps.length > 0 && (

Related

)} {collections.length > 0 && (

In collections

)} setComments((prev) => prev.some((x) => x.id === c.id) ? prev : [...prev, c] )} onCommentDeleted={(id) => setComments((prev) => prev.map((c) => c.id === id ? { ...c, deleted: true, body: "" } : c ) )} onCommentUpdated={(updated) => setComments((prev) => prev.map((c) => (c.id === updated.id ? updated : c)) )} />
{playlistModalOpen && ( setPlaylistModalOpen(false)} /> )}
); }