import {useEffect, useRef} from 'react' import type {LogEntry} from '../types' import {formatBytes} from '../util' type Props = { log: LogEntry[] onClear: () => void } const time = (t: number) => new Date(t).toLocaleTimeString([], {hour: '2-digit', minute: '2-digit', second: '2-digit'}) export function MessageLog({log, onClear}: Props) { const bottomRef = useRef(null) // autoscroll to the newest entry useEffect(() => { bottomRef.current?.scrollIntoView({behavior: 'smooth', block: 'end'}) }, [log.length]) return (

Message log

{log.length === 0 && (

Messages and transfers will appear here.

)} {log.map(e => ( ))}
) } function LogRow({e}: {e: LogEntry}) { if (e.kind === 'system') { return (
{time(e.time)} {e.text}
) } const arrow = e.dir === 'out' ? '→' : '←' const who = e.dir === 'out' ? `to ${e.peerName}` : `from ${e.peerName}` return (
{time(e.time)} {arrow} {who} {e.kind === 'text' ? ( {e.text} ) : ( 📦 {e.fileName} · {formatBytes(e.size ?? 0)} {e.blobUrl && ( download )} {e.sha256 && ( sha256 {e.sha256.slice(0, 12)}… )} )}
) }