Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix(ui): support persistent notification actions (#7734)

wpxp123456d31d624165d0
diff --git a/packages/ui/src/services/notification/__tests__/desktop-notification.service.spec.ts b/packages/ui/src/services/notification/__tests__/desktop-notification.service.spec.ts
index 874b244abe..4a8d5a77f4 100644
--- a/packages/ui/src/services/notification/__tests__/desktop-notification.service.spec.ts
+++ b/packages/ui/src/services/notification/__tests__/desktop-notification.service.spec.ts
@@ -15,7 +15,8 @@
*/
import { Injector } from '@univerjs/core';
-import { describe, expect, it } from 'vitest';
+import { toast } from '@univerjs/design';
+import { afterEach, describe, expect, it, vi } from 'vitest';
import { BuiltInUIPart, IUIPartsService, UIPartsService } from '../../parts/parts.service';
import { DesktopNotificationService } from '../desktop-notification.service';
import { INotificationService } from '../notification.service';
@@ -28,12 +29,42 @@ function createService(): INotificationService {
}
describe('DesktopNotificationService', () => {
+ afterEach(() => {
+ vi.restoreAllMocks();
+ });
+
it('shows a notification and returns a disposable close handle', () => {
const service = createService();
+ const onClick = vi.fn();
+ const showToast = vi.spyOn(toast, 'error').mockReturnValue('conflict-toast');
+ const dismissToast = vi.spyOn(toast, 'dismiss');
- const disposable = service.show({ title: 'Saved', content: 'Workbook saved', type: 'success' });
+ const disposable = service.show({
+ title: 'Conflict',
+ content: 'Reload the page',
+ type: 'error',
+ duration: Infinity,
+ closable: false,
+ dismissible: false,
+ action: {
+ label: 'Reload page',
+ onClick,
+ },
+ });
- expect(() => disposable.dispose()).not.toThrow();
+ expect(showToast).toHaveBeenCalledWith('Conflict', {
+ position: 'top-right',
+ description: 'Reload the page',
+ duration: Infinity,
+ closeButton: false,
+ dismissible: false,
+ action: {
+ label: 'Reload page',
+ onClick,
+ },
+ });
+ disposable.dispose();
+ expect(dismissToast).toHaveBeenCalledWith('conflict-toast');
});
it('registers the notification UI part when constructed', () => {
diff --git a/packages/ui/src/services/notification/desktop-notification.service.ts b/packages/ui/src/services/notification/desktop-notification.service.ts
index 5ea499a60e..b54aa4d629 100644
--- a/packages/ui/src/services/notification/desktop-notification.service.ts
+++ b/packages/ui/src/services/notification/desktop-notification.service.ts
@@ -34,9 +34,9 @@ export class DesktopNotificationService extends Disposable implements INotificat
}
show(params: INotificationOptions): IDisposable {
- notification.show(params);
+ const notificationID = notification.show(params);
- return toDisposable(() => { /* empty */ });
+ return toDisposable(() => notification.dismiss(notificationID));
}
protected _initUIPart(): void {
diff --git a/packages/ui/src/views/notification/Notification.tsx b/packages/ui/src/views/notification/Notification.tsx
index 044a68106c..c9f0b12a8a 100644
--- a/packages/ui/src/views/notification/Notification.tsx
+++ b/packages/ui/src/views/notification/Notification.tsx
@@ -51,6 +51,12 @@ export interface INotificationOptions {
icon?: React.ReactNode;
closable?: boolean;
+
+ /** Whether the notification can be dismissed by user interaction */
+ dismissible?: boolean;
+
+ /** An action rendered inside the notification */
+ action?: NonNullable<Parameters<typeof toast>[1]>['action'];
}
export const notification = {
@@ -61,14 +67,19 @@ export const notification = {
content,
duration,
closable = true,
+ dismissible = true,
position = 'top-right',
+ action,
} = options;
- toast[type](title, {
+ return toast[type](title, {
position,
description: content,
duration,
closeButton: closable,
+ dismissible,
+ action,
});
},
+ dismiss: (id: string | number) => toast.dismiss(id),
};