Skip to content

Commit

Permalink
chore: reworked contexts js files into ts
Browse files Browse the repository at this point in the history
  • Loading branch information
flops committed Dec 9, 2024
1 parent e44f768 commit 1c0ea1c
Show file tree
Hide file tree
Showing 18 changed files with 191 additions and 94 deletions.
7 changes: 1 addition & 6 deletions .babelrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@
}
}
],
[
"@babel/preset-typescript",
{
"allowDeclareFields": true
}
],
"@babel/preset-typescript",
"@babel/preset-react"
],
"plugins": []
Expand Down
31 changes: 17 additions & 14 deletions src/components/DashKit/DashKit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import noop from 'lodash/noop';
import pick from 'lodash/pick';

import {DEFAULT_GROUP, DEFAULT_NAMESPACE} from '../../constants';
import {DashKitDnDContext} from '../../context/DashKitContext';
import {DashKitDnDContext} from '../../context';
import type {
Config,
ConfigItem,
Expand All @@ -13,7 +13,7 @@ import type {
ItemDropProps,
ItemsStateAndParams,
} from '../../shared';
import {
import type {
AddConfigItem,
AddNewItemOptions,
ContextProps,
Expand All @@ -31,10 +31,9 @@ import {RegisterManager, UpdateManager, reflowLayout} from '../../utils';
import {DashKitDnDWrapper} from '../DashKitDnDWrapper/DashKitDnDWrapper';
import DashKitView from '../DashKitView/DashKitView';
import GridLayout from '../GridLayout/GridLayout';
import {OverlayControlItem, OverlayControlsCtxShape} from '../OverlayControls/OverlayControls';
import type {OverlayControlItem, PreparedCopyItemOptions} from '../OverlayControls/OverlayControls';

interface DashKitGeneralProps
extends Pick<OverlayControlsCtxShape, 'getPreparedCopyItemOptions' | 'onCopyFulfill'> {
interface DashKitGeneralProps {
config: Config;
editMode: boolean;
draggableHandleClassName?: string;
Expand All @@ -43,32 +42,36 @@ interface DashKitGeneralProps
}

interface DashKitDefaultProps {
defaultGlobalParams: GlobalParams;
globalParams: GlobalParams;
itemsStateAndParams: ItemsStateAndParams;
settings: SettingsProps;
context: ContextProps;
noOverlay: boolean;
focusable?: boolean;
groups?: DashKitGroup[];

onItemEdit: (item: ConfigItem) => void;
onChange: (data: {
config: Config;
itemsStateAndParams: ItemsStateAndParams;
groups?: DashKitGroup[];
}) => void;

onDrop?: (dropProps: ItemDropProps) => void;

onItemMountChange?: (item: ConfigItem, state: {isAsync: boolean; isMounted: boolean}) => void;
onItemRender?: (item: ConfigItem) => void;

getPreparedCopyItemOptions?: (options: PreparedCopyItemOptions) => PreparedCopyItemOptions;
onCopyFulfill?: (error: null | Error, data?: PreparedCopyItemOptions) => void;

onDragStart?: ItemManipulationCallback;
onDrag?: ItemManipulationCallback;
onDragStop?: ItemManipulationCallback;
onResizeStart?: ItemManipulationCallback;
onResize?: ItemManipulationCallback;
onResizeStop?: ItemManipulationCallback;

defaultGlobalParams: GlobalParams;
globalParams: GlobalParams;
itemsStateAndParams: ItemsStateAndParams;
settings: SettingsProps;
context: ContextProps;
noOverlay: boolean;
focusable?: boolean;
groups?: DashKitGroup[];
}

export interface DashKitProps extends DashKitGeneralProps, Partial<DashKitDefaultProps> {}
Expand Down
2 changes: 1 addition & 1 deletion src/components/DashKitDnDWrapper/DashKitDnDWrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

import {DashKitDnDContext} from '../../context/DashKitContext';
import {DashKitDnDContext} from '../../context';
import type {DraggedOverItem, ItemDragProps} from '../../shared';

type DashKitDnDWrapperProps = {
Expand Down
2 changes: 1 addition & 1 deletion src/components/DashKitView/DashKitView.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

import {DashKitContext} from '../../context/DashKitContext';
import {DashKitContext} from '../../context';
import {withContext} from '../../hocs/withContext';
import {useCalcPropsLayout} from '../../hooks/useCalcLayout';
import type {RegisterManager} from '../../utils';
Expand Down
2 changes: 1 addition & 1 deletion src/components/GridItem/GridItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react';
import PropTypes from 'prop-types';

import {FOCUSED_CLASS_NAME} from '../../constants';
import {DashKitContext} from '../../context/DashKitContext';
import {DashKitContext} from '../../context';
import {cn} from '../../utils/cn';
import Item from '../Item/Item';
import OverlayControls from '../OverlayControls/OverlayControls';
Expand Down
2 changes: 1 addition & 1 deletion src/components/GridLayout/GridLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
DRAGGABLE_CANCEL_CLASS_NAME,
TEMPORARY_ITEM_ID,
} from '../../constants';
import {DashKitContext} from '../../context/DashKitContext';
import {DashKitContext} from '../../context';
import {resolveLayoutGroup} from '../../utils';
import GridItem from '../GridItem/GridItem';

Expand Down
15 changes: 2 additions & 13 deletions src/components/MobileLayout/MobileLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

import {DashKitContext} from '../../context/DashKitContext';
import {DashKitContext} from '../../context';
import {cn} from '../../utils/cn';
import Item from '../Item/Item';

Expand All @@ -13,7 +13,6 @@ const b = cn('dashkit-mobile-layout');
type MobileLayoutProps = {};

type MobileLayoutState = {
activeTabId: string | null;
indexesOfItemsWithActiveAutoheight: Record<string, boolean>;
};

Expand All @@ -24,7 +23,7 @@ export default class MobileLayout extends React.PureComponent<
MobileLayoutState
> {
static contextType = DashKitContext;
declare context: React.ContextType<typeof DashKitContext>;
context!: React.ContextType<typeof DashKitContext>;

pluginsRefs: PlugibRefObject[] = [];
sortedLayoutItems: ReturnType<typeof getSortedConfigItems> | null = null;
Expand All @@ -34,19 +33,9 @@ export default class MobileLayout extends React.PureComponent<
_memoAdjustWidgetLayout: Array<(props: {needSetDefault: boolean}) => void> = [];

state: MobileLayoutState = {
activeTabId: null,
indexesOfItemsWithActiveAutoheight: {},
};

componentDidUpdate(_prevProps: MobileLayoutProps, prevState: MobileLayoutState) {
if (prevState.activeTabId !== this.context.config.id) {
this.setState({
activeTabId: this.context.config.id,
indexesOfItemsWithActiveAutoheight: {},
});
}
}

render() {
const {config, layout} = this.context;

Expand Down
29 changes: 7 additions & 22 deletions src/components/OverlayControls/OverlayControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ import {
OVERLAY_CONTROLS_CLASS_NAME,
OVERLAY_ICON_SIZE,
} from '../../constants';
import {DashkitOvelayControlsContext} from '../../context/DashKitContext';
import {DashkitOvelayControlsContext, OverlayControlsCtxShape} from '../../context';
import {i18n} from '../../i18n';
import {
type ConfigItem,
type ConfigLayout,
type ItemParams,
type ItemState,
type ItemsStateAndParamsBase,
type PluginBase,
type StringParams,
isItemWithTabs,
resolveItemInnerId,
} from '../../shared';
Expand Down Expand Up @@ -63,7 +61,7 @@ export interface OverlayCustomControlItem {
title?: string;
icon?: MenuItemProps['icon'];
iconSize?: number | string;
handler?: (item: ConfigItem, params: StringParams, state: ItemState) => void;
handler?: (item: ConfigItem, params: ItemParams, state: ItemState) => void;
visible?: (item: ConfigItem) => boolean;
className?: string;
qa?: string;
Expand Down Expand Up @@ -94,19 +92,6 @@ export type PreparedCopyItemOptions<C extends object = {}> = PreparedCopyItemOpt
copyContext?: C;
};

export interface OverlayControlsCtxShape {
overlayControls?: Record<string, OverlayControlItem[]>;
context: Record<string, any>;
menu: MenuItem[];
itemsStateAndParams: ItemsStateAndParamsBase;
itemsParams: Record<string, StringParams>;
editItem: (item: ConfigItem) => void;
removeItem: (id: string) => void;
getLayoutItem: (id: string) => ConfigLayout | void;
getPreparedCopyItemOptions?: (options: PreparedCopyItemOptions) => PreparedCopyItemOptions;
onCopyFulfill?: (error: null | Error, data?: PreparedCopyItemOptions) => void;
}

type OverlayControlsCtx = React.Context<OverlayControlsCtxShape>;

const DEFAULT_DROPDOWN_MENU = [MenuItems.Copy, MenuItems.Delete];
Expand Down Expand Up @@ -266,13 +251,13 @@ class OverlayControls extends React.Component<OverlayControlsProps> {
}
private renderDropdownMenu(isOnlyOneItem: boolean) {
const {view, size, onItemClick} = this.props;
const {menu: contextMenu, itemsParams, itemsStateAndParams} = this.context;
const {menu: contextMenu, itemsParams, itemsState} = this.context;

const configItem = this.props.configItem;
const itemParams = itemsParams[configItem.id];
const itemState = itemsStateAndParams[configItem.id]?.state || {};
const itemState = itemsState?.[configItem.id] || {};

const menu = contextMenu?.length > 0 ? contextMenu : DEFAULT_DROPDOWN_MENU;
const menu = contextMenu && contextMenu.length > 0 ? contextMenu : DEFAULT_DROPDOWN_MENU;

const isDefaultMenu = this.isDefaultMenu(menu);

Expand Down Expand Up @@ -435,7 +420,7 @@ class OverlayControls extends React.Component<OverlayControlsProps> {
this.props.onItemClick?.();
};
private onEditItem = () => {
this.context.editItem(this.props.configItem);
this.context.editItem?.(this.props.configItem);
this.props.onItemClick?.();
};
private onRemoveItem = () => {
Expand Down
9 changes: 0 additions & 9 deletions src/context/DashKitContext.js

This file was deleted.

79 changes: 79 additions & 0 deletions src/context/DashKitContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import React from 'react';

import type {RegisterManager} from '..//utils';
import type {DashKitProps} from '../components/DashKit';
import type {
ConfigItem,
ConfigLayout,
ItemParams,
ItemState,
ItemStateAndParams,
ItemStateAndParamsChangeOptions,
} from '../shared';

type DashkitPropsPassedToCtx = Pick<
DashKitProps,
| 'config'
| 'groups'
| 'context'
| 'noOverlay'
| 'focusable'
| 'globalParams'
| 'editMode'
| 'settings'
| 'onItemMountChange'
| 'onItemRender'
| 'draggableHandleClassName'
// default handlers bypass
| 'onDragStart'
| 'onDrag'
| 'onDragStop'
| 'onResizeStart'
| 'onResize'
| 'onResizeStop'
>;

type PluginType = string;

export type DashKitCtxShape = DashkitPropsPassedToCtx & {
registerManager: RegisterManager;
forwardedMetaRef: React.ForwardedRef<any>;

layout: ConfigLayout[];
temporaryLayout: ConfigLayout[] | null;
memorizeOriginalLayout: (
widgetId: string,
preAutoHeightLayout: ConfigLayout,
postAutoHeightLayout: ConfigLayout,
) => void;
revertToOriginalLayout: (widgetId: string) => void;

itemsState?: Record<string, ItemState>;
itemsParams: Record<string, ItemParams>;
onItemStateAndParamsChange: (
id: string,
stateAndParams: ItemStateAndParams,
options: ItemStateAndParamsChangeOptions,
) => void;

getItemsMeta: (pluginsRefs: Array<React.RefObject<any>>) => Array<Promise<any>>;
reloadItems: (
pluginsRefs: Array<React.RefObject<any>>,
data: {silentLoading: boolean; noVeil: boolean},
) => void;

onDrop: (newLayout: ConfigLayout, item: ConfigItem) => void;
onDropDragOver: (
e: DragEvent | MouseEvent,
group: string | void,
gridProps: Partial<ReactGridLayout.ReactGridLayoutProps>,
groupLayout: ConfigLayout[],
sharedItem: (Partial<ConfigLayout> & {type: PluginType}) | void,
) => void | boolean;
outerDnDEnable: boolean;
dragOverPlugin: null | PluginType;
};

const DashKitContext = React.createContext<DashKitCtxShape>({} as DashKitCtxShape);

export {DashKitContext};
16 changes: 16 additions & 0 deletions src/context/DashKitDnDContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';

import type {DraggedOverItem, ItemDragProps} from '../shared/types';

export type DashKitDnDCtxShape = {
dragProps: ItemDragProps | null;
dragImagePreview: HTMLImageElement;
onDragStart: (e: React.DragEvent<Element>, itemDragProps: ItemDragProps) => void;
onDragEnd: (e: React.DragEvent<Element>) => void;
onDropDragOver?: (
draggedItem: DraggedOverItem,
sharedItem: DraggedOverItem | null,
) => void | boolean;
};

export const DashKitDnDContext = React.createContext<DashKitDnDCtxShape | void>(undefined);
26 changes: 26 additions & 0 deletions src/context/DashkitOverlayControlsContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react';

import type {DashKitProps} from '../components';
import type {ConfigLayout, ItemParams, ItemState} from '../shared';
import type {MenuItem} from '../typings';

export type OverlayControlsCtxShape = Pick<
DashKitProps,
| 'context'
| 'overlayControls'
| 'itemsStateAndParams'
| 'getPreparedCopyItemOptions'
| 'onCopyFulfill'
> & {
menu: DashKitProps['overlayMenuItems'] | MenuItem[];
itemsState?: Record<string, ItemState>;
itemsParams: Record<string, ItemParams>;

editItem: DashKitProps['onItemEdit'];
removeItem: (id: string) => void;
getLayoutItem: (id: string) => ConfigLayout | void;
};

export const DashkitOvelayControlsContext = React.createContext<OverlayControlsCtxShape | void>(
undefined,
);
3 changes: 3 additions & 0 deletions src/context/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './DashKitContext';
export * from './DashkitOverlayControlsContext';
export * from './DashKitDnDContext';
2 changes: 1 addition & 1 deletion src/hocs/prepareItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react';
import isEqual from 'lodash/isEqual';
import PropTypes from 'prop-types';

import {DashKitContext} from '../context/DashKitContext';
import {DashKitContext} from '../context';

export function prepareItem(Component) {
return class PrepareItem extends React.Component {
Expand Down
Loading

0 comments on commit 1c0ea1c

Please sign in to comment.