"use strict";(self.webpackChunkmattermost_webapp=self.webpackChunkmattermost_webapp||[]).push([[5660],{25660:(e,t,a)=>{a.r(t),a.d(t,{default:()=>i}),a(15724);var n=a(46177),r=a.n(n),s=a(97792),c=(a(23100),a(36418)),u=a.n(c),l=a(92600),o=a(53654),f=a(63155);const i=(0,c.memo)((e=>{let{fileInfo:t,fileUrl:a,scale:n,handleBgClose:i}=e;const p=(0,c.useRef)({}),d=(0,c.useRef)(null),g=(0,c.useRef)(null),m=(0,c.useRef)({}),[v,w]=(0,c.useState)({pdf:null,pages:{},pagesLoaded:{},numPages:0}),[P,h]=(0,c.useState)("loading");(0,c.useEffect)((()=>{w({pdf:null,pages:{},pagesLoaded:{},numPages:0}),h("loading"),(async()=>{try{(e=>{w((t=>({...t,pdf:e,numPages:e.numPages})));for(let t=0;t<e.numPages;t++)m.current[`pdfCanvasRef-${t}`]=u().createRef();h("success")})(await s.YE({url:a,cMapUrl:(0,f.vV)()+"/static/cmaps/",cMapPacked:!0}).promise)}catch(t){e=String(t),console.log("Unable to load PDF preview: "+e),h("fail")}var e})(),p.current={}}),[a]);const E=(0,c.useCallback)((async(e,t)=>{if(v.pagesLoaded[t])return v.pages[t];const a=await e.getPage(t+1);return w((e=>({...e,pages:{...e.pages,[t]:a},pagesLoaded:{...e.pagesLoaded,[t]:!0}}))),a}),[v.pages,v.pagesLoaded]),C=(0,c.useCallback)((async e=>{const t=m.current[`pdfCanvasRef-${e}`].current;if(!t)return;if(e>=3&&!(e=>{const t=e.getBoundingClientRect(),a=d.current?.scrollTop??0,n=a+(g.current?.clientHeight??0);return t.top>=a&&t.top<=n||t.bottom>=a&&t.bottom<=n||t.top<=a&&t.bottom>=n})(t))return;if(p.current[e])return;const a=await E(v.pdf,e),r=t.getContext("2d"),s=a.getViewport({scale:n});t.height=s.height,t.width=s.width;const c={canvasContext:r,viewport:s};await a.render(c).promise,p.current[e]=!0}),[E,v.pdf,n]);(0,c.useEffect)((()=>{const e=r()((()=>{if("success"===P)for(let e=0;e<v.numPages;e++)C(e)}),100);return d.current&&(g.current=d.current.parentElement,g.current?.addEventListener("scroll",e)),()=>{e.cancel(),g.current&&g.current.removeEventListener("scroll",e)}}),[v.numPages,C,P]);const R=(0,c.useRef)(n);(0,c.useEffect)((()=>{if(n!==R.current&&(R.current=n,p.current={},"success"===P))for(let e=0;e<v.numPages;e++)C(e)}),[v.numPages,C,n,P]);const b=(0,c.useRef)(P);if((0,c.useEffect)((()=>{if(b.current!==P&&"success"===P)for(let e=0;e<v.numPages;e++)C(e);b.current=P}),[v.numPages,C,P]),"loading"===P)return u().createElement("div",{ref:d,className:"view-image__loading"},u().createElement(o.A,null));if("fail"===P)return u().createElement(l.A,{fileInfo:t,fileUrl:a});const k=[];for(let e=0;e<v.numPages;e++)k.push(u().createElement("canvas",{ref:m.current[`pdfCanvasRef-${e}`],key:"previewpdfcanvas"+e})),e<v.numPages-1&&v.numPages>1&&k.push(u().createElement("div",{key:"previewpdfspacer"+e,className:"pdf-preview-spacer"}));return u().createElement("div",{ref:d,className:"post-code","data-testid":"pdf-container",onClick:i},k)}))}}]);
//# sourceMappingURL=5660.c322b1696eeb9cfaeb4e.js.map