import 'dart:js_interop'; import 'dart:js_interop_unsafe'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:pipelines_app/features/attention/domain/attention_models.dart'; import 'package:pipelines_app/features/attention/notify/notification_bridge.dart'; import 'package:pipelines_app/features/attention/notify/notification_controller.dart'; import 'package:pipelines_app/features/attention/notify/notification_ledger.dart'; import 'package:pipelines_app/features/attention/notify/notification_models.dart'; import 'package:pipelines_app/features/attention/notify/notification_store.dart'; import 'package:pipelines_app/features/attention/runtime/attention_controller.dart'; import 'package:pipelines_app/features/attention/runtime/attention_store.dart'; import 'package:pipelines_app/features/attention/ui/notification_permission_tile.dart'; /// Browser capture entrypoint for the notification capability. /// /// It drives the real [NotificationController] over the real web bridge, so a /// browser drive can prove the seed is silent, one live transition raises /// exactly one notification with the entry's own words and run+kind identity, a /// dismissed or replayed transition raises none, a second decision gate of one /// run raises a fresh notification, and the Settings tile reads honestly in /// every state on both hosts. Nothing here opens a socket or touches real /// operator data; the store is in memory. void main() { final params = Uri.base.queryParameters; final mode = params['mode'] ?? 'transition'; final cupertino = params['host'] == 'cupertino'; final scale = double.tryParse(params['scale'] ?? '') ?? 1; final brightness = params['theme'] == 'light' ? Brightness.light : Brightness.dark; Widget scaled(BuildContext context, Widget? app) => MediaQuery( data: MediaQuery.of(context).copyWith(textScaler: TextScaler.linear(scale)), child: app!, ); final home = switch (mode) { 'tiles' => const _TileGallery(), 'tile' => _SingleTile(state: _stateOf(params['state'])), _ => _NotificationProbe(mode: mode), }; if (cupertino) { runApp( CupertinoApp( theme: CupertinoThemeData(brightness: brightness), builder: scaled, home: home, ), ); return; } runApp( MaterialApp( theme: ThemeData( platform: TargetPlatform.android, brightness: brightness, ), builder: scaled, home: home, ), ); } /// The report the browser drive reads, as a plain JS string. void _publishReport(String value) => globalContext.setProperty('__probeReport'.toJS, value.toJS); /// The permission state named by a `state` query parameter, defaulting to the /// undecided one. NotificationPermissionState _stateOf(String? name) { for (final state in NotificationPermissionState.values) { if (state.wireName == name) return state; } return NotificationPermissionState.notRequested; } String _at(Duration before) => DateTime.now().toUtc().subtract(before).toIso8601String(); Map _summary( int id, String status, { String name = 'release', String? title, String diagnostic = '', }) => { 'id': id, 'name': name, 'title': title ?? 'Run $id', 'status': status, 'diagnostic': diagnostic, 'created_at': _at(const Duration(hours: 1)), // The server leaves finished_at empty while a run is in flight or waiting. 'finished_at': status == 'waiting' || status == 'running' ? '' : _at(const Duration(minutes: 55)), }; Map _frame( int id, String status, { String name = 'release', String? title, String diagnostic = '', }) => { 'type': 'run_event', 'run_id': id, 'name': name, 'title': title ?? 'Run $id', 'status': status, 'diagnostic': diagnostic, 'finished_at': status == 'waiting' || status == 'running' ? '' : _at(const Duration(minutes: 1)), }; /// Publish exactly as the manager wires it: the accumulated transitions and the /// current qualifying keys from the same attention snapshot. int _publish(NotificationController notify, AttentionController attention) => notify.publishTransitions( attention.transitions, qualifying: notificationQualifyingKeys(attention.rows), dismissed: attention.dismissed, ); /// Runs one notification scenario against the real bridge, then reports it and /// draws the live permission tile underneath. class _NotificationProbe extends StatefulWidget { const _NotificationProbe({required this.mode}); final String mode; @override State<_NotificationProbe> createState() => _NotificationProbeState(); } class _NotificationProbeState extends State<_NotificationProbe> { String _report = 'running'; NotificationPermissionState _permission = NotificationPermissionState.unsupported; bool _asked = false; @override void initState() { super.initState(); WidgetsBinding.instance.addPostFrameCallback((_) => _run()); } Future _run() async { final bridge = createNotificationBridge(); final notify = NotificationController( store: MemoryNotificationStore(), bridge: bridge, ); await notify.load(); final attention = AttentionController(store: MemoryAttentionStore()); await attention.load(); switch (widget.mode) { case 'seed': attention.seedRows([ _summary(1, 'failed', diagnostic: 'Stage 3 stopped'), _summary(2, 'waiting'), ]); _publish(notify, attention); case 'dismissed': attention.seedRows([_summary(1, 'running')]); await attention.dismiss(attentionEntryFor(_summary(1, 'failed'))!); attention.observeFrame(_frame(1, 'failed')); _publish(notify, attention); case 'reconnect': attention.seedRows([_summary(1, 'running')]); attention.observeFrame(_frame(1, 'failed')); _publish(notify, attention); attention.resetForReconnect(); attention.seedRows([_summary(1, 'failed')], reconnect: true); _publish(notify, attention); attention.observeFrame(_frame(1, 'failed')); _publish(notify, attention); case 'secondgate': // A waiting run has no finished_at, so two gates of one run carry the // same timestamps; leaving the state is what makes the second new. attention.seedRows([_summary(7, 'running')]); attention.observeFrame(_frame(7, 'waiting')); _publish(notify, attention); attention.observeFrame(_frame(7, 'running')); _publish(notify, attention); attention.observeFrame(_frame(7, 'waiting')); _publish(notify, attention); case 'denied': case 'unsupported': case 'default': attention.seedRows([_summary(1, 'running')]); attention.observeFrame(_frame(1, 'failed')); _publish(notify, attention); default: attention.seedRows([_summary(1, 'running', name: 'nightly')]); attention.observeFrame( _frame( 1, 'failed', name: 'nightly', title: 'Nightly audit', diagnostic: 'Stage 3 stopped', ), ); _publish(notify, attention); } if (!mounted) return; final report = 'mode=${widget.mode} posted=${notify.postedCount} ' 'state=${notify.permission.wireName} asked=${notify.asked} ' 'notified=${notify.notified.length}'; _publishReport(report); setState(() { _report = report; _permission = notify.permission; _asked = notify.asked; }); notify.dispose(); attention.dispose(); } @override Widget build(BuildContext context) { return Scaffold( body: SafeArea( child: ListView( padding: const EdgeInsets.all(16), children: [ Text(_report), const SizedBox(height: 16), NotificationPermissionTile( state: _permission, asked: _asked, onRequest: () {}, ), ], ), ), ); } } /// One permission state on its own, so each can be captured and read at the /// hardest width and text scale. class _SingleTile extends StatelessWidget { const _SingleTile({required this.state}); final NotificationPermissionState state; @override Widget build(BuildContext context) { return Scaffold( body: SafeArea( child: SingleChildScrollView( padding: const EdgeInsets.all(16), child: NotificationPermissionTile(state: state, onRequest: () {}), ), ), ); } } /// Every permission state at once, so both hosts can be captured and read in /// one page without touching a real permission. class _TileGallery extends StatelessWidget { const _TileGallery(); @override Widget build(BuildContext context) { return Scaffold( body: SafeArea( child: SingleChildScrollView( padding: const EdgeInsets.all(16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ for (final state in NotificationPermissionState.values) ...[ Text(state.wireName), NotificationPermissionTile(state: state, onRequest: () {}), ], ], ), ), ), ); } }