Skip to content

Commit

Permalink
Add support for lazy-loaded assets in release-download-count (#6039)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Oct 3, 2022
1 parent 05c85ce commit 08d79ec
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions source/features/release-download-count.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {abbreviateNumber} from 'js-abbreviation-number';

import features from '../feature-manager';
import * as api from '../github-helpers/api';
import observe from '../helpers/selector-observer';

type Release = {
releaseAssets: {
Expand Down Expand Up @@ -44,30 +45,18 @@ async function getAssetsForTag(tags: string[]): Promise<Tag> {
return assets;
}

async function init(): Promise<void | false> {
const releases = new Map<string, HTMLElement>();
async function addCounts(assetsList: HTMLElement): Promise<void> {
const releaseName = assetsList
.closest('[data-test-selector="release-card"]')!
.previousElementSibling!
.querySelector('.octicon-tag ~ span')!
.textContent!
.trim();

if (pageDetect.isSingleTag() && select.exists('.Box-footer .octicon-package')) {
const name = select('.Box svg.octicon-tag ~ span')!.textContent!.trim();
releases.set(name, select('.Box-footer')!);
} else {
for (const release of select.all('[data-test-selector="release-card"] > .Box')) {
if (!select.exists('.octicon-package', release)) {
continue;
}

// Get the tag name from the heading link
const name = select('.Box-body a.Link--primary', release)!.href.split('/').pop()!;
releases.set(name, release);
}
}

if (releases.size === 0) {
return false;
}

const assets = await getAssetsForTag([...releases.keys()]);
const assets = await getAssetsForTag([releaseName]);

// TODO: Use batchedFunction instead
const releases = [[releaseName, assetsList]] as const;
for (const [name, release] of releases) {
const sortedDownloads = assets[api.escapeKey(name)].sort((a, b) => b.downloadCount - a.downloadCount);
for (const assetName of select.all('.octicon-package ~ a .text-bold', release)) {
Expand Down Expand Up @@ -104,6 +93,11 @@ async function init(): Promise<void | false> {
}
}

function init(signal: AbortSignal): void {
// TODO: Replace with :has selector to be safer
observe('[data-test-selector="release-card"] details .Box ul', addCounts, {signal});
}

void features.add(import.meta.url, {
include: [
pageDetect.isReleasesOrTags,
Expand Down

0 comments on commit 08d79ec

Please sign in to comment.