Refactor from separate playing + waiting arrays to a single queue array where the first 2 entries are considered "playing".
- Simpler data model: One array instead of two
- Easier reordering: Just reorder within single array (no cross-array moves)
- Less complex logic: No sanitization needed, no moving between arrays
- Cleaner code: Remove
effectivePlayingCount,sanitised*variables - More intuitive: Queue position = array index
Changes:
- Replace
playing+waitingwith singlequeuearray - Remove complex sanitization logic from
reorderCourtEntrants - Simplify
removePlayerFromCourt(no array transfers) - Simplify
addPlayerToCourt(just append to queue) - Update
advanceCourtto remove first 2 entries - matchStartTime logic: Set when queue has exactly 2 entries, or when entry is removed and next player is promoted to position 0 or 1
Functions to update:
initializeCourtIfNeeded():{ queue: [], matchStartTime: null }subscribeToCourts():{ queue: snapshot.data().queue || [] }addPlayerToCourt():- Check duplicates in single
queuearray - Append to queue
- Set matchStartTime if
queue.length === 2
- Check duplicates in single
removePlayerFromCourt():- Filter out from
queue - Set matchStartTime if after removal we have exactly 2 players AND a player moved into top 2
- Filter out from
updatePlayerInCourt(): Find and update in singlequeuearrayadvanceCourt(): Remove first 2 from queue (currently playing), set matchStartTime if new top 2 existsreorderCourtEntrants():- Takes single
newQueuearray - Remove duplicates (keep first occurrence)
- Check if top 2 changed to determine matchStartTime
- Takes single
Changes:
- Courts state:
{ queue: [] }instead of{ playing: [], waiting: [] } - Pass
queueto Court component instead ofplayersandwaiting - Remove separate handlers for playing vs waiting
- Single
onRemovePlayerhandler (removes from queue at any position) - Single
onEditPlayerhandler (updates in queue at any position) - Update
onReorderEntriesto take single queue array
Changes:
- Accept
queueprop instead ofplayersandwaiting - Derive:
const playing = queue.slice(0, 2) - Derive:
const waiting = queue.slice(2) - Keep
PLAYING_SLOT_COUNT = 2constant - Remove separate
onRemovePlaying/onRemoveWaitingprops → singleonRemovePlayer - Remove separate
onEditPlaying/onEditWaitingprops → singleonEditPlayer - Update
onReorderEntriesto pass single reordered queue array - Update drag-and-drop to work with single queue array (simpler!)
- Remove
effectivePlayingCountcalculations - Update
renderQueueEntrantto calculate position based on index in full queue
Simplifications:
// Before: complex logic with playing.length checks
const effectivePlayingCount = Math.min(playingCount, PLAYING_SLOT_COUNT);
const positionLabel = section === "waiting" ? index - effectivePlayingCount + 1 : index + 1;
// After: simple index-based position
const positionLabel = index + 1;Before:
{
playing: [entrant1, entrant2],
waiting: [entrant3, entrant4, ...],
matchStartTime: timestamp
}After:
{
queue: [entrant1, entrant2, entrant3, entrant4, ...],
matchStartTime: timestamp
}Initialization: Since "start fresh" selected, just update initialization code. Existing courts will use new schema on next write.
- courtRepository.js - Update all functions to use
queueinstead ofplaying/waiting - App.js - Update state and prop passing to use single queue
- Court.js - Update to derive playing/waiting from queue prop
- Test manually - Add players, remove, reorder, advance queue
- Set to
Date.now()when:- Queue length becomes exactly 2 (first match starts)
- Top 2 players change (new match starts)
- Set to
nullwhen:- Queue has < 2 players (no match)
- Preserve existing time if top 2 players haven't changed
// Before: complex sanitization, splitting, cross-array checks
const seenIds = new Set();
const sanitisedPlaying = []; // max 2
const sanitisedWaiting = []; // rest
// ... 40 lines of logic
// After: simple deduplication
const seenIds = new Set();
const newQueue = [];
nextQueue.forEach(entrant => {
if (!entrant || seenIds.has(entrant.id)) return;
newQueue.push(entrant);
seenIds.add(entrant.id);
});- ~50-100 lines of code removed
- Simpler mental model: "It's just a queue"
- Easier debugging: One array to inspect instead of two
- Better maintainability: Less state to keep in sync
- Cleaner drag-and-drop: No section restrictions in reorder logic