Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions apps/web/src/components/emoji-chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,46 @@ import { useCallback, useEffect, useRef, useState } from "react";
import { createPortal } from "react-dom";
import data from "@emoji-mart/data";
import Picker from "@emoji-mart/react";
import type { ChatMessage } from "@together/shared";

/**
* Scrollable chat log that keeps the newest message in view. Auto-scrolls only
* when the viewer is already pinned to the bottom, so scrolling up to re-read
* history is not interrupted by incoming messages.
*/
export function ChatMessages({ messages }: { messages: ChatMessage[] }) {
const containerRef = useRef<HTMLDivElement>(null);
const pinnedToBottomRef = useRef(true);

const handleScroll = useCallback(() => {
const el = containerRef.current;
if (!el) return;
const distanceFromBottom = el.scrollHeight - el.scrollTop - el.clientHeight;
pinnedToBottomRef.current = distanceFromBottom < 48;
}, []);

useEffect(() => {
const el = containerRef.current;
if (!el || !pinnedToBottomRef.current) return;
el.scrollTop = el.scrollHeight;
}, [messages.length]);

return (
<div
ref={containerRef}
onScroll={handleScroll}
className="min-h-0 flex-1 overflow-y-auto p-3 space-y-2"
>
{messages.map((msg) => (
<div key={msg.id} className="text-sm">
<span className="font-medium text-[var(--accent)]">{msg.senderName}</span>
<span className="mx-1 text-[var(--text-muted)]">·</span>
<span>{msg.body}</span>
</div>
))}
</div>
);
}

interface EmojiPickerButtonProps {
onSelect: (emoji: string) => void;
Expand Down
12 changes: 2 additions & 10 deletions apps/web/src/components/room-client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
} from "lucide-react";
import { useRoomSocket } from "@/hooks/use-room-socket";
import { useYouTubePlayer } from "@/hooks/use-youtube-player";
import { ChatInput } from "@/components/emoji-chat";
import { ChatInput, ChatMessages } from "@/components/emoji-chat";
import { PlaybackSeekBar } from "@/components/playback-seek-bar";
import { SettingsDrawer } from "@/components/room-settings";
import { useUserPreferences } from "@/hooks/use-user-preferences";
Expand Down Expand Up @@ -410,15 +410,7 @@ export function RoomClient({

const chatPanel = (
<>
<div className="min-h-0 flex-1 overflow-y-auto p-3 space-y-2">
{roomState?.chat.map((msg) => (
<div key={msg.id} className="text-sm">
<span className="font-medium text-[var(--accent)]">{msg.senderName}</span>
<span className="mx-1 text-[var(--text-muted)]">·</span>
<span>{msg.body}</span>
</div>
))}
</div>
<ChatMessages messages={roomState?.chat ?? []} />
<ChatInput
onSend={(body) => send({ type: "chat", body })}
slowModeSeconds={settings?.slowModeSeconds}
Expand Down
Loading