Skip to content

Commit

Permalink
chore(dashboard): remove AnyWidget type use default Widget type instead
Browse files Browse the repository at this point in the history
  • Loading branch information
square-li committed Mar 10, 2023
1 parent 7c37c2d commit bcf37ea
Show file tree
Hide file tree
Showing 42 changed files with 123 additions and 121 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState } from 'react';
import { DashboardState } from '~/store/state';
import { AnyWidget } from '~/types';
import { Widget } from '~/types';
import { DragEvent, PointClickEvent } from '../../grid';
import { determineTargetGestures } from './determineTargetGestures';
import { Gesture } from './types';
Expand All @@ -10,7 +10,7 @@ import { useSelectionGestures } from './useSelection';

type GestureHooksProps = {
dashboardConfiguration: DashboardState['dashboardConfiguration'];
selectedWidgets: AnyWidget[];
selectedWidgets: Widget[];
cellSize: DashboardState['grid']['cellSize'];
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import React, { useCallback } from 'react';
import { useDispatch } from 'react-redux';
import { onMoveWidgetsAction } from '~/store/actions';
import { DashboardState } from '~/store/state';
import { Position, AnyWidget } from '~/types';
import { Position, Widget } from '~/types';
import { toGridPosition } from '~/util/position';
import { DragEvent } from '../../grid';
import { Gesture } from './types';

type MoveHooksProps = {
setActiveGesture: React.Dispatch<React.SetStateAction<Gesture>>;
selectedWidgets: AnyWidget[];
selectedWidgets: Widget[];
cellSize: DashboardState['grid']['cellSize'];
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import React, { useState, useCallback } from 'react';
import React, { useCallback, useState } from 'react';
import { useDispatch } from 'react-redux';
import { Anchor, onResizeWidgetsAction } from '~/store/actions';
import { DashboardState } from '~/store/state';
import { Position, AnyWidget } from '~/types';
import { Position, Widget } from '~/types';
import { toGridPosition } from '~/util/position';
import { DragEvent } from '../../grid';
import { Gesture } from './types';

type ResizeHooksProps = {
setActiveGesture: React.Dispatch<React.SetStateAction<Gesture>>;
selectedWidgets: AnyWidget[];
selectedWidgets: Widget[];
cellSize: DashboardState['grid']['cellSize'];
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, { useState, useCallback } from 'react';
import React, { useCallback, useState } from 'react';
import { useDispatch } from 'react-redux';
import { onSelectWidgetsAction } from '~/store/actions';
import { DashboardState } from '~/store/state';
import { Position, AnyWidget, Selection } from '~/types';
import { Position, Selection, Widget } from '~/types';
import { getSelectedWidgets, pointSelect, selectedRect } from '~/util/select';
import { DragEvent } from '../../grid';
import { Gesture } from './types';
Expand All @@ -15,7 +15,7 @@ type SelectionHooksProps = {

export const useSelectionGestures = ({ setActiveGesture, dashboardConfiguration, cellSize }: SelectionHooksProps) => {
const dispatch = useDispatch();
const selectWidgets = (widgets: AnyWidget[], union: boolean) => {
const selectWidgets = (widgets: Widget[], union: boolean) => {
dispatch(
onSelectWidgetsAction({
widgets,
Expand Down
8 changes: 4 additions & 4 deletions packages/dashboard/src/components/internalDashboard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Box from '@cloudscape-design/components/box';
import { SiteWiseQuery } from '@iot-app-kit/source-iotsitewise';
import { WebglContext } from '@iot-app-kit/react-components';

import { AnyWidget, Position } from '~/types';
import { Position, Widget } from '~/types';
import { selectedRect } from '~/util/select';
import { DashboardMessages } from '~/messages';

Expand All @@ -30,9 +30,9 @@ import {
onBringWidgetsToFrontAction,
onCopyWidgetsAction,
onCreateWidgetsAction,
onDeleteWidgetsAction,
onPasteWidgetsAction,
onSendWidgetsToBackAction,
onDeleteWidgetsAction,
} from '~/store/actions';
import { DashboardState, SaveableDashboard } from '~/store/state';
import { widgetCreator } from '~/store/actions/createWidget/presets';
Expand Down Expand Up @@ -71,7 +71,7 @@ const InternalDashboard: React.FC<InternalDashboardProps> = ({
const [viewFrame, setViewFrameElement] = useState<HTMLDivElement | undefined>(undefined);

const dispatch = useDispatch();
const createWidgets = (widgets: AnyWidget[]) =>
const createWidgets = (widgets: Widget[]) =>
dispatch(
onCreateWidgetsAction({
widgets,
Expand Down Expand Up @@ -132,7 +132,7 @@ const InternalDashboard: React.FC<InternalDashboardProps> = ({

const { x, y } = toGridPosition(position, cellSize);

const widget: AnyWidget = {
const widget: Widget = {
...widgetPresets,
x: Math.floor(x),
y: Math.floor(y),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import {
} from '@cloudscape-design/components';
import ExpandableSectionHeader from '../../shared/expandableSectionHeader';
import { NonCancelableEventHandler } from '@cloudscape-design/components/internal/events';
import { AnyWidget } from '~/types';
import { Widget } from '~/types';
import { useWidgetLense } from '../../utils/useWidgetLense';
import { BarChartWidget, LineChartWidget, ScatterChartWidget } from '~/customization/widgets/types';
import { merge } from 'lodash';
import { AxisSettings } from '../../../../customization/settings';

export const isAxisSettingsSupported = (widget: AnyWidget): boolean =>
export const isAxisSettingsSupported = (widget: Widget): boolean =>
['iot-line', 'iot-scatter', 'iot-bar'].some((t) => t === widget.type);

export type AxisWidget = LineChartWidget | ScatterChartWidget | BarChartWidget;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BaseChangeDetail } from '@cloudscape-design/components/input/interfaces
import { ExpandableSection, Grid, Input } from '@cloudscape-design/components';

import { trimWidgetPosition } from '~/util/trimWidgetPosition';
import { AnyWidget } from '~/types';
import { Widget } from '~/types';
import { useWidgetLense } from '../../utils/useWidgetLense';

const defaultMessages = {
Expand All @@ -14,23 +14,23 @@ const defaultMessages = {
title: 'Size and position',
};

export const BaseSettings: FC<AnyWidget> = (widget) => {
const [x, updateX] = useWidgetLense<AnyWidget, number>(
export const BaseSettings: FC<Widget> = (widget) => {
const [x, updateX] = useWidgetLense<Widget, number>(
widget,
(w) => w.x,
(w, x) => ({ ...w, x })
);
const [y, updateY] = useWidgetLense<AnyWidget, number>(
const [y, updateY] = useWidgetLense<Widget, number>(
widget,
(w) => w.y,
(w, y) => ({ ...w, y })
);
const [width, updateWidth] = useWidgetLense<AnyWidget, number>(
const [width, updateWidth] = useWidgetLense<Widget, number>(
widget,
(w) => w.width,
(w, width) => ({ ...w, width })
);
const [height, updateHeight] = useWidgetLense<AnyWidget, number>(
const [height, updateHeight] = useWidgetLense<Widget, number>(
widget,
(w) => w.height,
(w, height) => ({ ...w, height })
Expand All @@ -46,7 +46,7 @@ export const BaseSettings: FC<AnyWidget> = (widget) => {
updateHeight(Math.round(parseFloat(value)));
const gridDefinition = [{ colspan: 2 }, { colspan: 4 }, { colspan: 2 }, { colspan: 4 }];

const formattedValue = trimWidgetPosition({ x, y, width, height } as AnyWidget);
const formattedValue = trimWidgetPosition({ x, y, width, height } as Widget);
return (
<ExpandableSection headerText={defaultMessages.title} defaultExpanded>
<Grid gridDefinition={gridDefinition}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import ExpandableSectionHeader from '../../shared/expandableSectionHeader';
import { PropertyComponent } from './propertyComponent';
import { useWidgetLense } from '../../utils/useWidgetLense';
import { StyleSettingsMap } from '@iot-app-kit/core';
import { AnyWidget } from '~/types';
import { Widget } from '~/types';

export const isPropertiesAndAlarmsSupported = (widget: AnyWidget): boolean =>
export const isPropertiesAndAlarmsSupported = (widget: Widget): boolean =>
['iot-line', 'iot-scatter', 'iot-bar', 'iot-table', 'iot-kpi', 'iot-status'].some((t) => t === widget.type);

export type PropertiesAlarmsSectionProps = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
StatusWidget,
TableWidget,
} from '~/customization/widgets/types';
import { AnyWidget } from '~/types';
import { Widget } from '~/types';

export type ThresholdWidget =
| KPIWidget
Expand All @@ -29,7 +29,7 @@ export type ThresholdWidget =
| BarChartWidget
| TableWidget;

export const isThresholdsSupported = (widget: AnyWidget): boolean =>
export const isThresholdsSupported = (widget: Widget): boolean =>
['iot-kpi', 'iot-status', 'iot-line', 'iot-scatter', 'iot-bar', 'iot-table'].some((t) => t === widget.type);

const widgetsSupportsContainOp: string[] = ['iot-kpi', 'iot-status', 'iot-table'];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useCallback, useMemo } from 'react';
import { useWidgetActions } from '~/customization/hooks/useWidgetActions';
import { AnyWidget } from '~/types';
import { Widget } from '~/types';

export const useWidgetLense = <W extends AnyWidget, T>(
export const useWidgetLense = <W extends Widget, T>(
widget: W,
selector: (widget: W) => T,
updater: (widget: W, value: T) => W
Expand Down
6 changes: 3 additions & 3 deletions packages/dashboard/src/components/widgets/dynamicWidget.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

import { AnyWidget } from '~/types';
import { Widget } from '~/types';
import { WidgetsMessages } from '~/messages';
import { WidgetComponentMap } from '~/customization/widgetComponentMap';

Expand All @@ -21,15 +21,15 @@ const IconX: React.FC = () => (
);

export type DynamicWidgetProps = {
widget: AnyWidget;
widget: Widget;
widgetsMessages: WidgetsMessages;
};

export const getDragLayerProps = ({
widget,
widgetsMessages,
}: {
widget: AnyWidget;
widget: Widget;
widgetsMessages: WidgetsMessages;
}): DynamicWidgetProps => ({
widget,
Expand Down
4 changes: 2 additions & 2 deletions packages/dashboard/src/components/widgets/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import map from 'lodash/map';
import includes from 'lodash/includes';
import { SiteWiseQuery } from '@iot-app-kit/source-iotsitewise';

import { AnyWidget, DashboardConfiguration } from '~/types';
import { DashboardConfiguration, Widget } from '~/types';
import WidgetComponent from './widget';
import SelectionBox from './selectionBox';
import { DashboardMessages } from '~/messages';
Expand All @@ -15,7 +15,7 @@ export type WidgetsProps = {
readOnly: boolean;
query?: SiteWiseQuery;
dashboardConfiguration: DashboardConfiguration;
selectedWidgets: AnyWidget[];
selectedWidgets: Widget[];
cellSize: number;
dragEnabled: boolean;
messageOverrides: DashboardMessages;
Expand Down
4 changes: 2 additions & 2 deletions packages/dashboard/src/components/widgets/selectionBox.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

import { AnyWidget } from '~/types';
import { Widget } from '~/types';
import SelectionBoxAnchor from './selectionBoxAnchor';
import { gestureable } from '../internalDashboard/gestures/determineTargetGestures';
import { getSelectionBox } from '~/util/getSelectionBox';
Expand All @@ -9,7 +9,7 @@ import './selectionBox.css';
import { useLayers } from '../internalDashboard/useLayers';

export type SelectionBoxProps = {
selectedWidgets: AnyWidget[];
selectedWidgets: Widget[];
cellSize: number;
dragEnabled: boolean;
};
Expand Down
4 changes: 2 additions & 2 deletions packages/dashboard/src/components/widgets/widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react';
import { SiteWiseQuery } from '@iot-app-kit/source-iotsitewise';
import { DashboardMessages } from '~/messages';

import { DashboardConfiguration, AnyWidget } from '~/types';
import { DashboardConfiguration, Widget } from '~/types';
import { gestureable, idable } from '../internalDashboard/gestures/determineTargetGestures';
import DynamicWidgetComponent from './dynamicWidget';

Expand All @@ -14,7 +14,7 @@ export type WidgetProps = {
query?: SiteWiseQuery;
isSelected: boolean;
cellSize: number;
widget: AnyWidget;
widget: Widget;
viewport: DashboardConfiguration['viewport'];
messageOverrides: DashboardMessages;
};
Expand Down
12 changes: 6 additions & 6 deletions packages/dashboard/src/customization/api.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import React from 'react';
import { AnyWidget } from '~/types';
import { Widget } from '~/types';
import { ComponentLibraryComponentMap, ComponentLibraryComponentOrdering } from './componentLibraryComponentMap';
import { WidgetComponentMap } from './widgetComponentMap';
import { WidgetPropertiesGeneratorMap } from './widgetPropertiesGeneratorMap';

type RenderFunc<T extends AnyWidget> = (widget: T) => React.ReactElement;
type RenderFunc<T extends Widget> = (widget: T) => React.ReactElement;

type WidgetRegistrationOptions<T extends AnyWidget> = {
type WidgetRegistrationOptions<T extends Widget> = {
render: RenderFunc<T>;
componentLibrary?: {
name: string;
icon: React.FC;
};
properties?: () => T['properties'];
initialSize?: Pick<AnyWidget, 'height' | 'width'>;
initialSize?: Pick<Widget, 'height' | 'width'>;
};
type RegisterWidget = <T extends AnyWidget>(type: string, options: WidgetRegistrationOptions<T>) => void;
type RegisterWidget = <T extends Widget>(type: string, options: WidgetRegistrationOptions<T>) => void;

/**
* function to register a new widget type in the dashboard
*/
export const registerWidget: RegisterWidget = <T extends AnyWidget>(
export const registerWidget: RegisterWidget = <T extends Widget>(
type: string,
options: WidgetRegistrationOptions<T>
) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/dashboard/src/customization/hooks/useIsSelected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import includes from 'lodash/includes';
import map from 'lodash/map';

import { DashboardState } from '~/store/state';
import { AnyWidget } from '~/types';
import { Widget } from '~/types';
import { useCallback } from 'react';

/**
Expand All @@ -12,7 +12,7 @@ import { useCallback } from 'react';
* used to determine if this widget is in the selection
*
*/
export const useIsSelected = <T extends AnyWidget>(widget: T) => {
export const useIsSelected = <T extends Widget>(widget: T) => {
const selectedWidgets = useSelector((state: DashboardState) => state.selectedWidgets);

const isSelected = useCallback(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { useDispatch } from 'react-redux';

import { onUpdateWidgetsAction } from '~/store/actions';
import { AnyWidget } from '~/types';
import { Widget } from '~/types';

/**
* Helper hook that can be exposed to consumers making their own widget components
*
* used to update itself in the store
*
*/
export const useWidgetActions = <T extends AnyWidget>() => {
export const useWidgetActions = <T extends Widget>() => {
const dispatch = useDispatch();

const update = (widget: T) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { AnyWidget } from '../types';
import { Widget } from '~/types';

/**
* map of widget type to a generator func to create properties when this widget is dropped into the grid
*
* also the initial size of that widget in real pixels
*
*/
export const WidgetPropertiesGeneratorMap: {

type WidgetPropertiesGenerator<W extends Widget = Widget> = {
[key in string]: {
//eslint-disable-next-line
properties?: () => Record<any, any>;
initialSize?: Pick<AnyWidget, 'height' | 'width'>;
properties?: () => W['properties'];
initialSize?: Pick<W, 'height' | 'width'>;
};
} = {};
};
export const WidgetPropertiesGeneratorMap: WidgetPropertiesGenerator = {};
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { bringWidgetsToFront } from '.';
import { DashboardState, initialState } from '../../state';

import { MockWidgetFactory, MOCK_KPI_WIDGET } from '../../../../testing/mocks';
import { AnyWidget } from '~/types';
import { MOCK_KPI_WIDGET, MockWidgetFactory } from '../../../../testing/mocks';
import { Widget } from '~/types';

const setupDashboardState = (widgets: AnyWidget[] = [], selectedWidgets: AnyWidget[] = []): DashboardState => ({
const setupDashboardState = (widgets: Widget[] = [], selectedWidgets: Widget[] = []): DashboardState => ({
...initialState,
dashboardConfiguration: {
...initialState.dashboardConfiguration,
Expand Down
Loading

0 comments on commit bcf37ea

Please sign in to comment.