import React from 'dom-chef'; import cache from 'webext-storage-cache'; import select from 'select-dom'; import {BookIcon} from '@primer/octicons-react'; import elementReady from 'element-ready'; import * as pageDetect from 'github-url-detection'; import features from '.'; import * as api from '../github-helpers/api'; import {buildRepoURL, getRepo} from '../github-helpers'; const getCacheKey = (): string => `changelog:${getRepo()!.nameWithOwner}`; const changelogNames = new Set(['changelog', 'news', 'changes', 'history', 'release', 'whatsnew']); function findChangelogName(files: string[]): string | false { for (const file of files) { if (changelogNames.has(file.toLowerCase().split('.', 1)[0])) { return file; } } return false; } function parseFromDom(): false { const files = select.all('[aria-labelledby="files"] .js-navigation-open[href*="/blob/"').map(file => file.title); void cache.set(getCacheKey(), findChangelogName(files)); return false; } const getChangelogName = cache.function(async (): Promise => { const {repository} = await api.v4(` repository() { object(expression: "HEAD:") { ...on Tree { entries { name type } } } } `); const files: string[] = []; for (const entry of repository.object.entries) { if (entry.type === 'blob') { files.push(entry.name); } } return findChangelogName(files); }, { cacheKey: getCacheKey }); async function init(): Promise { const changelog = await getChangelogName(); if (!changelog) { return false; } (await elementReady('.subnav div', {waitForChildren: false}))!.after( Changelog ); } void features.add(__filebasename, { include: [ pageDetect.isReleasesOrTags ], awaitDomReady: false, init }, { include: [ pageDetect.isRepoHome ], init: parseFromDom });