Files
SCM-Manager/scm-ui/ui-webapp/src/repos/sources/containers/Content.tsx

225 lines
5.8 KiB
TypeScript
Raw Normal View History

import React from 'react';
import { connect } from 'react-redux';
import { translate } from 'react-i18next';
import classNames from 'classnames';
import styled from 'styled-components';
import { ExtensionPoint } from '@scm-manager/ui-extensions';
import { File, Repository } from '@scm-manager/ui-types';
import {
DateFromNow,
ErrorNotification,
FileSize,
Icon,
} from '@scm-manager/ui-components';
import { getSources } from '../modules/sources';
import FileButtonAddons from '../components/content/FileButtonAddons';
import SourcesView from './SourcesView';
import HistoryView from './HistoryView';
2018-10-15 16:45:54 +02:00
type Props = {
loading: boolean;
file: File;
repository: Repository;
revision: string;
path: string;
// context props
t: (p: string) => string;
2018-10-15 16:45:54 +02:00
};
type State = {
collapsed: boolean;
showHistory: boolean;
errorFromExtension?: Error;
2018-10-15 16:45:54 +02:00
};
2019-10-16 17:28:06 +02:00
const VCenteredChild = styled.div`
align-items: center;
`;
const RightMarginIcon = styled(Icon)`
margin-right: 0.5em;
`;
const RightMarginFileButtonAddons = styled(FileButtonAddons)`
margin-right: 0.5em;
`;
const LighterGreyBackgroundPanelBlock = styled.div`
background-color: #fbfbfb;
`;
const LighterGreyBackgroundTable = styled.table`
background-color: #fbfbfb;
`;
2018-10-29 09:59:04 +01:00
2018-10-15 16:45:54 +02:00
class Content extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
collapsed: true,
showHistory: false,
2018-10-15 16:45:54 +02:00
};
}
2018-10-29 16:45:32 +01:00
toggleCollapse = () => {
this.setState(prevState => ({
collapsed: !prevState.collapsed,
2018-10-29 16:45:32 +01:00
}));
};
setShowHistoryState(showHistory: boolean) {
this.setState({
...this.state,
showHistory,
});
}
handleExtensionError = (error: Error) => {
this.setState({
errorFromExtension: error,
});
};
2018-10-29 09:32:30 +01:00
showHeader() {
const { file, revision } = this.props;
const { showHistory, collapsed } = this.state;
const icon = collapsed ? 'angle-right' : 'angle-down';
2018-10-29 09:32:30 +01:00
2018-11-28 09:28:45 +01:00
const selector = file._links.history ? (
<RightMarginFileButtonAddons
2018-11-28 09:28:45 +01:00
file={file}
historyIsSelected={showHistory}
showHistory={(changeShowHistory: boolean) =>
this.setShowHistoryState(changeShowHistory)
}
/>
) : null;
2018-10-29 09:32:30 +01:00
return (
<span className="has-cursor-pointer">
<VCenteredChild className={classNames('media', 'is-flex')}>
<div className="media-content" onClick={this.toggleCollapse}>
<RightMarginIcon name={icon} color="inherit" />
2018-12-19 11:07:39 +01:00
<span className="is-word-break">{file.name}</span>
2018-10-29 16:45:32 +01:00
</div>
<div className="buttons is-grouped">
{selector}
2019-09-12 14:44:42 +02:00
<ExtensionPoint
name="repos.sources.content.actionbar"
props={{
file,
revision,
handleExtensionError: this.handleExtensionError,
2019-09-12 14:44:42 +02:00
}}
renderAll={true}
/>
</div>
</VCenteredChild>
2018-10-29 16:45:32 +01:00
</span>
2018-10-29 09:32:30 +01:00
);
}
2018-10-29 16:45:32 +01:00
showMoreInformation() {
const collapsed = this.state.collapsed;
const { file, revision, t, repository } = this.props;
2018-11-01 09:19:29 +01:00
const date = <DateFromNow date={file.lastModified} />;
const description = file.description ? (
<p>
{file.description.split('\n').map((item, key) => {
2018-11-01 09:19:29 +01:00
return (
<span key={key}>
{item}
<br />
</span>
);
})}
</p>
) : null;
const fileSize = file.directory ? '' : <FileSize bytes={file.length} />;
2018-10-29 16:45:32 +01:00
if (!collapsed) {
return (
<LighterGreyBackgroundPanelBlock className="panel-block">
<LighterGreyBackgroundTable className="table">
2018-11-01 09:19:29 +01:00
<tbody>
<tr>
<td>{t('sources.content.path')}</td>
2018-12-19 11:07:39 +01:00
<td className="is-word-break">{file.path}</td>
2018-11-01 09:19:29 +01:00
</tr>
<tr>
<td>{t('sources.content.branch')}</td>
2018-12-19 11:07:39 +01:00
<td className="is-word-break">{revision}</td>
2018-11-01 09:19:29 +01:00
</tr>
<tr>
<td>{t('sources.content.size')}</td>
2018-11-21 16:55:13 +01:00
<td>{fileSize}</td>
</tr>
<tr>
<td>{t('sources.content.lastModified')}</td>
2018-11-01 10:58:47 +01:00
<td>{date}</td>
2018-11-01 09:19:29 +01:00
</tr>
<tr>
<td>{t('sources.content.description')}</td>
2018-12-19 11:07:39 +01:00
<td className="is-word-break">{description}</td>
2018-11-01 09:19:29 +01:00
</tr>
2019-02-21 17:08:23 +01:00
<ExtensionPoint
name="repos.content.metadata"
2019-02-28 17:15:20 +01:00
renderAll={true}
props={{
file,
repository,
revision,
}}
2019-02-28 17:15:20 +01:00
/>
2018-11-01 09:19:29 +01:00
</tbody>
</LighterGreyBackgroundTable>
</LighterGreyBackgroundPanelBlock>
2018-10-29 16:45:32 +01:00
);
}
return null;
}
2018-10-29 09:32:30 +01:00
render() {
const { file, revision, repository, path } = this.props;
const { showHistory, errorFromExtension } = this.state;
2018-10-29 09:32:30 +01:00
const header = this.showHeader();
2018-11-28 09:28:45 +01:00
const content =
showHistory && file._links.history ? (
<HistoryView file={file} repository={repository} />
) : (
<SourcesView
revision={revision}
file={file}
repository={repository}
path={path}
/>
);
2018-10-29 16:45:32 +01:00
const moreInformation = this.showMoreInformation();
2018-10-29 09:32:30 +01:00
return (
<div>
2019-02-01 17:03:00 +01:00
<div className="panel">
<div className="panel-heading">{header}</div>
2018-10-29 16:45:32 +01:00
{moreInformation}
{content}
2019-02-01 17:03:00 +01:00
</div>
{errorFromExtension && <ErrorNotification error={errorFromExtension} />}
2018-10-29 09:32:30 +01:00
</div>
);
2018-10-15 16:45:54 +02:00
}
}
2018-10-25 13:45:52 +02:00
const mapStateToProps = (state: any, ownProps: Props) => {
const { repository, revision, path } = ownProps;
const file = getSources(state, repository, revision, path);
return {
file,
2018-10-25 13:45:52 +02:00
};
};
export default connect(mapStateToProps)(translate('repos')(Content));