Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | 125x 131x 131x 131x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 14x 14x 14x 14x 14x 14x 14x 14x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 14x 14x 14x 14x 14x 14x 10x 14x 125x 14x 14x 14x 14x 14x 14x 14x 14x 125x 40x 40x 40x 28x 28x 28x 40x 14x 14x 14x 14x 2x 14x 125x 10x 10x 10x 10x 22x 10x 9x 10x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 124x 125x 125x 125x 125x 145x 125x 125x 125x 125x 125x 125x 125x 125x 128x 126x 126x 126x 126x 126x 126x 126x 127x 125x 125x 125x 125x 125x | import { css, html, PropertyValues, TemplateResult } from 'lit';
import { TembaList } from './TembaList';
import { Options } from '../display/Options';
import { Icon } from '../Icons';
import { CustomEventType, Notification } from '../interfaces';
import {
RealtimeSubscription,
subscribeToNotifications
} from '../live/Realtime';
import { deleteRequest } from '../utils';
export class NotificationList extends TembaList {
reverseRefresh = false;
internalFocusDisabled = true;
// fed by socket publications instead of interval polling
protected pollingEnabled = false;
private realtimeSubscription: RealtimeSubscription = null;
// publications that arrived before the initial fetch completed - a direct
// prepend would be clobbered when the fetch lands, so they wait for it
private pendingPubs: Notification[] = [];
private fetchedOnce = false;
private subscribedOnce = false;
static get styles() {
return css`
:host {
--option-hover-bg: #f9f9f9;
}
.header {
padding: 0.25em 1em;
background: #f9f9f9;
border-top-left-radius: var(--curvature);
border-top-right-radius: var(--curvature);
display: flex;
color: #999;
border-bottom: 1px solid #f3f3f3;
}
.header temba-icon {
margin-right: 0.35em;
}
.footer {
background: #f9f9f9;
}
.title {
font-weight: normal;
}
`;
}
constructor() {
super();
this.valueKey = 'url';
this.renderOption = (notification: Notification): TemplateResult => {
let icon = null;
let body = null;
const color = '#333';
if (notification.type === 'incident:started') {
if (notification.incident.type === 'org:flagged') {
icon = Icon.incidents;
body =
'Your workspace was flagged, please contact support for assistance.';
} else if (notification.incident.type === 'org:suspended') {
icon = Icon.incidents;
body =
'Your workspace was suspended, please contact support for assistance.';
} else if (notification.incident.type === 'channel:disconnected') {
icon = Icon.channel;
body = 'Your android channel is not connected';
} else if (notification.incident.type === 'channel:templates_failed') {
icon = Icon.channel;
body = 'Your WhatsApp channel templates failed syncing';
} else if (notification.incident.type === 'webhooks:unhealthy') {
icon = Icon.webhook;
body = 'Your webhook calls are not working properly.';
}
} else if (notification.type === 'import:finished') {
if (notification.import.type === 'contact') {
icon = Icon.contact_import;
body = `Imported ${notification.import.num_records.toLocaleString()} contacts`;
}
} else if (notification.type === 'export:finished') {
if (notification.export.type === 'contact') {
icon = Icon.contact_export;
body = `Exported ${notification.export.num_records.toLocaleString()} contacts`;
} else if (notification.export.type === 'message') {
icon = Icon.message_export;
body = `Exported ${notification.export.num_records.toLocaleString()} messages`;
} else if (notification.export.type === 'results') {
icon = Icon.results_export;
body = 'Exported flow results';
} else if (notification.export.type === 'ticket') {
icon = Icon.tickets_export;
body = `Exported ${notification.export.num_records.toLocaleString()} tickets`;
} else if (notification.export.type === 'definition') {
icon = Icon.definitions_export;
body = 'Exported definitions';
}
} else if (notification.type === 'tickets:activity') {
icon = Icon.tickets;
body = 'New ticket activity';
} else if (notification.type === 'tickets:opened') {
icon = Icon.tickets;
body = 'New unassigned ticket';
}
return html`<div
style="color:${color};display:flex;align-items:flex-start;flex-direction:row;font-weight:${notification.is_seen
? 400
: 500}"
>
${icon
? html`<div style="margin-right:0.6em">
<temba-icon name="${icon}"></temba-icon>
</div>`
: null}
<div style="display:flex;flex-direction:column">
<div style="line-height:1.1em">${body}</div>
<temba-date
style="font-size:80%"
value=${notification.created_on}
display="duration"
></temba-date>
</div>
</div>`;
};
}
public connectedCallback() {
super.connectedCallback();
this.realtimeSubscription = subscribeToNotifications(
(notification) => this.handleNotification(notification),
() => this.handleSubscribed()
);
}
public disconnectedCallback() {
super.disconnectedCallback();
if (this.realtimeSubscription) {
this.realtimeSubscription.unsubscribe();
this.realtimeSubscription = null;
}
}
public willUpdate(changed: PropertyValues): void {
super.willUpdate(changed);
if (changed.has('loading') && !this.loading) {
this.fetchedOnce = true;
const pending = this.pendingPubs;
this.pendingPubs = [];
pending.forEach((notification) => this.prependNotification(notification));
}
}
private handleNotification(notification: Notification) {
// ignore redeliveries of notifications we already have so e.g. the
// menu's unseen badge only lights for genuinely new ones
if (
this.items.some((item) => item.url === notification.url) ||
this.pendingPubs.some((item) => item.url === notification.url)
) {
return;
}
// announce arrival, even mid-fetch - e.g. the menu's unseen badge
this.fireCustomEvent(CustomEventType.NotificationReceived, {
notification
});
if (this.loading || !this.fetchedOnce) {
this.pendingPubs.push(notification);
} else {
this.prependNotification(notification);
}
}
private prependNotification(notification: Notification) {
// dedupe by url (our valueKey) and prepend
this.items = [
notification,
...this.items.filter((item) => item.url !== notification.url)
];
this.mostRecentItem = notification;
}
/**
* Fires on every (re)subscribe including after reconnects. The initial
* endpoint fetch covers the first one regardless of which completes first,
* so only refetch page one on resubscribes, to catch anything missed
* while offline.
*/
private handleSubscribed() {
if (this.subscribedOnce && this.fetchedOnce) {
this.refresh();
}
this.subscribedOnce = true;
}
// urls marked seen on the server but still shown bold for this viewing
private seenUrls = new Set<string>();
/**
* Marks everything currently unseen as seen on the server, without any
* refetching - the socket keeps our items current and seen state is fully
* known here. Items stay bold for the current viewing and unbold on the
* next call (e.g. the next popup open). Resolves false if the server
* didn't record it, in which case nothing advances and the next call
* retries.
*/
public markSeen(): Promise<boolean> {
// unbold whatever was marked seen last time
if (this.seenUrls.size > 0) {
this.items = this.items.map((item) =>
this.seenUrls.has(item.url) ? { ...item, is_seen: true } : item
);
}
const unseen = this.items.filter((item) => !item.is_seen);
if (unseen.length === 0 || !this.endpoint) {
return Promise.resolve(true);
}
// only advance our seen state if the server actually recorded it,
// otherwise we'd unbold items the server still considers unseen
return deleteRequest(this.endpoint)
.then((response) => {
if (!response.ok) {
console.warn(
`failed marking notifications seen (${response.status})`
);
return false;
}
this.seenUrls = new Set(unseen.map((item) => item.url));
return true;
})
.catch((error) => {
console.warn('failed marking notifications seen', error);
return false;
});
}
public renderHeader(): TemplateResult {
return html`<div class="header">
<temba-icon name="notification"></temba-icon>
<div class="title">Notifications</div>
</div>`;
}
public scrollToTop(): void {
// scroll back to the top
window.setTimeout(() => {
const options = this.shadowRoot.querySelector('temba-options') as Options;
options.scrollToTop();
}, 1000);
}
}
|