Skip to content

Commit 73ee30f

Browse files
committed
fix(hub): say "with warnings" when a badge has no errors
Unseen errors and warnings are counted apart. The dock title says "with errors" when an error is unseen, else "with warnings"; the badge stays red for both. Refs #340
1 parent 90a9f11 commit 73ee30f

5 files changed

Lines changed: 66 additions & 20 deletions

File tree

‎apps/docs/src/content/getting-started/popup-and-hub.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ The hub keeps a list of messages. Pangular Inspector posts to it when something
157157

158158
Each message names the pages it came from (also as `page:<pageId>` labels) and has an **Open in Pangular** action that opens the dock that shows it. An error opens a toast the first time it shows up. When the same item comes back, from the same page after a reload or from another tab, the first message counts it (`(3 times)`) instead of posting it again. A call that only reads, like `change-detection` without `record`, posts nothing. Text in messages is masked like the rest of the panel. An agent message shows the first line of the tool's answer and the `page` argument, not the other arguments.
159159

160-
A dock shows how many items it has that you have not seen yet: NgRx writes on **NgRx**, Analog writes on **Analog**, and everything else on **Angular**. The **Pangular** group shows the total. The badge is red while one of them is an error or a warning. Opening a dock clears its count, and opening **All tools** clears every count. The count is also part of the dock name that screen readers and the tooltip use, for example `Angular (2 new, with errors)`.
160+
A dock shows how many items it has that you have not seen yet: NgRx writes on **NgRx**, Analog writes on **Analog**, and everything else on **Angular**. The **Pangular** group shows the total. The badge is red while one of them is an error or a warning. Opening a dock clears its count, and opening **All tools** clears every count. The count is also part of the dock name that screen readers and the tooltip use, for example `Angular (2 new, with errors)`. When the unseen items hold warnings, such as an NG05xx hydration message, but no error, the name ends in `with warnings` instead.
161161

162162
Messages follow the config. An inspector that is off posts nothing (forms errors need `inspectors.forms`, failed navigations `inspectors.router`, hydration messages `inspectors.http`), and an agent tool turned off with `agent.tools`, `agent.readOnly` or `actions` posts nothing either. See [Configuration](./configuration.md).
163163

