Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(manager-react-components): useNotifications prevent early clears #14234

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { vitest } from 'vitest';
import { vi, vitest } from 'vitest';
import React, { useEffect } from 'react';
import { render } from '@testing-library/react';
import { useNotifications, NotificationType } from './useNotifications';
Expand Down Expand Up @@ -29,17 +29,25 @@ function ClearNotifications() {
}

describe('notifications component', () => {
it('should list notifications', async () => {
it('should render and clear notifications only after 1000ms', async () => {
vi.useFakeTimers();
let { container } = render(<Notifications />);
expect(container.children.length).toBe(0);
render(<AddNotification />);
container = await render(<Notifications />).container;
expect(container.children.length).toBe(2);
});
it('should clear notifications', async () => {
let { container } = render(<Notifications />);
expect(container.children.length).not.toBe(0);

vi.advanceTimersByTime(999);

render(<ClearNotifications />);
container = render(<Notifications />).container;
expect(container.children.length).toBe(2);

vi.advanceTimersByTime(1);

render(<ClearNotifications />);
container = render(<Notifications />).container;
expect(container.children.length).toBe(0);

vi.restoreAllMocks();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface Notification {
content: ReactNode;
type: NotificationType;
dismissable?: boolean;
creationTimestamp?: number;
}

export interface NotificationState {
Expand Down Expand Up @@ -44,7 +45,13 @@ export const useNotifications = create<NotificationState>((set, get) => ({
uid: state.uid + 1,
notifications: [
...state.notifications,
{ uid: state.uid, content, type, dismissable },
{
uid: state.uid,
content,
type,
dismissable,
creationTimestamp: Date.now(),
},
],
})),
addSuccess: (content: ReactNode, dismissable = false) =>
Expand All @@ -61,7 +68,12 @@ export const useNotifications = create<NotificationState>((set, get) => ({
({ uid }) => uid !== toRemoveUid,
),
})),
clearNotifications: () => set(() => ({ notifications: [] })),
clearNotifications: () =>
set((state) => ({
notifications: state.notifications.filter(
(notification) => Date.now() - notification.creationTimestamp < 1000,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(notification) => Date.now() - notification.creationTimestamp < 1000,
(notification) => Date.now() - notification.creationTimestamp < DEFAULT_MINIMUN_LIFETIME,

Optionnal suggestion : Possible to add constant for default minimum lifetime and maybe add parameters if we want more or less on the future ?

Copy link
Contributor Author

@tristanwagner tristanwagner Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me the whole api should be reworked, for example we should be able to set "clearAfterRead" per notification instead of for all of them, but I think it should be another subject, right now I'm just trying to be up to date with MRC v1 on this issue

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI we opened this PR to sync the changes we introduced in MRC 1.x : #14170

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ok wasn't aware of that I'll just close this one then

),
})),
}));

export default useNotifications;
Loading