- 공통 ChatMessage(이름 줄 + 말풍선, 내 말은 오른쪽, 이어 말하면 이름 한 번) — 방 채팅·마피아·라이어 게임, 그림 맞히기 이름 표시 - useStickToBottom: 개수 대신 목록 내용 변화를 보고 맨 아래 유지, 위로 올려 읽는 중엔 그대로, 보내면 다시 맨 아래 - 휴대폰 채팅 버튼의 새 메시지 수도 100줄 뒤에 0이 되던 문제(마지막 본 id 기준으로) - e2e/chat.e2e.ts: PC 대기실·게임 옆 칸·휴대폰 시트 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
30 lines
1.6 KiB
TypeScript
30 lines
1.6 KiB
TypeScript
/**
|
|
* One chat message, shared by the room chat and the games with their own chat (마피아, 라이어 게임): the name sits on
|
|
* its own small coloured line above a bubble with the text, so who said it and what they said never run together.
|
|
* My own messages sit on the right without a name; a run of messages from the same person shows the name once.
|
|
* Hidden names stay in the DOM for screen readers (`.sr-only`).
|
|
*/
|
|
import type { CSSProperties, ReactNode } from 'react';
|
|
|
|
/** Hues that stay readable as text on the panel in both themes (lightness comes from `--chat-name-l`). */
|
|
const HUES = [212, 152, 28, 275, 335, 188, 48, 0, 248, 100];
|
|
|
|
export function nameHue(id: string): number {
|
|
let h = 0;
|
|
for (let i = 0; i < id.length; i++) h = (h * 31 + id.charCodeAt(i)) >>> 0;
|
|
return HUES[h % HUES.length]!;
|
|
}
|
|
|
|
export function ChatMessage({ from, name, mine = false, cont = false, className, children }: { from: string; name: string; mine?: boolean; cont?: boolean; className?: string; children: ReactNode }) {
|
|
return (
|
|
<div className={`chat-line${mine ? ' mine' : ''}${cont ? ' cont' : ''}${className ? ` ${className}` : ''}`} style={{ '--name-h': nameHue(from) } as CSSProperties}>
|
|
<strong className={`chat-name${mine || cont ? ' sr-only' : ''}`}>{mine ? '나' : name}</strong>
|
|
<span className="chat-text">{children}</span>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
/** Same sender as the message right before, within 2 minutes: the name is not repeated. */
|
|
type RunKey = { from?: string | null; at: number } | undefined;
|
|
export const sameRun = (prev: RunKey, cur: RunKey): boolean => !!prev && !!cur?.from && prev.from === cur.from && cur.at - prev.at < 120_000;
|