Skip to content

Commit

Permalink
Prevent file viewer unnecessary re-renders (#6662)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-yz-liu authored Jul 8, 2023
1 parent b889f32 commit d0e4b99
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 19 deletions.
2 changes: 1 addition & 1 deletion app/assets/javascripts/Components/Result/binary_viewer.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";

export class BinaryViewer extends React.Component {
export class BinaryViewer extends React.PureComponent {
render() {
return (
<div>
Expand Down
6 changes: 3 additions & 3 deletions app/assets/javascripts/Components/Result/image_viewer.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React from "react";
import {render} from "react-dom";

export class ImageViewer extends React.Component {
constructor() {
super();
export class ImageViewer extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
rotation: window.start_image_rotation || 0,
zoom: window.start_image_zoom || 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";
import {markupTextInRange} from "../Helpers/range_selector";

export class NotebookViewer extends React.Component {
export class NotebookViewer extends React.PureComponent {
constructor(props) {
super(props);
this.iframe = React.createRef();
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/Components/Result/pdf_viewer.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";

export class PDFViewer extends React.Component {
export class PDFViewer extends React.PureComponent {
constructor(props) {
super(props);
this.pdfContainer = React.createRef();
Expand Down
6 changes: 1 addition & 5 deletions app/assets/javascripts/Components/Result/result.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ class Result extends React.Component {
this.setState({
annotationModal: INITIAL_ANNOTATION_MODAL_STATE,
});
this.refreshAnnotations();
}); // Resetting back to original
};

Expand Down Expand Up @@ -305,10 +304,7 @@ class Result extends React.Component {

data = this.extend_with_selection_data(data);
if (data) {
$.post(
Routes.add_existing_annotation_course_annotations_path(this.props.course_id),
data
).then(this.refreshAnnotations);
$.post(Routes.add_existing_annotation_course_annotations_path(this.props.course_id), data);
}
};

Expand Down
31 changes: 24 additions & 7 deletions app/assets/javascripts/Components/Result/submission_file_panel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class SubmissionFilePanel extends React.Component {
focusLine: null,
annotationFocus: undefined,
selectedFileType: null,
visibleAnnotations: [],
};
this.submissionFileViewer = React.createRef();
}
Expand All @@ -32,6 +33,15 @@ export class SubmissionFilePanel extends React.Component {
(prevProps.loading && !this.props.loading)
) {
this.refreshSelectedFile();
} else if (
prevProps.annotations !== this.props.annotations &&
this.state.selectedFile !== null
) {
const submission_file_id = this.state.selectedFile[1];
const visibleAnnotations = this.props.annotations.filter(
a => a.submission_file_id === submission_file_id
);
this.setState({visibleAnnotations});
}
}

Expand Down Expand Up @@ -64,7 +74,17 @@ export class SubmissionFilePanel extends React.Component {
}
selectedFile = this.getFirstFile(this.props.fileData);
}
this.setState({selectedFile});

let visibleAnnotations;
if (selectedFile === null) {
visibleAnnotations = [];
} else {
const submission_file_id = selectedFile[1];
visibleAnnotations = this.props.annotations.filter(
a => a.submission_file_id === submission_file_id
);
}
this.setState({selectedFile, visibleAnnotations});

// TODO: Incorporate DownloadSubmissionModal as true child of this component.
if (this.props.canDownload) {
Expand Down Expand Up @@ -120,6 +140,7 @@ export class SubmissionFilePanel extends React.Component {
selectedFile: [file, id],
focusLine: focusLine,
annotationFocus: annotationFocus,
visibleAnnotations: this.props.annotations.filter(a => a.submission_file_id === id),
});
localStorage.setItem("file", file);
};
Expand All @@ -130,17 +151,13 @@ export class SubmissionFilePanel extends React.Component {
};

render() {
let submission_file_id, visibleAnnotations, submission_file_mime_type;
let submission_file_id, submission_file_mime_type;
if (this.state.selectedFile === null) {
submission_file_id = null;
submission_file_mime_type = null;
visibleAnnotations = [];
} else {
submission_file_id = this.state.selectedFile[1];
submission_file_mime_type = getType(this.state.selectedFile[0]);
visibleAnnotations = this.props.annotations.filter(
a => a.submission_file_id === submission_file_id
);
}
return (
<React.Fragment>
Expand Down Expand Up @@ -172,7 +189,7 @@ export class SubmissionFilePanel extends React.Component {
mime_type={submission_file_mime_type}
result_id={this.props.result_id}
selectedFile={submission_file_id}
annotations={visibleAnnotations}
annotations={this.state.visibleAnnotations}
focusLine={this.state.focusLine}
annotationFocus={this.state.annotationFocus}
released_to_students={this.props.released_to_students}
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/Components/Result/text_viewer.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";
import {render} from "react-dom";

export class TextViewer extends React.Component {
export class TextViewer extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
Expand Down

0 comments on commit d0e4b99

Please sign in to comment.