|
| 1 | +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:js_interop'; |
| 6 | + |
| 7 | +import 'package:collection/collection.dart'; |
| 8 | +import 'package:web/web.dart'; |
| 9 | + |
| 10 | +import '../../web_util.dart'; |
| 11 | + |
| 12 | +/// Forget dismissed messages that are more than 2 years old |
| 13 | +late final _deadline = DateTime.now().subtract(Duration(days: 365 * 2)); |
| 14 | + |
| 15 | +/// Don't save more than 50 entries |
| 16 | +const _maxMissedMessages = 50; |
| 17 | + |
| 18 | +/// Create a dismissible widget on [element]. |
| 19 | +/// |
| 20 | +/// A `data-dismissible-by` is required, this must be a CSS selected for a |
| 21 | +/// child element that when clicked will dismiss the message by removing |
| 22 | +/// [element]. |
| 23 | +/// |
| 24 | +/// A hash is computed from `innerText` of [element], once dismissed this hash |
| 25 | +/// will be stored in `localStorage`. And next time this widget is instantiated |
| 26 | +/// with the same `innerText` it'll be removed immediately. |
| 27 | +/// |
| 28 | +/// Hashes of dismissed messages will be stored for up to 2 years. |
| 29 | +/// No more than 50 dismissed messages are retained in `localStorage`. |
| 30 | +void create(HTMLElement element, Map<String, String> options) { |
| 31 | + final by = options['by']; |
| 32 | + if (by == null) throw UnsupportedError('data-dismissible-by required'); |
| 33 | + |
| 34 | + late final hash = _cheapNaiveHash(element.innerText); |
| 35 | + if (_dismissed.any((e) => e.hash == hash)) { |
| 36 | + element.remove(); |
| 37 | + return; |
| 38 | + } else { |
| 39 | + element.style.display = 'revert'; |
| 40 | + } |
| 41 | + |
| 42 | + final dismiss = (Event e) { |
| 43 | + e.preventDefault(); |
| 44 | + |
| 45 | + element.remove(); |
| 46 | + _dismissed.add(( |
| 47 | + hash: hash, |
| 48 | + date: DateTime.now(), |
| 49 | + )); |
| 50 | + _saveDismissed(); |
| 51 | + }; |
| 52 | + |
| 53 | + var dismissible = false; |
| 54 | + for (final dismisser in element.querySelectorAll(by).toList()) { |
| 55 | + if (!dismisser.isA<HTMLElement>()) continue; |
| 56 | + dismisser.addEventListener('click', dismiss.toJS); |
| 57 | + dismissible = true; |
| 58 | + } |
| 59 | + if (!dismissible) { |
| 60 | + throw UnsupportedError( |
| 61 | + 'data-dismissible-by must point to a child element', |
| 62 | + ); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +/// Create a cheap naive hash from [text]. |
| 67 | +String _cheapNaiveHash(String text) { |
| 68 | + final half = (text.length / 2).floor(); |
| 69 | + return text.length.toRadixString(16).padLeft(2, '0') + |
| 70 | + text.hashCode.toRadixString(16).padLeft(2, '0') + |
| 71 | + text.substring(0, half).hashCode.toRadixString(16).padLeft(2, '0') + |
| 72 | + text.substring(half).hashCode.toRadixString(16).padLeft(2, '0'); |
| 73 | +} |
| 74 | + |
| 75 | +/// LocalStorage key where we store the hashes of messages that have been |
| 76 | +/// dismissed. |
| 77 | +/// |
| 78 | +/// Data is stored on the format: `<hash>@<date>;<hash>@<date>;...` |
| 79 | +const _dismissedMessageslocalStorageKey = 'dismissed-messages'; |
| 80 | + |
| 81 | +late final _dismissed = [ |
| 82 | + ...?window.localStorage |
| 83 | + .getItem(_dismissedMessageslocalStorageKey) |
| 84 | + ?.split(';') |
| 85 | + .where((e) => e.contains('@')) |
| 86 | + .map((entry) { |
| 87 | + final [hash, date, ...] = entry.split('@'); |
| 88 | + return ( |
| 89 | + hash: hash, |
| 90 | + date: DateTime.tryParse(date) ?? DateTime.fromMicrosecondsSinceEpoch(0), |
| 91 | + ); |
| 92 | + }).where((entry) => entry.date.isAfter(_deadline)), |
| 93 | +]; |
| 94 | + |
| 95 | +void _saveDismissed() { |
| 96 | + window.localStorage.setItem( |
| 97 | + _dismissedMessageslocalStorageKey, |
| 98 | + _dismissed |
| 99 | + .sortedBy((e) => e.date) // Sort by date |
| 100 | + .reversed // Reverse ordering to prefer newest dates |
| 101 | + .take(_maxMissedMessages) // Limit how many entries we save |
| 102 | + .map((e) => e.hash + '@' + e.date.toIso8601String().split('T').first) |
| 103 | + .join(';'), |
| 104 | + ); |
| 105 | +} |
0 commit comments