Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use React hooks #877

Merged
merged 2 commits into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 26 additions & 34 deletions src/client/modals/source-modal/source-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import React from "react";
import React, { useState } from "react";

import { Fn } from "../../../common/utils/general/general";
import { Button } from "../../components/button/button";
Expand All @@ -34,40 +34,32 @@ interface SourceModalProps {
source: string;
}

interface SourceModalState {
copied: boolean;
}

export class SourceModal extends React.Component<SourceModalProps, SourceModalState> {
export const SourceModal: React.FunctionComponent<SourceModalProps> = ({ copyLabel = STRINGS.copyDefinition, onClose, source, title, className, header }) => {
const [copied, setCopied] = useState(false);

state: SourceModalState = { copied: false };
const onCopy = () => setCopied(true);
Copy link
Contributor

Choose a reason for hiding this comment

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

useCallback


onCopy = () => this.setState({ copied: true });
const SyntaxHighlighter = React.lazy(() => import(/* webpackChunkName: "highlighter" */ "./highlighter"));

render() {
const { copyLabel = STRINGS.copyDefinition, onClose, source, title, className, header } = this.props;
const SyntaxHighlighter = React.lazy(() => import(/* webpackChunkName: "highlighter" */ "./highlighter"));

return <Modal
onClose={onClose}
title={title}
className={classNames("source-modal", className)}
>
<div className="content">
{header}
<React.Suspense fallback={Loader}>
<SyntaxHighlighter>
{source}
</SyntaxHighlighter>
</React.Suspense>
<div className="button-bar">
<Button type="primary" className="close" onClick={onClose} title={STRINGS.close} />
<SafeCopyToClipboard text={source} onCopy={this.onCopy}>
<Button type="secondary" title={copyLabel} />
</SafeCopyToClipboard>
{this.state.copied && <div className="copied-hint">{STRINGS.copied}</div>}
</div>
return <Modal
onClose={onClose}
title={title}
className={classNames("source-modal", className)}
>
<div className="content">
{header}
<React.Suspense fallback={Loader}>
<SyntaxHighlighter>
{source}
</SyntaxHighlighter>
</React.Suspense>
<div className="button-bar">
<Button type="primary" className="close" onClick={onClose} title={STRINGS.close} />
<SafeCopyToClipboard text={source} onCopy={onCopy}>
<Button type="secondary" title={copyLabel} />
</SafeCopyToClipboard>
{copied && <div className="copied-hint">{STRINGS.copied}</div>}
</div>
</Modal>;
}
}
</div>
</Modal>;
};
153 changes: 72 additions & 81 deletions src/client/visualizations/scatterplot/scatterplot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/
import { Datum } from "plywood";
import React from "react";
import React, { useState } from "react";

import { ChartProps } from "../../../common/models/chart-props/chart-props";
import makeQuery from "../../../common/utils/query/visualization-query";
Expand Down Expand Up @@ -43,97 +43,88 @@ import { YAxis } from "./y-axis";

const TICK_SIZE = 10;

interface ScatterplotState {
hoveredPoint: Datum | null;
}
type HoveredPoint = Datum | null;

