08ab9ac71f
Full sweep of hardcoded English strings across:
- Meal pairing and drink pairing dialogs (titles, descriptions, role/
type labels, regenerate/generate buttons, progress labels) — also
fixed drink type labels that had French text hardcoded regardless
of locale ("Sans alcool"/"Chaud").
- Nutrition panel — had no useTranslations at all.
- Comments: header, empty/loading state, post/reply/cancel/delete
buttons, relative timestamps (just now/Xm ago/Xh ago/Xd ago), and
comment-reactions' toasts + aria-labels.
- Rating stars toasts.
- Direct messages: thread, conversation list, message button, both
/messages pages.
- People search page and component.
- URL import dialog (title, description, buttons, toasts).
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
119 lines
3.5 KiB
TypeScript
119 lines
3.5 KiB
TypeScript
"use client";
|
|
|
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
import { toast } from "sonner";
|
|
import { useTranslations } from "next-intl";
|
|
import { Send } from "lucide-react";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Textarea } from "@/components/ui/textarea";
|
|
import { cn } from "@/lib/utils";
|
|
|
|
type Message = {
|
|
id: string;
|
|
content: string;
|
|
senderId: string;
|
|
createdAt: string;
|
|
};
|
|
|
|
export function MessageThread({
|
|
conversationId,
|
|
currentUserId,
|
|
}: {
|
|
conversationId: string;
|
|
currentUserId: string;
|
|
}) {
|
|
const t = useTranslations("messages");
|
|
const [messages, setMessages] = useState<Message[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [content, setContent] = useState("");
|
|
const [sending, setSending] = useState(false);
|
|
const bottomRef = useRef<HTMLDivElement>(null);
|
|
|
|
const load = useCallback(async () => {
|
|
const res = await fetch(`/api/v1/conversations/${conversationId}/messages`);
|
|
if (res.ok) {
|
|
const data = (await res.json()) as { messages: Message[] };
|
|
setMessages(data.messages);
|
|
}
|
|
setLoading(false);
|
|
}, [conversationId]);
|
|
|
|
useEffect(() => {
|
|
void load();
|
|
const interval = setInterval(() => { void load(); }, 5000);
|
|
return () => clearInterval(interval);
|
|
}, [load]);
|
|
|
|
useEffect(() => {
|
|
bottomRef.current?.scrollIntoView({ behavior: "smooth" });
|
|
}, [messages.length]);
|
|
|
|
async function send() {
|
|
if (!content.trim()) return;
|
|
setSending(true);
|
|
try {
|
|
const res = await fetch(`/api/v1/conversations/${conversationId}/messages`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ content: content.trim() }),
|
|
});
|
|
if (!res.ok) {
|
|
const err = await res.json().catch(() => ({})) as { error?: string };
|
|
toast.error(err.error ?? t("sendFailed"));
|
|
return;
|
|
}
|
|
setContent("");
|
|
await load();
|
|
} finally {
|
|
setSending(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="flex flex-col h-full">
|
|
<div className="flex-1 overflow-y-auto space-y-3 p-4">
|
|
{loading ? (
|
|
<p className="text-sm text-muted-foreground">{t("loading")}</p>
|
|
) : messages.length === 0 ? (
|
|
<p className="text-sm text-muted-foreground text-center py-8">{t("noMessagesYet")}</p>
|
|
) : (
|
|
messages.map((m) => {
|
|
const isOwn = m.senderId === currentUserId;
|
|
return (
|
|
<div key={m.id} className={cn("flex", isOwn ? "justify-end" : "justify-start")}>
|
|
<div
|
|
className={cn(
|
|
"max-w-[70%] rounded-2xl px-4 py-2 text-sm whitespace-pre-wrap",
|
|
isOwn ? "bg-primary text-primary-foreground" : "bg-muted"
|
|
)}
|
|
>
|
|
{m.content}
|
|
</div>
|
|
</div>
|
|
);
|
|
})
|
|
)}
|
|
<div ref={bottomRef} />
|
|
</div>
|
|
<div className="border-t p-3 flex gap-2 items-end">
|
|
<Textarea
|
|
value={content}
|
|
onChange={(e) => setContent(e.target.value)}
|
|
onKeyDown={(e) => {
|
|
if (e.key === "Enter" && !e.shiftKey) {
|
|
e.preventDefault();
|
|
void send();
|
|
}
|
|
}}
|
|
placeholder={t("placeholder")}
|
|
rows={1}
|
|
className="resize-none"
|
|
/>
|
|
<Button size="icon" onClick={() => { void send(); }} disabled={!content.trim() || sending}>
|
|
<Send className="h-4 w-4" />
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|