import React, { useCallback, useEffect, useRef, useState } from "react"; import ColorHash from "color-hash"; import styles from "./DevTools.module.css"; const colorHash = new ColorHash({ lightness: 0.8 }); function UserId({ userId, ...rest }) { const shortUserId = userId.split(":")[0]; const color = colorHash.hex(shortUserId); return ( {shortUserId} ); } function CallId({ callId, ...rest }) { const shortId = callId.substr(callId.length - 16); const color = colorHash.hex(shortId); return ( {shortId} ); } export function DevTools({ manager }) { const [debugState, setDebugState] = useState(manager.debugState); const [selectedEvent, setSelectedEvent] = useState(); useEffect(() => { function onRoomDebug() { setDebugState(manager.debugState); } manager.on("debug", onRoomDebug); return () => { manager.removeListener("debug", onRoomDebug); }; }, [manager]); return (
{Array.from(debugState.entries()).map(([userId, props]) => ( ))} {selectedEvent && ( setSelectedEvent(null)} /> )}
); } function UserState({ userId, state, callId, events, onSelectEvent }) { const eventsRef = useRef(); const [autoScroll, setAutoScroll] = useState(true); useEffect(() => { if (autoScroll) { const el = eventsRef.current; el.scrollTop = el.scrollHeight - el.clientHeight; } }); const onScroll = useCallback(() => { const el = eventsRef.current; if (el.scrollHeight - el.scrollTop === el.clientHeight) { setAutoScroll(true); } else { setAutoScroll(false); } }, []); return (
{`(${state})`} {callId && }
{events.map((event, idx) => (
onSelectEvent(event)} > {event.type} {event.callId && ( )} {event.newCallId && ( <> {"->"} )} {event.to && ( )} {event.reason && ( {event.reason} )}
))}
); } function EventViewer({ event, onClose }) { return (

Event Type: {event.type}

{event.callId && (

Call Id:

)} {event.newCallId && (

New Call Id:

)} {event.to && (

To:

)} {event.reason && (

Reason: {event.reason}

)} {event.content && ( <>

Content:

            {JSON.stringify(event.content, undefined, 2)}
          
)}
); }