‎packages/devtools/src/__tests__/hub-docks.test.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ describe('hub docks', () => {
8181
it('keeps the explaining title when a badge is set, and drops only the badge when cleared', () => {
8282
const hub = fakeHub();
8383
const docks = registerHubDocks(hub, 'pangular', undefined, { capacitor: false });
84-
docks?.setBadge('pangular:capacitor', { text: '2', variant: 'danger' });
84+
docks?.setBadge('pangular:capacitor', { text: '2', variant: 'danger', severity: 'error' });
8585
expect(hub.docks.views.get('pangular:capacitor')).toMatchObject({
8686
title: 'Capacitor: no app connected (2 new, with errors)',
8787
badge: '2',
@@ -106,8 +106,10 @@ describe('hub docks', () => {
106106
const docks = registerHubDocks(hub, 'pangular');
107107
docks?.setBadge('pangular:ngrx', { text: '3', variant: 'info' });
108108
expect(hub.docks.views.get('pangular:ngrx')?.title).toBe('NgRx (3 new)');
109-
docks?.setBadge('pangular:group', { text: '99+', variant: 'danger' });
109+
docks?.setBadge('pangular:group', { text: '99+', variant: 'danger', severity: 'error' });
110110
expect(hub.docks.views.get('pangular:group')?.title).toBe('Pangular (99+ new, with errors)');
111+
docks?.setBadge('pangular:group', { text: '1', variant: 'danger', severity: 'warning' });
112+
expect(hub.docks.views.get('pangular:group')?.title).toBe('Pangular (1 new, with warnings)');
111113
docks?.setBadge('pangular:ngrx', undefined);
112114
expect(hub.docks.views.get('pangular:ngrx')?.title).toBe('NgRx');
113115
});

‎packages/devtools/src/__tests__/hub-messages.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ interface Entry {
2222
type: string;
2323
badge?: string;
2424
badgeVariant?: string;
25+
title?: string;
2526
groupId?: string;
2627
}
2728

@@ -254,6 +255,25 @@ describe('hub dock badges', () => {
254255
expect(hub.docks.views.get('pangular:group')?.badgeVariant).toBe('danger');
255256
});
256257

258+
it('says "with warnings" for a dock with only warnings, and "with errors" once an error comes', () => {
259+
const { feed, hub } = setup();
260+
const view = (id: string) => hub.docks.views.get(id);
261+
feed.hydration('p1', stats({ warnings: ['NG0505: no hydration info'] }));
262+
expect(view('pangular:angular')).toMatchObject({
263+
badge: '1',
264+
badgeVariant: 'danger',
265+
title: 'Angular (1 new, with warnings)',
266+
});
267+
expect(view('pangular:group')?.title).toBe('Pangular (1 new, with warnings)');
268+
feed.hydration('p1', stats({ mismatches: [{ component: 'app-a' }] }));
269+
expect(view('pangular:angular')).toMatchObject({
270+
badge: '2',
271+
badgeVariant: 'danger',
272+
title: 'Angular (2 new, with errors)',
273+
});
274+
expect(view('pangular:group')?.title).toBe('Pangular (2 new, with errors)');
275+
});
276+
257277
it('clears a dock when the panel opens it, and every dock for All tools', async () => {
258278
const { feed, badge, hub } = setup();
259279
await run(feed, 'pangular:dispatch-ngrx-action').handler({});

‎packages/devtools/src/hub-docks.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ export const PANGULAR_GROUP_ID = hubDockId('group');
5858
export interface HubDockBadge {
5959
text: string;
6060
variant: DevframeDockBadgeVariant;
61+
/** The worst unseen item, which the title names: `with errors` or `with warnings`. */
62+
severity?: 'error' | 'warning';
6163
}
6264

6365
/** The views whose dock says when the tool has nothing to show yet. */
@@ -109,10 +111,12 @@ function withStatus(
109111
return { ...entry, title: UNAVAILABLE_TITLES[view] };
110112
}
111113

114+
const SEVERITY_NOTES = { error: ', with errors', warning: ', with warnings' } as const;
115+
112116
/** The dock title with its badge, as `Angular (2 new, with errors)`. */
113117
export function badgeTitle(title: string, badge: HubDockBadge): string {
114-
const errors = badge.variant === 'danger' ? ', with errors' : '';
115-
return `${title} (${badge.text} new${errors})`;
118+
const note = badge.severity ? SEVERITY_NOTES[badge.severity] : '';
119+
return `${title} (${badge.text} new${note})`;
116120
}
117121

118122
export function registerHubDocks(
@@ -207,7 +211,13 @@ export function registerHubDocks(
207211
setBadge: (entryId, badge) => {
208212
if (!plain.has(entryId)) return;
209213
const current = badges.get(entryId);
210-
if (current?.text === badge?.text && current?.variant === badge?.variant) return;
214+
if (
215+
current?.text === badge?.text &&
216+
current?.variant === badge?.variant &&
217+
current?.severity === badge?.severity
218+
) {
219+
return;
220+
}
211221
if (badge) badges.set(entryId, badge);
212222
else badges.delete(entryId);
213223
ctx.docks.update(render(entryId));

‎packages/devtools/src/hub-messages.ts‎

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ export interface HubFeed {
5858
seen: (dockId: unknown) => void;
5959
}
6060

61+
/** A dock's unseen items, with how many of them are errors and warnings. */
62+
interface Unseen {
63+
count: number;
64+
errors: number;
65+
warnings: number;
66+
}
67+
6168
interface Posted {
6269
id: string;
6370
count: number;
@@ -125,7 +132,7 @@ export function createHubFeed(
125132
const messages = messagesOf(ctx);
126133
const posted = new Map<string, Posted>();
127134
const pages = new Map<string, { keys: Set<string>; at: number }>();
128-
const unseen = new Map<string, { count: number; errors: number }>();
135+
const unseen = new Map<string, Unseen>();
129136
let docks: HubDocks | undefined;
130137

131138
messages?.events?.on('messages:removed', (id) => {
@@ -152,30 +159,37 @@ export function createHubFeed(
152159
return true;
153160
};
154161

155-
const badgeOf = (count: number, errors: number): HubDockBadge | undefined =>
156-
count > 0
157-
? { text: count > 99 ? '99+' : String(count), variant: errors > 0 ? 'danger' : 'info' }
158-
: undefined;
162+
/** Red for any unseen error or warning; the title says which of the two. */
163+
const badgeOf = ({ count, errors, warnings }: Unseen): HubDockBadge | undefined => {
164+
if (count <= 0) return undefined;
165+
const severity = errors > 0 ? 'error' : warnings > 0 ? 'warning' : undefined;
166+
return {
167+
text: count > 99 ? '99+' : String(count),
168+
variant: severity ? 'danger' : 'info',
169+
...(severity ? { severity } : {}),
170+
};
171+
};
159172

160173
const renderBadges = () => {
161174
if (!docks) return;
162-
let count = 0;
163-
let errors = 0;
175+
const total: Unseen = { count: 0, errors: 0, warnings: 0 };
164176
for (const id of docks.ids) {
165-
const item = unseen.get(id);
166-
count += item?.count ?? 0;
167-
errors += item?.errors ?? 0;
168-
docks.setBadge(id, badgeOf(item?.count ?? 0, item?.errors ?? 0));
177+
const item = unseen.get(id) ?? { count: 0, errors: 0, warnings: 0 };
178+
total.count += item.count;
179+
total.errors += item.errors;
180+
total.warnings += item.warnings;
181+
docks.setBadge(id, badgeOf(item));
169182
}
170-
docks.setBadge(docks.groupId, badgeOf(count, errors));
183+
docks.setBadge(docks.groupId, badgeOf(total));
171184
};
172185

173186
const bump = (dock: string, level: DevframeMessageLevel) => {
174187
if (!docks) return;
175188
const target = docks.ids.includes(dock) ? dock : dockId('angular');
176-
const item = unseen.get(target) ?? { count: 0, errors: 0 };
189+
const item = unseen.get(target) ?? { count: 0, errors: 0, warnings: 0 };
177190
item.count++;
178-
if (level === 'error' || level === 'warn') item.errors++;
191+
if (level === 'error') item.errors++;
192+
else if (level === 'warn') item.warnings++;
179193
unseen.set(target, item);
180194
renderBadges();
181195
};

0 commit comments

Comments
 (0)