export class Scatterplot extends React.Component<ChartProps, ScatterplotState> {
state: ScatterplotState = {
hoveredPoint: null
};
export const Scatterplot: React.FunctionComponent<ChartProps> = ({ data, essence, stage }) => {
const [hoveredPoint, setHoveredPoint] = useState<HoveredPoint>(null);

getPlottingData = memoizeOne(preparePlottingData);
const getPlottingData = memoizeOne(preparePlottingData);

setPointHover = (datum: Datum): void =>
this.setState({ hoveredPoint: datum });
const setPointHover = (datum: Datum): void => setHoveredPoint(datum);

resetPointHover = (): void =>
this.setState({ hoveredPoint: null });
const resetPointHover = (): void => setHoveredPoint(null);

render() {
const { data, essence, stage } = this.props;
const mainSplit = essence.splits.splits.first();
const showHeatmap = (essence.visualizationSettings as ScatterplotSettings).showSummary;
const mainSplit = essence.splits.splits.first();
const showHeatmap = (essence.visualizationSettings as ScatterplotSettings).showSummary;

const {
xTicks,
yTicks,
xScale,
yScale,
xSeries,
ySeries,
plottingStage,
scatterplotData
} = this.getPlottingData(data, essence, stage);
const {
xTicks,
yTicks,
xScale,
yScale,
xSeries,
ySeries,
plottingStage,
scatterplotData
} = getPlottingData(data, essence, stage);

const xAxisLabelPosition = getXAxisLabelPosition(stage, plottingStage);
const xAxisLabelPosition = getXAxisLabelPosition(stage, plottingStage);

return <div className="scatterplot-container" style={stage.getWidthHeight()}>
<span className="axis-title axis-title-y" style={{ top: 10, left: 10 }}>{ySeries.title()}</span>
<span className="axis-title axis-title-x" style={{ bottom: xAxisLabelPosition.bottom, right: xAxisLabelPosition.right }}>{xSeries.title()}</span>
<Tooltip
datum={this.state.hoveredPoint}
return <div className="scatterplot-container" style={stage.getWidthHeight()}>
<span className="axis-title axis-title-y" style={{ top: 10, left: 10 }}>{ySeries.title()}</span>
<span className="axis-title axis-title-x" style={{ bottom: xAxisLabelPosition.bottom, right: xAxisLabelPosition.right }}>{xSeries.title()}</span>
<Tooltip
datum={hoveredPoint}
stage={plottingStage}
ySeries={ySeries}
xSeries={xSeries}
yScale={yScale}
xScale={xScale}
split={mainSplit}
timezone={essence.timezone}
showPrevious={essence.hasComparison()}/>
<svg viewBox={stage.getViewBox()}>
{showHeatmap && <Heatmap
stage={plottingStage}
ySeries={ySeries}
data={scatterplotData}
xBinCount={xTicks.length - 1}
yBinCount={yTicks.length - 1}
xScale={xScale}
xSeries={xSeries}
yScale={yScale}
xScale={xScale}
split={mainSplit}
timezone={essence.timezone}
showPrevious={essence.hasComparison()}/>
<svg viewBox={stage.getViewBox()}>
{showHeatmap && <Heatmap
stage={plottingStage}
data={scatterplotData}
xBinCount={xTicks.length - 1}
yBinCount={yTicks.length - 1}
xScale={xScale}
xSeries={xSeries}
yScale={yScale}
ySeries={ySeries}/>}
<GridLines orientation={"vertical"} stage={plottingStage} ticks={xTicks} scale={xScale}/>
<GridLines orientation={"horizontal"} stage={plottingStage} ticks={yTicks} scale={yScale}/>
<XAxis
scale={xScale}
stage={calculateXAxisStage(plottingStage)}
ticks={getTicksForAvailableSpace(xTicks, plottingStage.width)}
formatter={xSeries.formatter()}
tickSize={TICK_SIZE}/>
<YAxis
stage={calculateYAxisStage(plottingStage)}
ticks={getTicksForAvailableSpace(yTicks, plottingStage.height)}
tickSize={TICK_SIZE}
scale={yScale}
formatter={ySeries.formatter()}/>
<g transform={plottingStage.getTransform()}>
{scatterplotData.map(datum => {
return (
<Point
key={`point-${mainSplit.selectValue(datum)}`}
datum={datum}
xScale={xScale}
yScale={yScale}
xSeries={xSeries}
ySeries={ySeries}
setHover={this.setPointHover}
resetHover={this.resetPointHover}/>
);
})}
</g>
</svg>
</div>;
}
}
ySeries={ySeries}/>}
<GridLines orientation={"vertical"} stage={plottingStage} ticks={xTicks} scale={xScale}/>
<GridLines orientation={"horizontal"} stage={plottingStage} ticks={yTicks} scale={yScale}/>
<XAxis
scale={xScale}
stage={calculateXAxisStage(plottingStage)}
ticks={getTicksForAvailableSpace(xTicks, plottingStage.width)}
formatter={xSeries.formatter()}
tickSize={TICK_SIZE}/>
<YAxis
stage={calculateYAxisStage(plottingStage)}
ticks={getTicksForAvailableSpace(yTicks, plottingStage.height)}
tickSize={TICK_SIZE}
scale={yScale}
formatter={ySeries.formatter()}/>
<g transform={plottingStage.getTransform()}>
{scatterplotData.map(datum => {
return (
<Point
key={`point-${mainSplit.selectValue(datum)}`}
datum={datum}
xScale={xScale}
yScale={yScale}
xSeries={xSeries}
ySeries={ySeries}
setHover={setPointHover}
resetHover={resetPointHover}/>
);
})}
</g>
</svg>
</div>;
};

export function ScatterplotVisualization(props: VisualizationProps) {
return <React.Fragment>
Expand Down