Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 | 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 5x 5x 5x 5x 9x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 10x 9x 5x 1x 9x 5x 8x 9x 4x 9x 1x 1x | /**
* TopicSelectionOverlay (Epic 14, Story 14.F.3)
*
* The focused, in-page topic experience opened from Details ▸ Info "Change topic"
* (replaces the standalone /organizer/topics route for the event-bound flow).
* Two states (UX spec §9; FR40/41/42, AR7):
* A. Pick — filters on a horizontal top bar + a single card grid (or heat-map).
* B. Brainstorm — the selected topic pins to the top + SpeakerBrainstormingPanel.
*
* Recompose, not rewrite (NFR9): reuses useTopics / useSelectTopicForEvent
* (via useTopicSelection) / useSimilarTopics / useUserList / CreateTopicModal /
* SpeakerBrainstormingPanel / MultiTopicHeatMap. No backend; search is
* client-side over the loaded page. The standalone route is untouched.
*/
import React, { useEffect, useMemo, useState } from 'react';
import {
Alert,
Box,
Button,
Dialog,
DialogActions,
DialogContent,
DialogContentText,
DialogTitle,
FormControl,
IconButton,
InputLabel,
List,
ListItem,
ListItemText,
MenuItem,
Select,
Stack,
TextField,
ToggleButton,
ToggleButtonGroup,
Typography,
useMediaQuery,
useTheme,
} from '@mui/material';
import {
Add as AddIcon,
Close as CloseIcon,
ViewList as ListIcon,
ViewModule as HeatMapIcon,
} from '@mui/icons-material';
import type { SelectChangeEvent } from '@mui/material';
import { useTranslation } from 'react-i18next';
import { useNavigate } from 'react-router-dom';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { useTopics, useTopic } from '@/hooks/useTopics';
import { useEvents } from '@/hooks/useEvents';
import { useUserList } from '@/hooks/useUserManagement/useUserList';
import { topicService } from '@/services/topicService';
import type { Event } from '@/types/event.types';
import type { Topic, TopicFilters, TopicStatus } from '@/types/topic.types';
import { CreateTopicModal } from '@/components/TopicBacklogManager/CreateTopicModal';
import { SpeakerBrainstormingPanel } from '@/components/SpeakerBrainstormingPanel/SpeakerBrainstormingPanel';
import { MultiTopicHeatMap } from '@/components/TopicHeatMap';
import { TopicSelectionCard } from './TopicSelectionCard';
import { useTopicSelection } from './useTopicSelection';
export interface TopicSelectionOverlayProps {
open: boolean;
eventCode: string;
/** The event's current topic (event.topicCode) — opens straight into State B when set. */
currentTopicCode?: string;
onClose: () => void;
onConfirmed?: (topicCode: string) => void;
}
type ViewMode = 'list' | 'heatMap';
export const TopicSelectionOverlay: React.FC<TopicSelectionOverlayProps> = ({
open,
eventCode,
currentTopicCode,
onClose,
onConfirmed,
}) => {
const { t } = useTranslation(['events', 'organizer', 'common']);
const navigate = useNavigate();
const theme = useTheme();
const queryClient = useQueryClient();
const isMobile = useMediaQuery(theme.breakpoints.down('md'));
const [phase, setPhase] = useState<'pick' | 'brainstorm'>(
currentTopicCode ? 'brainstorm' : 'pick'
);
const [confirmedTopicCode, setConfirmedTopicCode] = useState<string | undefined>(
currentTopicCode
);
const [filters, setFilters] = useState<TopicFilters>({
page: 1,
limit: 50,
sort: '-stalenessScore',
include: 'history',
});
const [search, setSearch] = useState('');
const [viewMode, setViewMode] = useState<ViewMode>('list');
const [createOpen, setCreateOpen] = useState(false);
const [editingTopic, setEditingTopic] = useState<Topic | null>(null);
const [deletingTopic, setDeletingTopic] = useState<Topic | null>(null);
// On (re)open, reset the phase to match the event's current topic.
useEffect(() => {
Eif (open) {
setConfirmedTopicCode(currentTopicCode);
setPhase(currentTopicCode ? 'brainstorm' : 'pick');
setSearch('');
}
}, [open, currentTopicCode]);
const handleConfirmed = (topicCode: string) => {
setConfirmedTopicCode(topicCode);
setPhase('brainstorm');
onConfirmed?.(topicCode);
};
const selection = useTopicSelection({ eventCode, onConfirmed: handleConfirmed });
const { data, isLoading, isError } = useTopics(filters);
const { data: confirmedTopic } = useTopic(confirmedTopicCode ?? '');
const { data: allEventsData } = useEvents({ page: 1, limit: 1000 });
const { data: organizersData } = useUserList({
filters: { role: ['ORGANIZER'] },
pagination: { page: 1, limit: 100 },
});
const deleteTopicMutation = useMutation({
mutationFn: (topicCode: string) => topicService.deleteTopic(topicCode),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['topics'] });
setDeletingTopic(null);
},
});
const eventLookup = useMemo(() => {
const map = new Map<number, { title: string }>();
allEventsData?.data?.forEach((e: Event) => map.set(e.eventNumber, { title: e.title }));
return map;
}, [allEventsData]);
const organizers = useMemo(
() =>
(organizersData?.data ?? []).map((u) => ({
id: u.id,
name: `${u.firstName} ${u.lastName}`.trim() || u.id,
})),
[organizersData]
);
const visibleTopics = useMemo(() => {
const topics = data?.data ?? [];
const q = search.trim().toLowerCase();
return q ? topics.filter((tp) => tp.title.toLowerCase().includes(q)) : topics;
}, [data, search]);
const handleFilter =
(key: keyof TopicFilters) =>
(e: SelectChangeEvent<string>): void =>
setFilters((prev) => ({ ...prev, [key]: e.target.value || undefined, page: 1 }));
const renderTopBar = () => (
<Stack
direction={{ xs: 'column', md: 'row' }}
spacing={1.5}
alignItems={{ xs: 'stretch', md: 'center' }}
sx={{ mb: 2 }}
data-testid="topic-overlay-filter-bar"
>
<TextField
size="small"
label={t('eventPage.topicOverlay.searchPlaceholder', 'Search topics')}
value={search}
onChange={(e) => setSearch(e.target.value)}
sx={{ flexGrow: 1, minWidth: 160 }}
inputProps={{ 'data-testid': 'topic-overlay-search' }}
/>
<FormControl size="small" sx={{ minWidth: 140 }}>
<InputLabel>{t('organizer:topicBacklog.filters.category', 'Category')}</InputLabel>
<Select
value={filters.category ?? ''}
label={t('organizer:topicBacklog.filters.category', 'Category')}
onChange={handleFilter('category')}
data-testid="topic-overlay-filter-category"
>
<MenuItem value="">{t('organizer:topicBacklog.filters.all', 'All Categories')}</MenuItem>
<MenuItem value="technical">
{t('organizer:topicBacklog.filters.categories.technical', 'Technical')}
</MenuItem>
<MenuItem value="management">
{t('organizer:topicBacklog.filters.categories.management', 'Management')}
</MenuItem>
<MenuItem value="soft_skills">
{t('organizer:topicBacklog.filters.categories.softSkills', 'Soft Skills')}
</MenuItem>
<MenuItem value="industry_trends">
{t('organizer:topicBacklog.filters.categories.industryTrends', 'Industry Trends')}
</MenuItem>
<MenuItem value="tools_platforms">
{t('organizer:topicBacklog.filters.categories.toolsPlatforms', 'Tools & Platforms')}
</MenuItem>
</Select>
</FormControl>
<FormControl size="small" sx={{ minWidth: 140 }}>
<InputLabel>{t('common:labels.status', 'Status')}</InputLabel>
<Select
value={(filters.status as string) ?? ''}
label={t('common:labels.status', 'Status')}
onChange={(e) =>
setFilters((prev) => ({
...prev,
status: (e.target.value as TopicStatus) || undefined,
page: 1,
}))
}
>
<MenuItem value="">
{t('organizer:topicBacklog.filters.allStatuses', 'All Statuses')}
</MenuItem>
<MenuItem value="AVAILABLE">
{t('organizer:topicBacklog.filters.statuses.available', 'Available')}
</MenuItem>
<MenuItem value="CAUTION">
{t('organizer:topicBacklog.filters.statuses.caution', 'Caution')}
</MenuItem>
<MenuItem value="UNAVAILABLE">
{t('organizer:topicBacklog.filters.statuses.unavailable', 'Too Recent')}
</MenuItem>
</Select>
</FormControl>
<FormControl size="small" sx={{ minWidth: 150 }}>
<InputLabel>{t('common:labels.sortBy', 'Sort By')}</InputLabel>
<Select
value={filters.sort ?? '-stalenessScore'}
label={t('common:labels.sortBy', 'Sort By')}
onChange={handleFilter('sort')}
>
<MenuItem value="-stalenessScore">
{t('organizer:topicBacklog.filters.sortOptions.safestFirst', 'Safest First')}
</MenuItem>
<MenuItem value="stalenessScore">
{t('organizer:topicBacklog.filters.sortOptions.recentFirst', 'Recent First')}
</MenuItem>
<MenuItem value="-createdAt">
{t('organizer:topicBacklog.filters.sortOptions.newestFirst', 'Newest First')}
</MenuItem>
<MenuItem value="title">
{t('organizer:topicBacklog.filters.sortOptions.titleAZ', 'Title (A-Z)')}
</MenuItem>
<MenuItem value="-usageCount">
{t('organizer:topicBacklog.filters.sortOptions.mostUsed', 'Most Used')}
</MenuItem>
</Select>
</FormControl>
<ToggleButtonGroup
size="small"
exclusive
value={viewMode}
onChange={(_e, mode) => mode && setViewMode(mode)}
aria-label={t('organizer:topicBacklog.viewMode.label', 'View mode')}
>
<ToggleButton value="list" data-testid="topic-overlay-view-list">
<ListIcon fontSize="small" />
</ToggleButton>
<ToggleButton value="heatMap" data-testid="topic-overlay-view-heatmap">
<HeatMapIcon fontSize="small" />
</ToggleButton>
</ToggleButtonGroup>
<Button
variant="contained"
size="small"
startIcon={<AddIcon />}
onClick={() => {
setEditingTopic(null);
setCreateOpen(true);
}}
data-testid="topic-overlay-new-topic"
>
{t('organizer:topicBacklog.createNew', 'New topic')}
</Button>
</Stack>
);
const renderPick = () => (
<Box>
{renderTopBar()}
{isLoading && (
<Typography color="text.secondary">{t('common:status.loading', 'Loading…')}</Typography>
)}
{isError && (
<Alert severity="error">
{t('organizer:topicBacklog.error.loadFailed', 'Failed to load topics')}
</Alert>
)}
{data && viewMode === 'heatMap' && (
<MultiTopicHeatMap
topics={visibleTopics}
onTopicSelect={(tp) => selection.requestSelect(tp)}
eventLookup={eventLookup}
/>
)}
{data && viewMode === 'list' && visibleTopics.length === 0 && (
<Typography color="text.secondary" data-testid="topic-overlay-empty">
{t('eventPage.topicOverlay.emptyHint', 'No topics match — adjust filters or search.')}
</Typography>
)}
{data && viewMode === 'list' && visibleTopics.length > 0 && (
<Box
sx={{
display: 'grid',
gridTemplateColumns: { xs: '1fr', sm: '1fr 1fr', lg: '1fr 1fr 1fr' },
gap: 2,
}}
data-testid="topic-overlay-grid"
>
{visibleTopics.map((tp) => (
<TopicSelectionCard
key={tp.topicCode}
topic={tp}
hasHighSimilarity={selection.hasHighSimilarity(tp)}
isPending={selection.isPending}
onSelect={selection.requestSelect}
onEdit={(topic) => {
setEditingTopic(topic);
setCreateOpen(true);
}}
onDelete={(topic) => setDeletingTopic(topic)}
/>
))}
</Box>
)}
</Box>
);
const renderBrainstorm = () => (
<Box>
<Alert
severity="success"
data-testid="topic-overlay-pinned-banner"
action={
<Button
color="inherit"
size="small"
onClick={() => setPhase('pick')}
data-testid="topic-overlay-change-topic"
>
{t('eventPage.topicOverlay.banner.change', 'Change topic')}
</Button>
}
sx={{ mb: 2 }}
>
{t('eventPage.topicOverlay.banner.selected', 'Topic selected')}:{' '}
<strong>{confirmedTopic?.title ?? confirmedTopicCode}</strong>
{confirmedTopic?.stalenessScore !== undefined && ` · ${confirmedTopic.stalenessScore}%`}
</Alert>
<SpeakerBrainstormingPanel eventCode={eventCode} organizers={organizers} />
</Box>
);
return (
<>
<Dialog
open={open}
onClose={onClose}
fullScreen={isMobile}
maxWidth="lg"
fullWidth
data-testid="topic-overlay"
>
<DialogTitle
sx={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}
>
{phase === 'pick'
? t('eventPage.topicOverlay.pickHeading', 'Choose a topic')
: t('eventPage.topicOverlay.title', 'Topic & speakers')}
<IconButton
onClick={onClose}
data-testid="topic-overlay-close"
aria-label={t('common:actions.close', 'Close')}
>
<CloseIcon />
</IconButton>
</DialogTitle>
<DialogContent dividers>
{phase === 'pick' ? renderPick() : renderBrainstorm()}
</DialogContent>
{phase === 'brainstorm' && (
<DialogActions>
<Button onClick={onClose} data-testid="topic-overlay-skip">
{t('eventPage.topicOverlay.footer.skip', 'Skip for now')}
</Button>
<Button
variant="contained"
onClick={() => {
navigate(`/organizer/events/${eventCode}?tab=speakers&view=pool`);
onClose();
}}
data-testid="topic-overlay-continue"
>
{t('eventPage.topicOverlay.footer.continue', 'Continue to outreach →')}
</Button>
</DialogActions>
)}
</Dialog>
{/* Create / Edit topic */}
<CreateTopicModal
open={createOpen}
topic={editingTopic}
onClose={() => {
setCreateOpen(false);
setEditingTopic(null);
}}
/>
{/* Similarity confirm (high >0.7) */}
<Dialog
open={selection.mode === 'similar'}
onClose={selection.cancel}
maxWidth="sm"
fullWidth
>
<DialogTitle>
{t('organizer:topicBacklog.dialogs.similar.title', 'Similar Topics Detected')}
</DialogTitle>
<DialogContent>
<DialogContentText>
{t(
'organizer:topicBacklog.dialogs.similar.message',
'The following topics are highly similar. Proceed anyway?'
)}
</DialogContentText>
<List dense>
{selection.similarTopics.map((st) => (
<ListItem key={st.topicCode}>
<ListItemText primary={st.title} />
</ListItem>
))}
</List>
<TextField
fullWidth
multiline
rows={3}
label={t(
'organizer:topicBacklog.dialogs.similar.justification',
'Justification (optional)'
)}
value={selection.justification}
onChange={(e) => selection.setJustification(e.target.value)}
sx={{ mt: 1 }}
/>
</DialogContent>
<DialogActions>
<Button onClick={selection.cancel}>{t('common:actions.cancel', 'Cancel')}</Button>
<Button
variant="contained"
onClick={selection.commit}
data-testid="topic-overlay-similar-confirm"
>
{t('organizer:topicBacklog.dialogs.similar.confirm', 'Select Anyway')}
</Button>
</DialogActions>
</Dialog>
{/* Override (too recent) — justification required */}
<Dialog
open={selection.mode === 'override'}
onClose={selection.cancel}
maxWidth="sm"
fullWidth
>
<DialogTitle>
{t('organizer:topicBacklog.dialogs.override.title', 'Override Staleness Warning')}
</DialogTitle>
<DialogContent>
<DialogContentText>
{t(
'organizer:topicBacklog.dialogs.override.message',
'This topic was used recently. Please provide a justification for reusing it.'
)}
</DialogContentText>
<TextField
fullWidth
required
multiline
rows={4}
label={t('organizer:topicBacklog.dialogs.override.justification', 'Justification')}
value={selection.justification}
onChange={(e) => selection.setJustification(e.target.value)}
sx={{ mt: 1 }}
/>
</DialogContent>
<DialogActions>
<Button onClick={selection.cancel}>{t('common:actions.cancel', 'Cancel')}</Button>
<Button
variant="contained"
onClick={selection.commit}
disabled={!selection.justification.trim()}
data-testid="topic-overlay-override-confirm"
>
{t('organizer:topicBacklog.dialogs.override.confirm', 'Override and Select')}
</Button>
</DialogActions>
</Dialog>
{/* Delete confirm */}
<Dialog open={!!deletingTopic} onClose={() => setDeletingTopic(null)} maxWidth="sm" fullWidth>
<DialogTitle>
{t('organizer:topicBacklog.dialogs.delete.title', 'Delete Topic')}
</DialogTitle>
<DialogContent>
<DialogContentText>
{t(
'organizer:topicBacklog.dialogs.delete.message',
'Delete "{{title}}"? This cannot be undone.',
{
title: deletingTopic?.title ?? '',
}
)}
</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={() => setDeletingTopic(null)} disabled={deleteTopicMutation.isPending}>
{t('common:actions.cancel', 'Cancel')}
</Button>
<Button
color="error"
variant="contained"
disabled={deleteTopicMutation.isPending}
onClick={() => deletingTopic && deleteTopicMutation.mutate(deletingTopic.topicCode)}
data-testid="topic-overlay-delete-confirm"
>
{t('organizer:topicBacklog.dialogs.delete.confirm', 'Delete Topic')}
</Button>
</DialogActions>
</Dialog>
</>
);
};
export default TopicSelectionOverlay;
|