Component props
The panel
<PriceAlertsPanel alerts={usePriceAlerts()} isOpen={expanded === 'priceAlerts'} />
export function PriceAlertsPanel({ alerts, isOpen }: PriceAlertsPanelProps) {
const [selected, setSelected] = useState<ReadonlySet<string>>(new Set());
const [isConfirmOpen, setIsConfirmOpen] = useState(false);
// Assume granted until known, so the prompt doesn't flash.
const [canNotify, setCanNotify] = useState(true);
useEffect(() => {
const load = async () => {
try {
setCanNotify(await hasNotificationPermission());
} catch (error) {
logger.warn(`Failed to check notification permission: ${getErrorMessage(error)}`, {
error,
});
}
};
void load();
}, []);
// Drops listed here are seen, once the page itself is showing.
useEffect(() => {
if (!isOpen) return;
const handleVisible = async () => {
if (document.visibilityState !== 'visible') return;
try {
await markDropsSeen(alerts);
} catch (error) {
logger.warn(`Failed to mark price drops as seen: ${getErrorMessage(error)}`, { error });
}
};
const listener = () => void handleVisible();
listener();
document.addEventListener('visibilitychange', listener);
return () => document.removeEventListener('visibilitychange', listener);
}, [alerts, isOpen]);
// Alerts can be removed elsewhere while selected.
const selectedIds = alerts.map(({ id }) => id).filter((id) => selected.has(id));
const isAllSelected = selectedIds.length === alerts.length;
const handleToggle = (id: string) => {
const next = new Set(selectedIds);
if (!next.delete(id)) next.add(id);
setSelected(next);
};
const handleToggleAll = () => {
setSelected(new Set(isAllSelected ? [] : alerts.map(({ id }) => id)));
};
const handleRemove = async (ids: string[]) => {
try {
await removePriceAlerts(ids);
} catch (error) {
logger.warn(`Failed to remove price alerts: ${getErrorMessage(error)}`, { error });
}
};
const handleRemoveAll = () => {
setIsConfirmOpen(false);
void handleRemove(alerts.map(({ id }) => id));
};
const handleAllowNotifications = async () => {
try {
setCanNotify(await requestNotificationPermission());
} catch (error) {
logger.warn(`Failed to request notifications: ${getErrorMessage(error)}`, { error });
}
};
if (alerts.length === 0) {
return (
<Typography variant="caption" color="text.secondary" sx={{ display: 'block', p: 1.5 }}>
{i18n('price_alerts_empty')}
</Typography>
);
}
return (
<Box sx={{ p: 1 }}>
{!canNotify && (
<Stack direction="row" spacing={1} sx={{ alignItems: 'center', mb: 1 }}>
<Typography variant="caption" color="text.secondary" sx={{ flex: 1 }}>
{i18n('price_alerts_notifications_off')}
</Typography>
<Button size="small" variant="outlined" onClick={() => void handleAllowNotifications()}>
{i18n('price_alerts_allow_notifications')}
</Button>
</Stack>
)}
<FormControlLabel
control={
<Checkbox
size="small"
checked={isAllSelected}
indeterminate={selectedIds.length > 0 && !isAllSelected}
onChange={handleToggleAll}
/>
}
label={<Typography variant="caption">{i18n('price_alerts_select_all')}</Typography>}
sx={{ ml: 0 }}
/>
<List dense disablePadding>
{alerts.map((alert) => (
<ListItem
key={alert.id}
divider
disablePadding
secondaryAction={
<Tooltip title={i18n('settings_remove')}>
<IconButton
edge="end"
size="small"
onClick={() => void handleRemove([alert.id])}
aria-label={i18n('settings_remove_item', [alert.title])}
>
<DeleteIcon fontSize="small" />
</IconButton>
</Tooltip>
}
>
<ListItemIcon sx={{ minWidth: 0 }}>
<Checkbox
size="small"
checked={selected.has(alert.id)}
onChange={() => handleToggle(alert.id)}
slotProps={{
input: { 'aria-label': i18n('price_alerts_select_item', [alert.title]) },
}}
/>
</ListItemIcon>
<ListItemText
primary={
<Link
href={alert.permalink}
target="_blank"
rel="noopener noreferrer"
variant="body2"
>
{alert.title}
</Link>
}
secondary={describeAlert(alert).map((line) => (
<Box component="span" key={line} sx={{ display: 'block' }}>
{line}
</Box>
))}
slotProps={{ secondary: { variant: 'caption' } }}
/>
</ListItem>
))}
</List>
<Stack direction="row" spacing={1} sx={{ mt: 1 }}>
<Button
fullWidth
variant="outlined"
size="small"
disabled={selectedIds.length === 0}
onClick={() => void handleRemove(selectedIds)}
>
{i18n('price_alerts_delete_selected', [String(selectedIds.length)])}
</Button>
<Button
fullWidth
variant="outlined"
color="warning"
size="small"
onClick={() => setIsConfirmOpen(true)}
>
{i18n('price_alerts_delete_all')}
</Button>
</Stack>
<Dialog
open={isConfirmOpen}
onClose={() => setIsConfirmOpen(false)}
aria-labelledby="price-alerts-dialog-title"
maxWidth="xs"
fullWidth
>
<DialogTitle id="price-alerts-dialog-title">
{i18n('price_alerts_delete_all_title')}
</DialogTitle>
<DialogContent>
<DialogContentText>
{i18n('price_alerts_delete_all_body', [String(alerts.length)])}
</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={() => setIsConfirmOpen(false)}>{i18n('migration_cancel')}</Button>
<Button color="error" variant="contained" onClick={handleRemoveAll}>
{i18n('price_alerts_delete_all')}
</Button>
</DialogActions>
</Dialog>
</Box>
);
}
Lists the products being watched for price drops, with controls to stop watching one, several or all of them.