import * as React from "react"; import * as CompileConfig from "../../config.json"; import Config from "../config" import { ContentContainer, SponsorHideType, SponsorTime } from "../types"; import NoticeComponent from "./NoticeComponent"; import NoticeTextSelectionComponent from "./NoticeTextSectionComponent"; export enum SkipNoticeAction { None, Upvote, Downvote, CategoryVote, Unskip } export interface SkipNoticeProps { segments: SponsorTime[]; autoSkip: boolean; // Contains functions and variables from the content script needed by the skip notice contentContainer: ContentContainer; closeListener: () => void } export interface SkipNoticeState { noticeTitle?: string; messages?: string[]; messageOnClick?: (event: React.MouseEvent) => unknown; countdownTime?: number; maxCountdownTime?: () => number; countdownText?: string; unskipText?: string; unskipCallback?: (index: number) => void; downvoting?: boolean; choosingCategory?: boolean; thanksForVotingText?: string; //null until the voting buttons should be hidden actionState?: SkipNoticeAction; } class SkipNoticeComponent extends React.Component { segments: SponsorTime[]; autoSkip: boolean; // Contains functions and variables from the content script needed by the skip notice contentContainer: ContentContainer; amountOfPreviousNotices: number; audio: HTMLAudioElement; idSuffix: string; noticeRef: React.MutableRefObject; categoryOptionRef: React.RefObject; // Used to update on config change configListener: () => void; constructor(props: SkipNoticeProps) { super(props); this.noticeRef = React.createRef(); this.categoryOptionRef = React.createRef(); this.segments = props.segments; this.autoSkip = props.autoSkip; this.contentContainer = props.contentContainer; this.audio = null; const categoryName = chrome.i18n.getMessage(this.segments.length > 1 ? "multipleSegments" : "category_" + this.segments[0].category + "_short") || chrome.i18n.getMessage("category_" + this.segments[0].category); let noticeTitle = categoryName + " " + chrome.i18n.getMessage("skipped"); if (!this.autoSkip) { noticeTitle = chrome.i18n.getMessage("skip_category").replace("{0}", categoryName); } //add notice this.amountOfPreviousNotices = document.getElementsByClassName("sponsorSkipNotice").length; // Sort segments if (this.segments.length > 1) { this.segments.sort((a, b) => a.segment[0] - b.segment[0]); } //this is the suffix added at the end of every id for (const segment of this.segments) { this.idSuffix += segment.UUID; } this.idSuffix += this.amountOfPreviousNotices; // Setup state this.state = { noticeTitle, messages: [], messageOnClick: null, //the countdown until this notice closes maxCountdownTime: () => Config.config.skipNoticeDuration, countdownTime: Config.config.skipNoticeDuration, countdownText: null, unskipText: chrome.i18n.getMessage("unskip"), unskipCallback: (index) => this.unskip(index), downvoting: false, choosingCategory: false, thanksForVotingText: null, actionState: SkipNoticeAction.None } if (!this.autoSkip) { // Assume manual skip is only skipping 1 submission Object.assign(this.state, this.getUnskippedModeInfo(0, chrome.i18n.getMessage("skip"))); } } componentDidMount(): void { if (Config.config.audioNotificationOnSkip && this.audio) { this.audio.volume = this.contentContainer().v.volume * 0.1; if (this.autoSkip) this.audio.play(); } } render(): React.ReactElement { const noticeStyle: React.CSSProperties = { } if (this.contentContainer().onMobileYouTube) { noticeStyle.bottom = "4em"; noticeStyle.transform = "scale(0.8) translate(10%, 10%)"; } return ( this.contentContainer().v?.playbackRate} style={noticeStyle} ref={this.noticeRef} closeListener={() => this.closeListener()}> {(Config.config.audioNotificationOnSkip) && } {/* Text Boxes */} {this.getMessageBoxes()} {/* Bottom Row */} {/* Vote Button Container */} {!this.state.thanksForVotingText ? {/* Upvote Button */} this.prepAction(SkipNoticeAction.Upvote)}> {/* Report Button */} this.adjustDownvotingState(true)}> : {this.state.thanksForVotingText} } {/* Unskip Button */} {/* Never show button if autoSkip is enabled */} {!this.autoSkip ? "" : } {/* Downvote Options Row */} {this.state.downvoting && {/* Normal downvote */} {/* Category vote */} } {/* Category Chooser Row */} {this.state.choosingCategory && {/* Category Selector */} {/* Submit Button */} {this.segments.length === 1 && } } {/* Segment Chooser Row */} {this.state.actionState !== SkipNoticeAction.None && {this.getSubmissionChooser()} } ); } getSubmissionChooser(): JSX.Element[] { const elements: JSX.Element[] = []; for (let i = 0; i < this.segments.length; i++) { elements.push( ); } return elements; } prepAction(action: SkipNoticeAction): void { if (this.segments.length === 1) { this.performAction(0, action); } else { this.setState({ actionState: action }); } } getMessageBoxes(): JSX.Element[] | JSX.Element { if (this.state.messages.length === 0) { // Add a spacer if there is no text return ( ); } const elements: JSX.Element[] = []; for (let i = 0; i < this.state.messages.length; i++) { elements.push( ) } return elements; } /** * Performs the action from the current state * * @param index */ performAction(index: number, action?: SkipNoticeAction): void { switch (action ?? this.state.actionState) { case SkipNoticeAction.None: break; case SkipNoticeAction.Upvote: this.contentContainer().vote(1, this.segments[index].UUID, undefined, this); break; case SkipNoticeAction.Downvote: this.contentContainer().vote(0, this.segments[index].UUID, undefined, this); break; case SkipNoticeAction.CategoryVote: this.contentContainer().vote(undefined, this.segments[index].UUID, this.categoryOptionRef.current.value, this) break; case SkipNoticeAction.Unskip: this.state.unskipCallback(index); break; } this.setState({ actionState: SkipNoticeAction.None }); } adjustDownvotingState(value: boolean): void { if (!value) this.clearConfigListener(); this.setState({ downvoting: value, choosingCategory: false }); } clearConfigListener(): void { if (this.configListener) { Config.configListeners.splice(Config.configListeners.indexOf(this.configListener), 1); this.configListener = null; } } openCategoryChooser(): void { // Add as a config listener this.configListener = () => this.forceUpdate(); Config.configListeners.push(this.configListener); this.setState({ choosingCategory: true, downvoting: false }, () => { if (this.segments.length > 1) { // Use the action selectors as a submit button this.prepAction(SkipNoticeAction.CategoryVote); } }); } getCategoryOptions(): React.ReactElement[] { const elements = []; for (const category of CompileConfig.categoryList) { elements.push( ); } return elements; } unskip(index: number): void { this.contentContainer().unskipSponsorTime(this.segments[index]); this.unskippedMode(index, chrome.i18n.getMessage("reskip")); } /** Sets up notice to be not skipped yet */ unskippedMode(index: number, buttonText: string): void { //setup new callback and reset countdown this.setState(this.getUnskippedModeInfo(index, buttonText), () => { this.noticeRef.current.resetCountdown(); }); } getUnskippedModeInfo(index: number, buttonText: string): SkipNoticeState { const maxCountdownTime = () => { const sponsorTime = this.segments[index]; const duration = Math.round((sponsorTime.segment[1] - this.contentContainer().v.currentTime) * (1 / this.contentContainer().v.playbackRate)); return Math.max(duration, Config.config.skipNoticeDuration); }; return { unskipText: buttonText, unskipCallback: (index) => this.reskip(index), // change max duration to however much of the sponsor is left maxCountdownTime: maxCountdownTime, countdownTime: maxCountdownTime() } as SkipNoticeState; } reskip(index: number): void { this.contentContainer().reskipSponsorTime(this.segments[index]); const newState: SkipNoticeState = { unskipText: chrome.i18n.getMessage("unskip"), unskipCallback: this.unskip.bind(this), maxCountdownTime: () => Config.config.skipNoticeDuration, countdownTime: Config.config.skipNoticeDuration }; // See if the title should be changed if (!this.autoSkip) { newState.noticeTitle = chrome.i18n.getMessage("noticeTitle"); } //reset countdown this.setState(newState, () => { this.noticeRef.current.resetCountdown(); }); } afterVote(segment: SponsorTime, type: number, category: string): void { this.addVoteButtonInfo(chrome.i18n.getMessage("voted")); if (type === 0) { this.setNoticeInfoMessage(chrome.i18n.getMessage("hitGoBack")); this.adjustDownvotingState(false); } // Change the sponsor locally if (segment) { if (type === 0) { segment.hidden = SponsorHideType.Downvoted; } else if (category) { segment.category = category; } this.contentContainer().updatePreviewBar(); } } setNoticeInfoMessageWithOnClick(onClick: (event: React.MouseEvent) => unknown, ...messages: string[]): void { this.setState({ messages, messageOnClick: (event) => onClick(event) }); } setNoticeInfoMessage(...messages: string[]): void { this.setState({ messages }); } addVoteButtonInfo(message: string): void { this.setState({ thanksForVotingText: message }); } resetVoteButtonInfo(): void { this.setState({ thanksForVotingText: null }); } closeListener(): void { this.clearConfigListener(); this.props.closeListener(); } } export default SkipNoticeComponent;