2025-11-23 18:50:35 +01:00
|
|
|
import { useState, useEffect } from 'react';
|
|
|
|
|
import { Video, VideoOff, Clock, CheckCircle, XCircle, AlertTriangle, RefreshCw } from 'lucide-react';
|
|
|
|
|
import { matchingAPI } from '../../services/api';
|
|
|
|
|
import Avatar from '../common/Avatar';
|
2025-11-23 22:28:54 +01:00
|
|
|
import { SUGGESTION_STATUS, SUGGESTION_TYPE } from '../../constants';
|
2025-11-23 18:50:35 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* RecordingTab - Main component for managing recording partnerships
|
|
|
|
|
* Shows suggestions for who will record user's heats and who user needs to record
|
|
|
|
|
*/
|
|
|
|
|
const RecordingTab = ({ slug, event, myHeats }) => {
|
|
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
|
const [error, setError] = useState(null);
|
|
|
|
|
const [suggestions, setSuggestions] = useState(null);
|
|
|
|
|
const [recorderOptOut, setRecorderOptOut] = useState(false);
|
|
|
|
|
const [updatingOptOut, setUpdatingOptOut] = useState(false);
|
|
|
|
|
const [runningMatching, setRunningMatching] = useState(false);
|
|
|
|
|
|
|
|
|
|
// Load suggestions on mount
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
loadSuggestions();
|
|
|
|
|
}, [slug]);
|
|
|
|
|
|
|
|
|
|
const loadSuggestions = async () => {
|
|
|
|
|
try {
|
|
|
|
|
setLoading(true);
|
|
|
|
|
setError(null);
|
|
|
|
|
const data = await matchingAPI.getSuggestions(slug);
|
|
|
|
|
setSuggestions(data);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error('Failed to load suggestions:', err);
|
2025-11-29 15:04:41 +01:00
|
|
|
setError('Failed to load suggestions');
|
2025-11-23 18:50:35 +01:00
|
|
|
} finally {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleOptOutChange = async (newValue) => {
|
|
|
|
|
try {
|
|
|
|
|
setUpdatingOptOut(true);
|
|
|
|
|
await matchingAPI.setRecorderOptOut(slug, newValue);
|
|
|
|
|
setRecorderOptOut(newValue);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error('Failed to update opt-out:', err);
|
|
|
|
|
} finally {
|
|
|
|
|
setUpdatingOptOut(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleUpdateStatus = async (suggestionId, status) => {
|
|
|
|
|
try {
|
|
|
|
|
await matchingAPI.updateSuggestionStatus(slug, suggestionId, status);
|
|
|
|
|
// Reload suggestions
|
|
|
|
|
await loadSuggestions();
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error('Failed to update suggestion status:', err);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleRunMatching = async () => {
|
|
|
|
|
try {
|
|
|
|
|
setRunningMatching(true);
|
|
|
|
|
await matchingAPI.runMatching(slug);
|
|
|
|
|
await loadSuggestions();
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error('Failed to run matching:', err);
|
|
|
|
|
} finally {
|
|
|
|
|
setRunningMatching(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Calculate countdown to matching
|
|
|
|
|
const getCountdown = () => {
|
|
|
|
|
if (!event?.registrationDeadline) return null;
|
|
|
|
|
const deadline = new Date(event.registrationDeadline);
|
|
|
|
|
const now = new Date();
|
|
|
|
|
const diff = deadline.getTime() - now.getTime();
|
|
|
|
|
|
|
|
|
|
if (diff <= 0) return null;
|
|
|
|
|
|
|
|
|
|
const hours = Math.floor(diff / (1000 * 60 * 60));
|
|
|
|
|
const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
|
|
|
|
|
|
|
|
|
|
return { hours, minutes };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex items-center justify-center h-64">
|
|
|
|
|
<div className="flex flex-col items-center gap-3">
|
|
|
|
|
<RefreshCw className="w-8 h-8 animate-spin text-primary-600" />
|
2025-11-29 15:04:41 +01:00
|
|
|
<p className="text-gray-600">Loading...</p>
|
2025-11-23 18:50:35 +01:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="p-4">
|
|
|
|
|
<div className="bg-red-50 border border-red-200 rounded-lg p-4 text-red-700">
|
|
|
|
|
{error}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const countdown = getCountdown();
|
|
|
|
|
const toBeRecorded = suggestions?.toBeRecorded || [];
|
|
|
|
|
const toRecord = suggestions?.toRecord || [];
|
|
|
|
|
const matchingRunAt = suggestions?.matchingRunAt;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="p-4 space-y-6 overflow-y-auto h-full">
|
|
|
|
|
{/* Header with info */}
|
|
|
|
|
<div className="bg-gradient-to-r from-primary-50 to-primary-100 rounded-lg p-4">
|
|
|
|
|
<div className="flex items-center gap-3 mb-2">
|
|
|
|
|
<Video className="w-6 h-6 text-primary-600" />
|
2025-11-29 15:04:41 +01:00
|
|
|
<h3 className="text-lg font-semibold text-primary-900">Recording</h3>
|
2025-11-23 18:50:35 +01:00
|
|
|
</div>
|
|
|
|
|
<p className="text-sm text-primary-700">
|
2025-11-29 15:04:41 +01:00
|
|
|
System automatically pairs people for mutual recording of performances.
|
2025-11-23 18:50:35 +01:00
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Countdown or Status */}
|
|
|
|
|
{countdown ? (
|
|
|
|
|
<div className="bg-amber-50 border border-amber-200 rounded-lg p-4">
|
|
|
|
|
<div className="flex items-center gap-3">
|
|
|
|
|
<Clock className="w-5 h-5 text-amber-600" />
|
|
|
|
|
<div>
|
|
|
|
|
<p className="font-medium text-amber-800">
|
2025-11-29 15:04:41 +01:00
|
|
|
Matching will run in: {countdown.hours}h {countdown.minutes}min
|
2025-11-23 18:50:35 +01:00
|
|
|
</p>
|
|
|
|
|
<p className="text-sm text-amber-600">
|
2025-11-29 15:04:41 +01:00
|
|
|
After registration closes, the system will automatically pair recording partners.
|
2025-11-23 18:50:35 +01:00
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
) : matchingRunAt ? (
|
|
|
|
|
<div className="bg-green-50 border border-green-200 rounded-lg p-4">
|
|
|
|
|
<div className="flex items-center justify-between">
|
|
|
|
|
<div className="flex items-center gap-3">
|
|
|
|
|
<CheckCircle className="w-5 h-5 text-green-600" />
|
|
|
|
|
<div>
|
2025-11-29 15:04:41 +01:00
|
|
|
<p className="font-medium text-green-800">Matching completed</p>
|
2025-11-23 18:50:35 +01:00
|
|
|
<p className="text-sm text-green-600">
|
2025-11-29 15:04:41 +01:00
|
|
|
Last run: {new Date(matchingRunAt).toLocaleString('en-US')}
|
2025-11-23 18:50:35 +01:00
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<button
|
|
|
|
|
onClick={handleRunMatching}
|
|
|
|
|
disabled={runningMatching}
|
|
|
|
|
className="flex items-center gap-2 px-3 py-1.5 text-sm bg-green-600 text-white rounded-md hover:bg-green-700 disabled:opacity-50"
|
|
|
|
|
>
|
|
|
|
|
<RefreshCw className={`w-4 h-4 ${runningMatching ? 'animate-spin' : ''}`} />
|
2025-11-29 15:04:41 +01:00
|
|
|
{runningMatching ? 'Running...' : 'Run again'}
|
2025-11-23 18:50:35 +01:00
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<div className="bg-gray-50 border border-gray-200 rounded-lg p-4">
|
|
|
|
|
<div className="flex items-center justify-between">
|
|
|
|
|
<div className="flex items-center gap-3">
|
|
|
|
|
<AlertTriangle className="w-5 h-5 text-gray-500" />
|
2025-11-29 15:04:41 +01:00
|
|
|
<p className="text-gray-600">Matching has not been run yet</p>
|
2025-11-23 18:50:35 +01:00
|
|
|
</div>
|
|
|
|
|
<button
|
|
|
|
|
onClick={handleRunMatching}
|
|
|
|
|
disabled={runningMatching}
|
|
|
|
|
className="flex items-center gap-2 px-3 py-1.5 text-sm bg-primary-600 text-white rounded-md hover:bg-primary-700 disabled:opacity-50"
|
|
|
|
|
>
|
|
|
|
|
<RefreshCw className={`w-4 h-4 ${runningMatching ? 'animate-spin' : ''}`} />
|
2025-11-29 15:04:41 +01:00
|
|
|
{runningMatching ? 'Running...' : 'Run matching'}
|
2025-11-23 18:50:35 +01:00
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{/* My heats to be recorded */}
|
|
|
|
|
<div>
|
|
|
|
|
<h4 className="flex items-center gap-2 text-md font-semibold text-gray-900 mb-3">
|
|
|
|
|
<Video className="w-5 h-5 text-primary-600" />
|
2025-11-29 15:04:41 +01:00
|
|
|
My heats to be recorded ({toBeRecorded.length})
|
2025-11-23 18:50:35 +01:00
|
|
|
</h4>
|
|
|
|
|
|
|
|
|
|
{toBeRecorded.length === 0 ? (
|
|
|
|
|
<div className="bg-gray-50 rounded-lg p-4 text-center text-gray-500">
|
|
|
|
|
{myHeats.length === 0
|
2025-11-29 15:04:41 +01:00
|
|
|
? "You haven't declared any heats"
|
|
|
|
|
: 'No suggestions - run matching'
|
2025-11-23 18:50:35 +01:00
|
|
|
}
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<div className="space-y-2">
|
|
|
|
|
{toBeRecorded.map((suggestion) => (
|
|
|
|
|
<SuggestionCard
|
|
|
|
|
key={suggestion.id || suggestion.heat?.id}
|
|
|
|
|
suggestion={suggestion}
|
2025-11-23 22:28:54 +01:00
|
|
|
type={SUGGESTION_TYPE.TO_BE_RECORDED}
|
2025-11-23 22:21:12 +01:00
|
|
|
onAccept={() => handleUpdateStatus(suggestion.id, SUGGESTION_STATUS.ACCEPTED)}
|
|
|
|
|
onReject={() => handleUpdateStatus(suggestion.id, SUGGESTION_STATUS.REJECTED)}
|
2025-11-23 18:50:35 +01:00
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Heats I need to record */}
|
|
|
|
|
<div>
|
|
|
|
|
<h4 className="flex items-center gap-2 text-md font-semibold text-gray-900 mb-3">
|
|
|
|
|
<VideoOff className="w-5 h-5 text-amber-600" />
|
2025-11-29 15:04:41 +01:00
|
|
|
Recording others ({toRecord.length})
|
2025-11-23 18:50:35 +01:00
|
|
|
</h4>
|
|
|
|
|
|
|
|
|
|
{toRecord.length === 0 ? (
|
|
|
|
|
<div className="bg-gray-50 rounded-lg p-4 text-center text-gray-500">
|
2025-11-29 15:04:41 +01:00
|
|
|
You have no assigned heats to record
|
2025-11-23 18:50:35 +01:00
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<div className="space-y-2">
|
|
|
|
|
{toRecord.map((suggestion) => (
|
|
|
|
|
<SuggestionCard
|
|
|
|
|
key={suggestion.id || suggestion.heat?.id}
|
|
|
|
|
suggestion={suggestion}
|
2025-11-23 22:28:54 +01:00
|
|
|
type={SUGGESTION_TYPE.TO_RECORD}
|
2025-11-23 22:21:12 +01:00
|
|
|
onAccept={() => handleUpdateStatus(suggestion.id, SUGGESTION_STATUS.ACCEPTED)}
|
|
|
|
|
onReject={() => handleUpdateStatus(suggestion.id, SUGGESTION_STATUS.REJECTED)}
|
2025-11-23 18:50:35 +01:00
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Opt-out toggle */}
|
|
|
|
|
<div className="border-t pt-4">
|
|
|
|
|
<label className="flex items-center gap-3 cursor-pointer">
|
|
|
|
|
<input
|
|
|
|
|
type="checkbox"
|
|
|
|
|
checked={recorderOptOut}
|
|
|
|
|
onChange={(e) => handleOptOutChange(e.target.checked)}
|
|
|
|
|
disabled={updatingOptOut}
|
|
|
|
|
className="w-4 h-4 text-primary-600 rounded focus:ring-primary-500"
|
|
|
|
|
/>
|
|
|
|
|
<span className="text-sm text-gray-700">
|
2025-11-29 15:04:41 +01:00
|
|
|
I don't want to record others (opt-out)
|
2025-11-23 18:50:35 +01:00
|
|
|
</span>
|
|
|
|
|
{updatingOptOut && (
|
|
|
|
|
<RefreshCw className="w-4 h-4 animate-spin text-gray-400" />
|
|
|
|
|
)}
|
|
|
|
|
</label>
|
|
|
|
|
<p className="text-xs text-gray-500 mt-1 ml-7">
|
2025-11-29 15:04:41 +01:00
|
|
|
If checked, you'll be deprioritized when assigning recording partners.
|
2025-11-23 18:50:35 +01:00
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* SuggestionCard - Single suggestion item
|
|
|
|
|
*/
|
|
|
|
|
const SuggestionCard = ({ suggestion, type, onAccept, onReject }) => {
|
|
|
|
|
const heat = suggestion.heat;
|
|
|
|
|
const recorder = suggestion.recorder;
|
|
|
|
|
const dancer = heat?.user;
|
|
|
|
|
const status = suggestion.status;
|
|
|
|
|
|
|
|
|
|
// Format heat info
|
|
|
|
|
const heatInfo = heat
|
|
|
|
|
? `${heat.division?.abbreviation || '?'} ${heat.competitionType?.abbreviation || '?'} H${heat.heatNumber}`
|
|
|
|
|
: 'Unknown heat';
|
|
|
|
|
|
2025-11-23 22:28:54 +01:00
|
|
|
const person = type === SUGGESTION_TYPE.TO_BE_RECORDED ? recorder : dancer;
|
2025-11-29 15:04:41 +01:00
|
|
|
const personLabel = type === SUGGESTION_TYPE.TO_BE_RECORDED ? 'Recording you:' : 'You record:';
|
2025-11-23 18:50:35 +01:00
|
|
|
|
|
|
|
|
// Status badge
|
|
|
|
|
const getStatusBadge = () => {
|
|
|
|
|
switch (status) {
|
2025-11-23 22:21:12 +01:00
|
|
|
case SUGGESTION_STATUS.ACCEPTED:
|
2025-11-23 18:50:35 +01:00
|
|
|
return (
|
|
|
|
|
<span className="inline-flex items-center gap-1 px-2 py-0.5 text-xs font-medium bg-green-100 text-green-800 rounded">
|
|
|
|
|
<CheckCircle className="w-3 h-3" />
|
2025-11-29 15:04:41 +01:00
|
|
|
Accepted
|
2025-11-23 18:50:35 +01:00
|
|
|
</span>
|
|
|
|
|
);
|
2025-11-23 22:21:12 +01:00
|
|
|
case SUGGESTION_STATUS.REJECTED:
|
2025-11-23 18:50:35 +01:00
|
|
|
return (
|
|
|
|
|
<span className="inline-flex items-center gap-1 px-2 py-0.5 text-xs font-medium bg-red-100 text-red-800 rounded">
|
|
|
|
|
<XCircle className="w-3 h-3" />
|
2025-11-29 15:04:41 +01:00
|
|
|
Rejected
|
2025-11-23 18:50:35 +01:00
|
|
|
</span>
|
|
|
|
|
);
|
2025-11-23 22:21:12 +01:00
|
|
|
case SUGGESTION_STATUS.NOT_FOUND:
|
2025-11-23 18:50:35 +01:00
|
|
|
return (
|
|
|
|
|
<span className="inline-flex items-center gap-1 px-2 py-0.5 text-xs font-medium bg-amber-100 text-amber-800 rounded">
|
|
|
|
|
<AlertTriangle className="w-3 h-3" />
|
2025-11-29 15:04:41 +01:00
|
|
|
Not found
|
2025-11-23 18:50:35 +01:00
|
|
|
</span>
|
|
|
|
|
);
|
|
|
|
|
default:
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// No recorder found
|
2025-11-23 22:21:12 +01:00
|
|
|
if (status === SUGGESTION_STATUS.NOT_FOUND) {
|
2025-11-23 18:50:35 +01:00
|
|
|
return (
|
|
|
|
|
<div className="flex items-center justify-between p-3 bg-amber-50 border border-amber-200 rounded-lg">
|
|
|
|
|
<div className="flex items-center gap-3">
|
|
|
|
|
<div className="w-10 h-10 bg-amber-100 rounded-full flex items-center justify-center">
|
|
|
|
|
<AlertTriangle className="w-5 h-5 text-amber-600" />
|
|
|
|
|
</div>
|
|
|
|
|
<div>
|
|
|
|
|
<p className="font-medium text-amber-900">{heatInfo}</p>
|
2025-11-29 15:04:41 +01:00
|
|
|
<p className="text-sm text-amber-700">No available partner found</p>
|
2025-11-23 18:50:35 +01:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
{getStatusBadge()}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex items-center justify-between p-3 bg-white border border-gray-200 rounded-lg hover:border-gray-300 transition-colors">
|
|
|
|
|
<div className="flex items-center gap-3">
|
|
|
|
|
{person ? (
|
|
|
|
|
<Avatar
|
|
|
|
|
src={person.avatar}
|
|
|
|
|
username={person.username}
|
|
|
|
|
size={40}
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<div className="w-10 h-10 bg-gray-200 rounded-full" />
|
|
|
|
|
)}
|
|
|
|
|
<div>
|
|
|
|
|
<p className="font-medium text-gray-900">{heatInfo}</p>
|
|
|
|
|
<p className="text-sm text-gray-600">
|
|
|
|
|
{personLabel} <span className="font-medium">@{person?.username || '?'}</span>
|
|
|
|
|
{person?.city && (
|
|
|
|
|
<span className="text-gray-400"> ({person.city})</span>
|
|
|
|
|
)}
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="flex items-center gap-2">
|
2025-11-23 22:21:12 +01:00
|
|
|
{status === SUGGESTION_STATUS.PENDING ? (
|
2025-11-23 18:50:35 +01:00
|
|
|
<>
|
|
|
|
|
<button
|
|
|
|
|
onClick={onAccept}
|
|
|
|
|
className="p-2 text-green-600 hover:bg-green-50 rounded-md transition-colors"
|
2025-11-29 15:04:41 +01:00
|
|
|
title="Accept"
|
2025-11-23 18:50:35 +01:00
|
|
|
>
|
|
|
|
|
<CheckCircle className="w-5 h-5" />
|
|
|
|
|
</button>
|
|
|
|
|
<button
|
|
|
|
|
onClick={onReject}
|
|
|
|
|
className="p-2 text-red-600 hover:bg-red-50 rounded-md transition-colors"
|
2025-11-29 15:04:41 +01:00
|
|
|
title="Reject"
|
2025-11-23 18:50:35 +01:00
|
|
|
>
|
|
|
|
|
<XCircle className="w-5 h-5" />
|
|
|
|
|
</button>
|
|
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
getStatusBadge()
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default RecordingTab;
|