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

refactor: Migration of AnnotationLayerControl to TypeScript #28346

Merged
Merged
Changes from 2 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
89b8366
chore: Migration of AnnotationLayerControl index.js to TypeScript
EnxDev May 5, 2024
b054a21
Reuses the ExplorePageState[explore] interface and removes the Explor…
EnxDev May 8, 2024
fc2005c
Update index.tsx
EnxDev May 9, 2024
92ec433
Update index.tsx
EnxDev May 9, 2024
cea4b2e
Update index.tsx
EnxDev May 9, 2024
0dbcd64
chore: Migration of AnnotationLayerControl index.js to TypeScript
EnxDev May 5, 2024
3948773
Reuses the ExplorePageState[explore] interface and removes the Explor…
EnxDev May 8, 2024
e8be58d
Update index.tsx
EnxDev May 9, 2024
bcfb86f
Update index.tsx
EnxDev May 9, 2024
fe2077d
Update index.tsx
EnxDev May 9, 2024
2f33b5c
Merge remote-tracking branch 'origin/chore/typescript-migration-Annot…
EnxDev Jun 4, 2024
ff3fb2e
Removes unused included types
EnxDev Jun 4, 2024
317567e
Removes any types from Annotation type
EnxDev Jun 6, 2024
2edf52e
Merge branch 'master' of https://github.com/apache/superset into chor…
EnxDev Jun 28, 2024
aa3049f
cleanup
EnxDev Jun 28, 2024
a9f9190
Linted
EnxDev Jun 28, 2024
a8758e9
Merge branch 'master' of https://github.com/apache/superset into chor…
geido Sep 25, 2024
da19896
Merge branch 'master' of https://github.com/apache/superset into chor…
EnxDev Sep 25, 2024
4c0c6f0
Merge branch 'master' of https://github.com/apache/superset into chor…
geido Sep 25, 2024
79e9c7a
fix(Ts): Fix types
geido Sep 25, 2024
b43d17c
Merge branch 'chore/typescript-migration-AnnotationLayerControl' of h…
geido Sep 25, 2024
9f5f56d
fix(Annotation): Fix duplicate annotation creation
geido Sep 25, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,28 @@
* under the License.
*/
import React from 'react';
import PropTypes from 'prop-types';
import { List } from 'src/components';
import { connect } from 'react-redux';
import { t, withTheme } from '@superset-ui/core';
import { InfoTooltipWithTrigger } from '@superset-ui/chart-controls';
import {
HandlerFunction,
JsonObject,
Payload,
QueryFormData,
SupersetTheme,
t,
withTheme,
} from '@superset-ui/core';
import {
ControlStateMapping,
Dataset,
InfoTooltipWithTrigger,
} from '@superset-ui/chart-controls';
import AsyncEsmComponent from 'src/components/AsyncEsmComponent';
import { getChartKey } from 'src/explore/exploreUtils';
import { runAnnotationQuery } from 'src/components/Chart/chartAction';
import CustomListItem from 'src/explore/components/controls/CustomListItem';
import { ChartState, ExplorePageState } from 'src/explore/types';
import { Slice } from '@reduxjs/toolkit';
import ControlPopover, {
getSectionContainerElement,
} from '../ControlPopover/ControlPopover';
Expand All @@ -36,19 +49,37 @@ const AnnotationLayer = AsyncEsmComponent(
() => <div style={{ width: 450, height: 368 }} />,
);

const propTypes = {
colorScheme: PropTypes.string.isRequired,
annotationError: PropTypes.object,
annotationQuery: PropTypes.object,
vizType: PropTypes.string,
export interface Annotation {
name: string;
show?: boolean;
annotation: any;
timeout: any;
key: any;
formData?: any;
Copy link
Member

Choose a reason for hiding this comment

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

No any please

isDashboardRequest?: boolean;
force?: boolean;
}

validationErrors: PropTypes.array,
name: PropTypes.string.isRequired,
actions: PropTypes.object,
value: PropTypes.arrayOf(PropTypes.object),
onChange: PropTypes.func,
refreshAnnotationData: PropTypes.func,
};
export interface Props {
colorScheme: string;
annotationError: Record<string, string>;
annotationQuery: Record<string, AbortController>;
vizType: string;
validationErrors: JsonObject[];
name: string;
actions: {
setControlValue: HandlerFunction;
};
value: Annotation[];
onChange: (annotations: Annotation[]) => void;
refreshAnnotationData: (payload: Payload) => void;
theme: SupersetTheme;
}

export interface PopoverState {
popoverVisible: Record<number | string, boolean>;
addedAnnotationIndex: number | null;
}

