import React from 'dom-chef'; import {CachedFunction} from 'webext-storage-cache'; import {$, elementExists} from 'select-dom'; import * as pageDetect from 'github-url-detection'; import {CodeSquareIcon} from '@primer/octicons-react'; import features from '../feature-manager.js'; import api from '../github-helpers/api.js'; import {getCleanPathname} from '../github-helpers/index.js'; import createDropdownItem from '../github-helpers/create-dropdown-item.js'; import observe from '../helpers/selector-observer.js'; import GetGistCount from './profile-gists-link.gql'; const gistCount = new CachedFunction('gist-count', { async updater(username: string): Promise { const {user} = await api.v4(GetGistCount, { variables: {username}, }); return user.gists.totalCount; }, maxAge: {days: 1}, staleWhileRevalidate: {days: 3}, }); function getUser(): {url: string; name: string} { const name = getCleanPathname(); const url = pageDetect.isEnterprise() ? `/gist/${name}` : `https://gist.github.com/${name}`; return {url, name}; } async function appendTab(navigationBar: Element): Promise { const user = getUser(); const link = ( {' Gists '} ); navigationBar.append(link); navigationBar.replaceWith(navigationBar); // There are two UnderlineNav items (responsive…) that point to the same dropdown const overflowNav = $('.js-responsive-underlinenav .dropdown-menu ul')!; if (!elementExists('[data-rgh-label="Gists"]', overflowNav)) { overflowNav.append( createDropdownItem('Gists', user.url), ); } const count = await gistCount.get(user.name); if (count > 0) { link.append({count}); } } async function init(signal: AbortSignal): Promise { observe('nav[aria-label="User"] > ul', appendTab, {signal}); } void features.add(import.meta.url, { include: [ pageDetect.isUserProfile, ], init, }); /* Test URL: Has gists: https://github.com/fregante No gists: https://github.com/someone */