infinity-forge
Version:
70 lines • 3.98 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useRenderedFile = useRenderedFile;
var jsx_runtime_1 = require("react/jsx-runtime");
var react_1 = require("react");
var ui_1 = require("../../../../../ui/index.js");
function useRenderedFile(props) {
var _a, _b, _c;
var _d = (0, react_1.useState)(null), downloadUrl = _d[0], setDownloadUrl = _d[1];
var components = (0, ui_1.useComponentsFile)(props);
function format(name) {
var fileNameLimit = 10;
var splited = name === null || name === void 0 ? void 0 : name.split('.');
var hasExtension = Array.isArray(splited) ? splited.length > 1 : 0;
var fileName = hasExtension
? splited.slice(0, -1).join('.').substring(0, fileNameLimit)
: name === null || name === void 0 ? void 0 : name.substring(0, fileNameLimit);
return {
extension: hasExtension ? splited.pop() : '',
name: hasExtension ? "".concat(fileName, ".").concat(splited.pop()) : fileName,
};
}
var fileName = format((_a = props === null || props === void 0 ? void 0 : props.file) === null || _a === void 0 ? void 0 : _a.url);
var Image = function () {
var _a, _b, _c;
switch (fileName.extension || ((_a = props.file) === null || _a === void 0 ? void 0 : _a.fileType)) {
case 'application/pdf':
return (0, jsx_runtime_1.jsx)(ui_1.Icon, { name: 'IconPdf', color: '#f52421' });
case 'pdf':
return (0, jsx_runtime_1.jsx)(ui_1.Icon, { name: 'IconPdf', color: '#f52421' });
case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
return (0, jsx_runtime_1.jsx)(ui_1.Icon, { name: 'IconExcel' });
case 'xlsx':
return (0, jsx_runtime_1.jsx)(ui_1.Icon, { name: 'IconExcel' });
case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
return (0, jsx_runtime_1.jsx)(ui_1.Icon, { name: 'IconWord' });
case 'doc':
return (0, jsx_runtime_1.jsx)(ui_1.Icon, { name: 'IconWord' });
case 'docx':
return (0, jsx_runtime_1.jsx)(ui_1.Icon, { name: 'IconWord' });
default:
return (0, jsx_runtime_1.jsx)("img", { style: { height: '100%', width: '100%' }, src: ((_b = props === null || props === void 0 ? void 0 : props.file) === null || _b === void 0 ? void 0 : _b.url) || ((_c = props === null || props === void 0 ? void 0 : props.file) === null || _c === void 0 ? void 0 : _c.defaultImage) });
}
};
(0, react_1.useEffect)(function () {
var _a;
if (props.isLocalFile) {
fetch((_a = props === null || props === void 0 ? void 0 : props.file) === null || _a === void 0 ? void 0 : _a.url)
.then(function (response) { return response.blob(); })
.then(function (blob) {
var url = window.URL.createObjectURL(blob);
setDownloadUrl(url);
})
.catch(function (error) { return console.error('Error fetching file:', error); });
}
}, [(_b = props === null || props === void 0 ? void 0 : props.file) === null || _b === void 0 ? void 0 : _b.url, props.isLocalFile]);
return __assign({ Image: Image, fileName: ((_c = props.file) === null || _c === void 0 ? void 0 : _c.title) || (fileName === null || fileName === void 0 ? void 0 : fileName.name), downloadUrl: downloadUrl }, components);
}
//# sourceMappingURL=use-rendered-file.js.map