Skip to content

Commit 04852be

Browse files
feat(Notifications): add the max amount of notifications to render at… (#213)
Co-authored-by: Sergey Garin <sergey.garin@cube.dev>
1 parent 327cb2c commit 04852be

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

.changeset/rare-waves-approve.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cube-dev/ui-kit": patch
3+
---
4+
5+
Fix that allows notifications to be dismissed correctly when they are off the display limit.

src/components/overlays/NewNotifications/Bar/NotificationsBar.tsx

+32-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { Key, useLayoutEffect, useRef, useState } from 'react';
1+
import { Key, useEffect, useLayoutEffect, useRef, useState } from 'react';
22
import { TransitionGroup } from 'react-transition-group';
33
import { Item } from '@react-stately/collections';
44
import { useHover } from '@react-aria/interactions';
5-
import { useFocusRing, focusSafely } from '@react-aria/focus';
5+
import { focusSafely, useFocusRing } from '@react-aria/focus';
66

77
import { tasty } from '../../../../tasty';
88
import { CubeNotifyApiPropsWithID } from '../types';
9-
import { useNotificationsList, CollectionChildren } from '../hooks';
9+
import { CollectionChildren, useNotificationsList } from '../hooks';
1010
import { mergeProps } from '../../../../utils/react';
1111
import { useChainedCallback, useEvent } from '../../../../_internal';
1212

@@ -59,6 +59,7 @@ export function NotificationsBar(props: NotificationsBarProps): JSX.Element {
5959

6060
const ref = useRef<HTMLElement | null>(null);
6161
const [realLimit, setRealLimit] = useState(limit + 1);
62+
const lastShownIdRef = useRef<Key>();
6263

6364
const { listProps, state } = useNotificationsList({ items, children, ref });
6465
const { hoverProps, isHovered } = useHover({});
@@ -84,9 +85,17 @@ export function NotificationsBar(props: NotificationsBarProps): JSX.Element {
8485
moveFocus,
8586
);
8687

87-
const collection = [...state.collection].reverse();
88+
let collection = [...state.collection].reverse();
8889
const collectionLength = collection.length;
8990

91+
const lastShownNotificationIndex = collection.findIndex(
92+
(notification) => notification.props.id === lastShownIdRef.current,
93+
);
94+
95+
if (lastShownNotificationIndex !== -1) {
96+
collection = collection.slice(0, lastShownNotificationIndex);
97+
}
98+
9099
/**
91100
* Handy hack to improve animations if the limit is reached.
92101
*/
@@ -100,12 +109,25 @@ export function NotificationsBar(props: NotificationsBarProps): JSX.Element {
100109
}
101110
}, [realLimit]);
102111

103-
// Auto-dismiss all notifications that are off the limit.
104-
collection
105-
.slice(realLimit)
106-
.forEach((notification) =>
107-
chainedOnRemoveNotification(notification.props.id),
108-
);
112+
// Set the last notification that was gone off the limit
113+
collection.slice(realLimit).forEach((notification) => {
114+
// It's safe 'cause there is always only a single notification above the limit
115+
lastShownIdRef.current = notification.props.id;
116+
});
117+
118+
useEffect(() => {
119+
// Auto-dismiss the last shown notification that was off the limit
120+
if (lastShownIdRef.current) {
121+
onDismissNotification(lastShownIdRef.current);
122+
onRemoveNotification(lastShownIdRef.current);
123+
124+
const lastNotification = collection.find(
125+
(notification) => notification.props.id === lastShownIdRef.current,
126+
);
127+
128+
lastNotification?.props.onDismiss?.();
129+
}
130+
}, [lastShownIdRef.current]);
109131

110132
return (
111133
<NotificationsContainer

src/components/overlays/NewNotifications/Notifications.stories.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,6 @@ NotificationsQueue.play = async ({ canvasElement }) => {
462462

463463
const button = getByTestId('ClickMeButton');
464464
await userEvent.click(button);
465-
await wait(random(500, 1000));
465+
await wait(random(400, 800));
466466
}
467467
};

0 commit comments

Comments
 (0)