summaryrefslogtreecommitdiff
path: root/source/features/small-user-avatars.tsx
blob: dfaa45c415eb6a6ed7ad96566ee319d93038e5e1 (plain) (blame)
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
import React from 'dom-chef';

import onetime from 'onetime';

import features from '../feature-manager.js';
import observe from '../helpers/selector-observer.js';
import getUserAvatarURL from '../github-helpers/get-user-avatar.js';

function addAvatar(link: HTMLElement): void {
	const username = link.textContent;
	const size = 14;

	link.prepend(
		<img
			className="avatar avatar-user v-align-text-bottom mr-1 rgh-small-user-avatars"
			src={getUserAvatarURL(username, size)!}
			width={size}
			height={size}
			loading="lazy"
		/>,
	);
}

function addMentionAvatar(link: HTMLElement): void {
	const username = link.textContent.slice(1);
	const size = 16;

	link.prepend(
		<img
			className="avatar avatar-user mb-1 mr-1 rgh-small-user-avatars"
			style={{marginLeft: 1}}
			src={getUserAvatarURL(username, size)!}
			width={size}
			height={size}
			loading="lazy"
		/>,
	);
}

function init(): void {
	// Excludes bots
	observe([
		'.js-issue-row [data-hovercard-type="user"]',
		'.notification-thread-subscription [data-hovercard-type="user"]',
	], addAvatar);
	observe('.user-mention[data-hovercard-type="user"]', addMentionAvatar);
}

void features.add(import.meta.url, {
	init: onetime(init),
});

/*

Test URLs:

https://github.com/notifications/subscriptions
https://github.com/refined-github/refined-github/issues
https://github.com/refined-github/refined-github/pull/7004
https://github.com/refined-github/refined-github/releases
https://github.com/refined-github/refined-github/releases/tag/23.9.21
https://github.com/orgs/community/discussions/5841#discussioncomment-1450320

*/