@braineet/ui
Version:
Braineet design system
114 lines (110 loc) • 4.76 kB
JavaScript
"use strict";
exports.__esModule = true;
exports.UploadList = void 0;
var _react = _interopRequireDefault(require("react"));
var _reactSpring = require("react-spring");
var _ModalMediaViewer = _interopRequireDefault(require("../../modal-media-viewer/ModalMediaViewer"));
var _useUploadState = require("../useUploadState");
var _useUploadStore = require("../useUploadStore");
var _styles = require("./styles");
var _UploadItem = require("./UploadItem");
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["items"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _objectDestructuringEmpty(obj) { if (obj == null) throw new TypeError("Cannot destructure " + obj); }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
var UploadList = function UploadList(_ref) {
var state = _ref.state,
uploadItemStatus = _ref.uploadItemStatus,
transformItem = _ref.transformItem,
texts = _ref.texts;
var store = (0, _useUploadStore.useUploadStore)(state);
var items = store.items,
internalStore = _objectWithoutPropertiesLoose(store, _excluded);
var localItems = _react.default.useMemo(function () {
if (!transformItem) return items;
return items.map(transformItem);
}, [items, transformItem]);
var _React$useState = _react.default.useState(null),
mediaViewed = _React$useState[0],
setMediaViewed = _React$useState[1];
/** handles */
var handleOpenModalViewer = _react.default.useCallback(function (media) {
setMediaViewed(media);
}, []);
var handleCloseModalViewer = _react.default.useCallback(function () {
setMediaViewed(null);
}, []);
/** constants */
var enterStyles = function enterStyles(item) {
var _item$maxHeight, _item$maxHeight2, _item$maxHeight3;
var hasError = (item == null ? void 0 : item.status) === _useUploadState.UPLOAD_ITEM_STATUS.ERROR && (item == null ? void 0 : item.errorText);
var itemMaxHeightByStatus = {
default: (item == null ? void 0 : (_item$maxHeight = item.maxHeight) == null ? void 0 : _item$maxHeight.default) || 40,
uploading: (item == null ? void 0 : (_item$maxHeight2 = item.maxHeight) == null ? void 0 : _item$maxHeight2.uploading) || 40,
error: (item == null ? void 0 : (_item$maxHeight3 = item.maxHeight) == null ? void 0 : _item$maxHeight3.error) || 40
};
return {
maxHeight: (itemMaxHeightByStatus[item.status] || itemMaxHeightByStatus.default) + (hasError ? 18 : 0),
opacity: 1,
paddingBottom: 4,
overflow: 'hidden'
};
};
var leaveStyles = function leaveStyles() {
return {
maxHeight: 0,
opacity: 0,
paddingBottom: 0,
overflow: 'hidden'
};
};
var transitions = (0, _reactSpring.useTransition)(localItems, function (d) {
return d == null ? void 0 : d.uid;
}, {
initial: enterStyles,
from: leaveStyles,
leave: leaveStyles,
enter: enterStyles,
update: enterStyles,
config: function config(_, s) {
switch (s) {
case 'leave':
return {
tension: 300
};
default:
return {
tension: 170
};
}
}
});
return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_styles.UploadListContainer, {
as: "ul",
children: transitions.map(function (_ref2) {
var item = _ref2.item,
rest = _extends({}, (_objectDestructuringEmpty(_ref2.props), _ref2.props)),
s = _ref2.state;
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_UploadItem.UploadItem, _extends({
item: item,
state: s,
uploadItemStatus: uploadItemStatus,
style: _extends({
width: '100%',
boxSizing: 'content-box'
}, rest)
}, internalStore, {
onPreview: handleOpenModalViewer
}), item.uid);
})
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_ModalMediaViewer.default, _extends({
isOpen: !!mediaViewed,
onClose: handleCloseModalViewer,
texts: texts
}, mediaViewed))]
});
};
exports.UploadList = UploadList;