import * as React from "react"; import Config from "../config"; import { ActionType, Category, SegmentUUID, SponsorTime } from "../types"; import ThumbsUpSvg from "../svg-icons/thumbs_up_svg"; import ThumbsDownSvg from "../svg-icons/thumbs_down_svg"; import { downvoteButtonColor, SkipNoticeAction } from "../utils/noticeUtils"; import { VoteResponse } from "../messageTypes"; import { AnimationUtils } from "../../maze-utils/src/animationUtils"; import { Tooltip } from "../render/Tooltip"; import { formatJSErrorMessage, getLongErrorMessage } from "../../maze-utils/src/formating"; import { logRequest } from "../../maze-utils/src/background-request-proxy"; export interface ChapterVoteProps { vote: (type: number, UUID: SegmentUUID, category?: Category) => Promise; size?: string; } export interface ChapterVoteState { segment?: SponsorTime; show: boolean; size?: string; } class ChapterVoteComponent extends React.Component { tooltip?: Tooltip; constructor(props: ChapterVoteProps) { super(props); this.state = { segment: null, show: false, size: props.size ?? "22px" }; } render(): React.ReactElement { if (this.tooltip && !this.state.show) { this.tooltip.close(); this.tooltip = null; } return ( <> {/* Upvote Button */} {/* Downvote Button */} ); } private async vote(event: React.MouseEvent, type: number, element?: HTMLElement): Promise { event.stopPropagation(); if (this.state.segment) { const stopAnimation = AnimationUtils.applyLoadingAnimation(element ?? event.currentTarget as HTMLElement, 0.3); const response = await this.props.vote(type, this.state.segment.UUID); await stopAnimation(); if ("error" in response){ console.error("[SB] Caught error while attempting to vote on a chapter", response.error); alert(formatJSErrorMessage(response.error)); } else if (response.ok || response.status == 429) { this.setState({ show: type === 1 }); } else if (response.status !== 403) { logRequest({headers: null, ...response}, "SB", "vote on chapter"); alert(getLongErrorMessage(response.status, response.responseText)); } } } } export default ChapterVoteComponent;