Skip to content

Fix #156: Don't scroll upon region change - #165

Merged
openjck merged 1 commit into
mozilla:masterfrom
openjck:issue-156-page-moves
Jun 4, 2018
Merged

openjck merged 1 commit into
mozilla:masterfrom
openjck:issue-156-page-moves

Conversation

@openjck

@openjck openjck commented May 26, 2018

Copy link
Copy Markdown
Contributor

This commit ended up being a little more involved than expected. This
bug was happening because the MetricsGraphics component was re-loaded
every time the region selector changed. Fixing the bug meant only
loading the MetricsGraphics component once, which meant responding to
state changes a little more intelligently. So a lot of state-management
code was changed.

The good news is that I took the opportunity to improve performance a
bit. For example, chart descriptions are no longer re-rendered every
time chart data changes.

@openjck
openjck requested a review from spasovski May 26, 2018 01:43
);
} else {
newState.maybeMetricDescription = (
<p className="metric-description" dangerouslySetInnerHTML={

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it's worth considering something like react-markdownit to avoid using dangerouslySetInnerHTML (which probably calls that API under the hood anyways).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice find! I'll do that in a separate PR since the Markdown stuff isn't modified here.


if (multipleParagraphs) {
newState.maybeMetricDescription = (
<div className="metric-description">

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked briefly and didn't see anything suspicious in the CSS, but I assume everything is OK if we have div.metric-description here and p.metric-description below on L54.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that should be fine. It's a little weird, but I chose those names intentionally. When there is one paragraph, the metric description is that paragraph / <p>. When there are multiple paragraphs, the metric description is the element (in this case the <div>) which groups those paragraphs.

</div>
);
};
export default MetricOverview;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the switch to exporting a named class? Do we have more than one?

@openjck openjck Jun 1, 2018 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that, because getDerivedStateFromProps is static, the variables it references must also be static. For example, it references the static instance variable MetricOverview.markdownParser here:

https://github.com/openjck/ensemble/blob/acc4913763067f1202a6e05018c6c96381d7c5fc/src/components/views/MetricOverview.js#L47

I don't think there's a way to reference a static variable in an unnamed class, but let me try again.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I almost found a way to do this that works, but it has some weird side effects. For example, in MetricOverview.js, I'd need to refer to metricParser by the name this.a.metricParser, which is really strange. I have to imagine it's a result of babel doing something weird.

Definitely open to making this better. I just can't figure out how.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry should've mentioned this is a style nit. If it takes more than 8 bytes to address it - ignore it. Was more for consistency with other components.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah don't be sorry. I'm totally on board with you. I wish I knew of a way to do this without naming the class but I can't find a good solution to that. It may be my inexperience with static ES6 methods.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style matters. I really believe in that. Just be glad I didn't set up a mandatory pretty-printer for this project like I have in the past. 😜

Although prettier does look very cool.

});
}

if (Object.keys(newState).length > 0) return newState;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In marketplace we used to require curly brace blocks (even when ugly) for better uglifyjs support. I imagine things have improved since then though. Also could be just .length and still be truthy but this is a nit...it might be more readable as is.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I'll fix the .length thing. Do you have a link to info on the uglify thing? I don't always like writing to implementation but if it's still a distinction Uglify makes we could do it.

class MetricOverview extends React.Component {
// Create a markdown parser that only parses links. It needs to be static
// for static methods to use it.
static markdownParser = markdownIt('zero').enable('link');

@spasovski spasovski Jun 1, 2018 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder is JIT cares about static and whether it helps performance. Otherwise this reminds me of CSS' !important breeds more !important.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. And a valid comparison. Unfortunately getDerivedStateFromProps has to be static so I don't think we have another choice. At least not one that I know of, but I'm open to suggestions. I'm new to static methods in ES6.

@openjck openjck mentioned this pull request Jun 1, 2018
3 of 20 tasks
This commit ended up being a little more involved than expected. This
bug was happening because the MetricsGraphics component was re-loaded
every time the region selector changed. Fixing the bug meant only
loading the MetricsGraphics component once, which meant responding to
state changes a little more intelligently. So a lot of state-management
code was changed.

The good news is that I took the opportunity to improve performance a
bit. For example, chart descriptions are no longer re-rendered every
time chart data changes.
@openjck

openjck commented Jun 1, 2018

Copy link
Copy Markdown
Contributor Author

Updated and rebased. You might want to check extra carefully that I didn't mess up any of your chart resizing stuff. I'm pretty confident I didn't, but the rebase involved some of those changes.

@spasovski

Copy link
Copy Markdown
Contributor

r+ thanks

@openjck
openjck merged commit 789cc9e into mozilla:master Jun 4, 2018
@openjck
openjck deleted the issue-156-page-moves branch July 20, 2018 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants