(branchSelectorElement: HTMLElement)
| 42 | } |
| 43 | |
| 44 | async function add(branchSelectorElement: HTMLElement): Promise<void> { |
| 45 | // Apply here instead of `excludes` due to React loading: |
| 46 | // 1. Visit branch |
| 47 | // 2. Click "Code" repo tab |
| 48 | // 3. You're on `main` but the button is wrapped |
| 49 | // TODO: Move to excludes after https://github.com/refined-github/refined-github/issues/6554 |
| 50 | if (await isDefaultBranch()) { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | // The DOM varies between details-based DOM and React-based one |
| 55 | const selectorWrapper = branchSelectorElement.tagName === 'SUMMARY' |
| 56 | ? branchSelectorElement.parentElement! |
| 57 | : branchSelectorElement; |
| 58 | selectorWrapper.classList.add('rgh-highlight-non-default-branch'); |
| 59 | |
| 60 | const existingLink = $optional('.rgh-default-branch-button', branchSelectorElement.parentElement!); |
| 61 | |
| 62 | // React issues. Duplicates appear after a color scheme update |
| 63 | // https://github.com/refined-github/refined-github/issues/7098 |
| 64 | if (existingLink) { |
| 65 | // Border radius style is removed by the color scheme update |
| 66 | wrapButtons([existingLink, selectorWrapper]); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | if (pageDetect.isSingleFile()) { |
| 71 | fixFileHeaderOverlap(branchSelectorElement); |
| 72 | } |
| 73 | |
| 74 | const defaultLink = ( |
| 75 | <a |
| 76 | className="btn px-2 tmp-px-2 rgh-default-branch-button flex-self-start" |
| 77 | href={await getUrl(location.href)} |
| 78 | // Update on hover because the URL may change without a DOM refresh |
| 79 | // https://github.com/refined-github/refined-github/issues/6554 |
| 80 | // Inlined listener because `mouseenter` is too heavy for `delegate` |
| 81 | onMouseEnter={updateUrl} |
| 82 | |
| 83 | // Don't enable AJAX on this behavior because we need a full page reload to drop the button, same reason as above #6554 |
| 84 | // data-turbo-frame="repo-content-turbo-frame" |
| 85 | > |
| 86 | <ChevronLeftIcon /> |
| 87 | </a> |
| 88 | ); |
| 89 | |
| 90 | selectorWrapper.before( |
| 91 | tooltipped({label: 'View on the default branch', direction: 'se'}, defaultLink), |
| 92 | ); |
| 93 | wrapButtons([defaultLink, selectorWrapper]); |
| 94 | } |
| 95 | |
| 96 | async function init(signal: AbortSignal): Promise<void> { |
| 97 | observe(branchSelector, add, {signal}); |
nothing calls this directly
no test coverage detected