summaryrefslogtreecommitdiff
path: root/source/features/infinite-scroll.tsx
blob: be4f4022f0123f137eda22e10bbeb22ffc7ca45e (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 {$} 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((button: HTMLButtonElement) => {
	button.click();

	// 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(button);
	}
}, {wait: 200});

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

function copyFooter(originalFooter: HTMLElement): void {
	// Copy the footer links to the sidebar to make them more accessible. Also keep a copy in the footer.
	const footer = originalFooter.cloneNode(true);

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

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

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

	observe('.footer > .d-flex', copyFooter, {signal});
}

void features.add(import.meta.url, {
	include: [
		pageDetect.isDashboard,
	],
	init,
});

/*

## Test URLs

https://github.com/

*/