PromucFlow_constructor/app/client/src/components/editorComponents/EntityBottomTabs.tsx

87 lines
2.7 KiB
TypeScript
Raw Normal View History

import React, { useState, useEffect, RefObject } from "react";
import { useDispatch, useSelector } from "react-redux";
import { setCurrentTab } from "actions/debuggerActions";
import {
CollapsibleTabProps,
collapsibleTabRequiredPropKeys,
TabComponent,
TabProp,
chore: migrate toast (#17208) * Refactor toast to be passed the dispatch hook externally * Add comments explaining dilemma * use store.dispatch instead of a hook * use alpha version * Change imports * Refactor DebugButton out * update release * fix issue with incorrectly merged package.lock * fix syntax of alpha version * bump ds vesion * copy lock from release * update lock to have alpha * make changes * delete Toast * DS package version updated * import change from release * use new alpha version * update ds version * update ds version * chore: migrate editable text and friends (#17285) * Delete empty components * use alpha for ds * Deleted EditableTextSubComponent, import changes * Delete EditableText, import changes * use ds alpha 10 * Delete EditableTextWrapper.tsx * update ds to use next minor version * use new alpha * fix issue with merge Co-authored-by: Albin <albin@appsmith.com> * chore: migrate file picker v2 (#17308) * use alpha ds * Delete FilePickerV2, import changes * Delete FilePicker, change imports * update alpha version * chore: move copy url form into setting components (#17322) * move CopyUrlForm to src/pages/settings/formgroup * update ds version to use next minor release * feat: Migrate table component to design system (#17329) * feat: Migrate table component to design system * removed commented code in ads index file * fix: table no data hover effect removed Co-authored-by: Tanvi Bhakta <tanvibhakta@gmail.com> * feat: Banner message component migrated to design system (#17327) * feat: Banner image component migrated to design system * Version update for design system package * design system version updated Co-authored-by: Tanvi Bhakta <tanvibhakta@gmail.com> * feat: Tabs component migrated to design system (#17321) * feat: Tabs component migrated to design system * design system package version updated * Update app/client/src/components/editorComponents/form/FormDialogComponent.tsx * Update app/client/src/pages/Editor/PropertyPane/PropertyPaneTab.tsx * Tab component expand issue fix Co-authored-by: Tanvi Bhakta <tanvibhakta@gmail.com> Co-authored-by: Albin <albin@appsmith.com> Co-authored-by: albinAppsmith <87797149+albinAppsmith@users.noreply.github.com>
2022-10-13 20:13:44 +00:00
} from "design-system";
import { getCurrentDebuggerTab } from "selectors/debuggerSelectors";
import AnalyticsUtil from "utils/AnalyticsUtil";
import { DEBUGGER_TAB_KEYS } from "./Debugger/helpers";
type EntityBottomTabsProps = {
defaultIndex: number;
tabs: TabProp[];
responseViewer?: boolean;
onSelect?: (tab: any) => void;
selectedTabIndex?: number; // this is used in the event you want to directly control the index changes.
canCollapse?: boolean;
// Reference to container for collapsing or expanding content
containerRef?: RefObject<HTMLElement>;
// height of container when expanded
expandedHeight?: string;
};
type CollapsibleEntityBottomTabsProps = EntityBottomTabsProps &
CollapsibleTabProps;
// Tab is considered collapsible only when all required collapsible props are present
export const isCollapsibleEntityBottomTab = (
props: EntityBottomTabsProps | CollapsibleEntityBottomTabsProps,
): props is CollapsibleEntityBottomTabsProps =>
collapsibleTabRequiredPropKeys.every((key) => key in props);
// Using this if there are debugger related tabs
function EntityBottomTabs(
props: EntityBottomTabsProps | CollapsibleEntityBottomTabsProps,
) {
const [selectedIndex, setSelectedIndex] = useState(props.defaultIndex);
const currentTab = useSelector(getCurrentDebuggerTab);
const dispatch = useDispatch();
const onTabSelect = (index: number) => {
dispatch(setCurrentTab(props.tabs[index].key));
props.onSelect && props.onSelect(props.tabs[index]);
setIndex(index);
};
const setIndex = (index: number) => {
const tabKey = props.tabs[index]?.key;
setSelectedIndex(index);
if (Object.values<string>(DEBUGGER_TAB_KEYS).includes(tabKey)) {
AnalyticsUtil.logEvent("DEBUGGER_TAB_SWITCH", {
tabName: tabKey,
});
}
};
useEffect(() => {
const index = props.tabs.findIndex((tab) => tab.key === currentTab);
if (index >= 0) {
setIndex(index);
} else {
setIndex(props.defaultIndex);
}
}, [currentTab]);
return (
<TabComponent
onSelect={onTabSelect}
responseViewer={props.responseViewer}
selectedIndex={
props.selectedTabIndex ? props.selectedTabIndex : selectedIndex
}
tabs={props.tabs}
{...(isCollapsibleEntityBottomTab(props)
? {
containerRef: props.containerRef,
expandedHeight: props.expandedHeight,
}
: {})}
/>
);
}
export default EntityBottomTabs;