2019-10-19 16:38:07 +02:00
|
|
|
import React from 'react';
|
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
import { withRouter } from 'react-router-dom';
|
|
|
|
|
import { Branch, Repository } from '@scm-manager/ui-types';
|
|
|
|
|
import FileTree from '../components/FileTree';
|
2019-09-04 10:17:57 +02:00
|
|
|
import {
|
|
|
|
|
BranchSelector,
|
|
|
|
|
Breadcrumb,
|
|
|
|
|
ErrorNotification,
|
2019-10-19 16:38:07 +02:00
|
|
|
Loading,
|
|
|
|
|
} from '@scm-manager/ui-components';
|
|
|
|
|
import { translate } from 'react-i18next';
|
2018-12-17 11:29:20 +01:00
|
|
|
import {
|
|
|
|
|
fetchBranches,
|
|
|
|
|
getBranches,
|
|
|
|
|
getFetchBranchesFailure,
|
2019-10-19 16:38:07 +02:00
|
|
|
isFetchBranchesPending,
|
|
|
|
|
} from '../../branches/modules/branches';
|
|
|
|
|
import { compose } from 'redux';
|
|
|
|
|
import Content from './Content';
|
|
|
|
|
import { fetchSources, isDirectory } from '../modules/sources';
|
2018-09-27 16:32:37 +02:00
|
|
|
|
|
|
|
|
type Props = {
|
2019-10-19 16:38:07 +02:00
|
|
|
repository: Repository;
|
|
|
|
|
loading: boolean;
|
|
|
|
|
error: Error;
|
|
|
|
|
baseUrl: string;
|
|
|
|
|
branches: Branch[];
|
|
|
|
|
revision: string;
|
|
|
|
|
path: string;
|
|
|
|
|
currentFileIsDirectory: boolean;
|
2018-09-27 16:32:37 +02:00
|
|
|
|
|
|
|
|
// dispatch props
|
2019-10-19 16:38:07 +02:00
|
|
|
fetchBranches: (p: Repository) => void;
|
|
|
|
|
fetchSources: (p1: Repository, p2: string, p3: string) => void;
|
2018-10-19 09:17:26 +02:00
|
|
|
|
|
|
|
|
// Context props
|
2019-10-19 16:38:07 +02:00
|
|
|
history: any;
|
|
|
|
|
match: any;
|
|
|
|
|
location: any;
|
|
|
|
|
t: (p: string) => string;
|
2018-09-27 16:32:37 +02:00
|
|
|
};
|
|
|
|
|
|
2019-09-02 11:48:53 +02:00
|
|
|
type State = {
|
2019-10-19 16:38:07 +02:00
|
|
|
selectedBranch: any;
|
2019-09-02 11:48:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class Sources extends React.Component<Props, State> {
|
|
|
|
|
constructor(props: Props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.state = {
|
2019-10-19 16:38:07 +02:00
|
|
|
selectedBranch: null,
|
2019-09-02 11:48:53 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-27 16:32:37 +02:00
|
|
|
componentDidMount() {
|
2018-10-25 11:27:31 +02:00
|
|
|
const {
|
|
|
|
|
fetchBranches,
|
|
|
|
|
repository,
|
|
|
|
|
revision,
|
|
|
|
|
path,
|
2019-10-19 16:38:07 +02:00
|
|
|
fetchSources,
|
2018-10-25 11:27:31 +02:00
|
|
|
} = this.props;
|
2018-10-19 10:32:01 +02:00
|
|
|
|
2018-10-19 11:02:00 +02:00
|
|
|
fetchBranches(repository);
|
2018-10-25 11:27:31 +02:00
|
|
|
fetchSources(repository, revision, path);
|
2019-09-04 10:17:57 +02:00
|
|
|
|
2019-09-04 15:06:04 +02:00
|
|
|
this.redirectToDefaultBranch();
|
2018-10-25 11:27:31 +02:00
|
|
|
}
|
2019-09-04 10:17:57 +02:00
|
|
|
|
2018-10-25 11:27:31 +02:00
|
|
|
componentDidUpdate(prevProps) {
|
2019-09-04 15:06:04 +02:00
|
|
|
const { fetchSources, repository, revision, path } = this.props;
|
2018-10-25 11:27:31 +02:00
|
|
|
if (prevProps.revision !== revision || prevProps.path !== path) {
|
|
|
|
|
fetchSources(repository, revision, path);
|
|
|
|
|
}
|
2019-09-04 10:17:57 +02:00
|
|
|
|
2019-09-04 15:06:04 +02:00
|
|
|
this.redirectToDefaultBranch();
|
|
|
|
|
}
|
2019-09-04 10:17:57 +02:00
|
|
|
|
2019-09-04 15:06:04 +02:00
|
|
|
redirectToDefaultBranch = () => {
|
2019-09-09 08:31:37 +02:00
|
|
|
const { branches } = this.props;
|
|
|
|
|
if (this.shouldRedirectToDefaultBranch()) {
|
2019-09-04 10:17:57 +02:00
|
|
|
const defaultBranches = branches.filter(b => b.defaultBranch);
|
|
|
|
|
|
2019-09-04 15:39:02 +02:00
|
|
|
if (defaultBranches.length > 0) {
|
2019-09-09 08:31:37 +02:00
|
|
|
this.branchSelected(defaultBranches[0]);
|
2019-09-04 15:39:02 +02:00
|
|
|
}
|
2019-09-04 10:17:57 +02:00
|
|
|
}
|
2019-09-04 15:06:04 +02:00
|
|
|
};
|
2018-09-27 16:32:37 +02:00
|
|
|
|
2019-09-09 08:31:37 +02:00
|
|
|
shouldRedirectToDefaultBranch = () => {
|
2019-09-04 15:21:06 +02:00
|
|
|
const { branches, revision } = this.props;
|
|
|
|
|
return branches && !revision;
|
2019-09-04 13:34:30 +02:00
|
|
|
};
|
2018-09-27 16:32:37 +02:00
|
|
|
|
2018-10-19 09:17:26 +02:00
|
|
|
branchSelected = (branch?: Branch) => {
|
2018-10-22 10:23:45 +02:00
|
|
|
const { baseUrl, history, path } = this.props;
|
2018-10-19 09:17:26 +02:00
|
|
|
let url;
|
|
|
|
|
if (branch) {
|
2019-10-19 16:38:07 +02:00
|
|
|
this.setState({
|
|
|
|
|
selectedBranch: branch,
|
|
|
|
|
});
|
2018-10-22 11:26:03 +02:00
|
|
|
if (path) {
|
2018-10-23 13:03:15 +02:00
|
|
|
url = `${baseUrl}/${encodeURIComponent(branch.name)}/${path}`;
|
2018-10-22 11:26:03 +02:00
|
|
|
} else {
|
2018-10-23 13:03:15 +02:00
|
|
|
url = `${baseUrl}/${encodeURIComponent(branch.name)}/`;
|
2018-10-22 11:26:03 +02:00
|
|
|
}
|
2018-10-19 09:17:26 +02:00
|
|
|
} else {
|
2019-10-19 16:38:07 +02:00
|
|
|
this.setState({
|
|
|
|
|
selectedBranch: null,
|
|
|
|
|
});
|
2018-10-19 10:46:51 +02:00
|
|
|
url = `${baseUrl}/`;
|
2018-10-19 09:17:26 +02:00
|
|
|
}
|
2018-10-19 10:46:51 +02:00
|
|
|
history.push(url);
|
2018-10-19 09:17:26 +02:00
|
|
|
};
|
|
|
|
|
|
2018-09-27 16:32:37 +02:00
|
|
|
render() {
|
2018-10-25 11:27:31 +02:00
|
|
|
const {
|
|
|
|
|
repository,
|
|
|
|
|
baseUrl,
|
|
|
|
|
loading,
|
|
|
|
|
error,
|
|
|
|
|
revision,
|
|
|
|
|
path,
|
2019-10-19 16:38:07 +02:00
|
|
|
currentFileIsDirectory,
|
2018-10-25 11:27:31 +02:00
|
|
|
} = this.props;
|
2018-09-27 16:32:37 +02:00
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
return <ErrorNotification error={error} />;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-19 16:02:21 +02:00
|
|
|
if (loading) {
|
2018-09-27 16:32:37 +02:00
|
|
|
return <Loading />;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-25 11:27:31 +02:00
|
|
|
if (currentFileIsDirectory) {
|
|
|
|
|
return (
|
2019-02-01 17:03:00 +01:00
|
|
|
<div className="panel">
|
2019-04-20 15:42:35 +02:00
|
|
|
{this.renderBranchSelector()}
|
2019-10-09 16:17:02 +02:00
|
|
|
{this.renderBreadcrumb()}
|
2018-10-25 11:27:31 +02:00
|
|
|
<FileTree
|
|
|
|
|
repository={repository}
|
|
|
|
|
revision={revision}
|
|
|
|
|
path={path}
|
|
|
|
|
baseUrl={baseUrl}
|
|
|
|
|
/>
|
2018-12-19 16:39:46 +01:00
|
|
|
</div>
|
2018-10-25 11:27:31 +02:00
|
|
|
);
|
|
|
|
|
} else {
|
2018-10-25 13:59:13 +02:00
|
|
|
return (
|
|
|
|
|
<Content repository={repository} revision={revision} path={path} />
|
|
|
|
|
);
|
2018-10-25 11:27:31 +02:00
|
|
|
}
|
2018-09-27 16:32:37 +02:00
|
|
|
}
|
2018-10-19 09:17:26 +02:00
|
|
|
|
|
|
|
|
renderBranchSelector = () => {
|
2018-12-17 11:29:20 +01:00
|
|
|
const { branches, revision, t } = this.props;
|
2018-10-25 11:27:31 +02:00
|
|
|
|
2018-11-16 14:21:55 +01:00
|
|
|
if (branches) {
|
2018-10-19 09:17:26 +02:00
|
|
|
return (
|
2019-04-20 15:42:35 +02:00
|
|
|
<div className="panel-heading">
|
|
|
|
|
<BranchSelector
|
|
|
|
|
branches={branches}
|
|
|
|
|
selectedBranch={revision}
|
2019-10-19 16:38:07 +02:00
|
|
|
label={t('changesets.branchSelectorLabel')}
|
2019-04-20 15:42:35 +02:00
|
|
|
selected={(b: Branch) => {
|
|
|
|
|
this.branchSelected(b);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2018-10-19 09:17:26 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
};
|
2019-10-09 16:17:02 +02:00
|
|
|
|
|
|
|
|
renderBreadcrumb = () => {
|
|
|
|
|
const { revision, path, baseUrl, branches, repository } = this.props;
|
|
|
|
|
const { selectedBranch } = this.state;
|
|
|
|
|
|
|
|
|
|
if (revision) {
|
|
|
|
|
return (
|
|
|
|
|
<Breadcrumb
|
|
|
|
|
revision={encodeURIComponent(revision)}
|
|
|
|
|
path={path}
|
|
|
|
|
baseUrl={baseUrl}
|
|
|
|
|
branch={selectedBranch}
|
|
|
|
|
defaultBranch={
|
|
|
|
|
branches && branches.filter(b => b.defaultBranch === true)[0]
|
|
|
|
|
}
|
2019-10-11 14:56:11 +02:00
|
|
|
branches={branches}
|
2019-10-09 16:17:02 +02:00
|
|
|
repository={repository}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
};
|
2018-09-27 16:32:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const mapStateToProps = (state, ownProps) => {
|
2018-10-22 10:23:45 +02:00
|
|
|
const { repository, match } = ownProps;
|
2018-10-23 10:41:10 +02:00
|
|
|
const { revision, path } = match.params;
|
2018-10-23 13:03:15 +02:00
|
|
|
const decodedRevision = revision ? decodeURIComponent(revision) : undefined;
|
2018-10-19 16:02:21 +02:00
|
|
|
const loading = isFetchBranchesPending(state, repository);
|
|
|
|
|
const error = getFetchBranchesFailure(state, repository);
|
2018-10-19 09:17:26 +02:00
|
|
|
const branches = getBranches(state, repository);
|
2018-10-25 13:59:13 +02:00
|
|
|
const currentFileIsDirectory = decodedRevision
|
|
|
|
|
? isDirectory(state, repository, decodedRevision, path)
|
|
|
|
|
: isDirectory(state, repository, revision, path);
|
2018-09-27 16:32:37 +02:00
|
|
|
|
|
|
|
|
return {
|
2018-10-19 16:02:21 +02:00
|
|
|
repository,
|
2018-10-23 13:03:15 +02:00
|
|
|
revision: decodedRevision,
|
2018-10-22 10:23:45 +02:00
|
|
|
path,
|
2018-09-27 16:32:37 +02:00
|
|
|
loading,
|
|
|
|
|
error,
|
2018-10-25 11:27:31 +02:00
|
|
|
branches,
|
2019-10-19 16:38:07 +02:00
|
|
|
currentFileIsDirectory,
|
2018-09-27 16:32:37 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const mapDispatchToProps = dispatch => {
|
|
|
|
|
return {
|
2018-10-19 10:32:01 +02:00
|
|
|
fetchBranches: (repository: Repository) => {
|
|
|
|
|
dispatch(fetchBranches(repository));
|
2018-10-25 11:27:31 +02:00
|
|
|
},
|
|
|
|
|
fetchSources: (repository: Repository, revision: string, path: string) => {
|
|
|
|
|
dispatch(fetchSources(repository, revision, path));
|
2019-10-19 16:38:07 +02:00
|
|
|
},
|
2018-09-27 16:32:37 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2018-10-19 16:02:21 +02:00
|
|
|
export default compose(
|
2019-10-19 16:38:07 +02:00
|
|
|
translate('repos'),
|
2018-10-19 16:02:21 +02:00
|
|
|
withRouter,
|
|
|
|
|
connect(
|
|
|
|
|
mapStateToProps,
|
2019-10-19 16:38:07 +02:00
|
|
|
mapDispatchToProps,
|
|
|
|
|
),
|
2018-09-27 16:32:37 +02:00
|
|
|
)(Sources);
|