summaryrefslogtreecommitdiff
path: root/source/features/infinite-scroll.tsx
blob: 4098f61efb4415455fc4aa15bb5fbbb378b78ea7 (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 debounce from 'debounce-fn';
import * as pageDetect from 'github-url-detection';

import features from '../feature-manager.js';
import observe from '../helpers/selector-observer.js';
import onAbort from '../helpers/abort-controller.js';

const loadMore = debounce(() => {
	const button = select('[role="tabpanel"]:not([hidden]) button.ajax-pagination-btn')!;
	button.click();
	button.textContent = 'Loading…';

	// If GH hasn't loaded the JS, the click will not load anything.
	// We can detect if it worked by looking at the button's state,
	// and then trying again (auto-debounced)
	if (!button.disabled) {
		loadMore();
	}
}, {wait: 200});

const inView = new IntersectionObserver(([{isIntersecting}]) => {
	if (isIntersecting) {
		loadMore();
	}
}, {
	rootMargin: '500px', // https://github.com/refined-github/refined-github/pull/505#issuecomment-309273098
});

function init(signal: AbortSignal): void {
	onAbort(signal, inView);
	observe('.ajax-pagination-btn', button => {
		inView.observe(button);
	}, {signal});

	// Copy the footer links to the sidebar to make them more accessible. Also keep a copy in the footer.
	const footer = select('.footer > .d-flex')!.cloneNode(true);

	for (const child of footer.children) {
		child.classList.remove('pl-lg-4', 'col-xl-3');
	}

	select('[aria-label^="Explore"]')!.append(
		<div className="footer mt-4 py-4 border-top">
			{footer}
		</div>,
	);
}

void features.add(import.meta.url, {
	include: [
		pageDetect.isDashboard,
	],
	deduplicate: 'has-rgh',
	awaitDomReady: true, // Must wait for the whole page to load anyway
	init,
});

/*

## Test URLs

https://github.com/

*/