Skip to content

Commit

Permalink
timely api call
Browse files Browse the repository at this point in the history
  • Loading branch information
janat08 committed Apr 20, 2019
1 parent eb6f4ec commit bac0822
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
46 changes: 35 additions & 11 deletions source/features/remove-projects-sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ The sidebar can't be used to create new projects.
New projects can still be created via the [`Create new…` menu](https://user-images.githubusercontent.com/1402241/34909214-18b6fb2e-f8cf-11e7-8556-bed748596d3b.png).
*/

import select from 'select-dom';
import * as api from '../libs/api';
import features from '../libs/features';
import {getOwnerAndRepo} from '../libs/utils';
import { safeElementReady } from '../libs/dom-utils';

async function init(): Promise<false | void> {
const project = select('[aria-label="Select projects"]');
if (!project) {
return false;
}
var loading = false
var res: Promise<any>

const {ownerName, repoName} = getOwnerAndRepo();
const {repository} = await api.v4(`
function query (){
const { ownerName, repoName } = getOwnerAndRepo();
// return await onetime((){}())
loading = true
res = api.v4(`
{
repository(owner: "${ownerName}", name: "${repoName}") {
projects(states: [OPEN, CLOSED]){
Expand All @@ -25,18 +25,42 @@ async function init(): Promise<false | void> {
}
}
`);
loading = false
}

if (repository.projects.totalCount === 0) {
project.parentElement!.remove();

async function init(): Promise<false | void> {
if (!loading) {
query()
}
const project = await safeElementReady('[aria-label="Select projects"]')
if (!project) {
return false;
}
loading = false
res.then(x => {
if (x.repository.projects.totalCount === 0) {
project.parentElement!.remove();
}
})
}

features.add({
id: 'remove-projects-sidebar',
include: [
features.isIssue,
features.isPRConversation
features.isPR
],
load: features.onAjaxedPages,
init
});

features.add({
id: 'remove-projects-sidebar',
include: [
features.isIssue,
features.isPR
],
load: features.onNavigation,
init: query
})
6 changes: 6 additions & 0 deletions source/libs/features.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ interface PrivateFeatureDetails extends FeatureDetails {
options: GlobalOptions;
}

function onNavigation(callback: ()=> void) {
addEventListener('pjax:start', callback)
}


/*
* When navigating back and forth in history, GitHub will preserve the DOM changes;
* This means that the old features will still be on the page and don't need to re-run.
Expand Down Expand Up @@ -199,6 +204,7 @@ export default {
getShortcuts,

// Loading mechanisms
onNavigation,
onDomReady,
onNewComments,
onFileListUpdate,
Expand Down

0 comments on commit bac0822

Please sign in to comment.