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 | 2x 2x 2x 140x 2x 16x 16x 16x 2x 1x 1x 108x 72x 16x 1x 15x 29x 15x 29x 1x 1x 15x 2x 4x 13x 65x 2x 25x | /**
* NewsletterSubscriberTable — Table with backend-driven sort
*
* Mirrors UserTable.tsx pattern but NO in-memory sort — render array as-is.
*
* Story 10.28: Newsletter Subscriber Management Page
*/
import React from 'react';
import {
Card,
CardActions,
CardContent,
Chip,
IconButton,
Paper,
Stack,
Table,
TableBody,
TableCell,
TableContainer,
TableHead,
TableRow,
TableSortLabel,
Typography,
} from '@mui/material';
import {
DeleteOutline,
MarkEmailRead,
PeopleAltOutlined,
PersonOutline as PersonIcon,
RestartAlt,
Unsubscribe,
} from '@mui/icons-material';
import Tooltip from '@mui/material/Tooltip';
import Box from '@mui/material/Box';
import { useTranslation } from 'react-i18next';
import { useBreakpoints } from '@/hooks/useBreakpoints';
import type { components } from '@/types/generated/event-newsletter-api.types';
type SubscriberResponse = components['schemas']['SubscriberResponse'];
type SubscriberAction = 'unsubscribe' | 'resubscribe' | 'unsuppress' | 'delete';
interface NewsletterSubscriberTableProps {
subscribers: SubscriberResponse[];
sortBy: string;
sortDir: 'asc' | 'desc';
onSortChange: (field: string, dir: 'asc' | 'desc') => void;
onAction: (action: SubscriberAction, subscriber: SubscriberResponse) => void;
}
const SORTABLE_COLUMNS = ['email', 'firstName', 'language', 'source', 'subscribedAt'] as const;
// Low-value columns hidden on phones (<600px) to avoid horizontal crowding.
const HIDE_ON_XS = new Set<string>(['language', 'source', 'subscribedAt']);
const hideOnXsSx = (col: string) =>
HIDE_ON_XS.has(col) ? { display: { xs: 'none', sm: 'table-cell' } } : undefined;
const NewsletterSubscriberTable: React.FC<NewsletterSubscriberTableProps> = ({
subscribers,
sortBy,
sortDir,
onSortChange,
onAction,
}) => {
const { t } = useTranslation('newsletterSubscribers');
const { isMobile } = useBreakpoints();
const handleSort = (field: string) => {
if (field === sortBy) {
onSortChange(field, sortDir === 'asc' ? 'desc' : 'asc');
} else {
onSortChange(field, 'asc');
}
};
const isActive = (sub: SubscriberResponse) => !sub.unsubscribedAt && !sub.suppressedAt;
const isSuppressed = (sub: SubscriberResponse) => !!sub.suppressedAt;
if (subscribers.length === 0) {
return (
<Paper sx={{ p: 4, textAlign: 'center' }}>
<PeopleAltOutlined sx={{ fontSize: 48, color: 'text.secondary', mb: 1 }} />
<Typography color="text.secondary">{t('table.empty')}</Typography>
</Paper>
);
}
const renderStatusChip = (sub: SubscriberResponse) =>
isSuppressed(sub) ? (
<Tooltip title={`${sub.bounceType ?? ''} — ${sub.bounceCount ?? 0} bounce(s)`}>
<Chip
label={t('status.suppressed')}
color="warning"
size="small"
data-testid={`suppressed-chip-${sub.id}`}
/>
</Tooltip>
) : (
<Chip
label={isActive(sub) ? t('status.active') : t('status.unsubscribed')}
color={isActive(sub) ? 'success' : 'default'}
size="small"
/>
);
// Directly-visible, status-conditional action buttons (no kebab menu).
// Rendered in both the desktop actions cell and the mobile card actions.
const renderActions = (sub: SubscriberResponse) => (
<Box
sx={{ display: 'flex', alignItems: 'center', justifyContent: 'flex-end', gap: 0.5 }}
data-testid={`actions-${sub.id}`}
>
{isActive(sub) && (
<Tooltip title={t('actions.unsubscribe')}>
<IconButton
size="small"
onClick={() => onAction('unsubscribe', sub)}
aria-label={t('actions.unsubscribe')}
data-testid={`action-unsubscribe-${sub.id}`}
>
<Unsubscribe fontSize="small" />
</IconButton>
</Tooltip>
)}
{!isActive(sub) && !isSuppressed(sub) && (
<Tooltip title={t('actions.resubscribe')}>
<IconButton
size="small"
onClick={() => onAction('resubscribe', sub)}
aria-label={t('actions.resubscribe')}
data-testid={`action-resubscribe-${sub.id}`}
>
<MarkEmailRead fontSize="small" />
</IconButton>
</Tooltip>
)}
{isSuppressed(sub) && (
<Tooltip title={t('actions.unsuppress')}>
<IconButton
size="small"
onClick={() => onAction('unsuppress', sub)}
aria-label={t('actions.unsuppress')}
data-testid={`action-unsuppress-${sub.id}`}
>
<RestartAlt fontSize="small" />
</IconButton>
</Tooltip>
)}
<Tooltip title={t('actions.delete')}>
<IconButton
size="small"
color="error"
onClick={() => onAction('delete', sub)}
aria-label={t('actions.delete')}
data-testid={`action-delete-${sub.id}`}
>
<DeleteOutline fontSize="small" />
</IconButton>
</Tooltip>
</Box>
);
if (isMobile) {
return (
<Box data-testid="subscriber-cards">
{subscribers.map((sub) => (
<Card key={sub.id} sx={{ mb: 2 }} data-testid={`subscriber-card-${sub.id}`}>
<CardContent>
<Stack spacing={1}>
<Typography variant="subtitle1" sx={{ wordBreak: 'break-word' }}>
{sub.email}
</Typography>
<Box sx={{ display: 'flex', alignItems: 'center', gap: 0.5 }}>
<Typography variant="body2" color="text.secondary">
{sub.firstName ?? '—'}
</Typography>
{sub.username && (
<Tooltip title={t('table.registeredUser')}>
<PersonIcon
sx={{ fontSize: 16, color: 'text.secondary' }}
aria-label={t('table.registeredUser')}
data-testid={`registered-badge-${sub.id}`}
/>
</Tooltip>
)}
</Box>
<Box>{renderStatusChip(sub)}</Box>
</Stack>
</CardContent>
<CardActions sx={{ justifyContent: 'flex-end' }}>{renderActions(sub)}</CardActions>
</Card>
))}
</Box>
);
}
return (
<TableContainer component={Paper}>
<Table data-testid="subscriber-table">
<TableHead>
<TableRow>
{SORTABLE_COLUMNS.map((col) => (
<TableCell key={col} sx={hideOnXsSx(col)}>
<TableSortLabel
active={sortBy === col}
direction={sortBy === col ? sortDir : 'asc'}
onClick={() => handleSort(col)}
data-testid={`sort-${col}`}
>
{t(`table.headers.${col}`)}
</TableSortLabel>
</TableCell>
))}
<TableCell>{t('table.headers.status')}</TableCell>
<TableCell align="right">{t('table.headers.actions')}</TableCell>
</TableRow>
</TableHead>
<TableBody>
{subscribers.map((sub) => (
<TableRow key={sub.id} hover>
<TableCell>{sub.email}</TableCell>
<TableCell>
<Box sx={{ display: 'flex', alignItems: 'center', gap: 0.5 }}>
{sub.firstName ?? '—'}
{sub.username && (
<Tooltip title={t('table.registeredUser')}>
<PersonIcon
sx={{ fontSize: 16, color: 'text.secondary' }}
aria-label={t('table.registeredUser')}
data-testid={`registered-badge-${sub.id}`}
/>
</Tooltip>
)}
</Box>
</TableCell>
<TableCell sx={hideOnXsSx('language')}>
<Chip label={sub.language ?? '—'} size="small" variant="outlined" />
</TableCell>
<TableCell sx={hideOnXsSx('source')}>
<Chip label={sub.source ?? '—'} size="small" variant="outlined" />
</TableCell>
<TableCell sx={hideOnXsSx('subscribedAt')}>
{sub.subscribedAt ? new Date(sub.subscribedAt).toLocaleDateString() : '—'}
</TableCell>
<TableCell>{renderStatusChip(sub)}</TableCell>
<TableCell align="right">{renderActions(sub)}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
);
};
export default NewsletterSubscriberTable;
|