Feature: Watch Together - Chat, Voting, Sync-Indicator, Join-per-Link
- Raumliste zeigt jetzt Teilnehmernamen - Join per Link (?wt=roomId) für einfaches Teilen - Sync-Indikator (grün/gelb/rot) zeigt Synchronstatus - Pause/Skip-Voting für Nicht-Host-Teilnehmer - In-Room Chat mit Nachrichtenverlauf - "Gesehene entfernen" Button für Host in der Queue - REST endpoint GET /api/watch-together/room/:id Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
c79a8675b0
commit
8aefb49ff3
3 changed files with 442 additions and 2 deletions
|
|
@ -8,6 +8,7 @@ interface RoomInfo {
|
|||
name: string;
|
||||
hostName: string;
|
||||
memberCount: number;
|
||||
memberNames: string[];
|
||||
hasPassword: boolean;
|
||||
playing: boolean;
|
||||
}
|
||||
|
|
@ -76,6 +77,11 @@ export default function WatchTogetherTab({ data }: { data: any }) {
|
|||
const [currentTime, setCurrentTime] = useState(0);
|
||||
const [playerError, setPlayerError] = useState<string | null>(null);
|
||||
const [addingToQueue, setAddingToQueue] = useState(false);
|
||||
const [chatMessages, setChatMessages] = useState<Array<{ sender: string; text: string; timestamp: number }>>([]);
|
||||
const [chatInput, setChatInput] = useState('');
|
||||
const [votes, setVotes] = useState<{ skip: number; pause: number; total: number; needed: number } | null>(null);
|
||||
const [syncStatus, setSyncStatus] = useState<'synced' | 'drifting' | 'desynced'>('synced');
|
||||
const [showChat, setShowChat] = useState(true);
|
||||
|
||||
// ── Refs ──
|
||||
const wsRef = useRef<WebSocket | null>(null);
|
||||
|
|
@ -93,6 +99,7 @@ export default function WatchTogetherTab({ data }: { data: any }) {
|
|||
const ytReadyRef = useRef(false);
|
||||
const seekingRef = useRef(false);
|
||||
const timeUpdateRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
||||
const chatEndRef = useRef<HTMLDivElement | null>(null);
|
||||
|
||||
// Mirror state to refs
|
||||
useEffect(() => { currentRoomRef.current = currentRoom; }, [currentRoom]);
|
||||
|
|
@ -106,6 +113,22 @@ export default function WatchTogetherTab({ data }: { data: any }) {
|
|||
}
|
||||
}, [data]);
|
||||
|
||||
// ── Auto-scroll chat ──
|
||||
useEffect(() => {
|
||||
chatEndRef.current?.scrollIntoView({ behavior: 'smooth' });
|
||||
}, [chatMessages]);
|
||||
|
||||
// ── Join via link ──
|
||||
useEffect(() => {
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
const roomId = params.get('wt');
|
||||
if (roomId && userName.trim()) {
|
||||
// Delay to let WS connect first
|
||||
const timer = setTimeout(() => joinRoom(roomId), 1500);
|
||||
return () => clearTimeout(timer);
|
||||
}
|
||||
}, []);
|
||||
|
||||
// ── Save name ──
|
||||
useEffect(() => {
|
||||
if (userName) localStorage.setItem('wt_name', userName);
|
||||
|
|
@ -361,6 +384,17 @@ export default function WatchTogetherTab({ data }: { data: any }) {
|
|||
}
|
||||
}
|
||||
|
||||
// Sync status
|
||||
if (msg.currentTime !== undefined) {
|
||||
const localTime = getCurrentTime();
|
||||
if (localTime !== null) {
|
||||
const drift = Math.abs(localTime - msg.currentTime);
|
||||
if (drift < 1.5) setSyncStatus('synced');
|
||||
else if (drift < 5) setSyncStatus('drifting');
|
||||
else setSyncStatus('desynced');
|
||||
}
|
||||
}
|
||||
|
||||
setCurrentRoom(prev => prev ? {
|
||||
...prev,
|
||||
currentVideo: newVideo || null,
|
||||
|
|
@ -378,6 +412,21 @@ export default function WatchTogetherTab({ data }: { data: any }) {
|
|||
setCurrentRoom(prev => prev ? { ...prev, members: msg.members, hostId: msg.hostId } : prev);
|
||||
break;
|
||||
|
||||
case 'vote_updated':
|
||||
setVotes(msg.votes);
|
||||
break;
|
||||
|
||||
case 'chat':
|
||||
setChatMessages(prev => {
|
||||
const next = [...prev, { sender: msg.sender, text: msg.text, timestamp: msg.timestamp }];
|
||||
return next.length > 100 ? next.slice(-100) : next;
|
||||
});
|
||||
break;
|
||||
|
||||
case 'chat_history':
|
||||
setChatMessages(msg.messages || []);
|
||||
break;
|
||||
|
||||
case 'error':
|
||||
if (msg.code === 'WRONG_PASSWORD') {
|
||||
setJoinModal(prev => prev ? { ...prev, error: msg.message } : prev);
|
||||
|
|
@ -476,6 +525,9 @@ export default function WatchTogetherTab({ data }: { data: any }) {
|
|||
setQueueUrl('');
|
||||
setDuration(0);
|
||||
setCurrentTime(0);
|
||||
setChatMessages([]);
|
||||
setVotes(null);
|
||||
setSyncStatus('synced');
|
||||
}, [wsSend, destroyPlayer]);
|
||||
|
||||
// ── Add to queue ──
|
||||
|
|
@ -498,6 +550,35 @@ export default function WatchTogetherTab({ data }: { data: any }) {
|
|||
setAddingToQueue(false);
|
||||
}, [queueUrl, wsSend]);
|
||||
|
||||
// ── Send chat ──
|
||||
const sendChat = useCallback(() => {
|
||||
const text = chatInput.trim();
|
||||
if (!text) return;
|
||||
setChatInput('');
|
||||
wsSend({ type: 'chat_message', text });
|
||||
}, [chatInput, wsSend]);
|
||||
|
||||
// ── Vote callbacks ──
|
||||
const voteSkip = useCallback(() => {
|
||||
wsSend({ type: 'vote_skip' });
|
||||
}, [wsSend]);
|
||||
|
||||
const votePause = useCallback(() => {
|
||||
wsSend({ type: 'vote_pause' });
|
||||
}, [wsSend]);
|
||||
|
||||
// ── Clear watched ──
|
||||
const clearWatched = useCallback(() => {
|
||||
wsSend({ type: 'clear_watched' });
|
||||
}, [wsSend]);
|
||||
|
||||
// ── Copy room link ──
|
||||
const copyRoomLink = useCallback(() => {
|
||||
if (!currentRoom) return;
|
||||
const url = `${window.location.origin}${window.location.pathname}?wt=${currentRoom.id}`;
|
||||
navigator.clipboard.writeText(url).catch(() => {});
|
||||
}, [currentRoom]);
|
||||
|
||||
// ── Remove from queue ──
|
||||
const removeFromQueue = useCallback((index: number) => {
|
||||
wsSend({ type: 'remove_from_queue', index });
|
||||
|
|
@ -611,6 +692,9 @@ export default function WatchTogetherTab({ data }: { data: any }) {
|
|||
{hostMember && <span className="wt-host-badge">Host: {hostMember.name}</span>}
|
||||
</div>
|
||||
<div className="wt-room-header-right">
|
||||
<div className={`wt-sync-dot wt-sync-${syncStatus}`} title={syncStatus === 'synced' ? 'Synchron' : syncStatus === 'drifting' ? 'Leichte Verzögerung' : 'Nicht synchron'} />
|
||||
<button className="wt-header-btn" onClick={copyRoomLink} title="Link kopieren">Link</button>
|
||||
<button className="wt-header-btn" onClick={() => setShowChat(c => !c)} title={showChat ? 'Chat ausblenden' : 'Chat einblenden'}>Chat</button>
|
||||
<button className="wt-fullscreen-btn" onClick={toggleFullscreen} title={isFullscreen ? 'Vollbild verlassen' : 'Vollbild'}>
|
||||
{isFullscreen ? '\u2716' : '\u26F6'}
|
||||
</button>
|
||||
|
|
@ -675,7 +759,18 @@ export default function WatchTogetherTab({ data }: { data: any }) {
|
|||
</>
|
||||
) : (
|
||||
<>
|
||||
<span className="wt-ctrl-status">{currentRoom.playing ? '\u25B6' : '\u23F8'}</span>
|
||||
<button className="wt-ctrl-btn wt-vote-btn" onClick={votePause} title="Pause abstimmen">
|
||||
{currentRoom.playing ? '\u23F8' : '\u25B6'}
|
||||
</button>
|
||||
<button className="wt-ctrl-btn wt-vote-btn" onClick={voteSkip} title="Skip abstimmen">
|
||||
{'\u23ED'}
|
||||
</button>
|
||||
{votes && votes.skip > 0 && (
|
||||
<span className="wt-vote-count">{votes.skip}/{votes.needed} Skip</span>
|
||||
)}
|
||||
{votes && votes.pause > 0 && (
|
||||
<span className="wt-vote-count">{votes.pause}/{votes.needed} Pause</span>
|
||||
)}
|
||||
<div className="wt-seek-readonly">
|
||||
<div className="wt-seek-progress" style={{ width: duration > 0 ? `${(currentTime / duration) * 100}%` : '0%' }} />
|
||||
</div>
|
||||
|
|
@ -698,7 +793,12 @@ export default function WatchTogetherTab({ data }: { data: any }) {
|
|||
</div>
|
||||
|
||||
<div className="wt-queue-panel">
|
||||
<div className="wt-queue-header">Warteschlange ({currentRoom.queue.length})</div>
|
||||
<div className="wt-queue-header">
|
||||
<span>Warteschlange ({currentRoom.queue.length})</span>
|
||||
{isHost && currentRoom.queue.some(q => q.watched) && (
|
||||
<button className="wt-queue-clear-btn" onClick={clearWatched} title="Gesehene entfernen">Gesehene entfernen</button>
|
||||
)}
|
||||
</div>
|
||||
<div className="wt-queue-list">
|
||||
{currentRoom.queue.length === 0 ? (
|
||||
<div className="wt-queue-empty">Keine Videos in der Warteschlange</div>
|
||||
|
|
@ -749,6 +849,36 @@ export default function WatchTogetherTab({ data }: { data: any }) {
|
|||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{showChat && (
|
||||
<div className="wt-chat-panel">
|
||||
<div className="wt-chat-header">Chat</div>
|
||||
<div className="wt-chat-messages">
|
||||
{chatMessages.length === 0 ? (
|
||||
<div className="wt-chat-empty">Noch keine Nachrichten</div>
|
||||
) : (
|
||||
chatMessages.map((msg, i) => (
|
||||
<div key={i} className="wt-chat-msg">
|
||||
<span className="wt-chat-sender">{msg.sender}</span>
|
||||
<span className="wt-chat-text">{msg.text}</span>
|
||||
</div>
|
||||
))
|
||||
)}
|
||||
<div ref={chatEndRef} />
|
||||
</div>
|
||||
<div className="wt-chat-input-row">
|
||||
<input
|
||||
className="wt-input wt-chat-input"
|
||||
placeholder="Nachricht..."
|
||||
value={chatInput}
|
||||
onChange={e => setChatInput(e.target.value)}
|
||||
onKeyDown={e => { if (e.key === 'Enter') sendChat(); }}
|
||||
maxLength={500}
|
||||
/>
|
||||
<button className="wt-btn wt-chat-send-btn" onClick={sendChat}>Senden</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
|
@ -808,6 +938,12 @@ export default function WatchTogetherTab({ data }: { data: any }) {
|
|||
<div className="wt-tile-name">{room.name}</div>
|
||||
<div className="wt-tile-host">{room.hostName}</div>
|
||||
</div>
|
||||
{room.memberNames && room.memberNames.length > 0 && (
|
||||
<div className="wt-tile-members-list">
|
||||
{room.memberNames.slice(0, 5).join(', ')}
|
||||
{room.memberNames.length > 5 && ` +${room.memberNames.length - 5}`}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue