summaryrefslogtreecommitdiff
path: root/source/features/show-followers-you-know.tsx
blob: 34955e55148a31a8d04e315297d97b58e709f6a4 (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
65
66
import React from 'dom-chef';
import select from 'select-dom';
import * as pageDetect from 'github-url-detection';

import features from '.';
import fetchDom from '../helpers/fetch-dom';
import {getCleanPathname} from '../github-helpers';

const fetchStargazers = async (): Promise<HTMLImageElement[]> => {
	const url = `/${getCleanPathname()}/followers/you_know`;
	const dom = await fetchDom(url);
	return select.all<HTMLImageElement>('.follow-list-item .avatar', dom);
};

const avatarSize = 35;
function renderAvatar(image: HTMLImageElement): HTMLElement {
	const imageUrl = new URL(image.src);
	imageUrl.searchParams.set('s', String(avatarSize * window.devicePixelRatio));
	image.src = String(imageUrl);
	image.width = avatarSize;
	image.height = avatarSize;

	return (
		<a
			href={(image.parentElement as HTMLAnchorElement).href}
			aria-label={image.alt.slice(1)}
			className="tooltipped tooltipped-n avatar-group-item mr-1"
		>
			{image}
		</a>
	);
}

async function init(): Promise<false | void> {
	const container = select('[itemtype="http://schema.org/Person"]');
	if (!container) {
		return false;
	}

	const stargazers = await fetchStargazers();
	if (stargazers.length === 0) {
		return false;
	}

	container.append(
		<div className="border-top pt-3 mt-3 clearfix hide-sm hide-md">
			<h2 className="mb-2 h4">Followers you know</h2>
			{stargazers.map(renderAvatar)}
		</div>
	);
}

void features.add({
	id: __filebasename,
	disabled: '#3345',
	description: 'Followers you know are shown on profile pages',
	screenshot: 'https://user-images.githubusercontent.com/2906365/42009293-b1503f62-7a57-11e8-88f5-9c2fb3651a14.png'
}, {
	include: [
		pageDetect.isUserProfile
	],
	exclude: [
		pageDetect.isOwnUserProfile
	],
	init
});