feat(chat): add country flags and competitor numbers with normalized data architecture

Implemented display of country flags and competitor numbers in event chat messages:
- Country flags displayed as emoji (🇸🇪, 🇵🇱, etc.) with proper emoji font support
- Competitor numbers shown in #123 format next to usernames
- Normalized data architecture with user and participant caches on frontend
- User data (username, avatar, country) and participant data (competitorNumber) cached separately
- Messages store only core data (id, content, userId, createdAt)
- Prevents data inconsistency when users update profile information
- Fixed duplicate message keys React warning with deduplication logic
- Backend sends nested user/participant objects for cache population
- Auto-updates across all messages when user changes avatar or country

Backend changes:
- Socket.IO event_message and message_history include nested user/participant data
- API /events/:slug/messages endpoint restructured with same nested format
- Batch lookup of competitor numbers for efficiency

Frontend changes:
- useEventChat hook maintains userCache and participantCache
- ChatMessage component accepts separate user/participant props
- ChatMessageList performs cache lookups during render
- Emoji font family support for cross-platform flag rendering
This commit is contained in:
Radosław Gierwiało
2025-11-29 19:49:06 +01:00
parent 671b16cb82
commit 4e9557bd29
6 changed files with 269 additions and 17 deletions

View File

@@ -159,6 +159,7 @@ router.get('/:slug/messages', authenticate, async (req, res, next) => {
id: true,
username: true,
avatar: true,
country: true,
},
},
},
@@ -166,6 +167,24 @@ router.get('/:slug/messages', authenticate, async (req, res, next) => {
take: parseInt(limit),
});
// Get competitor numbers for all users in this event
const userIds = [...new Set(messages.map(msg => msg.user.id))];
const eventParticipants = await prisma.eventParticipant.findMany({
where: {
eventId: event.id,
userId: { in: userIds },
},
select: {
userId: true,
competitorNumber: true,
},
});
// Create a map of userId to competitorNumber
const competitorNumberMap = new Map(
eventParticipants.map(ep => [ep.userId, ep.competitorNumber])
);
// Return in chronological order (oldest first)
res.json({
success: true,
@@ -173,11 +192,20 @@ router.get('/:slug/messages', authenticate, async (req, res, next) => {
id: msg.id,
roomId: msg.roomId,
userId: msg.user.id,
username: msg.user.username,
avatar: msg.user.avatar,
content: msg.content,
type: msg.type,
createdAt: msg.createdAt,
// Nested user data for caching
user: {
id: msg.user.id,
username: msg.user.username,
avatar: msg.user.avatar,
country: msg.user.country,
},
// Nested participant data for caching
participant: {
competitorNumber: competitorNumberMap.get(msg.user.id),
},
})),
hasMore: messages.length === parseInt(limit),
});