Files
pipelines/app/lib/features/attention/notify/notification_bridge_web.dart
T
alan 767643d991 Open the run's artifact from notifications
Phone and browser notifications that carry an artifact open it full
screen in the artifact viewer, with an Open run action and the preview as
expanded text; pipelines://artifact deep links route to the viewer.
2026-11-12 18:56:26 +00:00

98 lines
3.2 KiB
Dart

import 'dart:js_interop';
import 'dart:js_interop_unsafe';
import 'notification_bridge.dart';
import 'notification_models.dart';
/// The browser's `Notification` constructor.
///
/// Declared with `dart:js_interop` rather than `dart:html`, so the same source
/// compiles for the JavaScript and WebAssembly web targets. Every member is
/// reached only after [_supported] has confirmed the global exists, so an
/// unsupported browser never touches it.
@JS('Notification')
extension type _Notification._(JSObject _) implements JSObject {
external factory _Notification(JSString title, _NotificationOptions options);
external static JSString get permission;
external static JSPromise<JSString> requestPermission();
external set onclick(JSFunction? handler);
}
/// The `{ body, tag }` options object one notification is created with.
extension type _NotificationOptions._(JSObject _) implements JSObject {
external factory _NotificationOptions({JSString body, JSString tag});
}
/// Whether `globalThis.Notification` is really a constructor right now.
bool _supported() =>
globalContext.getProperty<JSAny?>('Notification'.toJS).isA<JSFunction>();
/// The browser's own secure-context flag; anything but a real boolean is false.
bool _secureContext() {
final value = globalContext.getProperty<JSAny?>('isSecureContext'.toJS);
return value.isA<JSBoolean>() && (value as JSBoolean).toDart;
}
/// The browser bridge: the real `Notification` API, reached through
/// `dart:js_interop`.
///
/// Nothing is fabricated. [environment] reports the constructor's own
/// `permission` word and the page's own `isSecureContext`; an unsupported
/// browser is reported as unsupported rather than as a default grant.
class WebNotificationBridge implements NotificationBridge {
const WebNotificationBridge();
@override
NotificationEnvironment environment() {
if (!_supported()) {
return const NotificationEnvironment(
supported: false,
secureContext: false,
permission: 'default',
);
}
final raw = _Notification.permission.toDart;
return NotificationEnvironment(
supported: true,
secureContext: _secureContext(),
permission: raw == 'granted' || raw == 'denied' ? raw : 'default',
);
}
@override
Future<String> requestPermission() async {
if (!_supported()) return 'default';
try {
return (await _Notification.requestPermission().toDart).toDart;
} catch (_) {
// A browser that refuses the request keeps the honest fallback wording.
return 'default';
}
}
@override
bool show(NotificationMessage message, {void Function()? onTap}) {
if (!_supported()) return false;
try {
final notification = _Notification(
message.title.toJS,
_NotificationOptions(body: message.body.toJS, tag: message.tag.toJS),
);
final open = onTap;
if (open != null) {
notification.onclick = (() => open()).toJS;
}
return true;
} catch (_) {
// Some browsers can throw even with permission granted; the in-app Needs
// you surface remains the only honest channel and nothing is faked.
return false;
}
}
}
NotificationBridge createNotificationBridge() => const WebNotificationBridge();