diff --git a/scm-ui/ui-webapp/src/repos/containers/ChangesetsRoot.tsx b/scm-ui/ui-webapp/src/repos/containers/ChangesetsRoot.tsx index 17b3acc991..ba37d6e104 100644 --- a/scm-ui/ui-webapp/src/repos/containers/ChangesetsRoot.tsx +++ b/scm-ui/ui-webapp/src/repos/containers/ChangesetsRoot.tsx @@ -17,7 +17,7 @@ type Props = WithTranslation & class ChangesetsRoot extends React.Component { componentDidMount() { const { branches, baseUrl } = this.props; - if (branches?.length > 0 && this.isSelectedBranchIsNotABranch()) { + if (branches?.length > 0 && this.isSelectedBranchNotABranch()) { const defaultBranch = branches?.filter(b => b.defaultBranch === true)[0]; this.props.history.push(`${baseUrl}/branch/${encodeURIComponent(defaultBranch.name)}/changesets/`); } @@ -30,7 +30,7 @@ class ChangesetsRoot extends React.Component { return url; }; - isSelectedBranchIsNotABranch = () => { + isSelectedBranchNotABranch = () => { const { branches, selectedBranch } = this.props; return branches?.filter(b => b.name === selectedBranch).length === 0; }; @@ -64,7 +64,7 @@ class ChangesetsRoot extends React.Component { <> diff --git a/scm-ui/ui-webapp/src/repos/sources/components/content/SourcecodeViewer.tsx b/scm-ui/ui-webapp/src/repos/sources/components/content/SourcecodeViewer.tsx index dc2fc34c60..7c21471a6c 100644 --- a/scm-ui/ui-webapp/src/repos/sources/components/content/SourcecodeViewer.tsx +++ b/scm-ui/ui-webapp/src/repos/sources/components/content/SourcecodeViewer.tsx @@ -12,6 +12,7 @@ type State = { content: string; error?: Error; loaded: boolean; + currentFileRevision: string; }; class SourcecodeViewer extends React.Component { @@ -20,31 +21,45 @@ class SourcecodeViewer extends React.Component { this.state = { content: "", - loaded: false + loaded: false, + currentFileRevision: "" }; } componentDidMount() { const { file } = this.props; - getContent(file._links.self.href) - .then(result => { - if (result.error) { - this.setState({ - ...this.state, - error: result.error, - loaded: true - }); - } else { - this.setState({ - ...this.state, - content: result, - loaded: true - }); - } - }) - .catch(err => {}); + const { currentFileRevision } = this.state; + if (file.revision !== currentFileRevision) { + this.fetchContent(); + } } + componentDidUpdate() { + const { file } = this.props; + const { currentFileRevision } = this.state; + if (file.revision !== currentFileRevision) { + this.fetchContent(); + } + } + + fetchContent = () => { + const { file } = this.props; + getContent(file._links.self.href) + .then(content => { + this.setState({ + content, + loaded: true, + currentFileRevision: file.revision + }); + }) + .catch(error => { + this.setState({ + error, + loaded: true + }); + }); + }; + render() { const { content, error, loaded } = this.state; const language = this.props.language; @@ -70,17 +85,7 @@ export function getLanguage(language: string) { } export function getContent(url: string) { - return apiClient - .get(url) - .then(response => response.text()) - .then(response => { - return response; - }) - .catch(err => { - return { - error: err - }; - }); + return apiClient.get(url).then(response => response.text()); } export default withTranslation("repos")(SourcecodeViewer);