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: Transform URLShortLinkModal to Typescript #11971

Merged
Merged
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,38 @@
* under the License.
*/
import React from 'react';
import PropTypes from 'prop-types';
import { t } from '@superset-ui/core';
import CopyToClipboard from './CopyToClipboard';
import { getShortUrl } from '../utils/common';
import withToasts from '../messageToasts/enhancers/withToasts';
import ModalTrigger from './ModalTrigger';

const propTypes = {
url: PropTypes.string,
emailSubject: PropTypes.string,
emailContent: PropTypes.string,
addDangerToast: PropTypes.func.isRequired,
title: PropTypes.string,
triggerNode: PropTypes.node.isRequired,
type URLShortLinkModalProps = {
url: string;
emailSubject: string;
emailContent: string;
title?: string;
addDangerToast: (msg: string) => void;
triggerNode: JSX.Element;
Copy link
Member

Choose a reason for hiding this comment

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

any reason for JSX.Element and not React.ReactNode?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

what are the differences? I checked and it's not clear

Copy link
Member

Choose a reason for hiding this comment

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

ReactNode includes JSX.Element (an alias of ReactElement<any, any>), as well as string, null, undefined and node arrays.

Copy link
Member

Choose a reason for hiding this comment

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

In this case it’s probably better to use JSX.Element since you’d want the trigger element to always exist and not be a text node.

};

class URLShortLinkModal extends React.Component {
constructor(props) {
type URLShortLinkModalState = {
shortUrl: string;
};

class URLShortLinkModal extends React.Component<
URLShortLinkModalProps,
URLShortLinkModalState
> {
static defaultProps = {
url: window.location.href.substring(window.location.origin.length),
emailSubject: '',
Copy link
Member

Choose a reason for hiding this comment

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

if we default this to empty string, can we make the props type themselves non-undefined? Or maybe get rid of the empty string default val

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed!

emailContent: '',
};

modal: ModalTrigger | null;

constructor(props: URLShortLinkModalProps) {
super(props);
this.state = {
shortUrl: '',
Expand All @@ -45,11 +59,11 @@ class URLShortLinkModal extends React.Component {
this.getCopyUrl = this.getCopyUrl.bind(this);
}

onShortUrlSuccess(shortUrl) {
onShortUrlSuccess(shortUrl: string) {
this.setState(() => ({ shortUrl }));
}

setModalRef(ref) {
setModalRef(ref: ModalTrigger | null) {
this.modal = ref;
}

Expand Down Expand Up @@ -88,12 +102,4 @@ class URLShortLinkModal extends React.Component {
}
}

URLShortLinkModal.defaultProps = {
url: window.location.href.substring(window.location.origin.length),
emailSubject: '',
emailContent: '',
};

URLShortLinkModal.propTypes = propTypes;

export default withToasts(URLShortLinkModal);