import { showToastError } from '../../../common/showToast' ; import { getSettings , saveSettings } from '../settings' ; type CloseVoteResponse = Readonly < { Success : boolean ; ResultChangedState : boolean ; Message : string ; Tooltip : string ; Count : number ; } > ; export const makeHandleCloseVoteResponse = ( questionId , setCanSendRequestToTrue ) => ( result ) => { if ( result . ResultChangedState ) { window . location . href = window . location . href ; return ; } if ( ! result . Success ) { setCanSendRequestToTrue ( ) ; showToastError ( result . Message ) ; return ; } const downvoteButton = document . querySelector < HTMLElement > ( '.question .js-vote-down-btn' ) ! ; downvoteButton . removeAttribute ( 'data-cpuserscript-one-click-vtc-imminent-downvote' ) ; const oneClickVTCContainer = document . querySelector ( '[data-cpuserscript-one-click-vtc]' ) ! ; oneClickVTCContainer . remove ( ) ; updateCloseVoteCount ( result ) ; if ( window . StackExchange . options . user . rep < 3000 ) { const { raisedCloseFlags } = getSettings ( ) ; raisedCloseFlags . push ( questionId ) ; if ( raisedCloseFlags . length > 100 ) { saveSettings ( { raisedCloseFlags : raisedCloseFlags . slice ( - 100 ) } ) ; } saveSettings ( { raisedCloseFlags } ) ; } } ; const updateCloseVoteCount = ( result ) => { type SETypeHere = { vote_closingAndFlagging : { updateCloseLinkCount : ( closeVoteResponse : CloseVoteResponse , closeQuestionLink : JQuery ) => void ; } ; } ; const { updateCloseLinkCount } = ( window . StackExchange as unknown as SETypeHere ) . vote_closingAndFlagging ; const haveSEUpdateCloseLinkCount = ( ) => { updateCloseLinkCount ( result , window . $ ( '.js-close-question-link' ) ) ; } ; haveSEUpdateCloseLinkCount ( ) ; const postcell = document . querySelector ( '.question .postcell' ) ! ; const outerObserver = new MutationObserver ( ( mutations , observer ) => { for ( const { removedNodes } of mutations ) { if ( [ ... removedNodes ] . includes ( postcell ) ) { haveSEUpdateCloseLinkCount ( ) ; observer . disconnect ( ) ; return ; } } } ) ; outerObserver . observe ( postcell . parentElement ! , { childList : true } ) ; window . setTimeout ( ( ) => { outerObserver . disconnect ( ) ; } , 1000 ) ; } ;