Forum photos, a one-row bottom menu, and the site's own question windows
Photos in posts and replies: a "Fotó" button in the new thread form and the reply boxes. The browser makes the picture small before uploading it (1600 pixels at most, WebP, JPEG where WebP cannot be made); the forum checks it is a still picture, strips its metadata and makes a square preview (forum-photos/ in the storage). Five a member a day, five a post; one taken off before sending is deleted and does not count. Under the post a row of square previews; a click opens a viewer paged with arrows, swipes and keys. Seen by whoever may read the thread (long public cache only for public topics). Unsent photos go after a day, those of removed posts after a year; removed with the account and listed in the data export. Terms and privacy notice updated. The forum's bottom menu keeps its five items in one row (the fifth wrapped to a second row). On iPhones, when the keyboard goes away without the usual tap (a photo picker opening over it), the page is put back in place instead of the menu floating mid-screen over an empty strip. The browser's own confirm() and prompt() windows (23 places: deleting, revoking, blocking, muting, moderators' reasons, admin actions) are replaced by a question window in the site's look, following the forum's light or dark theme, with named buttons and a text box for reasons. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
// by its server and client components.
|
||||
|
||||
/** The forum's colours: light by default, dark on request (a cookie). */
|
||||
import type { ForumPhoto } from "./photos";
|
||||
import type { ChartResult, InterAspect } from "./types";
|
||||
|
||||
export const FORUM_THEME_COOKIE = "sf_forum_theme";
|
||||
@@ -129,6 +130,8 @@ export interface ForumPost {
|
||||
share?: ShareCardData | null;
|
||||
/** Previews of the links a provider knows (YouTube for now). */
|
||||
links?: LinkPreview[] | null;
|
||||
/** Photos uploaded with the post. */
|
||||
photos?: ForumPhoto[] | null;
|
||||
}
|
||||
|
||||
/** A link the forum's link module knows, under a post. */
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
"use client";
|
||||
|
||||
// Photos in forum posts, in the browser: made small before the upload (the
|
||||
// longest side 1600 pixels, WebP; JPEG where the browser cannot make WebP),
|
||||
// so phones send a fraction of the original. The forum checks it again and
|
||||
// strips what metadata could be left.
|
||||
import { useCallback, useRef, useState } from "react";
|
||||
|
||||
export const MAX_SIDE = 1600;
|
||||
/** Photos under one post (the forum allows five a member a day too). */
|
||||
export const PER_POST = 5;
|
||||
|
||||
export interface ForumPhoto {
|
||||
id: string;
|
||||
url: string;
|
||||
thumb: string;
|
||||
width: number;
|
||||
height: number;
|
||||
}
|
||||
|
||||
type Drawable = ImageBitmap | HTMLImageElement;
|
||||
|
||||
async function decode(file: File): Promise<Drawable> {
|
||||
if (typeof createImageBitmap === "function") {
|
||||
try {
|
||||
return await createImageBitmap(file, { imageOrientation: "from-image" });
|
||||
} catch {
|
||||
// Some formats only an <img> can read.
|
||||
}
|
||||
}
|
||||
const url = URL.createObjectURL(file);
|
||||
try {
|
||||
const img = new Image();
|
||||
img.src = url;
|
||||
await img.decode();
|
||||
return img;
|
||||
} finally {
|
||||
setTimeout(() => URL.revokeObjectURL(url), 1000);
|
||||
}
|
||||
}
|
||||
|
||||
function toBlob(canvas: HTMLCanvasElement, type: string, quality: number): Promise<Blob | null> {
|
||||
return new Promise((resolve) => canvas.toBlob(resolve, type, quality));
|
||||
}
|
||||
|
||||
/** The picture at most MAX_SIDE pixels long, as WebP (or JPEG). */
|
||||
export async function resizeForUpload(file: File): Promise<Blob> {
|
||||
const picture = await decode(file);
|
||||
const width = "naturalWidth" in picture ? picture.naturalWidth : picture.width;
|
||||
const height = "naturalHeight" in picture ? picture.naturalHeight : picture.height;
|
||||
if (!width || !height) throw new Error("A képet nem sikerült beolvasni.");
|
||||
const scale = Math.min(1, MAX_SIDE / Math.max(width, height));
|
||||
const canvas = document.createElement("canvas");
|
||||
canvas.width = Math.max(1, Math.round(width * scale));
|
||||
canvas.height = Math.max(1, Math.round(height * scale));
|
||||
const ctx = canvas.getContext("2d");
|
||||
if (!ctx) throw new Error("A képet nem sikerült feldolgozni.");
|
||||
ctx.imageSmoothingQuality = "high";
|
||||
ctx.drawImage(picture, 0, 0, canvas.width, canvas.height);
|
||||
if ("close" in picture) picture.close();
|
||||
const webp = await toBlob(canvas, "image/webp", 0.82);
|
||||
if (webp && webp.type === "image/webp") return webp;
|
||||
const jpeg = await toBlob(canvas, "image/jpeg", 0.85);
|
||||
if (!jpeg) throw new Error("A képet nem sikerült feldolgozni.");
|
||||
return jpeg;
|
||||
}
|
||||
|
||||
export interface PickedPhoto {
|
||||
key: string;
|
||||
/** A local preview while it uploads, then the forum's square preview. */
|
||||
preview: string;
|
||||
id: string | null;
|
||||
}
|
||||
|
||||
/** The photos picked for one post: resized, uploaded one by one, removable before sending. */
|
||||
export function usePhotoUploads() {
|
||||
const [items, setItems] = useState<PickedPhoto[]>([]);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const counter = useRef(0);
|
||||
|
||||
const add = useCallback(
|
||||
async (files: FileList | File[]) => {
|
||||
setError(null);
|
||||
const room = PER_POST - items.length;
|
||||
const chosen = Array.from(files).slice(0, Math.max(0, room));
|
||||
if (files.length > room) setError(`Egy hozzászóláshoz legfeljebb ${PER_POST} fotó tartozhat.`);
|
||||
for (const file of chosen) {
|
||||
const key = `p${++counter.current}`;
|
||||
const local = URL.createObjectURL(file);
|
||||
setItems((list) => [...list, { key, preview: local, id: null }]);
|
||||
try {
|
||||
const blob = await resizeForUpload(file);
|
||||
const res = await fetch("/api/v1/forum/photos", {
|
||||
method: "POST",
|
||||
body: blob,
|
||||
headers: { "content-type": blob.type },
|
||||
credentials: "same-origin",
|
||||
});
|
||||
const data = await res.json().catch(() => null);
|
||||
if (!res.ok) throw new Error(data?.error?.message ?? "A fotót nem sikerült feltölteni.");
|
||||
setItems((list) => list.map((p) => (p.key === key ? { ...p, id: data.id, preview: data.thumb } : p)));
|
||||
URL.revokeObjectURL(local);
|
||||
} catch (e) {
|
||||
setItems((list) => list.filter((p) => p.key !== key));
|
||||
URL.revokeObjectURL(local);
|
||||
setError(e instanceof Error ? e.message : "A fotót nem sikerült feltölteni.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
},
|
||||
[items.length],
|
||||
);
|
||||
|
||||
// Taken off before sending: the forum forgets it, and it does not count in the day's five.
|
||||
const remove = useCallback(
|
||||
(key: string) => {
|
||||
const gone = items.find((p) => p.key === key);
|
||||
if (gone?.id) {
|
||||
void fetch(`/api/v1/forum/photos/${gone.id}`, { method: "DELETE", credentials: "same-origin" }).catch(() => {});
|
||||
}
|
||||
setItems((list) => list.filter((p) => p.key !== key));
|
||||
},
|
||||
[items],
|
||||
);
|
||||
const reset = useCallback(() => {
|
||||
setItems([]);
|
||||
setError(null);
|
||||
}, []);
|
||||
|
||||
return {
|
||||
items,
|
||||
error,
|
||||
add,
|
||||
remove,
|
||||
reset,
|
||||
ids: items.filter((p) => p.id).map((p) => p.id as string),
|
||||
busy: items.some((p) => !p.id),
|
||||
full: items.length >= PER_POST,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user