Skip to content

Commit

Permalink
Meta: Show toast in more features (refined-github#6380)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored Mar 1, 2023
1 parent b03be18 commit bab8261
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
19 changes: 12 additions & 7 deletions source/features/expand-all-hidden-comments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ import delegate, {DelegateEvent} from 'delegate-it';
import * as pageDetect from 'github-url-detection';

import features from '../feature-manager';
import showToast from '../github-helpers/toast';

const paginationButtonSelector = '.ajax-pagination-form button[type="submit"]';

async function handleAltClick({altKey, delegateTarget}: DelegateEvent<MouseEvent, HTMLButtonElement>): Promise<void> {
if (!altKey) {
return;
}

let paginationButton: HTMLButtonElement | undefined = delegateTarget;
let wrapper: Element = paginationButton.form!.parentElement!;
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
async function expandHidden(paginationButton: HTMLButtonElement | undefined) {
let wrapper: Element = paginationButton!.form!.parentElement!;
const isExpandingMainThread = wrapper.id === 'js-progressive-timeline-item-container';

while (paginationButton) {
Expand All @@ -29,6 +26,14 @@ async function handleAltClick({altKey, delegateTarget}: DelegateEvent<MouseEvent
}
}

async function handleAltClick({altKey, delegateTarget}: DelegateEvent<MouseEvent, HTMLButtonElement>): Promise<void> {
if (!altKey) {
return;
}

await showToast(expandHidden(delegateTarget), {message: 'Expanding…', doneMessage: 'Expanded'});
}

function init(signal: AbortSignal): void {
delegate(document, paginationButtonSelector, 'click', handleAltClick, {signal});
}
Expand Down
27 changes: 14 additions & 13 deletions source/features/update-pr-from-base-branch.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import React from 'dom-chef';
import select from 'select-dom';

import {AlertIcon} from '@primer/octicons-react';
import * as pageDetect from 'github-url-detection';
import delegate, {DelegateEvent} from 'delegate-it';
import delegate from 'delegate-it';

import features from '../feature-manager';
import observe from '../helpers/selector-observer';
import * as api from '../github-helpers/api';
import {getBranches} from '../github-helpers/pr-branches';
import getPrInfo from '../github-helpers/get-pr-info';
import {getConversationNumber} from '../github-helpers';
import showToast from '../github-helpers/toast';

const selectorForPushablePRNotice = '.merge-pr > .color-fg-muted:first-child';

Expand All @@ -21,24 +21,25 @@ async function mergeBranches(): Promise<AnyObject> {
});
}

async function handler({delegateTarget}: DelegateEvent): Promise<void> {
async function handler(): Promise<void> {
const {base, head} = getBranches();
if (!confirm(`Merge the ${base.local} branch into ${head.local}?`)) {
return;
}

const statusMeta = delegateTarget.parentElement!;
statusMeta.textContent = 'Updating branch…';
features.unload(import.meta.url);

const response = await mergeBranches();
if (response.ok) {
statusMeta.remove();
} else {
statusMeta.textContent = response.message ?? 'Error';
statusMeta.prepend(<AlertIcon/>, ' ');
throw new api.RefinedGitHubAPIError('update-pr-from-base-branch: ' + JSON.stringify(response));
}
await showToast(async () => {
const response = await mergeBranches().catch(error => error);
if (response instanceof Error || !response.ok) {
features.log.error(import.meta.url, response);
// Reads Error#message or GitHub’s "message" response
throw new Error(`Error updating the branch: ${response.message as string}`);
}
}, {
message: 'Updating branch…',
doneMessage: 'Branch updated',
});
}

async function addButton(position: Element): Promise<void> {
Expand Down

0 comments on commit bab8261

Please sign in to comment.