Skip to content

Commit

Permalink
Avoid API call when possible in clean-issue-filters (#2979)
Browse files Browse the repository at this point in the history
  • Loading branch information
yakov116 committed Apr 12, 2020
1 parent 66544c0 commit 80a68b8
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions source/features/clean-issue-filters.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import select from 'select-dom';
import cache from 'webext-storage-cache';
import elementReady from 'element-ready';
import features from '../libs/features';
import * as api from '../libs/api';
import {getOwnerAndRepo, getRepoURL, getRepoGQL} from '../libs/utils';

interface Counts {
projects: number;
milestones: number;
}

const getCounts = cache.function(async (): Promise<Counts> => {
const hasAnyProjects = cache.function(async (): Promise<boolean> => {
const {repository, organization} = await api.v4(`
repository(${getRepoGQL()}) {
projects { totalCount }
milestones { totalCount }
}
organization(login: "${getOwnerAndRepo().ownerName!}") {
projects { totalCount }
Expand All @@ -22,36 +17,51 @@ const getCounts = cache.function(async (): Promise<Counts> => {
allowErrors: true
});

return {
projects:
(repository.projects.totalCount as number) +
(organization?.projects?.totalCount as number ?? 0),
milestones: repository.milestones.totalCount
};
return Boolean(repository.projects.totalCount) && Boolean(organization?.projects?.totalCount);
}, {
maxAge: 3,
staleWhileRevalidate: 20,
cacheKey: () => __featureName__ + ':' + getRepoURL()
cacheKey: () => `has-projects:${getRepoURL()}`
});

function removeParent(element?: Element): void {
if (element) { // If may be missing when the feature is entirely disabled
element.parentElement!.remove();
function getCount(element: HTMLElement): number {
return Number(element.textContent!.trim());
}

async function hideMilestones(): Promise<void> {
const milestones = select('[data-selected-links^="repo_milestones"] .Counter')!;
if (getCount(milestones) === 0) {
(await elementReady('[data-hotkey="m"]'))!.parentElement!.remove();
}
}

async function init(): Promise<void> {
const {projects, milestones} = await getCounts();
async function hasProjects(): Promise<boolean> {
const activeProjectsCounter = select('[data-hotkey="g b"] .Counter');
if (activeProjectsCounter && getCount(activeProjectsCounter) > 0) {
return true;
}

if (projects === 0) {
elementReady('[data-hotkey="p"]').then(removeParent);
const isOrganization = select.exists('[rel=author][data-hovercard-type="organization"]');
if (!activeProjectsCounter && !isOrganization) {
// No tab = Projects disabled in repo
// No organization = no Projects in organization
return false;
}

if (milestones === 0) {
elementReady('[data-hotkey="m"]').then(removeParent);
return hasAnyProjects();
}

async function hideProjects(): Promise<void> {
if (!await hasProjects()) {
(await elementReady('[data-hotkey="p"]'))!.parentElement!.remove();
}
}

function init(): void {
hideMilestones();
hideProjects();
}

features.add({
id: __featureName__,
description: 'Hides `Projects` and `Milestones` filters in discussion lists if they are empty.',
Expand Down

0 comments on commit 80a68b8

Please sign in to comment.