Hide segments when voting from the popup

This commit is contained in:
Ajay
2022-02-06 14:04:54 -05:00
parent 4a8e769596
commit 32fa9c3398
4 changed files with 59 additions and 31 deletions

View File

@@ -645,18 +645,9 @@ class SkipNoticeComponent extends React.Component<SkipNoticeProps, SkipNoticeSta
this.addVoteButtonInfo(chrome.i18n.getMessage("voted"));
// Change the sponsor locally
if (segment) {
if (type === 0) {
segment.hidden = SponsorHideType.Downvoted;
} else if (category) {
segment.category = category; // This is the actual segment on the video page
this.segments[index].category = category; //this is the segment inside the skip notice.
} else if (type === 1) {
segment.hidden = SponsorHideType.Visible;
}
this.contentContainer().updatePreviewBar();
if (segment && category) {
// This is the segment inside the skip notice
this.segments[index].category = category;
}
}

View File

@@ -203,6 +203,9 @@ function messageListener(request: Message, sender: unknown, sendResponse: (respo
onMobileYouTube
}));
return true;
case "submitVote":
vote(request.type, request.UUID).then((response) => sendResponse(response));
return true;
}
}
@@ -1689,7 +1692,7 @@ function clearSponsorTimes() {
}
//if skipNotice is null, it will not affect the UI
async function vote(type: number, UUID: SegmentUUID, category?: Category, skipNotice?: SkipNoticeComponent): Promise<void> {
async function vote(type: number, UUID: SegmentUUID, category?: Category, skipNotice?: SkipNoticeComponent): Promise<VoteResponse> {
if (skipNotice !== null && skipNotice !== undefined) {
//add loading info
skipNotice.addVoteButtonInfo.bind(skipNotice)(chrome.i18n.getMessage("Loading"))
@@ -1717,6 +1720,8 @@ async function vote(type: number, UUID: SegmentUUID, category?: Category, skipNo
}
}
}
return response;
}
async function voteAsync(type: number, UUID: SegmentUUID, category?: Category): Promise<VoteResponse> {
@@ -1746,7 +1751,25 @@ async function voteAsync(type: number, UUID: SegmentUUID, category?: Category):
type: type,
UUID: UUID,
category: category
}, resolve);
}, (response) => {
if (response.successType === 1) {
// Change the sponsor locally
const segment = utils.getSponsorTimeFromUUID(sponsorTimes, UUID);
if (segment) {
if (type === 0) {
segment.hidden = SponsorHideType.Downvoted;
} else if (category) {
segment.category = category;
} else if (type === 1) {
segment.hidden = SponsorHideType.Visible;
}
updatePreviewBar();
}
}
resolve(response);
});
});
}

View File

@@ -2,7 +2,7 @@
// Message and Response Types
//
import { SponsorTime } from "./types";
import { SegmentUUID, SponsorTime } from "./types";
interface BaseMessage {
from?: string;
@@ -29,7 +29,13 @@ interface IsInfoFoundMessage {
updating: boolean;
}
export type Message = BaseMessage & (DefaultMessage | BoolValueMessage | IsInfoFoundMessage);
interface submitVoteMessage {
message: "submitVote";
type: number;
UUID: SegmentUUID;
}
export type Message = BaseMessage & (DefaultMessage | BoolValueMessage | IsInfoFoundMessage | submitVoteMessage);
export interface IsInfoFoundMessageResponse {
found: boolean;
@@ -59,7 +65,8 @@ export type MessageResponse =
| GetChannelIDResponse
| SponsorStartResponse
| IsChannelWhitelistedResponse
| Record<string, never>;
| Record<string, never>
| VoteResponse;
export interface VoteResponse {
successType: number;

View File

@@ -589,21 +589,28 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
//add loading info
addVoteMessage(chrome.i18n.getMessage("Loading"), UUID);
//send the vote message to the tab
chrome.runtime.sendMessage({
message: "submitVote",
type: type,
UUID: UUID
}, function (response) {
if (response != undefined) {
//see if it was a success or failure
if (response.successType == 1 || (response.successType == -1 && response.statusCode == 429)) {
//success (treat rate limits as a success)
addVoteMessage(chrome.i18n.getMessage("voted"), UUID);
} else if (response.successType == -1) {
addVoteMessage(GenericUtils.getErrorMessage(response.statusCode, response.responseText), UUID);
messageHandler.query({
active: true,
currentWindow: true
}, tabs => {
messageHandler.sendMessage(
tabs[0].id,
{
message: "submitVote",
type: type,
UUID: UUID
}, function (response) {
if (response != undefined) {
//see if it was a success or failure
if (response.successType == 1 || (response.successType == -1 && response.statusCode == 429)) {
//success (treat rate limits as a success)
addVoteMessage(chrome.i18n.getMessage("voted"), UUID);
} else if (response.successType == -1) {
addVoteMessage(GenericUtils.getErrorMessage(response.statusCode, response.responseText), UUID);
}
}
}
}
);
});
}