plugin-ai-chat-file-preview 1.0.0 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/client/index.js
CHANGED
|
@@ -7,4 +7,4 @@
|
|
|
7
7
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("@nocobase/plugin-ai/client"),require("@nocobase/client"),require("react"),require("@ant-design/icons"),require("antd")):"function"==typeof define&&define.amd?define("plugin-ai-chat-file-preview",["@nocobase/plugin-ai/client","@nocobase/client","react","@ant-design/icons","antd"],t):"object"==typeof exports?exports["plugin-ai-chat-file-preview"]=t(require("@nocobase/plugin-ai/client"),require("@nocobase/client"),require("react"),require("@ant-design/icons"),require("antd")):e["plugin-ai-chat-file-preview"]=t(e["@nocobase/plugin-ai/client"],e["@nocobase/client"],e.react,e["@ant-design/icons"],e.antd)}(self,function(e,t,n,r,o){return function(){var i,a,u,l,c,s,f={581:function(e){e.exports=function(e,t){return"undefined"!=typeof __deoptimization_sideEffect__&&__deoptimization_sideEffect__(e,t),t}},482:function(e){"use strict";e.exports=r},772:function(e){"use strict";e.exports=t},645:function(t){"use strict";t.exports=e},721:function(e){"use strict";e.exports=o},156:function(e){"use strict";e.exports=n}},d={};function p(e){var t=d[e];if(void 0!==t)return t.exports;var n=d[e]={exports:{}};return f[e](n,n.exports,p),n.exports}p.m=f,p.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return p.d(t,{a:t}),t},p.d=function(e,t){for(var n in t)p.o(t,n)&&!p.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},p.f={},p.e=function(e){return Promise.all(Object.keys(p.f).reduce(function(t,n){return p.f[n](e,t),t},[]))},p.u=function(e){return""+({395:"0ef9cbd6bccd7aff",463:"7e04271656f34237"})[e]+".js"},p.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||Function("return this")()}catch(e){if("object"==typeof window)return window}}(),p.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},i={},a="plugin-ai-chat-file-preview:",p.l=function(e,t,n,r){if(i[e])return void i[e].push(t);if(void 0!==n)for(var o,u,l=document.getElementsByTagName("script"),c=0;c<l.length;c++){var s=l[c];if(s.getAttribute("src")==e||s.getAttribute("data-webpack")==a+n){o=s;break}}o||(u=!0,(o=document.createElement("script")).charset="utf-8",o.timeout=120,p.nc&&o.setAttribute("nonce",p.nc),o.setAttribute("data-webpack",a+n),o.src=e),i[e]=[t];var f=function(t,n){o.onerror=o.onload=null,clearTimeout(d);var r=i[e];if(delete i[e],o.parentNode&&o.parentNode.removeChild(o),r&&r.forEach(function(e){return e(n)}),t)return t(n)},d=setTimeout(f.bind(null,void 0,{type:"timeout",target:o}),12e4);o.onerror=f.bind(null,o.onerror),o.onload=f.bind(null,o.onload),u&&document.head.appendChild(o)},p.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},(u=window.__webpack_public_path__||"/").endsWith("/")||(u+="/"),p.p=u+"static/plugins/plugin-ai-chat-file-preview/dist/client/",l={909:0},p.f.j=function(e,t){var n=p.o(l,e)?l[e]:void 0;if(0!==n)if(n)t.push(n[2]);else{var r=new Promise(function(t,r){n=l[e]=[t,r]});t.push(n[2]=r);var o=p.p+p.u(e),i=Error();p.l(o,function(t){if(p.o(l,e)&&(0!==(n=l[e])&&(l[e]=void 0),n)){var r=t&&("load"===t.type?"missing":t.type),o=t&&t.target&&t.target.src;i.message="Loading chunk "+e+" failed.\n("+r+": "+o+")",i.name="ChunkLoadError",i.type=r,i.request=o,n[1](i)}},"chunk-"+e,e)}},c=function(e,t){var n,r,o=t[0],i=t[1],a=t[2],u=0;if(o.some(function(e){return 0!==l[e]})){for(n in i)p.o(i,n)&&(p.m[n]=i[n]);a&&a(p)}for(e&&e(t);u<o.length;u++)r=o[u],p.o(l,r)&&l[r]&&l[r][0](),l[r]=0},(s=self.webpackChunkplugin_ai_chat_file_preview=self.webpackChunkplugin_ai_chat_file_preview||[]).forEach(c.bind(null,0)),s.push=c.bind(null,s.push.bind(s));var h={};return!function(){"use strict";p.r(h),p.d(h,{PluginAIChatFilePreviewClient:function(){return eh},default:function(){return ev}});var e=p(772),t=p(156),n=p.n(t),r=p(645),o=p(721),i=p(482);function a(e,t,n,r,o,i,a){try{var u=e[i](a),l=u.value}catch(e){n(e);return}u.done?t(l):Promise.resolve(l).then(r,o)}function u(e){return function(){var t=this,n=arguments;return new Promise(function(r,o){var i=e.apply(t,n);function u(e){a(i,r,o,u,l,"next",e)}function l(e){a(i,r,o,u,l,"throw",e)}u(void 0)})}}function l(e,t){var n,r,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(i){return function(u){var l=[i,u];if(n)throw TypeError("Generator is already executing.");for(;a;)try{if(n=1,r&&(o=2&l[0]?r.return:l[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,l[1])).done)return o;switch(r=0,o&&(l=[2&l[0],o.value]),l[0]){case 0:case 1:o=l;break;case 4:return a.label++,{value:l[1],done:!1};case 5:a.label++,r=l[1],l=[0];continue;case 7:l=a.ops.pop(),a.trys.pop();continue;default:if(!(o=(o=a.trys).length>0&&o[o.length-1])&&(6===l[0]||2===l[0])){a=0;continue}if(3===l[0]&&(!o||l[1]>o[0]&&l[1]<o[3])){a.label=l[1];break}if(6===l[0]&&a.label<o[1]){a.label=o[1],o=l;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(l);break}o[2]&&a.ops.pop(),a.trys.pop();continue}l=t.call(e,a)}catch(e){l=[6,e],r=0}finally{n=o=0}if(5&l[0])throw l[1];return{value:l[0]?l[1]:void 0,done:!0}}}}var c="blobs";function s(){return new Promise(function(e,t){var n=indexedDB.open("nb-ai-chat-file-preview",1);n.onupgradeneeded=function(){var e=n.result;e.objectStoreNames.contains(c)||e.createObjectStore(c)},n.onsuccess=function(){return e(n.result)},n.onerror=function(){return t(n.error)}})}function f(e,t){return"".concat(e,":").concat(t)}var d={get:function(e,t){return u(function(){var n;return l(this,function(r){switch(r.label){case 0:return[4,s()];case 1:return n=r.sent(),[2,new Promise(function(r,o){var i=n.transaction(c,"readonly").objectStore(c).get(f(e,t));i.onsuccess=function(){return r(i.result||null)},i.onerror=function(){return o(i.error)}})]}})})()},put:function(e,t,n){return u(function(){var r;return l(this,function(o){switch(o.label){case 0:return[4,s()];case 1:return r=o.sent(),[2,new Promise(function(o,i){var a=r.transaction(c,"readwrite");a.objectStore(c).put(n,f(e,t)),a.oncomplete=function(){return o()},a.onerror=function(){return i(a.error)}})]}})})()},delete:function(e,t){return u(function(){var n;return l(this,function(r){switch(r.label){case 0:return[4,s()];case 1:return n=r.sent(),[2,new Promise(function(r,o){var i=n.transaction(c,"readwrite");i.objectStore(c).delete(f(e,t)),i.oncomplete=function(){return r()},i.onerror=function(){return o(i.error)}})]}})})()},clearSession:function(e){return u(function(){var t;return l(this,function(n){switch(n.label){case 0:return[4,s()];case 1:return t=n.sent(),[2,new Promise(function(n,r){var o=t.transaction(c,"readwrite"),i=o.objectStore(c).openCursor();i.onsuccess=function(){var t=i.result;t&&("string"==typeof t.key&&t.key.startsWith("".concat(e,":"))&&t.delete(),t.continue())},o.oncomplete=function(){return n()},o.onerror=function(){return r(o.error)}})]}})})()},clearAll:function(){return u(function(){var e;return l(this,function(t){switch(t.label){case 0:return[4,s()];case 1:return e=t.sent(),[2,new Promise(function(t,n){var r=e.transaction(c,"readwrite");r.objectStore(c).clear(),r.oncomplete=function(){return t()},r.onerror=function(){return n(r.error)}})]}})})()}};function v(){var t=(0,e.useApp)();return{t:function(e){return t.i18n.t(e,{ns:"@nocobase/plugin-ai-chat-file-preview"})}}}var m=p(581);function y(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=Array(t);n<t;n++)r[n]=e[n];return r}function b(e,t,n,r,o,i,a){try{var u=e[i](a),l=u.value}catch(e){n(e);return}u.done?t(l):Promise.resolve(l).then(r,o)}function g(e){return function(){var t=this,n=arguments;return new Promise(function(r,o){var i=e.apply(t,n);function a(e){b(i,r,o,a,u,"next",e)}function u(e){b(i,r,o,a,u,"throw",e)}a(void 0)})}}function w(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var n,r,o=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=o){var i=[],a=!0,u=!1;try{for(o=o.call(e);!(a=(n=o.next()).done)&&(i.push(n.value),!t||i.length!==t);a=!0);}catch(e){u=!0,r=e}finally{try{a||null==o.return||o.return()}finally{if(u)throw r}}return i}}(e,t)||function(e,t){if(e){if("string"==typeof e)return y(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);if("Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n)return Array.from(n);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return y(e,t)}}(e,t)||function(){throw TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function x(e,t){var n,r,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(i){return function(u){var l=[i,u];if(n)throw TypeError("Generator is already executing.");for(;a;)try{if(n=1,r&&(o=2&l[0]?r.return:l[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,l[1])).done)return o;switch(r=0,o&&(l=[2&l[0],o.value]),l[0]){case 0:case 1:o=l;break;case 4:return a.label++,{value:l[1],done:!1};case 5:a.label++,r=l[1],l=[0];continue;case 7:l=a.ops.pop(),a.trys.pop();continue;default:if(!(o=(o=a.trys).length>0&&o[o.length-1])&&(6===l[0]||2===l[0])){a=0;continue}if(3===l[0]&&(!o||l[1]>o[0]&&l[1]<o[3])){a.label=l[1];break}if(6===l[0]&&a.label<o[1]){a.label=o[1],o=l;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(l);break}o[2]&&a.ops.pop(),a.trys.pop();continue}l=t.call(e,a)}catch(e){l=[6,e],r=0}finally{n=o=0}if(5&l[0])throw l[1];return{value:l[0]?l[1]:void 0,done:!0}}}}var S=["application/pdf"],E=["image/png","image/jpeg","image/gif","image/webp","image/svg+xml","image/bmp"],k=["text/plain","text/csv","text/html","text/css","text/javascript","application/json","application/xml","text/xml","text/yaml","application/x-yaml"],j=["application/vnd.openxmlformats-officedocument.wordprocessingml.document"],_=["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet","application/vnd.ms-excel"],C=["pdf"],O=["png","jpg","jpeg","gif","webp","svg","bmp"],P=["txt","csv","html","css","js","json","xml","log","md","yaml","yml","xaml"],I=["docx"],A=["xlsx","xls"];function L(e){var t="string"==typeof e?e:null==e?void 0:e.url;return t?t.startsWith("http://")||t.startsWith("https://")?t:"".concat(location.origin,"/").concat(t.replace(/^\//,"")):""}function R(e,t,n){if((null==e?void 0:e.mimetype)&&t.includes(e.mimetype))return!0;var r,o,i=-1!==(o=(r=("string"==typeof e?e:(null==e?void 0:e.extname)||(null==e?void 0:e.name)||(null==e?void 0:e.filename)||(null==e?void 0:e.url)||"").split("?")[0].split("#")[0]).lastIndexOf("."))?r.slice(o+1).toLowerCase().replace(/^\./,""):"";return!!i&&n.includes(i)}var F=function(e){return R(e,S,C)},T=function(e){return R(e,E,O)},U=function(e){return R(e,k,P)},B=function(e){return R(e,j,I)},q=function(e){return R(e,_,A)};function M(e,t){return D.apply(this,arguments)}function D(){return(D=g(function(e,t){var n,r;return x(this,function(o){switch(o.label){case 0:return n={},t&&(n.Authorization="Bearer ".concat(t)),[4,fetch(e,{method:"GET",headers:n,credentials:"include"})];case 1:if(!(r=o.sent()).ok)throw Error("Failed to fetch file: ".concat(r.status));return[2,r.blob()]}})})).apply(this,arguments)}function N(){return(N=g(function(e,t){var n,r;return x(this,function(o){switch(o.label){case 0:return n={},t&&(n.Authorization="Bearer ".concat(t)),[4,fetch(e,{method:"GET",headers:n,credentials:"include"})];case 1:if(!(r=o.sent()).ok)throw Error("Failed to fetch file: ".concat(r.status));return[2,r.text()]}})})).apply(this,arguments)}function z(){return(z=g(function(e,t){var n,r,o,i,a;return x(this,function(u){switch(u.label){case 0:if(!(n=L(e)))return[2];return[4,M(n,t)];case 1:return r=u.sent(),o=(null==e?void 0:e.title)&&(null==e?void 0:e.extname)?"".concat(e.title).concat(e.extname):(null==e?void 0:e.filename)||(null==e?void 0:e.name)||"download",(i=document.createElement("a")).href=a=URL.createObjectURL(r),i.download=o,document.body.appendChild(i),i.click(),document.body.removeChild(i),setTimeout(function(){return URL.revokeObjectURL(a)},1e3),[2]}})})).apply(this,arguments)}function W(e){var t=e.msg;return n().createElement("div",{style:{display:"flex",justifyContent:"center",alignItems:"center",height:"100%",width:"100%"}},n().createElement(o.Spin,{tip:t}))}function H(e){var t=e.msg;return n().createElement("div",{style:{padding:20,textAlign:"center",color:"#ff4d4f"}},t)}function G(e,n,r){var o=w((0,t.useState)(null),2),i=o[0],a=o[1],u=w((0,t.useState)(!0),2),l=u[0],c=u[1],s=w((0,t.useState)(null),2),f=s[0],p=s[1],h=(0,t.useRef)(null),v=(null==e?void 0:e.id)||(null==e?void 0:e.uid)||"",m=L(e);return(0,t.useEffect)(function(){var e=!1;if(!m){c(!1),p("No file URL");return}return c(!0),p(null),g(function(){var t,o,i,u,l;return x(this,function(s){switch(s.label){case 0:if(s.trys.push([0,4,,5]),!(r&&v))return[3,2];return[4,d.get(r,String(v))];case 1:if((t=s.sent())&&!e)return h.current=o=URL.createObjectURL(t),a(o),c(!1),[2];s.label=2;case 2:return[4,M(m,n)];case 3:if(i=s.sent(),e)return[2];return r&&v&&d.put(r,String(v),i).catch(function(){}),h.current=u=URL.createObjectURL(i),a(u),c(!1),[3,5];case 4:if(l=s.sent(),e)return[2];return p(l.message||"Failed to load"),c(!1),[3,5];case 5:return[2]}})})(),function(){e=!0,h.current&&(URL.revokeObjectURL(h.current),h.current=null)}},[m,n,r,v]),{blobUrl:i,loading:l,error:f}}function X(t){var r,o=t.file,i=t.sessionId,a=(0,e.useAPIClient)(),u=v().t,l=G(o,(null==(r=a.auth)?void 0:r.token)||"",i),c=l.blobUrl,s=l.loading,f=l.error;return s?n().createElement(W,{msg:u("Loading preview...")}):f||!c?n().createElement(H,{msg:u("Failed to load file preview")}):n().createElement("iframe",{src:c,width:"100%",height:"100%",style:{border:"none"}})}function $(t){var r,o=t.file,i=t.sessionId,a=(0,e.useAPIClient)(),u=v().t,l=G(o,(null==(r=a.auth)?void 0:r.token)||"",i),c=l.blobUrl,s=l.loading,f=l.error;return s?n().createElement(W,{msg:u("Loading preview...")}):f||!c?n().createElement(H,{msg:u("Failed to load file preview")}):n().createElement("img",{src:c,style:{maxWidth:"100%",maxHeight:"100%",objectFit:"contain"},alt:(null==o?void 0:o.filename)||""})}function J(r){var o,i,a,u,l,c,s,f,p,h,m,y,b=r.file,S=r.sessionId,E=(0,e.useAPIClient)(),k=v().t,j=(null==(y=E.auth)?void 0:y.token)||"",_=(i=(o=w((0,t.useState)(null),2))[0],a=o[1],l=(u=w((0,t.useState)(!0),2))[0],c=u[1],f=(s=w((0,t.useState)(null),2))[0],p=s[1],h=(null==b?void 0:b.id)||(null==b?void 0:b.uid)||"",m=L(b),(0,t.useEffect)(function(){var e=!1;if(!m){c(!1),p("No file URL");return}return c(!0),p(null),g(function(){var t,n,r,o;return x(this,function(i){switch(i.label){case 0:if(i.trys.push([0,5,,6]),!(S&&h))return[3,3];return[4,d.get(S,String(h))];case 1:if(!((t=i.sent())&&!e))return[3,3];return[4,t.text()];case 2:return a(i.sent()),c(!1),[2];case 3:return[4,function(e,t){return N.apply(this,arguments)}(m,j)];case 4:if(n=i.sent(),e)return[2];return S&&h&&(r=new Blob([n],{type:"text/plain"}),d.put(S,String(h),r).catch(function(){})),a(n),c(!1),[3,6];case 5:if(o=i.sent(),e)return[2];return p(o.message||"Failed to load"),c(!1),[3,6];case 6:return[2]}})})(),function(){e=!0}},[m,j,S,h]),{text:i,loading:l,error:f}),C=_.text,O=_.loading,P=_.error;return O?n().createElement(W,{msg:k("Loading preview...")}):P||null===C?n().createElement(H,{msg:k("Failed to load file preview")}):n().createElement("pre",{style:{width:"100%",height:"100%",overflow:"auto",padding:16,margin:0,fontSize:13,lineHeight:1.6,whiteSpace:"pre-wrap",wordWrap:"break-word",background:"#f5f5f5",border:"none"}},C)}function Y(r){var o,i=r.file,a=r.sessionId,u=(0,e.useAPIClient)(),l=v().t,c=(null==(o=u.auth)?void 0:o.token)||"",s=(0,t.useRef)(null),f=w((0,t.useState)(!0),2),h=f[0],y=f[1],b=w((0,t.useState)(null),2),S=b[0],E=b[1],k=L(i),j=(null==i?void 0:i.id)||(null==i?void 0:i.uid)||"";return(0,t.useEffect)(function(){var e=!1;if(!k||!s.current){y(!1),E("No file URL");return}return y(!0),E(null),g(function(){var t,n,r,o;return x(this,function(i){switch(i.label){case 0:if(i.trys.push([0,10,,11]),!(a&&j))return[3,5];return[4,d.get(a,String(j))];case 1:if(!((n=i.sent())&&!e))return[3,2];return t=n,[3,4];case 2:return[4,M(k,c)];case 3:if(t=i.sent(),e)return[2];d.put(a,String(j),t).catch(function(){}),i.label=4;case 4:return[3,7];case 5:return[4,M(k,c)];case 6:t=i.sent(),i.label=7;case 7:if(e||!s.current)return[2];return[4,m("imported_-1dm6lbf_component",p.e("395").then(p.bind(p,835)))];case 8:if(r=i.sent(),e||!s.current)return[2];return s.current.innerHTML="",[4,r.renderAsync(t,s.current,void 0,{className:"docx-preview-wrapper",inWrapper:!0,ignoreWidth:!1,ignoreHeight:!1,ignoreFonts:!1,breakPages:!0,ignoreLastRenderedPageBreak:!0,experimental:!1,trimXmlDeclaration:!0,useBase64URL:!0,renderHeaders:!0,renderFooters:!0,renderFootnotes:!0,renderEndnotes:!0})];case 9:return i.sent(),y(!1),[3,11];case 10:if(o=i.sent(),e)return[2];return E(o.message||"Failed to render DOCX"),y(!1),[3,11];case 11:return[2]}})})(),function(){e=!0}},[k,c,a,j]),n().createElement("div",{style:{width:"100%",height:"100%",position:"relative"}},h&&n().createElement(W,{msg:l("Loading preview...")}),S&&n().createElement(H,{msg:l("Failed to load file preview")}),n().createElement("div",{ref:s,style:{width:"100%",height:"100%",overflow:"auto",display:h||S?"none":"block"}}))}function K(r){var o,i=r.file,a=r.sessionId,u=(0,e.useAPIClient)(),l=v().t,c=(null==(o=u.auth)?void 0:o.token)||"",s=w((0,t.useState)(!0),2),f=s[0],h=s[1],y=w((0,t.useState)(null),2),b=y[0],S=y[1],E=w((0,t.useState)([]),2),k=E[0],j=E[1],_=w((0,t.useState)(""),2),C=_[0],O=_[1],P=w((0,t.useState)({}),2),I=P[0],A=P[1],R=L(i),F=(null==i?void 0:i.id)||(null==i?void 0:i.uid)||"";return(0,t.useEffect)(function(){var e=!1;if(!R){h(!1),S("No file URL");return}return h(!0),S(null),g(function(){var t,n,r,o,i,u,l,s,f,v,y,b,g,w;return x(this,function(x){switch(x.label){case 0:if(x.trys.push([0,10,,11]),!(a&&F))return[3,5];return[4,d.get(a,String(F))];case 1:if(!((n=x.sent())&&!e))return[3,2];return t=n,[3,4];case 2:return[4,M(R,c)];case 3:if(t=x.sent(),e)return[2];d.put(a,String(F),t).catch(function(){}),x.label=4;case 4:return[3,7];case 5:return[4,M(R,c)];case 6:t=x.sent(),x.label=7;case 7:if(e)return[2];return[4,m("imported_-1lj2ifg_component",p.e("463").then(p.bind(p,312)))];case 8:if(r=x.sent(),e)return[2];return[4,t.arrayBuffer()];case 9:if(o=x.sent(),e)return[2];u=(i=r.read(o,{type:"array"})).SheetNames,l={},s=!0,f=!1,v=void 0;try{for(y=u[Symbol.iterator]();!(s=(b=y.next()).done);s=!0)l[g=b.value]=r.utils.sheet_to_html(i.Sheets[g],{id:"xlsx-preview-table"})}catch(e){f=!0,v=e}finally{try{s||null==y.return||y.return()}finally{if(f)throw v}}if(e)return[2];return j(u),O(u[0]||""),A(l),h(!1),[3,11];case 10:if(w=x.sent(),e)return[2];return S(w.message||"Failed to render XLSX"),h(!1),[3,11];case 11:return[2]}})})(),function(){e=!0}},[R,c,a,F]),n().createElement("div",{style:{width:"100%",height:"100%",display:"flex",flexDirection:"column"}},f&&n().createElement(W,{msg:l("Loading preview...")}),b&&n().createElement(H,{msg:l("Failed to load file preview")}),!f&&!b&&n().createElement(n().Fragment,null,k.length>1&&n().createElement("div",{style:{display:"flex",gap:0,borderBottom:"1px solid #e8e8e8",background:"#fafafa",padding:"0 8px",flexShrink:0,overflowX:"auto"}},k.map(function(e){return n().createElement("button",{key:e,onClick:function(){return O(e)},style:{padding:"8px 16px",border:"none",borderBottom:C===e?"2px solid #1890ff":"2px solid transparent",background:C===e?"#fff":"transparent",color:C===e?"#1890ff":"#666",fontWeight:C===e?600:400,cursor:"pointer",fontSize:13,whiteSpace:"nowrap",transition:"all 0.2s"}},e)})),n().createElement("div",{style:{flex:1,overflow:"auto",padding:0},dangerouslySetInnerHTML:{__html:I[C]||""}}),n().createElement("style",null,"\n #xlsx-preview-table { border-collapse: collapse; width: 100%; font-size: 13px; }\n #xlsx-preview-table td, #xlsx-preview-table th {\n border: 1px solid #e8e8e8; padding: 6px 10px; text-align: left;\n max-width: 33vw; white-space: normal; word-break: break-word;\n }\n #xlsx-preview-table tr:first-child td, #xlsx-preview-table tr:first-child th {\n background: #fafafa; font-weight: 600; position: sticky; top: 0; z-index: 1;\n }\n #xlsx-preview-table tr:nth-child(even) { background: #fafafa; }\n #xlsx-preview-table tr:hover { background: #f0f7ff; }\n ")))}var Q=function(r){var a=r.open,u=r.file,l=r.sessionId,c=r.onClose,s=(0,e.useAPIClient)(),f=v().t,p=w((0,t.useState)(!1),2),h=p[0],m=p[1],y=(null==u?void 0:u.id)||(null==u?void 0:u.uid)||"",b=(0,t.useCallback)(g(function(){var e,t;return x(this,function(n){switch(n.label){case 0:if(!u)return[2];t=(null==(e=s.auth)?void 0:e.token)||"",m(!0),n.label=1;case 1:return n.trys.push([1,3,4,5]),[4,function(e,t){return z.apply(this,arguments)}(u,t)];case 2:return n.sent(),[3,5];case 3:return n.sent(),o.message.error(f("Failed to download file")),[3,5];case 4:return m(!1),[7];case 5:return[2]}})}),[u,s,f]),S=(0,t.useCallback)(g(function(){return x(this,function(e){switch(e.label){case 0:if(!l||!y)return[2];return[4,d.delete(l,String(y))];case 1:return e.sent(),o.message.success(f("Cache cleared")),[2]}})}),[l,y,f]),E=(0,t.useMemo)(function(){return u?F(u)?X:T(u)?$:U(u)?J:B(u)?Y:q(u)?K:null:null},[u]),k=null!=E,j=(null==u?void 0:u.title)&&(null==u?void 0:u.extname)?"".concat(u.title).concat(u.extname):(null==u?void 0:u.filename)||(null==u?void 0:u.name)||"File";return n().createElement(o.Modal,{open:a,title:j,onCancel:c,destroyOnClose:!0,footer:[l&&y?n().createElement(o.Button,{key:"clear-cache",icon:n().createElement(i.DeleteOutlined,null),onClick:S},f("Clear cache")):null,n().createElement(o.Button,{key:"download",icon:n().createElement(i.DownloadOutlined,null),onClick:b,loading:h},f("Download")),n().createElement(o.Button,{key:"close",onClick:c},f("Close"))].filter(Boolean),width:k?"85vw":520,centered:!0},n().createElement("div",{style:{maxWidth:"100%",maxHeight:k?"calc(100vh - 256px)":"auto",height:k?"70vh":"auto",width:"100%",background:"white",display:"flex",flexDirection:"column",justifyContent:"center",alignItems:"center",overflowY:"auto"}},k&&u?n().createElement(E,{file:u,sessionId:l}):n().createElement(o.Alert,{type:"info",style:{width:"100%"},description:f("This file type cannot be previewed. Click Download to save the file."),showIcon:!0})))};function V(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=Array(t);n<t;n++)r[n]=e[n];return r}function Z(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function ee(e){return(ee=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function et(e,t){return(et=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function en(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var n,r,o=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=o){var i=[],a=!0,u=!1;try{for(o=o.call(e);!(a=(n=o.next()).done)&&(i.push(n.value),!t||i.length!==t);a=!0);}catch(e){u=!0,r=e}finally{try{a||null==o.return||o.return()}finally{if(u)throw r}}return i}}(e,t)||function(e,t){if(e){if("string"==typeof e)return V(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);if("Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n)return Array.from(n);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return V(e,t)}}(e,t)||function(){throw TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function er(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(e){}return(er=function(){return!!e})()}function eo(e){return{id:e.id,uid:e.uid,url:e.url,filename:e.filename||e.name,name:e.name||e.filename,title:e.title,extname:e.extname,mimetype:e.mimetype,size:e.size}}var ei=function(o){var i=o.children,a=en((0,t.useState)(!1),2),u=a[0],l=a[1],c=en((0,t.useState)(null),2),s=c[0],f=c[1],p=en((0,t.useState)(""),2),h=p[0],v=p[1],m=(0,e.useAPIClient)(),y=r.useChatMessagesStore.use.messages(),b=r.useChatMessagesStore.use.attachments(),g=(0,t.useRef)(y),w=(0,t.useRef)(b);g.current=y,w.current=b;var x=(0,t.useRef)("");(0,t.useEffect)(function(){var e=m.axios.interceptors.request.use(function(e){var t=e.url||"";if(t.includes("aiConversations:getMessages")){var n=t.match(/sessionId=([^&]+)/);n&&(x.current=decodeURIComponent(n[1]))}if(t.includes("aiConversations:sendMessages")&&e.data)try{var r="string"==typeof e.data?JSON.parse(e.data):e.data;(null==r?void 0:r.sessionId)&&(x.current=r.sessionId)}catch(e){}return e});return function(){m.axios.interceptors.request.eject(e)}},[m]),(0,t.useEffect)(function(){var e=function(e){var t,n,r,o=e.target;if(!o)return;var i=o.closest('[class*="attachment-list-card"]');if(!(!i||!i.closest(".ant-layout")||i.classList.toString().includes("type-preview")||o.closest('[class*="-remove"]'))){var a=function(e,t,n){if(!e)return null;var r=!0,o=!1,i=void 0;try{for(var a,u=t[Symbol.iterator]();!(r=(a=u.next()).done);r=!0){var l=a.value,c=l.content||l,s=null==c?void 0:c.attachments;if(null==s?void 0:s.length){var f=!0,d=!1,p=void 0;try{for(var h,v=s[Symbol.iterator]();!(f=(h=v.next()).done);f=!0){var m=h.value;if((m.filename||m.name||"")===e||"".concat(m.title||"").concat(m.extname||"")===e)return eo(m)}}catch(e){d=!0,p=e}finally{try{f||null==v.return||v.return()}finally{if(d)throw p}}}}}catch(e){o=!0,i=e}finally{try{r||null==u.return||u.return()}finally{if(o)throw i}}var y=!0,b=!1,g=void 0;try{for(var w,x=(n||[])[Symbol.iterator]();!(y=(w=x.next()).done);y=!0){var S=w.value;if((S.filename||S.name||"")===e||"".concat(S.title||"").concat(S.extname||"")===e)return eo(S)}}catch(e){b=!0,g=e}finally{try{y||null==x.return||x.return()}finally{if(b)throw g}}return null}((n=i.querySelector('[class*="ellipsis-prefix"]'),r=i.querySelector('[class*="ellipsis-suffix"]'),n&&r?(n.textContent||"")+(r.textContent||""):(null==(t=i.textContent)?void 0:t.trim())||""),g.current,w.current);a&&(F(a)||T(a)||U(a)||B(a)||q(a))&&(e.preventDefault(),e.stopPropagation(),v(x.current||""),f(a),l(!0))}};return document.addEventListener("click",e,{capture:!0}),function(){return document.removeEventListener("click",e,{capture:!0})}},[]),(0,t.useEffect)(function(){var e=m.axios.interceptors.response.use(function(e){try{var t,n=(null==(t=e.config)?void 0:t.url)||"";if(n.includes("aiConversations:destroy")){var r=n.match(/filterByTk=([^&]+)/);r&&d.clearSession(decodeURIComponent(r[1])).catch(function(){})}}catch(e){}return e});return function(){m.axios.interceptors.response.eject(e)}},[m]);var S=(0,t.useCallback)(function(){l(!1),f(null)},[]);return n().createElement(n().Fragment,null,i,n().createElement(Q,{open:u,file:s,sessionId:h,onClose:S}))},ea=function(e){var t,n;if("function"!=typeof e&&null!==e)throw TypeError("Super expression must either be null or a function");function r(e){var t,n,o;if(!(this instanceof r))throw TypeError("Cannot call a class as a function");return n=r,o=[e],n=ee(n),(t=function(e,t){var n;if(t&&("object"==((n=t)&&"undefined"!=typeof Symbol&&n.constructor===Symbol?"symbol":typeof n)||"function"==typeof t))return t;if(void 0===e)throw ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(this,er()?Reflect.construct(n,o||[],ee(this).constructor):n.apply(this,o))).state={hasError:!1},t}return r.prototype=Object.create(e&&e.prototype,{constructor:{value:r,writable:!0,configurable:!0}}),e&&et(r,e),n=[{key:"getDerivedStateFromError",value:function(){return{hasError:!0}}}],t=[{key:"render",value:function(){return this.state.hasError,this.props.children}}],Z(r.prototype,t),n&&Z(r,n),r}(n().Component),eu=function(e){var t=e.children;return n().createElement(ea,null,n().createElement(ei,null,t))};function el(e,t,n,r,o,i,a){try{var u=e[i](a),l=u.value}catch(e){n(e);return}u.done?t(l):Promise.resolve(l).then(r,o)}function ec(e,t,n){return(ec=ep()?Reflect.construct:function(e,t,n){var r=[null];r.push.apply(r,t);var o=new(Function.bind.apply(e,r));return n&&ef(o,n.prototype),o}).apply(null,arguments)}function es(e){return(es=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function ef(e,t){return(ef=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function ed(e){var t="function"==typeof Map?new Map:void 0;return(ed=function(e){if(null===e||-1===Function.toString.call(e).indexOf("[native code]"))return e;if("function"!=typeof e)throw TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(e))return t.get(e);t.set(e,n)}function n(){return ec(e,arguments,es(this).constructor)}return n.prototype=Object.create(e.prototype,{constructor:{value:n,enumerable:!1,writable:!0,configurable:!0}}),ef(n,e)})(e)}function ep(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(e){}return(ep=function(){return!!e})()}var eh=function(e){var t;if("function"!=typeof e&&null!==e)throw TypeError("Super expression must either be null or a function");function n(){var e,t;if(!(this instanceof n))throw TypeError("Cannot call a class as a function");return e=n,t=arguments,e=es(e),function(e,t){var n;if(t&&("object"==((n=t)&&"undefined"!=typeof Symbol&&n.constructor===Symbol?"symbol":typeof n)||"function"==typeof t))return t;if(void 0===e)throw ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(this,ep()?Reflect.construct(e,t||[],es(this).constructor):e.apply(this,t))}return n.prototype=Object.create(e&&e.prototype,{constructor:{value:n,writable:!0,configurable:!0}}),e&&ef(n,e),t=[{key:"load",value:function(){var e,t=this;return(e=function(){return function(e,t){var n,r,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(i){return function(u){var l=[i,u];if(n)throw TypeError("Generator is already executing.");for(;a;)try{if(n=1,r&&(o=2&l[0]?r.return:l[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,l[1])).done)return o;switch(r=0,o&&(l=[2&l[0],o.value]),l[0]){case 0:case 1:o=l;break;case 4:return a.label++,{value:l[1],done:!1};case 5:a.label++,r=l[1],l=[0];continue;case 7:l=a.ops.pop(),a.trys.pop();continue;default:if(!(o=(o=a.trys).length>0&&o[o.length-1])&&(6===l[0]||2===l[0])){a=0;continue}if(3===l[0]&&(!o||l[1]>o[0]&&l[1]<o[3])){a.label=l[1];break}if(6===l[0]&&a.label<o[1]){a.label=o[1],o=l;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(l);break}o[2]&&a.ops.pop(),a.trys.pop();continue}l=t.call(e,a)}catch(e){l=[6,e],r=0}finally{n=o=0}if(5&l[0])throw l[1];return{value:l[0]?l[1]:void 0,done:!0}}}}(this,function(e){return t.app.use(eu),[2]})},function(){var t=this,n=arguments;return new Promise(function(r,o){var i=e.apply(t,n);function a(e){el(i,r,o,a,u,"next",e)}function u(e){el(i,r,o,a,u,"throw",e)}a(void 0)})})()}}],function(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}(n.prototype,t),n}(ed(e.Plugin)),ev=eh}(),h}()});
|
|
10
|
+
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("@nocobase/plugin-ai/client"),require("@nocobase/client"),require("react"),require("@ant-design/icons"),require("antd")):"function"==typeof define&&define.amd?define("plugin-ai-chat-file-preview",["@nocobase/plugin-ai/client","@nocobase/client","react","@ant-design/icons","antd"],t):"object"==typeof exports?exports["plugin-ai-chat-file-preview"]=t(require("@nocobase/plugin-ai/client"),require("@nocobase/client"),require("react"),require("@ant-design/icons"),require("antd")):e["plugin-ai-chat-file-preview"]=t(e["@nocobase/plugin-ai/client"],e["@nocobase/client"],e.react,e["@ant-design/icons"],e.antd)}(self,function(e,t,n,r,o){return function(){var i,a,u,l,c,s,f={581:function(e){e.exports=function(e,t){return"undefined"!=typeof __deoptimization_sideEffect__&&__deoptimization_sideEffect__(e,t),t}},482:function(e){"use strict";e.exports=r},772:function(e){"use strict";e.exports=t},645:function(t){"use strict";t.exports=e},721:function(e){"use strict";e.exports=o},156:function(e){"use strict";e.exports=n}},d={};function p(e){var t=d[e];if(void 0!==t)return t.exports;var n=d[e]={exports:{}};return f[e](n,n.exports,p),n.exports}p.m=f,p.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return p.d(t,{a:t}),t},p.d=function(e,t){for(var n in t)p.o(t,n)&&!p.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},p.f={},p.e=function(e){return Promise.all(Object.keys(p.f).reduce(function(t,n){return p.f[n](e,t),t},[]))},p.u=function(e){return""+({395:"0ef9cbd6bccd7aff",463:"7e04271656f34237"})[e]+".js"},p.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||Function("return this")()}catch(e){if("object"==typeof window)return window}}(),p.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},i={},a="plugin-ai-chat-file-preview:",p.l=function(e,t,n,r){if(i[e])return void i[e].push(t);if(void 0!==n)for(var o,u,l=document.getElementsByTagName("script"),c=0;c<l.length;c++){var s=l[c];if(s.getAttribute("src")==e||s.getAttribute("data-webpack")==a+n){o=s;break}}o||(u=!0,(o=document.createElement("script")).charset="utf-8",o.timeout=120,p.nc&&o.setAttribute("nonce",p.nc),o.setAttribute("data-webpack",a+n),o.src=e),i[e]=[t];var f=function(t,n){o.onerror=o.onload=null,clearTimeout(d);var r=i[e];if(delete i[e],o.parentNode&&o.parentNode.removeChild(o),r&&r.forEach(function(e){return e(n)}),t)return t(n)},d=setTimeout(f.bind(null,void 0,{type:"timeout",target:o}),12e4);o.onerror=f.bind(null,o.onerror),o.onload=f.bind(null,o.onload),u&&document.head.appendChild(o)},p.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},(u=window.__webpack_public_path__||"/").endsWith("/")||(u+="/"),p.p=u+"static/plugins/plugin-ai-chat-file-preview/dist/client/",l={909:0},p.f.j=function(e,t){var n=p.o(l,e)?l[e]:void 0;if(0!==n)if(n)t.push(n[2]);else{var r=new Promise(function(t,r){n=l[e]=[t,r]});t.push(n[2]=r);var o=p.p+p.u(e),i=Error();p.l(o,function(t){if(p.o(l,e)&&(0!==(n=l[e])&&(l[e]=void 0),n)){var r=t&&("load"===t.type?"missing":t.type),o=t&&t.target&&t.target.src;i.message="Loading chunk "+e+" failed.\n("+r+": "+o+")",i.name="ChunkLoadError",i.type=r,i.request=o,n[1](i)}},"chunk-"+e,e)}},c=function(e,t){var n,r,o=t[0],i=t[1],a=t[2],u=0;if(o.some(function(e){return 0!==l[e]})){for(n in i)p.o(i,n)&&(p.m[n]=i[n]);a&&a(p)}for(e&&e(t);u<o.length;u++)r=o[u],p.o(l,r)&&l[r]&&l[r][0](),l[r]=0},(s=self.webpackChunkplugin_ai_chat_file_preview=self.webpackChunkplugin_ai_chat_file_preview||[]).forEach(c.bind(null,0)),s.push=c.bind(null,s.push.bind(s));var v={};return!function(){"use strict";p.r(v),p.d(v,{PluginAIChatFilePreviewClient:function(){return eh},default:function(){return em}});var e=p(772),t=p(156),n=p.n(t),r=p(645),o=p(721),i=p(482);function a(e,t,n,r,o,i,a){try{var u=e[i](a),l=u.value}catch(e){n(e);return}u.done?t(l):Promise.resolve(l).then(r,o)}function u(e){return function(){var t=this,n=arguments;return new Promise(function(r,o){var i=e.apply(t,n);function u(e){a(i,r,o,u,l,"next",e)}function l(e){a(i,r,o,u,l,"throw",e)}u(void 0)})}}function l(e,t){var n,r,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(i){return function(u){var l=[i,u];if(n)throw TypeError("Generator is already executing.");for(;a;)try{if(n=1,r&&(o=2&l[0]?r.return:l[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,l[1])).done)return o;switch(r=0,o&&(l=[2&l[0],o.value]),l[0]){case 0:case 1:o=l;break;case 4:return a.label++,{value:l[1],done:!1};case 5:a.label++,r=l[1],l=[0];continue;case 7:l=a.ops.pop(),a.trys.pop();continue;default:if(!(o=(o=a.trys).length>0&&o[o.length-1])&&(6===l[0]||2===l[0])){a=0;continue}if(3===l[0]&&(!o||l[1]>o[0]&&l[1]<o[3])){a.label=l[1];break}if(6===l[0]&&a.label<o[1]){a.label=o[1],o=l;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(l);break}o[2]&&a.ops.pop(),a.trys.pop();continue}l=t.call(e,a)}catch(e){l=[6,e],r=0}finally{n=o=0}if(5&l[0])throw l[1];return{value:l[0]?l[1]:void 0,done:!0}}}}var c="blobs";function s(){return new Promise(function(e,t){var n=indexedDB.open("nb-ai-chat-file-preview",1);n.onupgradeneeded=function(){var e=n.result;e.objectStoreNames.contains(c)||e.createObjectStore(c)},n.onsuccess=function(){return e(n.result)},n.onerror=function(){return t(n.error)}})}function f(e,t){return"".concat(e,":").concat(t)}var d={get:function(e,t){return u(function(){var n;return l(this,function(r){switch(r.label){case 0:return[4,s()];case 1:return n=r.sent(),[2,new Promise(function(r,o){var i=n.transaction(c,"readonly").objectStore(c).get(f(e,t));i.onsuccess=function(){return r(i.result||null)},i.onerror=function(){return o(i.error)}})]}})})()},put:function(e,t,n){return u(function(){var r;return l(this,function(o){switch(o.label){case 0:return[4,s()];case 1:return r=o.sent(),[2,new Promise(function(o,i){var a=r.transaction(c,"readwrite");a.objectStore(c).put(n,f(e,t)),a.oncomplete=function(){return o()},a.onerror=function(){return i(a.error)}})]}})})()},delete:function(e,t){return u(function(){var n;return l(this,function(r){switch(r.label){case 0:return[4,s()];case 1:return n=r.sent(),[2,new Promise(function(r,o){var i=n.transaction(c,"readwrite");i.objectStore(c).delete(f(e,t)),i.oncomplete=function(){return r()},i.onerror=function(){return o(i.error)}})]}})})()},clearSession:function(e){return u(function(){var t;return l(this,function(n){switch(n.label){case 0:return[4,s()];case 1:return t=n.sent(),[2,new Promise(function(n,r){var o=t.transaction(c,"readwrite"),i=o.objectStore(c).openCursor();i.onsuccess=function(){var t=i.result;t&&("string"==typeof t.key&&t.key.startsWith("".concat(e,":"))&&t.delete(),t.continue())},o.oncomplete=function(){return n()},o.onerror=function(){return r(o.error)}})]}})})()},clearAll:function(){return u(function(){var e;return l(this,function(t){switch(t.label){case 0:return[4,s()];case 1:return e=t.sent(),[2,new Promise(function(t,n){var r=e.transaction(c,"readwrite");r.objectStore(c).clear(),r.oncomplete=function(){return t()},r.onerror=function(){return n(r.error)}})]}})})()}};function h(){var t=(0,e.useApp)();return{t:function(e){return t.i18n.t(e,{ns:"@nocobase/plugin-ai-chat-file-preview"})}}}var m=p(581);function y(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=Array(t);n<t;n++)r[n]=e[n];return r}function b(e,t,n,r,o,i,a){try{var u=e[i](a),l=u.value}catch(e){n(e);return}u.done?t(l):Promise.resolve(l).then(r,o)}function g(e){return function(){var t=this,n=arguments;return new Promise(function(r,o){var i=e.apply(t,n);function a(e){b(i,r,o,a,u,"next",e)}function u(e){b(i,r,o,a,u,"throw",e)}a(void 0)})}}function w(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var n,r,o=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=o){var i=[],a=!0,u=!1;try{for(o=o.call(e);!(a=(n=o.next()).done)&&(i.push(n.value),!t||i.length!==t);a=!0);}catch(e){u=!0,r=e}finally{try{a||null==o.return||o.return()}finally{if(u)throw r}}return i}}(e,t)||function(e,t){if(e){if("string"==typeof e)return y(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);if("Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n)return Array.from(n);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return y(e,t)}}(e,t)||function(){throw TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function x(e,t){var n,r,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(i){return function(u){var l=[i,u];if(n)throw TypeError("Generator is already executing.");for(;a;)try{if(n=1,r&&(o=2&l[0]?r.return:l[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,l[1])).done)return o;switch(r=0,o&&(l=[2&l[0],o.value]),l[0]){case 0:case 1:o=l;break;case 4:return a.label++,{value:l[1],done:!1};case 5:a.label++,r=l[1],l=[0];continue;case 7:l=a.ops.pop(),a.trys.pop();continue;default:if(!(o=(o=a.trys).length>0&&o[o.length-1])&&(6===l[0]||2===l[0])){a=0;continue}if(3===l[0]&&(!o||l[1]>o[0]&&l[1]<o[3])){a.label=l[1];break}if(6===l[0]&&a.label<o[1]){a.label=o[1],o=l;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(l);break}o[2]&&a.ops.pop(),a.trys.pop();continue}l=t.call(e,a)}catch(e){l=[6,e],r=0}finally{n=o=0}if(5&l[0])throw l[1];return{value:l[0]?l[1]:void 0,done:!0}}}}var S=["application/pdf"],E=["image/png","image/jpeg","image/gif","image/webp","image/svg+xml","image/bmp"],k=["text/plain","text/csv","text/html","text/css","text/javascript","application/json","application/xml","text/xml","text/yaml","application/x-yaml"],_=["application/vnd.openxmlformats-officedocument.wordprocessingml.document"],j=["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet","application/vnd.ms-excel"],C=["pdf"],O=["png","jpg","jpeg","gif","webp","svg","bmp"],P=["txt","csv","html","css","js","json","xml","log","md","yaml","yml","xaml"],L=["docx"],I=["xlsx","xls"];function A(e){var t="string"==typeof e?e:null==e?void 0:e.url;return t?t.startsWith("http://")||t.startsWith("https://")?t:"".concat(location.origin,"/").concat(t.replace(/^\//,"")):""}function R(e,t,n){if((null==e?void 0:e.mimetype)&&t.includes(e.mimetype))return!0;var r,o,i=-1!==(o=(r=("string"==typeof e?e:(null==e?void 0:e.extname)||(null==e?void 0:e.name)||(null==e?void 0:e.filename)||(null==e?void 0:e.url)||"").split("?")[0].split("#")[0]).lastIndexOf("."))?r.slice(o+1).toLowerCase().replace(/^\./,""):"";return!!i&&n.includes(i)}var T=function(e){return R(e,S,C)},F=function(e){return R(e,E,O)},U=function(e){return R(e,k,P)},B=function(e){return R(e,_,L)},z=function(e){return R(e,j,I)};function q(e,t){return M.apply(this,arguments)}function M(){return(M=g(function(e,t){return x(this,function(n){switch(n.label){case 0:return[4,e.axios.get(t,{responseType:"blob"})];case 1:return[2,n.sent().data]}})})).apply(this,arguments)}function D(){return(D=g(function(e,t,n){var r,o,i,a,u,l,c;return x(this,function(s){switch(s.label){case 0:if(r=A(e),o=String((null==e?void 0:e.id)||(null==e?void 0:e.uid)||r),i=(null==e?void 0:e.filename)||(null==e?void 0:e.name)||(null==e?void 0:e.title),a=null==e?void 0:e.size,!(i&&a))return[3,2];return[4,d.get("local_uploads","local_".concat(i,"_").concat(a)).catch(function(){return null})];case 1:if(u=s.sent())return[2,u];s.label=2;case 2:if(!(n&&o))return[3,4];return[4,d.get(n,o).catch(function(){return null})];case 3:if(l=s.sent())return[2,l];s.label=4;case 4:if(!r)throw Error("No valid URL");return[4,q(t,r)];case 5:return c=s.sent(),n&&o&&d.put(n,o,c).catch(function(){}),[2,c]}})})).apply(this,arguments)}function N(){return(N=g(function(e,t){var n,r,o,i;return x(this,function(a){switch(a.label){case 0:return[4,function(e,t,n){return D.apply(this,arguments)}(e,t,"")];case 1:return n=a.sent(),r=(null==e?void 0:e.title)&&(null==e?void 0:e.extname)?"".concat(e.title).concat(e.extname):(null==e?void 0:e.filename)||(null==e?void 0:e.name)||"download",(o=document.createElement("a")).href=i=URL.createObjectURL(n),o.download=r,document.body.appendChild(o),o.click(),document.body.removeChild(o),setTimeout(function(){return URL.revokeObjectURL(i)},1e3),[2]}})})).apply(this,arguments)}function W(e){var t=e.msg;return n().createElement("div",{style:{display:"flex",justifyContent:"center",alignItems:"center",height:"100%",width:"100%"}},n().createElement(o.Spin,{tip:t}))}function H(e){var t=e.msg;return n().createElement("div",{style:{padding:20,textAlign:"center",color:"#ff4d4f"}},t)}function X(e,n,r){var o=w((0,t.useState)(null),2),i=o[0],a=o[1],u=w((0,t.useState)(!0),2),l=u[0],c=u[1],s=w((0,t.useState)(null),2),f=s[0],p=s[1],v=(0,t.useRef)(null),h=(null==e?void 0:e.id)||(null==e?void 0:e.uid)||"",m=A(e);return(0,t.useEffect)(function(){var e=!1;if(!m){c(!1),p("No file URL");return}return c(!0),p(null),g(function(){var t,o,i,u,l;return x(this,function(s){switch(s.label){case 0:if(s.trys.push([0,4,,5]),!(r&&h))return[3,2];return[4,d.get(r,String(h))];case 1:if((t=s.sent())&&!e)return v.current=o=URL.createObjectURL(t),a(o),c(!1),[2];s.label=2;case 2:return[4,q(n,m)];case 3:if(i=s.sent(),e)return[2];return r&&h&&d.put(r,String(h),i).catch(function(){}),v.current=u=URL.createObjectURL(i),a(u),c(!1),[3,5];case 4:if(l=s.sent(),e)return[2];return p(l.message||"Failed to load"),c(!1),[3,5];case 5:return[2]}})})(),function(){e=!0,v.current&&(URL.revokeObjectURL(v.current),v.current=null)}},[m,n,r,h]),{blobUrl:i,loading:l,error:f}}function G(t){var r=t.file,o=t.sessionId,i=(0,e.useAPIClient)(),a=h().t,u=X(r,i,o),l=u.blobUrl,c=u.loading,s=u.error;return c?n().createElement(W,{msg:a("Loading preview...")}):s||!l?n().createElement(H,{msg:a("Failed to load file preview")}):n().createElement("iframe",{src:l,width:"100%",height:"100%",style:{border:"none"}})}function $(t){var r=t.file,o=t.sessionId,i=(0,e.useAPIClient)(),a=h().t,u=X(r,i,o),l=u.blobUrl,c=u.loading,s=u.error;return c?n().createElement(W,{msg:a("Loading preview...")}):s||!l?n().createElement(H,{msg:a("Failed to load file preview")}):n().createElement("img",{src:l,style:{maxWidth:"100%",maxHeight:"100%",objectFit:"contain"},alt:(null==r?void 0:r.filename)||""})}function J(r){var o,i,a,u,l,c,s,f,p,v,m,y=r.file,b=r.sessionId,S=(0,e.useAPIClient)(),E=h().t,k=(i=(o=w((0,t.useState)(null),2))[0],a=o[1],l=(u=w((0,t.useState)(!0),2))[0],c=u[1],f=(s=w((0,t.useState)(null),2))[0],p=s[1],v=(null==y?void 0:y.id)||(null==y?void 0:y.uid)||"",m=A(y),(0,t.useEffect)(function(){var e=!1;if(!m){c(!1),p("No file URL");return}return c(!0),p(null),g(function(){var t,n,r,o;return x(this,function(i){switch(i.label){case 0:if(i.trys.push([0,5,,6]),!(b&&v))return[3,3];return[4,d.get(b,String(v))];case 1:if(!((t=i.sent())&&!e))return[3,3];return[4,t.text()];case 2:return a(i.sent()),c(!1),[2];case 3:return[4,fetchFileAsText(S,m)];case 4:if(n=i.sent(),e)return[2];return b&&v&&(r=new Blob([n],{type:"text/plain"}),d.put(b,String(v),r).catch(function(){})),a(n),c(!1),[3,6];case 5:if(o=i.sent(),e)return[2];return p(o.message||"Failed to load"),c(!1),[3,6];case 6:return[2]}})})(),function(){e=!0}},[m,S,b,v]),{text:i,loading:l,error:f}),_=k.text,j=k.loading,C=k.error;return j?n().createElement(W,{msg:E("Loading preview...")}):C||null===_?n().createElement(H,{msg:E("Failed to load file preview")}):n().createElement("pre",{style:{width:"100%",height:"100%",overflow:"auto",padding:16,margin:0,fontSize:13,lineHeight:1.6,whiteSpace:"pre-wrap",wordWrap:"break-word",background:"#f5f5f5",border:"none"}},_)}function V(r){var o=r.file,i=r.sessionId,a=(0,e.useAPIClient)(),u=h().t,l=(0,t.useRef)(null),c=w((0,t.useState)(!0),2),s=c[0],f=c[1],v=w((0,t.useState)(null),2),y=v[0],b=v[1],S=A(o),E=(null==o?void 0:o.id)||(null==o?void 0:o.uid)||"";return(0,t.useEffect)(function(){var e=!1;if(!S||!l.current){f(!1),b("No file URL");return}return f(!0),b(null),g(function(){var t,n,r,o;return x(this,function(u){switch(u.label){case 0:if(u.trys.push([0,10,,11]),!(i&&E))return[3,5];return[4,d.get(i,String(E))];case 1:if(!((n=u.sent())&&!e))return[3,2];return t=n,[3,4];case 2:return[4,q(a,S)];case 3:if(t=u.sent(),e)return[2];d.put(i,String(E),t).catch(function(){}),u.label=4;case 4:return[3,7];case 5:return[4,q(a,S)];case 6:t=u.sent(),u.label=7;case 7:if(e||!l.current)return[2];return[4,m("imported_-1dm6lbf_component",p.e("395").then(p.bind(p,835)))];case 8:if(r=u.sent(),e||!l.current)return[2];return l.current.innerHTML="",[4,r.renderAsync(t,l.current,void 0,{className:"docx-preview-wrapper",inWrapper:!0,ignoreWidth:!1,ignoreHeight:!1,ignoreFonts:!1,breakPages:!0,ignoreLastRenderedPageBreak:!0,experimental:!1,trimXmlDeclaration:!0,useBase64URL:!0,renderHeaders:!0,renderFooters:!0,renderFootnotes:!0,renderEndnotes:!0})];case 9:return u.sent(),f(!1),[3,11];case 10:if(o=u.sent(),e)return[2];return b(o.message||"Failed to render DOCX"),f(!1),[3,11];case 11:return[2]}})})(),function(){e=!0}},[S,a,i,E]),n().createElement("div",{style:{width:"100%",height:"100%",position:"relative"}},s&&n().createElement(W,{msg:u("Loading preview...")}),y&&n().createElement(H,{msg:u("Failed to load file preview")}),n().createElement("div",{ref:l,style:{width:"100%",height:"100%",overflow:"auto",display:s||y?"none":"block"}}))}function Y(r){var o=r.file,i=r.sessionId,a=(0,e.useAPIClient)(),u=h().t,l=w((0,t.useState)(!0),2),c=l[0],s=l[1],f=w((0,t.useState)(null),2),v=f[0],y=f[1],b=w((0,t.useState)([]),2),S=b[0],E=b[1],k=w((0,t.useState)(""),2),_=k[0],j=k[1],C=w((0,t.useState)({}),2),O=C[0],P=C[1],L=A(o),I=(null==o?void 0:o.id)||(null==o?void 0:o.uid)||"";return(0,t.useEffect)(function(){var e=!1;if(!L){s(!1),y("No file URL");return}return s(!0),y(null),g(function(){var t,n,r,o,u,l,c,f,v,h,b,g,w,S;return x(this,function(x){switch(x.label){case 0:if(x.trys.push([0,10,,11]),!(i&&I))return[3,5];return[4,d.get(i,String(I))];case 1:if(!((n=x.sent())&&!e))return[3,2];return t=n,[3,4];case 2:return[4,q(a,L)];case 3:if(t=x.sent(),e)return[2];d.put(i,String(I),t).catch(function(){}),x.label=4;case 4:return[3,7];case 5:return[4,q(a,L)];case 6:t=x.sent(),x.label=7;case 7:if(e)return[2];return[4,m("imported_-1lj2ifg_component",p.e("463").then(p.bind(p,312)))];case 8:if(r=x.sent(),e)return[2];return[4,t.arrayBuffer()];case 9:if(o=x.sent(),e)return[2];l=(u=r.read(o,{type:"array"})).SheetNames,c={},f=!0,v=!1,h=void 0;try{for(b=l[Symbol.iterator]();!(f=(g=b.next()).done);f=!0)c[w=g.value]=r.utils.sheet_to_html(u.Sheets[w],{id:"xlsx-preview-table"})}catch(e){v=!0,h=e}finally{try{f||null==b.return||b.return()}finally{if(v)throw h}}if(e)return[2];return E(l),j(l[0]||""),P(c),s(!1),[3,11];case 10:if(S=x.sent(),e)return[2];return y(S.message||"Failed to render XLSX"),s(!1),[3,11];case 11:return[2]}})})(),function(){e=!0}},[L,a,i,I]),n().createElement("div",{style:{width:"100%",height:"100%",display:"flex",flexDirection:"column"}},c&&n().createElement(W,{msg:u("Loading preview...")}),v&&n().createElement(H,{msg:u("Failed to load file preview")}),!c&&!v&&n().createElement(n().Fragment,null,S.length>1&&n().createElement("div",{style:{display:"flex",gap:0,borderBottom:"1px solid #e8e8e8",background:"#fafafa",padding:"0 8px",flexShrink:0,overflowX:"auto"}},S.map(function(e){return n().createElement("button",{key:e,onClick:function(){return j(e)},style:{padding:"8px 16px",border:"none",borderBottom:_===e?"2px solid #1890ff":"2px solid transparent",background:_===e?"#fff":"transparent",color:_===e?"#1890ff":"#666",fontWeight:_===e?600:400,cursor:"pointer",fontSize:13,whiteSpace:"nowrap",transition:"all 0.2s"}},e)})),n().createElement("div",{style:{flex:1,overflow:"auto",padding:0},dangerouslySetInnerHTML:{__html:O[_]||""}}),n().createElement("style",null,"\n #xlsx-preview-table { border-collapse: collapse; width: 100%; font-size: 13px; }\n #xlsx-preview-table td, #xlsx-preview-table th {\n border: 1px solid #e8e8e8; padding: 6px 10px; text-align: left;\n max-width: 33vw; white-space: normal; word-break: break-word;\n }\n #xlsx-preview-table tr:first-child td, #xlsx-preview-table tr:first-child th {\n background: #fafafa; font-weight: 600; position: sticky; top: 0; z-index: 1;\n }\n #xlsx-preview-table tr:nth-child(even) { background: #fafafa; }\n #xlsx-preview-table tr:hover { background: #f0f7ff; }\n ")))}var K=function(r){var a=r.open,u=r.file,l=r.sessionId,c=r.onClose,s=(0,e.useAPIClient)(),f=h().t,p=w((0,t.useState)(!1),2),v=p[0],m=p[1],y=(null==u?void 0:u.id)||(null==u?void 0:u.uid)||"",b=(0,t.useCallback)(g(function(){return x(this,function(e){switch(e.label){case 0:if(!u)return[2];m(!0),e.label=1;case 1:return e.trys.push([1,3,4,5]),[4,function(e,t){return N.apply(this,arguments)}(u,s)];case 2:return e.sent(),[3,5];case 3:return e.sent(),o.message.error(f("Failed to download file")),[3,5];case 4:return m(!1),[7];case 5:return[2]}})}),[u,s,f]),S=(0,t.useCallback)(g(function(){return x(this,function(e){switch(e.label){case 0:if(!l||!y)return[2];return[4,d.delete(l,String(y))];case 1:return e.sent(),o.message.success(f("Cache cleared")),[2]}})}),[l,y,f]),E=(0,t.useMemo)(function(){return u?T(u)?G:F(u)?$:U(u)?J:B(u)?V:z(u)?Y:null:null},[u]),k=null!=E,_=(null==u?void 0:u.title)&&(null==u?void 0:u.extname)?"".concat(u.title).concat(u.extname):(null==u?void 0:u.filename)||(null==u?void 0:u.name)||"File";return n().createElement(o.Modal,{open:a,title:_,onCancel:c,destroyOnClose:!0,footer:[l&&y?n().createElement(o.Button,{key:"clear-cache",icon:n().createElement(i.DeleteOutlined,null),onClick:S},f("Clear cache")):null,n().createElement(o.Button,{key:"download",icon:n().createElement(i.DownloadOutlined,null),onClick:b,loading:v},f("Download")),n().createElement(o.Button,{key:"close",onClick:c},f("Close"))].filter(Boolean),width:k?"85vw":520,centered:!0},n().createElement("div",{style:{maxWidth:"100%",maxHeight:k?"calc(100vh - 256px)":"auto",height:k?"70vh":"auto",width:"100%",background:"white",display:"flex",flexDirection:"column",justifyContent:"center",alignItems:"center",overflowY:"auto"}},k&&u?n().createElement(E,{file:u,sessionId:l}):n().createElement(o.Alert,{type:"info",style:{width:"100%"},description:f("This file type cannot be previewed. Click Download to save the file."),showIcon:!0})))};function Q(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=Array(t);n<t;n++)r[n]=e[n];return r}function Z(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function ee(e){return(ee=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function et(e,t){return null!=t&&"undefined"!=typeof Symbol&&t[Symbol.hasInstance]?!!t[Symbol.hasInstance](e):e instanceof t}function en(e,t){return(en=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function er(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var n,r,o=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=o){var i=[],a=!0,u=!1;try{for(o=o.call(e);!(a=(n=o.next()).done)&&(i.push(n.value),!t||i.length!==t);a=!0);}catch(e){u=!0,r=e}finally{try{a||null==o.return||o.return()}finally{if(u)throw r}}return i}}(e,t)||function(e,t){if(e){if("string"==typeof e)return Q(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);if("Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n)return Array.from(n);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return Q(e,t)}}(e,t)||function(){throw TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function eo(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(e){}return(eo=function(){return!!e})()}function ei(e){return{id:e.id,uid:e.uid,url:e.url,filename:e.filename||e.name,name:e.name||e.filename,title:e.title,extname:e.extname,mimetype:e.mimetype,size:e.size}}var ea=function(o){var i=o.children,a=er((0,t.useState)(!1),2),u=a[0],l=a[1],c=er((0,t.useState)(null),2),s=c[0],f=c[1],p=er((0,t.useState)(""),2),v=p[0],h=p[1],m=(0,e.useAPIClient)(),y=r.useChatMessagesStore.use.messages(),b=r.useChatMessagesStore.use.attachments(),g=(0,t.useRef)(y),w=(0,t.useRef)(b);g.current=y,w.current=b;var x=(0,t.useRef)("");(0,t.useEffect)(function(){var e=m.axios.interceptors.request.use(function(e){var t=e.url||"";if(t.includes("aiConversations:getMessages")){var n=t.match(/sessionId=([^&]+)/);n&&(x.current=decodeURIComponent(n[1]))}if(t.includes("aiConversations:sendMessages")&&e.data)try{var r="string"==typeof e.data?JSON.parse(e.data):e.data;(null==r?void 0:r.sessionId)&&(x.current=r.sessionId)}catch(e){}return e});return function(){m.axios.interceptors.request.eject(e)}},[m]),(0,t.useEffect)(function(){var e=function(e){var t;(null==(t=e.dataTransfer)?void 0:t.files)&&Array.from(e.dataTransfer.files).forEach(function(e){e.name&&e.size&&d.put("local_uploads","local_".concat(e.name,"_").concat(e.size),e).catch(function(){})})},t=function(e){var t=e.target;(null==t?void 0:t.type)==="file"&&t.files&&Array.from(t.files).forEach(function(e){e.name&&e.size&&d.put("local_uploads","local_".concat(e.name,"_").concat(e.size),e).catch(function(){})})};return window.addEventListener("drop",e,!0),document.addEventListener("change",t,!0),function(){window.removeEventListener("drop",e,!0),document.removeEventListener("change",t,!0)}},[]),(0,t.useEffect)(function(){(null==b?void 0:b.length)&&b.forEach(function(e){var t=e.originFileObj||e;if(et(t,Blob)||et(t,File)){var n=e.name||e.filename||t.name,r=e.size||t.size;n&&r&&d.put("local_uploads","local_".concat(n,"_").concat(r),t).catch(function(){})}})},[b]),(0,t.useEffect)(function(){var e=document.createElement("style");return e.innerHTML='\n div[class*="attachment-list-card"], div[class*="attachments-list-item"] {\n position: relative !important;\n cursor: pointer !important;\n }\n /* Prevent pointer events on inner text and icons so the outer div receives the absolute click */\n div[class*="attachment-list-card"] a,\n div[class*="attachment-list-card"] [class*="-icon"],\n div[class*="attachment-list-card"] span {\n pointer-events: none !important;\n }\n /* Re-enable pointer events for the delete button specifically */\n div[class*="attachment-list-card"] [class*="-remove"],\n div[class*="attachment-list-card"] button,\n div[class*="attachment-list-card"] .ant-btn {\n pointer-events: auto !important;\n }\n /* Visual "Preview" badge at the top-left corner */\n div[class*="attachment-list-card"]::after, div[class*="attachments-list-item"]::after {\n content: \'\uD83D\uDC41️ Xem\';\n position: absolute;\n top: 0;\n left: 0;\n background: #1890ff;\n color: #fff;\n font-size: 10px;\n line-height: 1;\n padding: 4px 6px;\n border-top-left-radius: 4px;\n border-bottom-right-radius: 8px;\n z-index: 10;\n pointer-events: none;\n box-shadow: 1px 1px 4px rgba(0,0,0,0.15);\n }\n ',document.head.appendChild(e),function(){e.remove()}},[]),(0,t.useEffect)(function(){var e=function(e){var t,n,r,o=e.target;if(!o||"function"!=typeof o.closest)return;var i=o.closest('[class*="attachment-list-card"]');if(!(!i||o.closest('[class*="-remove"]')||o.closest(".ant-btn"))){var a=(n=i.querySelector('[class*="ellipsis-prefix"]'),r=i.querySelector('[class*="ellipsis-suffix"]'),n&&r?(n.textContent||"")+(r.textContent||""):(null==(t=i.textContent)?void 0:t.trim())||""),u=i.querySelectorAll("a"),c="";u.forEach(function(e){e.href&&(c=e.href)});var s=function(e,t,n){if(!e)return null;var r=!0,o=!1,i=void 0;try{for(var a,u=t[Symbol.iterator]();!(r=(a=u.next()).done);r=!0){var l=a.value,c=l.content||l,s=null==c?void 0:c.attachments;if(null==s?void 0:s.length){var f=!0,d=!1,p=void 0;try{for(var v,h=s[Symbol.iterator]();!(f=(v=h.next()).done);f=!0){var m=v.value;if((m.filename||m.name||"")===e||"".concat(m.title||"").concat(m.extname||"")===e)return ei(m)}}catch(e){d=!0,p=e}finally{try{f||null==h.return||h.return()}finally{if(d)throw p}}}}}catch(e){o=!0,i=e}finally{try{r||null==u.return||u.return()}finally{if(o)throw i}}var y=!0,b=!1,g=void 0;try{for(var w,x=(n||[])[Symbol.iterator]();!(y=(w=x.next()).done);y=!0){var S=w.value;if((S.filename||S.name||"")===e||"".concat(S.title||"").concat(S.extname||"")===e)return ei(S)}}catch(e){b=!0,g=e}finally{try{y||null==x.return||x.return()}finally{if(b)throw g}}return null}(a,g.current,w.current)||function(e,t,n){if(!e)return null;var r=function(e,t){return!!e&&!!t&&e.split("?")[0].replace(location.origin,"").replace(/^\//,"")===t.split("?")[0].replace(location.origin,"").replace(/^\//,"")},o=!0,i=!1,a=void 0;try{for(var u,l=t[Symbol.iterator]();!(o=(u=l.next()).done);o=!0){var c=u.value,s=c.content||c,f=null==s?void 0:s.attachments;if(null==f?void 0:f.length){var d=!0,p=!1,v=void 0;try{for(var h,m=f[Symbol.iterator]();!(d=(h=m.next()).done);d=!0){var y=h.value;if(r(y.url,e)||r(y.preview,e))return ei(y)}}catch(e){p=!0,v=e}finally{try{d||null==m.return||m.return()}finally{if(p)throw v}}}}}catch(e){i=!0,a=e}finally{try{o||null==l.return||l.return()}finally{if(i)throw a}}var b=!0,g=!1,w=void 0;try{for(var x,S=(n||[])[Symbol.iterator]();!(b=(x=S.next()).done);b=!0){var E=x.value;if(r(E.url,e)||r(E.preview,e))return ei(E)}}catch(e){g=!0,w=e}finally{try{b||null==S.return||S.return()}finally{if(g)throw w}}return null}(c,g.current,w.current);s&&(T(s)||F(s)||U(s)||B(s)||z(s))&&(e.preventDefault(),e.stopPropagation(),h(x.current||""),f(s),l(!0))}};return document.addEventListener("click",e,{capture:!0}),function(){return document.removeEventListener("click",e,{capture:!0})}},[]),(0,t.useEffect)(function(){var e=m.axios.interceptors.response.use(function(e){try{var t,n=(null==(t=e.config)?void 0:t.url)||"";if(n.includes("aiConversations:destroy")){var r=n.match(/filterByTk=([^&]+)/);r&&d.clearSession(decodeURIComponent(r[1])).catch(function(){})}}catch(e){}return e});return function(){m.axios.interceptors.response.eject(e)}},[m]);var S=(0,t.useCallback)(function(){l(!1),f(null)},[]);return n().createElement(n().Fragment,null,i,n().createElement(K,{open:u,file:s,sessionId:v,onClose:S}))},eu=function(e){var t,n;if("function"!=typeof e&&null!==e)throw TypeError("Super expression must either be null or a function");function r(e){var t,n,o;if(!(this instanceof r))throw TypeError("Cannot call a class as a function");return n=r,o=[e],n=ee(n),(t=function(e,t){var n;if(t&&("object"==((n=t)&&"undefined"!=typeof Symbol&&n.constructor===Symbol?"symbol":typeof n)||"function"==typeof t))return t;if(void 0===e)throw ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(this,eo()?Reflect.construct(n,o||[],ee(this).constructor):n.apply(this,o))).state={hasError:!1},t}return r.prototype=Object.create(e&&e.prototype,{constructor:{value:r,writable:!0,configurable:!0}}),e&&en(r,e),n=[{key:"getDerivedStateFromError",value:function(){return{hasError:!0}}}],t=[{key:"render",value:function(){return this.state.hasError,this.props.children}}],Z(r.prototype,t),n&&Z(r,n),r}(n().Component),el=function(e){var t=e.children;return n().createElement(eu,null,n().createElement(ea,null,t))};function ec(e,t,n,r,o,i,a){try{var u=e[i](a),l=u.value}catch(e){n(e);return}u.done?t(l):Promise.resolve(l).then(r,o)}function es(e,t,n){return(es=ev()?Reflect.construct:function(e,t,n){var r=[null];r.push.apply(r,t);var o=new(Function.bind.apply(e,r));return n&&ed(o,n.prototype),o}).apply(null,arguments)}function ef(e){return(ef=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function ed(e,t){return(ed=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function ep(e){var t="function"==typeof Map?new Map:void 0;return(ep=function(e){if(null===e||-1===Function.toString.call(e).indexOf("[native code]"))return e;if("function"!=typeof e)throw TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(e))return t.get(e);t.set(e,n)}function n(){return es(e,arguments,ef(this).constructor)}return n.prototype=Object.create(e.prototype,{constructor:{value:n,enumerable:!1,writable:!0,configurable:!0}}),ed(n,e)})(e)}function ev(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(e){}return(ev=function(){return!!e})()}var eh=function(e){var t;if("function"!=typeof e&&null!==e)throw TypeError("Super expression must either be null or a function");function n(){var e,t;if(!(this instanceof n))throw TypeError("Cannot call a class as a function");return e=n,t=arguments,e=ef(e),function(e,t){var n;if(t&&("object"==((n=t)&&"undefined"!=typeof Symbol&&n.constructor===Symbol?"symbol":typeof n)||"function"==typeof t))return t;if(void 0===e)throw ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(this,ev()?Reflect.construct(e,t||[],ef(this).constructor):e.apply(this,t))}return n.prototype=Object.create(e&&e.prototype,{constructor:{value:n,writable:!0,configurable:!0}}),e&&ed(n,e),t=[{key:"load",value:function(){var e,t=this;return(e=function(){return function(e,t){var n,r,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(i){return function(u){var l=[i,u];if(n)throw TypeError("Generator is already executing.");for(;a;)try{if(n=1,r&&(o=2&l[0]?r.return:l[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,l[1])).done)return o;switch(r=0,o&&(l=[2&l[0],o.value]),l[0]){case 0:case 1:o=l;break;case 4:return a.label++,{value:l[1],done:!1};case 5:a.label++,r=l[1],l=[0];continue;case 7:l=a.ops.pop(),a.trys.pop();continue;default:if(!(o=(o=a.trys).length>0&&o[o.length-1])&&(6===l[0]||2===l[0])){a=0;continue}if(3===l[0]&&(!o||l[1]>o[0]&&l[1]<o[3])){a.label=l[1];break}if(6===l[0]&&a.label<o[1]){a.label=o[1],o=l;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(l);break}o[2]&&a.ops.pop(),a.trys.pop();continue}l=t.call(e,a)}catch(e){l=[6,e],r=0}finally{n=o=0}if(5&l[0])throw l[1];return{value:l[0]?l[1]:void 0,done:!0}}}}(this,function(e){return t.app.use(el),[2]})},function(){var t=this,n=arguments;return new Promise(function(r,o){var i=e.apply(t,n);function a(e){ec(i,r,o,a,u,"next",e)}function u(e){ec(i,r,o,a,u,"throw",e)}a(void 0)})})()}}],function(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}(n.prototype,t),n}(ep(e.Plugin)),em=eh}(),v}()});
|
package/package.json
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
"description": "Preview uploaded file attachments directly in AI Employee chat with in-browser caching.",
|
|
7
7
|
"description.vi-VN": "Xem trước file đính kèm đã tải lên trực tiếp trong chat AI Employee với bộ nhớ đệm trình duyệt.",
|
|
8
8
|
"description.zh-CN": "在AI员工聊天中直接预览上传的文件附件,支持浏览器缓存。",
|
|
9
|
-
"version": "1.0.
|
|
9
|
+
"version": "1.0.3",
|
|
10
10
|
"main": "dist/server/index.js",
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"xlsx": "^0.18.5",
|
|
@@ -72,6 +72,31 @@ function findFileByDisplayName(displayName: string, messages: any[], pendingAtta
|
|
|
72
72
|
return null;
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
+
/**
|
|
76
|
+
* Find file metadata matching the extracted URL
|
|
77
|
+
*/
|
|
78
|
+
function findFileByUrl(url: string, messages: any[], pendingAttachments: any[]): PreviewFile | null {
|
|
79
|
+
if (!url) return null;
|
|
80
|
+
const matchUrl = (u1: string, u2: string) => {
|
|
81
|
+
if (!u1 || !u2) return false;
|
|
82
|
+
const clean1 = u1.split('?')[0].replace(location.origin, '').replace(/^\//, '');
|
|
83
|
+
const clean2 = u2.split('?')[0].replace(location.origin, '').replace(/^\//, '');
|
|
84
|
+
return clean1 === clean2;
|
|
85
|
+
};
|
|
86
|
+
for (const msg of messages) {
|
|
87
|
+
const content = msg.content || msg;
|
|
88
|
+
const attachments = content?.attachments;
|
|
89
|
+
if (!attachments?.length) continue;
|
|
90
|
+
for (const att of attachments) {
|
|
91
|
+
if (matchUrl(att.url, url) || matchUrl(att.preview, url)) return attToPreviewFile(att);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
for (const att of pendingAttachments || []) {
|
|
95
|
+
if (matchUrl(att.url, url) || matchUrl(att.preview, url)) return attToPreviewFile(att);
|
|
96
|
+
}
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
|
|
75
100
|
/**
|
|
76
101
|
* Inner component that reads from plugin-ai's zustand stores via hooks.
|
|
77
102
|
* Uses refs to make latest state available inside the DOM click handler.
|
|
@@ -127,28 +152,118 @@ const ChatFilePreviewInner: React.FC<{ children: React.ReactNode }> = ({ childre
|
|
|
127
152
|
};
|
|
128
153
|
}, [apiClient]);
|
|
129
154
|
|
|
155
|
+
// Track global drop and input change events to intercept file object selection
|
|
156
|
+
useEffect(() => {
|
|
157
|
+
const handleDrop = (e: DragEvent) => {
|
|
158
|
+
if (e.dataTransfer?.files) {
|
|
159
|
+
Array.from(e.dataTransfer.files).forEach((f) => {
|
|
160
|
+
if (f.name && f.size) {
|
|
161
|
+
SessionBlobCache.put('local_uploads', `local_${f.name}_${f.size}`, f).catch(() => {});
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
const handleChange = (e: Event) => {
|
|
168
|
+
const target = e.target as HTMLInputElement;
|
|
169
|
+
if (target?.type === 'file' && target.files) {
|
|
170
|
+
Array.from(target.files).forEach((f) => {
|
|
171
|
+
if (f.name && f.size) {
|
|
172
|
+
SessionBlobCache.put('local_uploads', `local_${f.name}_${f.size}`, f).catch(() => {});
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
window.addEventListener('drop', handleDrop, true);
|
|
179
|
+
document.addEventListener('change', handleChange, true);
|
|
180
|
+
return () => {
|
|
181
|
+
window.removeEventListener('drop', handleDrop, true);
|
|
182
|
+
document.removeEventListener('change', handleChange, true);
|
|
183
|
+
};
|
|
184
|
+
}, []);
|
|
185
|
+
|
|
186
|
+
// Intercept antd upload origin files via Zustand state store
|
|
187
|
+
useEffect(() => {
|
|
188
|
+
if (!pendingAttachments?.length) return;
|
|
189
|
+
pendingAttachments.forEach((att: any) => {
|
|
190
|
+
const fileObj = att.originFileObj || att;
|
|
191
|
+
if (fileObj instanceof Blob || fileObj instanceof File) {
|
|
192
|
+
const name = att.name || att.filename || fileObj.name;
|
|
193
|
+
const size = att.size || fileObj.size;
|
|
194
|
+
if (name && size) {
|
|
195
|
+
SessionBlobCache.put('local_uploads', `local_${name}_${size}`, fileObj).catch(() => {});
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
}, [pendingAttachments]);
|
|
200
|
+
|
|
201
|
+
// Inject global CSS for explicit UI and native z-index click interception for child texts/tags
|
|
202
|
+
useEffect(() => {
|
|
203
|
+
const style = document.createElement('style');
|
|
204
|
+
style.innerHTML = `
|
|
205
|
+
div[class*="attachment-list-card"], div[class*="attachments-list-item"] {
|
|
206
|
+
position: relative !important;
|
|
207
|
+
cursor: pointer !important;
|
|
208
|
+
}
|
|
209
|
+
/* Prevent pointer events on inner text and icons so the outer div receives the absolute click */
|
|
210
|
+
div[class*="attachment-list-card"] a,
|
|
211
|
+
div[class*="attachment-list-card"] [class*="-icon"],
|
|
212
|
+
div[class*="attachment-list-card"] span {
|
|
213
|
+
pointer-events: none !important;
|
|
214
|
+
}
|
|
215
|
+
/* Re-enable pointer events for the delete button specifically */
|
|
216
|
+
div[class*="attachment-list-card"] [class*="-remove"],
|
|
217
|
+
div[class*="attachment-list-card"] button,
|
|
218
|
+
div[class*="attachment-list-card"] .ant-btn {
|
|
219
|
+
pointer-events: auto !important;
|
|
220
|
+
}
|
|
221
|
+
/* Visual "Preview" badge at the top-left corner */
|
|
222
|
+
div[class*="attachment-list-card"]::after, div[class*="attachments-list-item"]::after {
|
|
223
|
+
content: '👁️ Xem';
|
|
224
|
+
position: absolute;
|
|
225
|
+
top: 0;
|
|
226
|
+
left: 0;
|
|
227
|
+
background: #1890ff;
|
|
228
|
+
color: #fff;
|
|
229
|
+
font-size: 10px;
|
|
230
|
+
line-height: 1;
|
|
231
|
+
padding: 4px 6px;
|
|
232
|
+
border-top-left-radius: 4px;
|
|
233
|
+
border-bottom-right-radius: 8px;
|
|
234
|
+
z-index: 10;
|
|
235
|
+
pointer-events: none;
|
|
236
|
+
box-shadow: 1px 1px 4px rgba(0,0,0,0.15);
|
|
237
|
+
}
|
|
238
|
+
`;
|
|
239
|
+
document.head.appendChild(style);
|
|
240
|
+
return () => {
|
|
241
|
+
style.remove();
|
|
242
|
+
};
|
|
243
|
+
}, []);
|
|
244
|
+
|
|
130
245
|
// Click interceptor: capture clicks on FileCard elements
|
|
131
246
|
useEffect(() => {
|
|
132
247
|
const handler = (e: MouseEvent) => {
|
|
133
|
-
const
|
|
134
|
-
if (!
|
|
248
|
+
const el = e.target as Element;
|
|
249
|
+
if (!el || typeof el.closest !== 'function') return;
|
|
135
250
|
|
|
136
251
|
// Find closest FileCard element — uses ant-design/x class pattern
|
|
137
|
-
const cardEl =
|
|
252
|
+
const cardEl = el.closest('[class*="attachment-list-card"]') as HTMLElement;
|
|
138
253
|
if (!cardEl) return;
|
|
139
254
|
|
|
140
|
-
// Ensure it's within a chatbox-like container
|
|
141
|
-
const chatContainer = cardEl.closest('.ant-layout');
|
|
142
|
-
if (!chatContainer) return;
|
|
143
|
-
|
|
144
|
-
// Skip image cards (antd Image component handles its own preview)
|
|
145
|
-
if (cardEl.classList.toString().includes('type-preview')) return;
|
|
146
|
-
|
|
147
255
|
// Skip remove button clicks
|
|
148
|
-
if (
|
|
256
|
+
if (el.closest('[class*="-remove"]') || el.closest('.ant-btn')) return;
|
|
149
257
|
|
|
150
258
|
const displayName = getDisplayNameFromCard(cardEl);
|
|
151
|
-
const
|
|
259
|
+
const urlNodes = cardEl.querySelectorAll('a');
|
|
260
|
+
let fallbackUrl = '';
|
|
261
|
+
urlNodes.forEach((node) => {
|
|
262
|
+
if (node.href) fallbackUrl = node.href;
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
const file = findFileByDisplayName(displayName, messagesRef.current, pendingAttachmentsRef.current) ||
|
|
266
|
+
findFileByUrl(fallbackUrl, messagesRef.current, pendingAttachmentsRef.current);
|
|
152
267
|
|
|
153
268
|
if (!file) return;
|
|
154
269
|
if (!isPreviewableFile(file)) return;
|
|
@@ -76,30 +76,40 @@ export function isPreviewableUrl(url: string): boolean {
|
|
|
76
76
|
return isPreviewableFile({ url });
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
const headers: Record<string, string> = {};
|
|
83
|
-
if (token) headers['Authorization'] = `Bearer ${token}`;
|
|
84
|
-
const res = await fetch(url, { method: 'GET', headers, credentials: 'include' });
|
|
85
|
-
if (!res.ok) throw new Error(`Failed to fetch file: ${res.status}`);
|
|
86
|
-
return res.blob();
|
|
79
|
+
async function fetchFileAsBlob(apiClient: any, url: string): Promise<Blob> {
|
|
80
|
+
const res = await apiClient.axios.get(url, { responseType: 'blob' });
|
|
81
|
+
return res.data;
|
|
87
82
|
}
|
|
88
83
|
|
|
89
|
-
async function
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
84
|
+
async function resolveFileBlob(file: any, apiClient: any, sessionId: string): Promise<Blob> {
|
|
85
|
+
const fileUrl = resolveFileUrl(file);
|
|
86
|
+
const fileId = String(file?.id || file?.uid || fileUrl);
|
|
87
|
+
const name = file?.filename || file?.name || file?.title;
|
|
88
|
+
const size = file?.size;
|
|
89
|
+
|
|
90
|
+
if (name && size) {
|
|
91
|
+
const localBlob = await SessionBlobCache.get('local_uploads', `local_${name}_${size}`).catch(() => null);
|
|
92
|
+
if (localBlob) return localBlob;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (sessionId && fileId) {
|
|
96
|
+
const sessionBlob = await SessionBlobCache.get(sessionId, fileId).catch(() => null);
|
|
97
|
+
if (sessionBlob) return sessionBlob;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (!fileUrl) throw new Error('No valid URL');
|
|
101
|
+
const serverBlob = await fetchFileAsBlob(apiClient, fileUrl);
|
|
102
|
+
|
|
103
|
+
if (sessionId && fileId) {
|
|
104
|
+
SessionBlobCache.put(sessionId, fileId, serverBlob).catch(() => {});
|
|
105
|
+
}
|
|
106
|
+
return serverBlob;
|
|
95
107
|
}
|
|
96
108
|
|
|
97
109
|
// ─── Download helper ───────────────────────────────────────────────
|
|
98
110
|
|
|
99
|
-
async function downloadFileWithAuth(file: any,
|
|
100
|
-
const
|
|
101
|
-
if (!url) return;
|
|
102
|
-
const blob = await fetchFileAsBlob(url, token);
|
|
111
|
+
async function downloadFileWithAuth(file: any, apiClient: any) {
|
|
112
|
+
const blob = await resolveFileBlob(file, apiClient, '');
|
|
103
113
|
const name = file?.title && file?.extname ? `${file.title}${file.extname}` : file?.filename || file?.name || 'download';
|
|
104
114
|
const a = document.createElement('a');
|
|
105
115
|
const objectUrl = URL.createObjectURL(blob);
|
|
@@ -127,7 +137,7 @@ function ErrorMsg({ msg }: { msg: string }) {
|
|
|
127
137
|
|
|
128
138
|
// ─── Hook: load blob with session cache ────────────────────────────
|
|
129
139
|
|
|
130
|
-
function useCachedBlobUrl(file: any,
|
|
140
|
+
function useCachedBlobUrl(file: any, apiClient: any, sessionId: string) {
|
|
131
141
|
const [blobUrl, setBlobUrl] = useState<string | null>(null);
|
|
132
142
|
const [loading, setLoading] = useState(true);
|
|
133
143
|
const [error, setError] = useState<string | null>(null);
|
|
@@ -162,7 +172,7 @@ function useCachedBlobUrl(file: any, token: string, sessionId: string) {
|
|
|
162
172
|
}
|
|
163
173
|
|
|
164
174
|
// Fetch from server
|
|
165
|
-
const blob = await fetchFileAsBlob(
|
|
175
|
+
const blob = await fetchFileAsBlob(apiClient, fileUrl);
|
|
166
176
|
if (cancelled) return;
|
|
167
177
|
|
|
168
178
|
// Cache to IndexedDB
|
|
@@ -188,12 +198,12 @@ function useCachedBlobUrl(file: any, token: string, sessionId: string) {
|
|
|
188
198
|
blobUrlRef.current = null;
|
|
189
199
|
}
|
|
190
200
|
};
|
|
191
|
-
}, [fileUrl,
|
|
201
|
+
}, [fileUrl, apiClient, sessionId, fileId]);
|
|
192
202
|
|
|
193
203
|
return { blobUrl, loading, error };
|
|
194
204
|
}
|
|
195
205
|
|
|
196
|
-
function useCachedTextContent(file: any,
|
|
206
|
+
function useCachedTextContent(file: any, apiClient: any, sessionId: string) {
|
|
197
207
|
const [text, setText] = useState<string | null>(null);
|
|
198
208
|
const [loading, setLoading] = useState(true);
|
|
199
209
|
const [error, setError] = useState<string | null>(null);
|
|
@@ -225,7 +235,7 @@ function useCachedTextContent(file: any, token: string, sessionId: string) {
|
|
|
225
235
|
}
|
|
226
236
|
}
|
|
227
237
|
|
|
228
|
-
const content = await fetchFileAsText(
|
|
238
|
+
const content = await fetchFileAsText(apiClient, fileUrl);
|
|
229
239
|
if (cancelled) return;
|
|
230
240
|
|
|
231
241
|
// Cache as blob
|
|
@@ -246,7 +256,7 @@ function useCachedTextContent(file: any, token: string, sessionId: string) {
|
|
|
246
256
|
return () => {
|
|
247
257
|
cancelled = true;
|
|
248
258
|
};
|
|
249
|
-
}, [fileUrl,
|
|
259
|
+
}, [fileUrl, apiClient, sessionId, fileId]);
|
|
250
260
|
|
|
251
261
|
return { text, loading, error };
|
|
252
262
|
}
|
|
@@ -256,8 +266,7 @@ function useCachedTextContent(file: any, token: string, sessionId: string) {
|
|
|
256
266
|
function PdfPreviewer({ file, sessionId }: { file: any; sessionId: string }) {
|
|
257
267
|
const apiClient = useAPIClient();
|
|
258
268
|
const { t } = useTranslation();
|
|
259
|
-
const
|
|
260
|
-
const { blobUrl, loading, error } = useCachedBlobUrl(file, token, sessionId);
|
|
269
|
+
const { blobUrl, loading, error } = useCachedBlobUrl(file, apiClient, sessionId);
|
|
261
270
|
|
|
262
271
|
if (loading) return <LoadingIndicator msg={t('Loading preview...')} />;
|
|
263
272
|
if (error || !blobUrl) return <ErrorMsg msg={t('Failed to load file preview')} />;
|
|
@@ -267,8 +276,7 @@ function PdfPreviewer({ file, sessionId }: { file: any; sessionId: string }) {
|
|
|
267
276
|
function ImagePreviewer({ file, sessionId }: { file: any; sessionId: string }) {
|
|
268
277
|
const apiClient = useAPIClient();
|
|
269
278
|
const { t } = useTranslation();
|
|
270
|
-
const
|
|
271
|
-
const { blobUrl, loading, error } = useCachedBlobUrl(file, token, sessionId);
|
|
279
|
+
const { blobUrl, loading, error } = useCachedBlobUrl(file, apiClient, sessionId);
|
|
272
280
|
|
|
273
281
|
if (loading) return <LoadingIndicator msg={t('Loading preview...')} />;
|
|
274
282
|
if (error || !blobUrl) return <ErrorMsg msg={t('Failed to load file preview')} />;
|
|
@@ -278,8 +286,7 @@ function ImagePreviewer({ file, sessionId }: { file: any; sessionId: string }) {
|
|
|
278
286
|
function TextPreviewer({ file, sessionId }: { file: any; sessionId: string }) {
|
|
279
287
|
const apiClient = useAPIClient();
|
|
280
288
|
const { t } = useTranslation();
|
|
281
|
-
const
|
|
282
|
-
const { text, loading, error } = useCachedTextContent(file, token, sessionId);
|
|
289
|
+
const { text, loading, error } = useCachedTextContent(file, apiClient, sessionId);
|
|
283
290
|
|
|
284
291
|
if (loading) return <LoadingIndicator msg={t('Loading preview...')} />;
|
|
285
292
|
if (error || text === null) return <ErrorMsg msg={t('Failed to load file preview')} />;
|
|
@@ -307,7 +314,6 @@ function TextPreviewer({ file, sessionId }: { file: any; sessionId: string }) {
|
|
|
307
314
|
function DocxPreviewer({ file, sessionId }: { file: any; sessionId: string }) {
|
|
308
315
|
const apiClient = useAPIClient();
|
|
309
316
|
const { t } = useTranslation();
|
|
310
|
-
const token = apiClient.auth?.token || '';
|
|
311
317
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
312
318
|
const [loading, setLoading] = useState(true);
|
|
313
319
|
const [error, setError] = useState<string | null>(null);
|
|
@@ -334,12 +340,12 @@ function DocxPreviewer({ file, sessionId }: { file: any; sessionId: string }) {
|
|
|
334
340
|
if (cached && !cancelled) {
|
|
335
341
|
blob = cached;
|
|
336
342
|
} else {
|
|
337
|
-
blob = await fetchFileAsBlob(
|
|
343
|
+
blob = await fetchFileAsBlob(apiClient, fileUrl);
|
|
338
344
|
if (cancelled) return;
|
|
339
345
|
SessionBlobCache.put(sessionId, String(fileId), blob).catch(() => {});
|
|
340
346
|
}
|
|
341
347
|
} else {
|
|
342
|
-
blob = await fetchFileAsBlob(
|
|
348
|
+
blob = await fetchFileAsBlob(apiClient, fileUrl);
|
|
343
349
|
}
|
|
344
350
|
|
|
345
351
|
if (cancelled || !containerRef.current) return;
|
|
@@ -373,7 +379,7 @@ function DocxPreviewer({ file, sessionId }: { file: any; sessionId: string }) {
|
|
|
373
379
|
return () => {
|
|
374
380
|
cancelled = true;
|
|
375
381
|
};
|
|
376
|
-
}, [fileUrl,
|
|
382
|
+
}, [fileUrl, apiClient, sessionId, fileId]);
|
|
377
383
|
|
|
378
384
|
return (
|
|
379
385
|
<div style={{ width: '100%', height: '100%', position: 'relative' }}>
|
|
@@ -395,7 +401,6 @@ function DocxPreviewer({ file, sessionId }: { file: any; sessionId: string }) {
|
|
|
395
401
|
function XlsxPreviewer({ file, sessionId }: { file: any; sessionId: string }) {
|
|
396
402
|
const apiClient = useAPIClient();
|
|
397
403
|
const { t } = useTranslation();
|
|
398
|
-
const token = apiClient.auth?.token || '';
|
|
399
404
|
const [loading, setLoading] = useState(true);
|
|
400
405
|
const [error, setError] = useState<string | null>(null);
|
|
401
406
|
const [sheetNames, setSheetNames] = useState<string[]>([]);
|
|
@@ -424,12 +429,12 @@ function XlsxPreviewer({ file, sessionId }: { file: any; sessionId: string }) {
|
|
|
424
429
|
if (cached && !cancelled) {
|
|
425
430
|
blob = cached;
|
|
426
431
|
} else {
|
|
427
|
-
blob = await fetchFileAsBlob(
|
|
432
|
+
blob = await fetchFileAsBlob(apiClient, fileUrl);
|
|
428
433
|
if (cancelled) return;
|
|
429
434
|
SessionBlobCache.put(sessionId, String(fileId), blob).catch(() => {});
|
|
430
435
|
}
|
|
431
436
|
} else {
|
|
432
|
-
blob = await fetchFileAsBlob(
|
|
437
|
+
blob = await fetchFileAsBlob(apiClient, fileUrl);
|
|
433
438
|
}
|
|
434
439
|
|
|
435
440
|
if (cancelled) return;
|
|
@@ -458,7 +463,7 @@ function XlsxPreviewer({ file, sessionId }: { file: any; sessionId: string }) {
|
|
|
458
463
|
return () => {
|
|
459
464
|
cancelled = true;
|
|
460
465
|
};
|
|
461
|
-
}, [fileUrl,
|
|
466
|
+
}, [fileUrl, apiClient, sessionId, fileId]);
|
|
462
467
|
|
|
463
468
|
return (
|
|
464
469
|
<div style={{ width: '100%', height: '100%', display: 'flex', flexDirection: 'column' }}>
|
|
@@ -552,10 +557,9 @@ export const PreviewModal: React.FC<PreviewModalProps> = ({ open, file, sessionI
|
|
|
552
557
|
|
|
553
558
|
const onDownload = useCallback(async () => {
|
|
554
559
|
if (!file) return;
|
|
555
|
-
const token = apiClient.auth?.token || '';
|
|
556
560
|
setDownloading(true);
|
|
557
561
|
try {
|
|
558
|
-
await downloadFileWithAuth(file,
|
|
562
|
+
await downloadFileWithAuth(file, apiClient);
|
|
559
563
|
} catch {
|
|
560
564
|
message.error(t('Failed to download file'));
|
|
561
565
|
} finally {
|