-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/* | ||
* // This file is part of Invenio-App-Rdm | ||
* // Copyright (C) 2023 CERN. | ||
* // | ||
* // Invenio-App-Rdm is free software; you can redistribute it and/or modify it | ||
* // under the terms of the MIT License; see LICENSE file for more details. | ||
*/ | ||
|
||
import React, { Component } from "react"; | ||
import PropTypes from "prop-types"; | ||
import { NotificationContext } from "@js/invenio_administration"; | ||
import { withCancel, ErrorMessage } from "react-invenio-forms"; | ||
Check warning on line 12 in invenio_app_rdm/theme/assets/semantic-ui/js/invenio_app_rdm/administration/records/CompareRevisions.js GitHub Actions / JS / Tests (18.x)
|
||
import { Button, Modal, Dropdown, Grid } from "semantic-ui-react"; | ||
import { i18next } from "@translations/invenio_app_rdm/i18next"; | ||
import { Differ, Viewer } from "json-diff-kit"; | ||
|
||
export class CompareRevisions extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
loading: true, | ||
error: undefined, | ||
currentDiff: undefined, | ||
revisions: {}, | ||
}; | ||
this.differ = new Differ({ | ||
detectCircular: true, | ||
maxDepth: null, | ||
showModifications: true, | ||
arrayDiffMethod: "lcs", | ||
ignoreCase: false, | ||
ignoreCaseForKey: false, | ||
recursiveEqual: true, | ||
}); | ||
} | ||
|
||
componentWillUnmount() { | ||
Check warning on line 37 in invenio_app_rdm/theme/assets/semantic-ui/js/invenio_app_rdm/administration/records/CompareRevisions.js GitHub Actions / JS / Tests (18.x)
|
||
this.cancellableAction && this.cancellableAction.cancel(); | ||
} | ||
|
||
async componentDidMount() { | ||
const { resource } = this.props; | ||
this.setState({ loading: true }); | ||
Check failure on line 43 in invenio_app_rdm/theme/assets/semantic-ui/js/invenio_app_rdm/administration/records/CompareRevisions.js GitHub Actions / JS / Tests (18.x)
|
||
const response = await fetch( | ||
`https://127.0.0.1:5000/api/records/${resource.id}/revisions` | ||
); | ||
if (!response.ok) { | ||
this.setState({ error: response.statusText, loading: false }); | ||
Check failure on line 48 in invenio_app_rdm/theme/assets/semantic-ui/js/invenio_app_rdm/administration/records/CompareRevisions.js GitHub Actions / JS / Tests (18.x)
|
||
return; | ||
} | ||
|
||
const revisions = await response.json(); | ||
this.setState( | ||
Check failure on line 53 in invenio_app_rdm/theme/assets/semantic-ui/js/invenio_app_rdm/administration/records/CompareRevisions.js GitHub Actions / JS / Tests (18.x)
|
||
{ | ||
revisions: revisions.reduce((acc, item, index) => { | ||
acc[index] = item; // Use version_id as the key | ||
return acc; | ||
}, {}), | ||
}, | ||
() => { | ||
this.setState({ loading: false }); | ||
} | ||
); | ||
} | ||
|
||
static contextType = NotificationContext; | ||
|
||
computeDiff = (before, after) => { | ||
const diff = this.differ.diff(before, after); | ||
this.setState({ currentDiff: diff }); | ||
}; | ||
|
||
handleModalClose = () => { | ||
const { actionCancelCallback } = this.props; | ||
actionCancelCallback(); | ||
}; | ||
|
||
render() { | ||
const { error, loading, currentDiff } = this.state; | ||
|
||
const viewerProps = { | ||
indent: 4, | ||
lineNumbers: true, | ||
highlightInlineDiff: true, | ||
inlineDiffOptions: { | ||
mode: "word", | ||
wordSeparator: " ", | ||
}, | ||
hideUnchangedLines: true, | ||
syntaxHighlight: false, | ||
virtual: false, | ||
}; | ||
|
||
return ( | ||
<> | ||
{error && ( | ||
<ErrorMessage | ||
header={i18next.t("Unable to fetch revisions.")} | ||
content={error} | ||
icon="exclamation" | ||
className="text-align-left" | ||
negative | ||
/> | ||
)} | ||
<Modal.Content> | ||
{loading && <p>Loading...</p>} | ||
<Grid> | ||
<Grid.Row> | ||
<Dropdown | ||
loading={loading} | ||
placeholder="Select revision" | ||
fluid | ||
selection | ||
value={this.state.selectedRevision} | ||
Check warning on line 114 in invenio_app_rdm/theme/assets/semantic-ui/js/invenio_app_rdm/administration/records/CompareRevisions.js GitHub Actions / JS / Tests (18.x)
|
||
onChange={(e, { value }) => { | ||
this.computeDiff( | ||
this.state.revisions[value], | ||
Check warning on line 117 in invenio_app_rdm/theme/assets/semantic-ui/js/invenio_app_rdm/administration/records/CompareRevisions.js GitHub Actions / JS / Tests (18.x)
|
||
this.state.revisions[0] | ||
Check warning on line 118 in invenio_app_rdm/theme/assets/semantic-ui/js/invenio_app_rdm/administration/records/CompareRevisions.js GitHub Actions / JS / Tests (18.x)
|
||
); | ||
this.setState({ selectedRevision: value }); | ||
}} | ||
options={Object.values(this.state.revisions).map((rev, index) => ({ | ||
Check warning on line 122 in invenio_app_rdm/theme/assets/semantic-ui/js/invenio_app_rdm/administration/records/CompareRevisions.js GitHub Actions / JS / Tests (18.x)
|
||
key: index, | ||
text: `Revision ${index}`, | ||
value: index, | ||
}))} | ||
/> | ||
</Grid.Row> | ||
<Grid.Row> | ||
{!loading && currentDiff && ( | ||
<Viewer diff={currentDiff} {...viewerProps} /> | ||
)} | ||
</Grid.Row> | ||
</Grid> | ||
</Modal.Content> | ||
<Modal.Actions> | ||
<Button onClick={this.handleModalClose} floated="left"> | ||
Close | ||
</Button> | ||
</Modal.Actions> | ||
</> | ||
); | ||
} | ||
} | ||
|
||
CompareRevisions.propTypes = { | ||
resource: PropTypes.object.isRequired, | ||
actionCancelCallback: PropTypes.func.isRequired, | ||
actionSuccessCallback: PropTypes.func.isRequired, | ||
}; |