Notification history: Add checkboxes for multiple selection (#49392)

* Add checkboxes for selection

* className is optional...

* review comments
This commit is contained in:
Ashley Harrison 2022-05-23 12:50:26 +01:00 committed by GitHub
parent 755ec3b469
commit 349d9973de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 29 deletions

View File

@ -1,46 +1,43 @@
import { css } from '@emotion/css'; import { css, cx } from '@emotion/css';
import { formatDistanceToNow } from 'date-fns'; import { formatDistanceToNow } from 'date-fns';
import React, { ReactNode } from 'react'; import React, { ReactNode } from 'react';
import { GrafanaTheme2 } from '@grafana/data'; import { GrafanaTheme2 } from '@grafana/data';
import { config } from '@grafana/runtime'; import { config } from '@grafana/runtime';
import { Icon, IconButton, IconName, useTheme2 } from '@grafana/ui'; import { Icon, IconName, useTheme2 } from '@grafana/ui';
import { getIconFromSeverity } from '@grafana/ui/src/components/Alert/Alert'; import { getIconFromSeverity } from '@grafana/ui/src/components/Alert/Alert';
export type AlertVariant = 'success' | 'warning' | 'error' | 'info'; export type AlertVariant = 'success' | 'warning' | 'error' | 'info';
export interface Props { export interface Props {
className?: string;
title: string; title: string;
severity?: AlertVariant; severity?: AlertVariant;
timestamp?: number; timestamp?: number;
traceId?: string; traceId?: string;
children?: ReactNode; children?: ReactNode;
onRemove?: (event: React.MouseEvent) => void;
} }
export const StoredNotificationItem = ({ export const StoredNotificationItem = ({
className,
title, title,
severity = 'error', severity = 'error',
traceId, traceId,
timestamp, timestamp,
children, children,
onRemove,
}: Props) => { }: Props) => {
const theme = useTheme2(); const theme = useTheme2();
const styles = getStyles(theme, severity); const styles = getStyles(theme, severity);
const showTraceId = config.featureToggles.tracing && traceId; const showTraceId = config.featureToggles.tracing && traceId;
return ( return (
<div className={styles.wrapper}> <div className={cx(styles.wrapper, className)}>
<div className={styles.icon}> <div className={styles.icon}>
<Icon size="xl" name={getIconFromSeverity(severity) as IconName} /> <Icon size="xl" name={getIconFromSeverity(severity) as IconName} />
</div> </div>
<div className={styles.title}>{title}</div> <div className={styles.title}>{title}</div>
<div className={styles.body}>{children}</div> <div className={styles.body}>{children}</div>
<span className={styles.trace}>{showTraceId && `Trace ID: ${traceId}`}</span> <span className={styles.trace}>{showTraceId && `Trace ID: ${traceId}`}</span>
<div className={styles.close}>
<IconButton aria-label="Close alert" name="times" onClick={onRemove} size="lg" type="button" />
</div>
{timestamp && <span className={styles.timestamp}>{formatDistanceToNow(timestamp, { addSuffix: true })}</span>} {timestamp && <span className={styles.timestamp}>{formatDistanceToNow(timestamp, { addSuffix: true })}</span>}
</div> </div>
); );
@ -78,6 +75,7 @@ const getStyles = (theme: GrafanaTheme2, severity: AlertVariant) => {
alignSelf: 'center', alignSelf: 'center',
fontWeight: theme.typography.fontWeightMedium, fontWeight: theme.typography.fontWeightMedium,
color: theme.colors.text.primary, color: theme.colors.text.primary,
paddingTop: theme.spacing(1),
}), }),
body: css({ body: css({
gridArea: 'body', gridArea: 'body',
@ -96,13 +94,6 @@ const getStyles = (theme: GrafanaTheme2, severity: AlertVariant) => {
fontSize: theme.typography.pxToRem(10), fontSize: theme.typography.pxToRem(10),
color: theme.colors.text.secondary, color: theme.colors.text.secondary,
}), }),
close: css({
gridArea: 'close',
display: 'flex',
justifySelf: 'end',
padding: theme.spacing(1, 0.5),
background: 'none',
}),
timestamp: css({ timestamp: css({
gridArea: 'timestamp', gridArea: 'timestamp',
alignSelf: 'end', alignSelf: 'end',

View File

@ -1,9 +1,9 @@
import { css, cx } from '@emotion/css'; import { css, cx } from '@emotion/css';
import React, { useRef } from 'react'; import React, { useRef, useState } from 'react';
import { useEffectOnce } from 'react-use'; import { useEffectOnce } from 'react-use';
import { GrafanaTheme2 } from '@grafana/data'; import { GrafanaTheme2 } from '@grafana/data';
import { Button, Icon, useStyles2 } from '@grafana/ui'; import { Button, Checkbox, Icon, useStyles2 } from '@grafana/ui';
import { StoredNotificationItem } from 'app/core/components/AppNotifications/StoredNotificationItem'; import { StoredNotificationItem } from 'app/core/components/AppNotifications/StoredNotificationItem';
import { import {
clearAllNotifications, clearAllNotifications,
@ -17,6 +17,7 @@ import { useDispatch, useSelector } from 'app/types';
export function StoredNotifications() { export function StoredNotifications() {
const dispatch = useDispatch(); const dispatch = useDispatch();
const notifications = useSelector((state) => selectWarningsAndErrors(state.appNotifications)); const notifications = useSelector((state) => selectWarningsAndErrors(state.appNotifications));
const [selectedNotificationIds, setSelectedNotificationIds] = useState<string[]>([]);
const lastReadTimestamp = useRef(useSelector((state) => selectLastReadTimestamp(state.appNotifications))); const lastReadTimestamp = useRef(useSelector((state) => selectLastReadTimestamp(state.appNotifications)));
const styles = useStyles2(getStyles); const styles = useStyles2(getStyles);
@ -24,14 +25,27 @@ export function StoredNotifications() {
dispatch(readAllNotifications(Date.now())); dispatch(readAllNotifications(Date.now()));
}); });
const onClearNotification = (id: string) => { const clearSelectedNotifications = () => {
dispatch(clearNotification(id)); selectedNotificationIds.forEach((id) => {
dispatch(clearNotification(id));
});
setSelectedNotificationIds([]);
}; };
const clearAllNotifs = () => { const clearAllNotifs = () => {
dispatch(clearAllNotifications()); dispatch(clearAllNotifications());
}; };
const handleCheckboxToggle = (id: string, isChecked: boolean) => {
setSelectedNotificationIds((prevState) => {
if (isChecked && !prevState.includes(id)) {
return [...prevState, id];
} else {
return prevState.filter((notificationId) => notificationId !== id);
}
});
};
if (notifications.length === 0) { if (notifications.length === 0) {
return ( return (
<div className={styles.noNotifsWrapper}> <div className={styles.noNotifsWrapper}>
@ -44,19 +58,28 @@ export function StoredNotifications() {
return ( return (
<div className={styles.wrapper}> <div className={styles.wrapper}>
This page displays all past errors and warnings. Once dismissed, they cannot be retrieved. This page displays all past errors and warnings. Once dismissed, they cannot be retrieved.
<Button variant="destructive" onClick={clearAllNotifs} className={styles.clearAll}> <div className={styles.topRow}>
Clear all notifications <Button
</Button> variant="destructive"
onClick={selectedNotificationIds.length === 0 ? clearAllNotifs : clearSelectedNotifications}
className={styles.clearAll}
>
{selectedNotificationIds.length === 0 ? 'Clear all notifications' : 'Clear selected notifications'}
</Button>
</div>
<ul className={styles.list}> <ul className={styles.list}>
{notifications.map((notif) => ( {notifications.map((notif) => (
<li <li key={notif.id} className={styles.listItem}>
key={notif.id} <Checkbox
className={cx(styles.listItem, { [styles.newItem]: notif.timestamp > lastReadTimestamp.current })} value={selectedNotificationIds.includes(notif.id)}
> onChange={(event: React.ChangeEvent<HTMLInputElement>) =>
handleCheckboxToggle(notif.id, event.target.checked)
}
/>
<StoredNotificationItem <StoredNotificationItem
className={cx(styles.notification, { [styles.newItem]: notif.timestamp > lastReadTimestamp.current })}
severity={notif.severity} severity={notif.severity}
title={notif.title} title={notif.title}
onRemove={() => onClearNotification(notif.id)}
timestamp={notif.timestamp} timestamp={notif.timestamp}
traceId={notif.traceId} traceId={notif.traceId}
> >
@ -71,6 +94,11 @@ export function StoredNotifications() {
function getStyles(theme: GrafanaTheme2) { function getStyles(theme: GrafanaTheme2) {
return { return {
topRow: css({
alignItems: 'center',
display: 'flex',
justifyContent: 'flex-end',
}),
smallText: css({ smallText: css({
fontSize: theme.typography.pxToRem(10), fontSize: theme.typography.pxToRem(10),
color: theme.colors.text.secondary, color: theme.colors.text.secondary,
@ -90,9 +118,10 @@ function getStyles(theme: GrafanaTheme2) {
gap: theme.spacing(1), gap: theme.spacing(1),
}), }),
listItem: css({ listItem: css({
listStyle: 'none',
gap: theme.spacing(1),
alignItems: 'center', alignItems: 'center',
display: 'flex',
gap: theme.spacing(2),
listStyle: 'none',
position: 'relative', position: 'relative',
}), }),
newItem: css({ newItem: css({
@ -113,6 +142,10 @@ function getStyles(theme: GrafanaTheme2) {
alignItems: 'center', alignItems: 'center',
gap: theme.spacing(1), gap: theme.spacing(1),
}), }),
notification: css({
flex: 1,
position: 'relative',
}),
wrapper: css({ wrapper: css({
display: 'flex', display: 'flex',
flexDirection: 'column', flexDirection: 'column',