const defaultProps = {
vizType: '',
Expand All @@ -57,9 +88,10 @@ const defaultProps = {
annotationQuery: {},
onChange: () => {},
};
class AnnotationLayerControl extends React.PureComponent<Props, PopoverState> {
static defaultProps = defaultProps;

class AnnotationLayerControl extends React.PureComponent {
constructor(props) {
constructor(props: Props) {
super(props);
this.state = {
popoverVisible: {},
Expand All @@ -75,7 +107,7 @@ class AnnotationLayerControl extends React.PureComponent {
AnnotationLayer.preload();
}

UNSAFE_componentWillReceiveProps(nextProps) {
UNSAFE_componentWillReceiveProps(nextProps: Props) {
const { name, annotationError, validationErrors, value } = nextProps;
if (Object.keys(annotationError).length && !validationErrors.length) {
this.props.actions.setControlValue(
Expand All @@ -89,7 +121,10 @@ class AnnotationLayerControl extends React.PureComponent {
}
}

addAnnotationLayer(originalAnnotation, newAnnotation) {
addAnnotationLayer = (
originalAnnotation: Annotation,
newAnnotation: Annotation,
) => {
let annotations = this.props.value;
if (annotations.includes(originalAnnotation)) {
annotations = annotations.map(anno =>
Expand All @@ -106,15 +141,15 @@ class AnnotationLayerControl extends React.PureComponent {
});

this.props.onChange(annotations);
}
};

handleVisibleChange(visible, popoverKey) {
handleVisibleChange = (visible: boolean, popoverKey: number | string) => {
this.setState(prevState => ({
popoverVisible: { ...prevState.popoverVisible, [popoverKey]: visible },
}));
}
};

removeAnnotationLayer(annotation) {
removeAnnotationLayer(annotation: Annotation) {
const annotations = this.props.value.filter(anno => anno !== annotation);
// So scrollbar doesnt get stuck on hidden
const element = getSectionContainerElement();
Expand All @@ -124,7 +159,11 @@ class AnnotationLayerControl extends React.PureComponent {
this.props.onChange(annotations);
}

renderPopover(popoverKey, annotation, error) {
renderPopover = (
popoverKey: number | string,
annotation: Annotation,
EnxDev marked this conversation as resolved.
Show resolved Hide resolved
error: string,
) => {
const id = annotation?.name || '_new';

return (
Expand All @@ -134,7 +173,7 @@ class AnnotationLayerControl extends React.PureComponent {
error={error}
colorScheme={this.props.colorScheme}
vizType={this.props.vizType}
addAnnotationLayer={newAnnotation =>
addAnnotationLayer={(newAnnotation: Annotation) =>
this.addAnnotationLayer(annotation, newAnnotation)
}
removeAnnotationLayer={() => this.removeAnnotationLayer(annotation)}
Expand All @@ -145,9 +184,9 @@ class AnnotationLayerControl extends React.PureComponent {
/>
</div>
);
}
};

renderInfo(anno) {
renderInfo(anno: Annotation) {
const { annotationError, annotationQuery, theme } = this.props;
if (annotationQuery[anno.name]) {
return (
Expand Down Expand Up @@ -175,7 +214,7 @@ class AnnotationLayerControl extends React.PureComponent {

render() {
const { addedAnnotationIndex } = this.state;
const addedAnnotation = this.props.value[addedAnnotationIndex];
const addedAnnotation = this.props.value[addedAnnotationIndex!];
EnxDev marked this conversation as resolved.
Show resolved Hide resolved

const annotations = this.props.value.map((anno, i) => (
<ControlPopover
Expand Down Expand Up @@ -210,7 +249,11 @@ class AnnotationLayerControl extends React.PureComponent {
{annotations}
<ControlPopover
trigger="click"
content={this.renderPopover(addLayerPopoverKey, addedAnnotation)}
content={this.renderPopover(
addLayerPopoverKey,
addedAnnotation,
'',
)}
title={t('Add annotation layer')}
visible={this.state.popoverVisible[addLayerPopoverKey]}
destroyTooltipOnHide
Expand All @@ -232,27 +275,38 @@ class AnnotationLayerControl extends React.PureComponent {
}
}

AnnotationLayerControl.propTypes = propTypes;
AnnotationLayerControl.defaultProps = defaultProps;

// Tried to hook this up through stores/control.jsx instead of using redux
// directly, could not figure out how to get access to the color_scheme
function mapStateToProps({ charts, explore }) {
function mapStateToProps({
charts,
explore,
}: {
charts: {
[key: string]: ChartState;
};
explore: Partial<ExplorePageState['explore']>;
}) {
EnxDev marked this conversation as resolved.
Show resolved Hide resolved
const chartKey = getChartKey(explore);
const chart = charts[chartKey] || charts[0] || {};

const defaultChartState: Partial<ChartState> = {
annotationError: {},
annotationQuery: {},
};

const chart =
chartKey && charts[chartKey] ? charts[chartKey] : defaultChartState;

return {
// eslint-disable-next-line camelcase
colorScheme: explore.controls?.color_scheme?.value,
annotationError: chart.annotationError,
annotationQuery: chart.annotationQuery,
vizType: explore.controls.viz_type.value,
annotationError: chart.annotationError ?? {},
annotationQuery: chart.annotationQuery ?? {},
vizType: explore.controls?.viz_type.value,
};
}

function mapDispatchToProps(dispatch) {
function mapDispatchToProps(dispatch: any) {
return {
refreshAnnotationData: annotationObj =>
refreshAnnotationData: (annotationObj: Annotation) =>
dispatch(runAnnotationQuery(annotationObj)),
};
}
Expand Down
Loading