This commit is contained in:
108
src/components/RoomCard.jsx
Normal file
108
src/components/RoomCard.jsx
Normal file
@@ -0,0 +1,108 @@
|
||||
import { Users, Play, Trash2, Radio, Loader2 } from 'lucide-react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { useState, useEffect } from 'react';
|
||||
import api from '../services/api';
|
||||
import { useLanguage } from '../contexts/LanguageContext';
|
||||
import toast from 'react-hot-toast';
|
||||
|
||||
export default function RoomCard({ room, onDelete }) {
|
||||
const navigate = useNavigate();
|
||||
const { t } = useLanguage();
|
||||
const [status, setStatus] = useState({ running: false, participantCount: 0 });
|
||||
const [starting, setStarting] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const checkStatus = async () => {
|
||||
try {
|
||||
const res = await api.get(`/rooms/${room.uid}/status`);
|
||||
setStatus(res.data);
|
||||
} catch {
|
||||
// Ignore errors
|
||||
}
|
||||
};
|
||||
checkStatus();
|
||||
const interval = setInterval(checkStatus, 15000);
|
||||
return () => clearInterval(interval);
|
||||
}, [room.uid]);
|
||||
|
||||
return (
|
||||
<div className="card-hover group p-5 cursor-pointer" onClick={() => navigate(`/rooms/${room.uid}`)}>
|
||||
<div className="flex items-start justify-between mb-3">
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="flex items-center gap-2">
|
||||
<h3 className="text-base font-semibold text-th-text truncate group-hover:text-th-accent transition-colors">
|
||||
{room.name}
|
||||
</h3>
|
||||
{status.running && (
|
||||
<span className="flex items-center gap-1 px-2 py-0.5 bg-th-success/15 text-th-success rounded-full text-xs font-medium">
|
||||
<Radio size={10} className="animate-pulse" />
|
||||
{t('common.live')}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<p className="text-sm text-th-text-s mt-0.5">
|
||||
{room.uid.substring(0, 8)}...
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Room info */}
|
||||
<div className="flex items-center gap-4 mb-4 text-xs text-th-text-s">
|
||||
<span className="flex items-center gap-1">
|
||||
<Users size={14} />
|
||||
{status.running ? t('room.participants', { count: status.participantCount }) : t('common.offline')}
|
||||
</span>
|
||||
{room.max_participants > 0 && (
|
||||
<span>Max: {room.max_participants}</span>
|
||||
)}
|
||||
{room.access_code && (
|
||||
<span className="px-1.5 py-0.5 bg-th-warning/15 text-th-warning rounded text-[10px] font-medium">
|
||||
{t('common.protected')}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Actions */}
|
||||
<div className="flex items-center gap-2 pt-3 border-t border-th-border">
|
||||
<button
|
||||
onClick={async (e) => {
|
||||
e.stopPropagation();
|
||||
setStarting(true);
|
||||
try {
|
||||
if (status.running) {
|
||||
const data = room.access_code ? { access_code: prompt(t('room.enterAccessCode')) } : {};
|
||||
const res = await api.post(`/rooms/${room.uid}/join`, data);
|
||||
if (res.data.joinUrl) window.open(res.data.joinUrl, '_blank');
|
||||
} else {
|
||||
const res = await api.post(`/rooms/${room.uid}/start`);
|
||||
if (res.data.joinUrl) window.open(res.data.joinUrl, '_blank');
|
||||
toast.success(t('room.meetingStarted'));
|
||||
setTimeout(() => {
|
||||
api.get(`/rooms/${room.uid}/status`).then(r => setStatus(r.data)).catch(() => {});
|
||||
}, 2000);
|
||||
}
|
||||
} catch (err) {
|
||||
toast.error(err.response?.data?.error || t('room.meetingStartFailed'));
|
||||
} finally {
|
||||
setStarting(false);
|
||||
}
|
||||
}}
|
||||
disabled={starting}
|
||||
className="btn-primary text-xs py-1.5 px-3 flex-1"
|
||||
>
|
||||
{starting ? <Loader2 size={14} className="animate-spin" /> : <Play size={14} />}
|
||||
{status.running ? t('room.join') : t('room.startMeeting')}
|
||||
</button>
|
||||
{onDelete && (
|
||||
<button
|
||||
onClick={(e) => { e.stopPropagation(); onDelete(room); }}
|
||||
className="btn-ghost text-xs py-1.5 px-2 text-th-error hover:text-th-error"
|
||||
title={t('common.delete')}
|
||||
>
|
||||
<Trash2 size={14} />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user