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