Skip to content

Commit

Permalink
fix: don't show empty tooltip if there isn't any feedback to send
Browse files Browse the repository at this point in the history
  • Loading branch information
double-beep authored Jun 12, 2024
1 parent bbe4744 commit 98b1d4a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 70 deletions.
6 changes: 2 additions & 4 deletions src/AdvancedFlagging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ import { Store, Cached } from './UserscriptTools/Store';

// <TODO>
// What's left for 2.0.0:
// - Section to ONLY send feedback
// - Manipulate the flag dialog:
// - send feedback to bots checkbox
// - Manipulate the flag & recommend deletion popups:
// - choose the bots to which to send feedback
// - choose the feedback to send
// - also the recommend deletion/Delete popup
// - Add option to flag post from recommend deletion popup
// - Flag post from recommend deletion popup
// </TODO>

function setupStyles(): void {
Expand Down
40 changes: 14 additions & 26 deletions src/UserscriptTools/Post.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getFlagToRaise } from '../AdvancedFlagging';
import { Flags } from '../FlagTypes';
import { getSvg, PostType, FlagNames, getFormDataFromObject, addXHRListener, toggleLoading, delay } from '../shared';
import { getSvg, PostType, FlagNames, getFormDataFromObject, addXHRListener, addProgress, FlagTypeFeedbacks } from '../shared';

import { CopyPastorAPI } from './CopyPastorAPI';
import { GenericBotAPI } from './GenericBotAPI';
Expand Down Expand Up @@ -307,38 +307,18 @@ export default class Post {
.find(item => item.sendWhenFlagRaised && item.reportType === flag);
if (!flagType) return;

// don't flag immediately
event.preventDefault();
event.stopPropagation();

if (Store.dryRun) {
console.log('Post', this.id, 'manually flagged as', flag, flagType);
}

const target = event.target as HTMLButtonElement;
toggleLoading(target);

this.progress = new Progress(submit);
this.progress.attach();

try {
await this.sendFeedbacks(flagType);
$(this.flagged).fadeIn();
} finally {
await delay(1000);
toggleLoading(target);
target.click();
}
await addProgress(event, flagType, this);
$(this.flagged).fadeIn();
}, { once: true });
});
}

public async sendFeedbacks({ feedbacks }: CachedFlag): Promise<boolean> {
let hasFailed = false;

// simultaneously send feedback to all bots
// hasFailed will be set to true if something goes wrong
const allPromises = (Object.values(this.reporters) as Reporter[])
public filterReporters(feedbacks: FlagTypeFeedbacks): Reporter[] {
return (Object.values(this.reporters) as Reporter[])
// keep only the bots the user has opted to send feedback to
.filter(reporter => {
const { name } = reporter;
Expand All @@ -355,7 +335,15 @@ export default class Post {
const feedback = feedbacks[name];

return sendFeedback && feedback && reporter.canSendFeedback(feedback);
})
});
}

public async sendFeedbacks({ feedbacks }: CachedFlag): Promise<boolean> {
let hasFailed = false;

// simultaneously send feedback to all bots
// hasFailed will be set to true if something goes wrong
const allPromises = this.filterReporters(feedbacks)
// return a promise that sends the feedback
// use .map() so that they run in paraller
.map(reporter => {
Expand Down
43 changes: 3 additions & 40 deletions src/review.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addXHRListener, delay, toggleLoading } from './shared';
import { addProgress, addXHRListener, delay } from './shared';
import { isDone } from './AdvancedFlagging';

import { MetaSmokeAPI } from './UserscriptTools/MetaSmokeAPI';
Expand All @@ -7,7 +7,6 @@ import { CopyPastorAPI } from './UserscriptTools/CopyPastorAPI';
import { Cached, Store } from './UserscriptTools/Store';

import Page from './UserscriptTools/Page';
import { Progress } from './UserscriptTools/Progress';

interface ReviewQueueResponse {
postId: number;
Expand Down Expand Up @@ -66,19 +65,7 @@ async function runOnNewTask(xhr: XMLHttpRequest): Promise<void> {
// in case "looks fine" flagtype is deleted
if (!flagType) return;

event.preventDefault();
event.stopPropagation();

const target = event.target as HTMLButtonElement;
toggleLoading(target);

const page = new Page(true);
await page.posts[0].sendFeedbacks(flagType);

await delay(1000);
toggleLoading(target);

target.click();
await addProgress(event, flagType);
}, { once: true });
}

Expand Down Expand Up @@ -113,31 +100,7 @@ export function setupReview(): void {
const flagType = Store.flagTypes.find(({ id }) => id === 7);
if (!flagType) return; // something went wrong

// don't recomment deletion immediately
event.preventDefault();
event.stopPropagation();

const target = event.target as HTMLButtonElement;

// indicate loading
toggleLoading(target);

try {
const page = new Page(true);
const post = page.posts[0];

post.progress = new Progress(target);
post.progress.attach();

await post.sendFeedbacks(flagType);
} finally {
// remove previously added indicators
await delay(1000);
toggleLoading(target);

// proceed with the vote
target.click();
}
await addProgress(event, flagType);
}, { once: true });
});
}
32 changes: 32 additions & 0 deletions src/shared.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { CachedFlag, Configuration, Store } from './UserscriptTools/Store';
import { Flags } from './FlagTypes';
import Page from './UserscriptTools/Page';
import { Progress } from './UserscriptTools/Progress';

type BasicPlacement = 'auto' | 'top' | 'right' | 'bottom' | 'left';
// Minimum TypeScript Version: 4.1
Expand Down Expand Up @@ -188,3 +190,33 @@ export function toggleLoading(button: HTMLButtonElement): void {
button.ariaDisabled = button.ariaDisabled === 'true' ? 'false' : 'true';
button.disabled = !button.disabled;
}

export async function addProgress(
event: Event,
flagType: CachedFlag,
post = new Page(true).posts[0]
): Promise<void> {
if (!post.filterReporters(flagType.feedbacks).length) return;

event.preventDefault();
event.stopPropagation();

const target = event.target as HTMLButtonElement;

// indicate loading
toggleLoading(target);

try {
post.progress = new Progress(target);
post.progress.attach();

await post.sendFeedbacks(flagType);
} finally {
// remove previously added indicators
await delay(1000);
toggleLoading(target);

// proceed with the vote
target.click();
}
}

0 comments on commit 98b1d4a

Please sign in to comment.