xonecas.io
8058
Ir

irvc / commit 6ca8327

fix: display an error when a message is too big

- max 40k chars, like slack.
- Introduces a error toast component, respects the theme.

xonecas2026-10-20 6ca832714a29842b1aa912f9eff4a5afa88ecb01 parent aac2743

8 files changed

auth.go+1−1
@@ -25,7 +25,7 @@ const (
2525 argonThreads = 4
2626 argonKeyLen = 32
2727 minPassword = 8
28- maxMessageLen = 4000
28+ maxMessageLen = 40000
2929 )
3030
3131 func randToken() string {
channels.go+19−4
@@ -7,6 +7,7 @@ import (
77 "strconv"
88 "strings"
99 "time"
10+ "unicode/utf8"
1011
1112 "github.com/gin-gonic/gin"
1213 )
@@ -402,6 +403,22 @@ type sendMessageRequest struct {
402403 Body string `json:"body"`
403404 }
404405
406+// checkBody rejects empty bodies and ones over the size cap, replying with the
407+// reason. The composer has no copy of the cap, so the toast shows this message.
408+func checkBody(c *gin.Context, body string) bool {
409+ if body == "" {
410+ c.JSON(http.StatusBadRequest, gin.H{"error": "invalid message"})
411+ return false
412+ }
413+ if n := utf8.RuneCountInString(body); n > maxMessageLen {
414+ c.JSON(http.StatusRequestEntityTooLarge, gin.H{
415+ "error": fmt.Sprintf("message is too long: %d of %d characters", n, maxMessageLen),
416+ })
417+ return false
418+ }
419+ return true
420+}
421+
405422 func (s *Server) handleSendMessage(c *gin.Context) {
406423 channelID, err := strconv.ParseInt(c.Param("id"), 10, 64)
407424 if err != nil {
@@ -414,8 +431,7 @@ func (s *Server) handleSendMessage(c *gin.Context) {
414431 return
415432 }
416433 body := strings.TrimSpace(req.Body)
417- if body == "" || len(body) > maxMessageLen {
418- c.JSON(http.StatusBadRequest, gin.H{"error": "invalid message"})
434+ if !checkBody(c, body) {
419435 return
420436 }
421437 user := c.MustGet("user").(User)
@@ -450,8 +466,7 @@ func (s *Server) handleEditMessage(c *gin.Context) {
450466 return
451467 }
452468 body := strings.TrimSpace(req.Body)
453- if body == "" || len(body) > maxMessageLen {
454- c.JSON(http.StatusBadRequest, gin.H{"error": "invalid message"})
469+ if !checkBody(c, body) {
455470 return
456471 }
457472 user := c.MustGet("user").(User)
web/src/components/Composer.tsx+3−1
@@ -10,6 +10,7 @@ import { ImagePlus, SendHorizontal, Smile, X } from "lucide-react";
1010 import { cn } from "cn";
1111 import { Button, IconButton } from "@/ui";
1212 import { api, gifs, upload } from "../lib/api";
13+import { toast } from "../lib/toast";
1314 import type { Channel, Gif, UserSummary } from "../lib/types";
1415
1516 const maxInputHeight = 200;
@@ -115,8 +116,9 @@ export default function Composer({
115116 method: "POST",
116117 body: JSON.stringify({ body: text }),
117118 });
118- } catch {
119+ } catch (e) {
119120 setBody(text);
121+ toast(e instanceof Error ? e.message : "could not send message");
120122 }
121123 }
122124
web/src/components/Toasts.tsx+16−0
@@ -0,0 +1,16 @@
1+import { useToasts } from "../lib/toast";
2+
3+// Toasts is the one place transient errors surface. It sits at the top of the
4+// window, out of the composer's way, and is inert to the pointer.
5+export default function Toasts() {
6+ const toasts = useToasts();
7+ return (
8+ <div className="pointer-events-none fixed inset-x-0 top-3 z-50 flex flex-col items-center gap-1 px-4">
9+ {toasts.map((t) => (
10+ <div key={t.id} role="status" className="toast">
11+ {t.text}
12+ </div>
13+ ))}
14+ </div>
15+ );
16+}
web/src/index.css+22−0
@@ -203,6 +203,12 @@
203203 @apply my-1 h-px bg-line;
204204 }
205205
206+ /* transient errors, driven by src/lib/toast.ts */
207+ .toast {
208+ @apply max-w-[min(28rem,100%)] rounded-md border border-danger/40 bg-raised px-3 py-1.5 text-xs break-words text-danger shadow-lg;
209+ animation: toast 5s ease-out forwards;
210+ }
211+
206212 /* sidebar rows */
207213 .row {
208214 @apply flex min-w-0 items-center gap-2 rounded-md px-2 py-2 text-left text-sm text-muted transition-colors hover:bg-raised hover:text-fg;
@@ -299,3 +305,19 @@
299305 display: none;
300306 }
301307 }
308+
309+/* Fade in, hold, fade out, so a toast is gone before toast.ts drops it at 5s. */
310+@keyframes toast {
311+ 0% {
312+ opacity: 0;
313+ transform: translateY(4px);
314+ }
315+ 3%,
316+ 90% {
317+ opacity: 1;
318+ transform: translateY(0);
319+ }
320+ 100% {
321+ opacity: 0;
322+ }
323+}
web/src/lib/toast.ts+37−0
@@ -0,0 +1,37 @@
1+import { useSyncExternalStore } from "react";
2+
3+export type Toast = { id: number; text: string };
4+
5+// How long a toast lives, matching the fade-out in index.css (.toast).
6+const life = 5000;
7+
8+let toasts: Toast[] = [];
9+let nextId = 1;
10+const subs = new Set<() => void>();
11+
12+function publish() {
13+ subs.forEach((fn) => fn());
14+}
15+
16+// toast surfaces a transient error and lets it fade away on its own.
17+export function toast(text: string) {
18+ const t = { id: nextId++, text };
19+ toasts = [...toasts, t];
20+ publish();
21+ setTimeout(() => {
22+ toasts = toasts.filter((x) => x.id !== t.id);
23+ publish();
24+ }, life);
25+}
26+
27+export function useToasts() {
28+ return useSyncExternalStore(
29+ (fn) => {
30+ subs.add(fn);
31+ return () => {
32+ subs.delete(fn);
33+ };
34+ },
35+ () => toasts,
36+ );
37+}
web/src/main.tsx+7−1
@@ -1,5 +1,11 @@
11 import { createRoot } from "react-dom/client";
22 import "./index.css";
33 import App from "./App";
4+import Toasts from "./components/Toasts";
45
5-createRoot(document.getElementById("root")!).render(<App />);
6+createRoot(document.getElementById("root")!).render(
7+ <>
8+ <App />
9+ <Toasts />
10+ </>,
11+);
web/src/views/Chat.tsx+2−1
@@ -7,6 +7,7 @@ import { firstChannel } from "../lib/channels";
77 import { usePresence } from "../lib/presence";
88 import { useSettings } from "../lib/settings";
99 import { useTheme } from "../lib/theme";
10+import { toast } from "../lib/toast";
1011 import { beep, useVoice, type VoiceState } from "../lib/voice";
1112 import type { Channel, Message, Reaction, User, UserSummary, VoiceOccupant } from "../lib/types";
1213 import CallView from "../components/CallView";
@@ -344,7 +345,7 @@ export default function Chat({ user, onLogout }: { user: User; onLogout: () => v
344345 setEditing(null);
345346 if (!body || body === messages.find((m) => m.id === id)?.body) return;
346347 await api(`/messages/${id}`, { method: "PATCH", body: JSON.stringify({ body }) }).catch(
347- () => {},
348+ (e: Error) => toast(e.message),
348349 );
349350 }