// External Modules import * as React from 'react'; import {connect} from 'react-redux'; import {Dispatch} from 'redux'; // Types import {IAttachment} from 'superdesk-api'; import {superdeskApi} from '../../apis'; import {IAssetItem, ISetItem, SET_STATE} from '../../interfaces'; import {IApplicationState} from '../../store'; // Redux Actions & Selectors import {getAssets} from '../../store/assets/selectors'; import {loadAssetsByIds} from '../../store/assets/actions'; // UI import {Icon, Label, IconButton} from 'superdesk-ui-framework/react'; import { ListItem, ListItemColumn, ListItemRow, ListItemActionMenu, } from '../../ui/list'; // Utils import {getIconTypeFromMimetype, getHumanReadableFileSize} from '../../utils/ui'; interface IProps { files: Array; assets: Dictionary; sets: Dictionary; readOnly: boolean; editFile: (file: IAttachment) => void; download: (file: IAttachment) => void; removeFile: (file: IAttachment) => void; loadAssetsByIds(ids: Array): Promise; } interface IState { loading: boolean; } const mapStateToProps = (state: IApplicationState) => ({ assets: getAssets(state), }); const mapDispatchToProps = (dispatch: Dispatch) => ({ loadAssetsByIds: (ids: Array) => dispatch(loadAssetsByIds(ids)), }); class SamsAttachmentsListComponent extends React.Component { constructor(props: IProps) { super(props); this.state = { loading: false, }; this.renderFile = this.renderFile.bind(this); } componentDidMount() { this.loadAssets(); } componentDidUpdate(prevProps: Readonly) { if (prevProps.files.length !== this.props.files.length) { this.loadAssets(); } } loadAssets() { this.setState({loading: true}); const {getMediaId} = superdeskApi.entities.attachment; const mediaIds = this.props.files.map( (file) => getMediaId(file), ); this.props.loadAssetsByIds(mediaIds) .then(() => { this.setState({loading: false}); }); } getAttachmentSet(file: IAttachment): ISetItem | undefined { const mediaId = superdeskApi.entities.attachment.getMediaId(file); const asset = this.props.assets?.[mediaId]; return this.props.sets?.[asset?.set_id]; } renderFile(file: IAttachment) { const {gettext} = superdeskApi.localization; const set = this.getAttachmentSet(file); const canEdit = this.state.loading === false && set?.state === SET_STATE.USABLE; return (

{file.title}

{file.filename} {`(${getHumanReadableFileSize(file.media.length)})`}
{file.description}
{file.internal === false ? null : ( )} {set?.state !== SET_STATE.DISABLED ? null : ( )}
this.props.download(file)} icon="download" /> {this.props.readOnly === true ? null : ( {canEdit === false ? null : ( this.props.editFile(file)} icon="pencil" /> )} this.props.removeFile(file)} icon="trash" /> )}
); } render() { return (
{this.state.loading === false ? null : (
)} {this.props.files.length === 0 ? null : (
    {this.props.files.map(this.renderFile)}
)}
); } } export const SamsAttachmentsList = connect( mapStateToProps, mapDispatchToProps, )(SamsAttachmentsListComponent);