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

[Chore]: Technical: Translate components to typescript #1812

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/components/common/field-selector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ type FieldType =
| string
| string[]
| {
name?: string;
format?: string;
name: string;
format: string | null;
}[]
| {
format?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,15 @@ import {FormattedMessage} from 'localization';

BrushConfigFactory.deps = [RangeSliderFactory];

function BrushConfigFactory(RangeSlider) {
const BrushConfig = ({config, onChange}) => (
type BrushConfigProps = {
config: {
size: number;
};
onChange: (config: {size: number}) => void;
};

function BrushConfigFactory(RangeSlider: ReturnType<typeof RangeSliderFactory>) {
const BrushConfig = ({config, onChange}: BrushConfigProps) => (
<SidePanelSection>
<PanelLabel>
<FormattedMessage id={'misc.brushRadius'} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import React, {Component} from 'react';
import PropTypes from 'prop-types';
import React, {Component, ComponentType, ReactElement} from 'react';
import styled from 'styled-components';
import Switch from 'components/common/switch';

Expand All @@ -34,22 +33,33 @@ import {
} from 'components/common/styled-components';
import {FormattedMessage} from 'localization';

interface InteractionPanelProps {
datasets: any;
config: {
id: string;
enabled: boolean;
label: string;
iconComponent: any;
config: any;
};
onConfigChange: any;
}

const StyledInteractionPanel = styled.div`
padding-bottom: 6px;
`;

InteractionPanelFactory.deps = [TooltipConfigFactory, BrushConfigFactory];

function InteractionPanelFactory(TooltipConfig, BrushConfig) {
return class InteractionPanel extends Component {
static propTypes = {
datasets: PropTypes.object.isRequired,
config: PropTypes.object.isRequired,
onConfigChange: PropTypes.func.isRequired
function InteractionPanelFactory(
TooltipConfig: ReturnType<typeof TooltipConfigFactory>,
BrushConfig: ReturnType<typeof BrushConfigFactory>
): ComponentType<InteractionPanelProps> {
return class InteractionPanel extends Component<InteractionPanelProps> {
state = {
isConfigActive: false
};

state = {isConfigActive: false};

_updateConfig = newProp => {
this.props.onConfigChange({
...this.props.config,
Expand All @@ -64,15 +74,14 @@ function InteractionPanelFactory(TooltipConfig, BrushConfig) {
render() {
const {config, datasets} = this.props;
const onChange = newConfig => this._updateConfig({config: newConfig});
let template = null;
let template: ReactElement | null = null;

switch (config.id) {
case 'tooltip':
template = (
<TooltipConfig datasets={datasets} config={config.config} onChange={onChange} />
);
break;

case 'brush':
template = <BrushConfig config={config.config} onChange={onChange} />;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import React from 'react';
import styled from 'styled-components';
import {injectIntl} from 'react-intl';
import {injectIntl, IntlShape} from 'react-intl';
import {FormattedMessage} from 'localization';

import {
Expand Down Expand Up @@ -68,9 +68,66 @@ const CompareSwitchWrapper = styled.div`
margin-bottom: 8px;
`;

type TooltipConfigProps = {
config: {
fieldsToShow: {
[key: string]: {name: string; format: string | null}[];
};
compareMode: boolean;
compareType: string[];
};
onChange: (config: {
fieldsToShow: {
[key: string]: {name: string; format: string | null}[];
};
compareMode: boolean;
compareType: string[];
}) => void;
datasets: {
[key: string]: {
id: string;
fields: {
format?: string;
id?: string;
name?: string;
fieldIdx?: number;
type?: number;
}[];
};
};
intl: IntlShape;
};

type DatasetTooltipConfigProps = {
config: {
fieldsToShow: {
[key: string]: {name: string; format: string | null}[];
};
compareMode: boolean;
compareType: string[];
};
onChange: (config: {
fieldsToShow: {
[key: string]: {name: string; format: string | null}[];
};
compareMode: boolean;
compareType: string[];
}) => void;
dataset: {
id: string;
fields: {
format?: string;
id?: string;
name?: string;
fieldIdx?: number;
type?: number;
}[];
};
};

TooltipConfigFactory.deps = [DatasetTagFactory, FieldSelectorFactory];
function TooltipConfigFactory(DatasetTag, FieldSelector) {
const DatasetTooltipConfig = ({config, onChange, dataset}) => {
function TooltipConfigFactory(DatasetTag, FieldSelector: ReturnType<typeof FieldSelectorFactory>) {
const DatasetTooltipConfig = ({config, onChange, dataset}: DatasetTooltipConfigProps) => {
const dataId = dataset.id;
return (
<SidePanelSection key={dataId}>
Expand Down Expand Up @@ -102,15 +159,18 @@ function TooltipConfigFactory(DatasetTag, FieldSelector) {
fields={dataset.fields}
value={config.fieldsToShow[dataId]}
onSelect={selected => {
const newConfig = {
const newConfig: DatasetTooltipConfigProps['config'] = {
...config,
fieldsToShow: {
...config.fieldsToShow,
//@ts-expect-error
[dataId]: selected.map(
f =>
config.fieldsToShow[dataId].find(
//@ts-expect-error
tooltipField => tooltipField.name === f.name
) || {
//@ts-expect-error
name: f.name,
// default initial tooltip is null
format: null
Expand All @@ -123,13 +183,14 @@ function TooltipConfigFactory(DatasetTag, FieldSelector) {
closeOnSelect={false}
multiSelect
inputTheme="secondary"
//@ts-expect-error
CustomChickletComponent={TooltipChickletFactory(dataId, config, onChange, dataset.fields)}
/>
</SidePanelSection>
);
};

const TooltipConfig = ({config, datasets, onChange, intl}) => {
const TooltipConfig = ({config, datasets, onChange, intl}: TooltipConfigProps) => {
return (
<TooltipConfigWrapper>
{Object.keys(config.fieldsToShow).map(dataId =>
Expand Down Expand Up @@ -175,8 +236,9 @@ function TooltipConfigFactory(DatasetTag, FieldSelector) {
inputTheme={'secondary'}
getOptionValue={d => d}
onChange={option => {
const newConfig = {
const newConfig: TooltipConfigProps['config'] = {
...config,
//@ts-expect-error
compareType: option
};
onChange(newConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import React, {Component} from 'react';
import React, {Component, ComponentType} from 'react';
import styled from 'styled-components';
import {ChickletButton, ChickletTag} from 'components/common/item-selector/chickleted-input';
import {Hash, Delete} from 'components/common/icons';
Expand All @@ -30,10 +30,40 @@ import {TOOLTIP_FORMATS, TOOLTIP_FORMAT_TYPES, TOOLTIP_KEY} from 'constants/tool
import {getFormatter} from 'utils/data-utils';
import TippyTooltip from 'components/common/tippy-tooltip';

interface TooltipChickletProps {
disabled: boolean;
item: {name: string};
displayOption: Function;
remove: any;
}

type TooltipConfig = {
fieldsToShow: {
[key: string]: {name: string; format: string | null}[];
};
compareMode: boolean;
compareType: string[];
};

type TooltipFields = {
format?: string;
id?: string;
name?: string;
fieldIdx?: number;
type?: number;
};

type TimeLabelFormat = {
id: string;
format: string | null;
type: string;
label: string;
};

const TIME_DISPLAY = '2020-05-11 14:00';
const getValue = fmt => fmt[TOOLTIP_KEY];

const addDTimeLabel = formats =>
const addDTimeLabel = (formats: TimeLabelFormat[]) =>
formats.map(f => ({
...f,
label:
Expand All @@ -42,11 +72,12 @@ const addDTimeLabel = formats =>
: f.label
}));

function getFormatLabels(fields, fieldName) {
const fieldType = fields.find(f => f.name === fieldName).type;
function getFormatLabels(fields: any, fieldName: string) {
const fieldType = fields.find((f: TooltipFields) => f.name === fieldName).type;
const tooltipTypes = (fieldType && FIELD_OPTS[fieldType].format.tooltip) || [];
const formatLabels = Object.values(TOOLTIP_FORMATS).filter(t => tooltipTypes.includes(t.type));

const formatLabels: TimeLabelFormat[] = Object.values(TOOLTIP_FORMATS).filter(t =>
tooltipTypes.includes(t.type)
);
return addDTimeLabel(formatLabels);
}

Expand Down Expand Up @@ -82,7 +113,7 @@ const IconDiv = styled.div.attrs({
: props.theme.textColor};
`;

function getFormatTooltip(formatLabels, format) {
function getFormatTooltip(formatLabels: TimeLabelFormat[], format: string | null) {
if (!format) {
return null;
}
Expand All @@ -93,11 +124,17 @@ function getFormatTooltip(formatLabels, format) {
return typeof format === 'object' ? JSON.stringify(format, null, 2) : String(format);
}

function TooltipChickletFactory(dataId, config, onChange, fields) {
class TooltipChicklet extends Component {
function TooltipChickletFactory(
dataId: string,
config: TooltipConfig,
onChange: (config: TooltipConfig) => void,
fields: TooltipFields[]
): ComponentType<TooltipChickletProps> {
class TooltipChicklet extends Component<TooltipChickletProps> {
state = {
show: false
};
private node!: HTMLElement;

componentDidMount() {
document.addEventListener('mousedown', this.handleClickOutside, false);
Expand All @@ -107,11 +144,10 @@ function TooltipChickletFactory(dataId, config, onChange, fields) {
document.removeEventListener('mousedown', this.handleClickOutside, false);
}

handleClickOutside = e => {
handleClickOutside = (e: any) => {
if (this.node.contains(e.target)) {
return;
}
this.setState({show: false});
};

render() {
Expand All @@ -129,7 +165,7 @@ function TooltipChickletFactory(dataId, config, onChange, fields) {
const hashStyle = show ? hashStyles.SHOW : hasFormat ? hashStyles.ACTIVE : null;

return (
<ChickletButton ref={node => (this.node = node)}>
<ChickletButton ref={(node: HTMLElement) => (this.node = node)}>
<ChickletTag>{displayOption(item)}</ChickletTag>
{formatLabels.length > 1 && (
<ChickletAddonWrapper>
Expand Down