summaryrefslogtreecommitdiff
path: root/source/features/clean-issue-filters.tsx
diff options
context:
space:
mode:
authorGravatar Laxman <notlmn@outlook.com> 2019-06-21 13:57:16 +0530
committerGravatar Federico Brigante <github@bfred.it> 2019-06-21 15:27:15 +0700
commit74ca4d61bb2adaf51a3a93aa94def7f9b1193695 (patch)
tree9a73fce63c4f2fa457df45277c2b1cc431ee8174 /source/features/clean-issue-filters.tsx
parentb0912c1264d45f082848db4bc0c5e687447160ed (diff)
downloadrefined-github-74ca4d61bb2adaf51a3a93aa94def7f9b1193695.tar.gz
refined-github-74ca4d61bb2adaf51a3a93aa94def7f9b1193695.tar.zst
refined-github-74ca4d61bb2adaf51a3a93aa94def7f9b1193695.zip
Extract cache to `webext-storage-cache` (#2159)
Diffstat (limited to 'source/features/clean-issue-filters.tsx')
-rw-r--r--source/features/clean-issue-filters.tsx35
1 files changed, 29 insertions, 6 deletions
diff --git a/source/features/clean-issue-filters.tsx b/source/features/clean-issue-filters.tsx
index befed5e6..608bd6dd 100644
--- a/source/features/clean-issue-filters.tsx
+++ b/source/features/clean-issue-filters.tsx
@@ -1,10 +1,23 @@
import select from 'select-dom';
+import cache from 'webext-storage-cache';
import features from '../libs/features';
import * as api from '../libs/api';
import {getOwnerAndRepo} from '../libs/utils';
-async function init(): Promise<void> {
+interface CacheEntry {
+ repoProjectCount: number;
+ orgProjectCount: number;
+ milestoneCount: number;
+}
+
+async function getCount(): Promise<CacheEntry> {
const {ownerName, repoName} = getOwnerAndRepo();
+ const cacheKey = `clean-issue-filters:${ownerName}/${repoName}`;
+ const cachedData = await cache.get<CacheEntry>(cacheKey);
+ if (cachedData) {
+ return cachedData;
+ }
+
const result = await api.v4(`
query {
repository(owner: "${ownerName}", name: "${repoName}") {
@@ -19,16 +32,26 @@ async function init(): Promise<void> {
allowErrors: true
});
+ const cacheEntry = {
+ repoProjectCount: result.repository.projects.totalCount,
+ orgProjectCount: result.organization ? result.organization.projects.totalCount : 0,
+ milestoneCount: result.repository.milestones.totalCount
+ };
+
+ cache.set(cacheKey, cacheEntry, 1);
+ return cacheEntry;
+}
+
+async function init(): Promise<void> {
+ const {repoProjectCount, orgProjectCount, milestoneCount} = await getCount();
+
// If the repo and organization has no projects, its selector will be empty
- if (
- result.repository.projects.totalCount === 0 &&
- (!result.organization || result.organization.projects.totalCount === 0)
- ) {
+ if (repoProjectCount === 0 && orgProjectCount === 0 && select.exists('[data-hotkey="p"')) {
select('[data-hotkey="p"')!.parentElement!.remove();
}
// If the repo has no milestones, its selector will be empty
- if (result.repository.milestones.totalCount === 0) {
+ if (milestoneCount === 0 && select.exists('[data-hotkey="m"')) {
select('[data-hotkey="m"')!.parentElement!.remove();
}
}