Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | 23x 23x 23x 23x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x | import { Stack } from "@mui/material";
import SectionCardBase from "@shared/SidePanel/components/SectionCard/SectionCardBase";
import GiveText from "@shared/Text/GiveText";
import GiveTruncateText from "@shared/Text/GiveTruncateText";
import { useMerchantSidePanelContext } from "../Provider/MerchantSidePanelProvider";
import { Virtuoso, VirtuosoHandle } from "react-virtuoso";
import { useRef } from "react";
import { ENTERPRISE_CONFIGURATION_MODAL } from "modals/modal_names";
import NiceModal from "@ebay/nice-modal-react";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { useAccessControl } from "@features/Permissions/AccessControl";
import RESOURCE_BASE, { OPERATIONS } from "@constants/permissions";
import { checkoutScrollbarStyles } from "@sections/PayBuilder/Checkout/helpers";
import { useAppTheme } from "@theme/v2/Provider";
import { useFormContext } from "react-hook-form";
import GiveButton from "@shared/Button/GiveButton";
import { useGetEnterpriseConfigurationById } from "../Modals/ProviderConfigurationModal/hooks/useGetEnterpriseConfigurationById";
import { EnterpriseConfigurationList } from "../Modals/ProviderConfigurationModal/constants/EnterpriseConfiguration.constants";
const ITEM_HEIGHT = 44;
const MAX_HEIGHT = 280;
const MAX_ITEMS_WITHOUT_SCROLL = Math.floor(MAX_HEIGHT / ITEM_HEIGHT);
const getHeight = (itemsLength: number): number =>
Math.min(itemsLength * ITEM_HEIGHT, MAX_HEIGHT);
type TConfiguration = {
id?: number;
name: string;
isActive: boolean;
};
export default function ProviderConfigurationSection() {
const { id } = useMerchantSidePanelContext();
const { isMobileView } = useCustomThemeV2();
const formContext = useFormContext();
// Use form data if available (create flow), otherwise use API data (side panel)
const formPermissions = formContext?.watch?.("permissions");
// Check if we're in create flow (formContext exists and has permissions)
const isCreateFlow = formContext && formPermissions !== undefined;
// Fetch configuration data for side panel view
const { data: configData } = useGetEnterpriseConfigurationById(id, {
enabled: !isCreateFlow && Boolean(id),
});
const configurations: TConfiguration[] =
isCreateFlow && formPermissions && Object.keys(formPermissions).length > 0
? Object.entries(formPermissions).map(([name, isActive]) => ({
name,
isActive: isActive as boolean,
}))
: configData?.data || [];
const dataLength = configurations.length;
const mobileDisplayCount = Math.min(5, dataLength);
const displayCount = isMobileView ? mobileDisplayCount : dataLength;
const listHeight = getHeight(displayCount) - 20; //the last item should not have the extra padding
const shouldShowScrollbar = dataLength > MAX_ITEMS_WITHOUT_SCROLL;
const scroller = useRef<VirtuosoHandle>(null);
const isUpdateConfigAllowed = useAccessControl({
resource: RESOURCE_BASE.ENTERPRISE,
operation: OPERATIONS.UPDATE,
});
const editHandler = () => {
NiceModal.show(ENTERPRISE_CONFIGURATION_MODAL, {
enterpriseId: id,
// In create flow, use onClose to update form instead of making API call
...(isCreateFlow &&
formContext?.setValue && {
onClose: (data: any) => formContext.setValue("permissions", data),
data: formPermissions || {},
}),
});
};
const actions = {
handleEdit: editHandler,
};
const disabledActions = {
handleEdit: { hidden: !isUpdateConfigAllowed },
};
return (
<SectionCardBase
leftTitle="Configurations"
actions={actions}
disabledActions={disabledActions}
childrenContainerSx={
shouldShowScrollbar && !isMobileView
? {
paddingRight: "4px",
".custom-virtuoso": {
...checkoutScrollbarStyles,
"&::-webkit-scrollbar-thumb": {
...checkoutScrollbarStyles["&::-webkit-scrollbar-thumb"],
border: "none",
},
"& > div": {
paddingRight: "10px",
},
},
}
: undefined
}
>
{dataLength > 0 && (
<>
<Virtuoso
ref={scroller}
style={{
height: listHeight,
width: "100%",
overflowY:
shouldShowScrollbar && !isMobileView ? "auto" : "hidden",
}}
className="custom-virtuoso"
data={isMobileView ? configurations.slice(0, 5) : configurations}
itemContent={(index, config) => {
const isLastItem = isMobileView
? index === mobileDisplayCount - 1
: index === dataLength - 1;
return (
<ConfigurationListItem
name={config.name}
isActive={config.isActive}
isLastItem={isLastItem}
key={index}
/>
);
}}
/>
{isMobileView && dataLength > 5 && (
<GiveButton
variant="filled"
color="light"
size="large"
label="See All"
onClick={editHandler}
disabled={!isUpdateConfigAllowed}
sx={{ width: "100%" }}
/>
)}
</>
)}
</SectionCardBase>
);
}
type ConfigurationProps = {
name: string;
isActive: boolean;
isLastItem?: boolean;
};
function ConfigurationListItem({
name,
isActive,
isLastItem,
}: ConfigurationProps) {
const { palette } = useAppTheme();
const displayName = EnterpriseConfigurationList.find(
(item) => item.name === name,
)?.displayName;
return (
<Stack
direction="row"
alignItems="center"
justifyContent="space-between"
gap="16px"
component="div"
paddingBottom={isLastItem ? 0 : "20px"}
>
<GiveTruncateText
variant="bodyS"
color="primary"
flex={1}
lineClamp={1}
sx={{
wordBreak: "break-all",
}}
>
{displayName}
</GiveTruncateText>
<GiveText
variant="bodyS"
sx={{
color: isActive
? palette.primitive?.success[50]
: palette.primitive?.error[50],
flexShrink: 0,
}}
>
{isActive ? "Enabled" : "Disabled"}
</GiveText>
</Stack>
);
}
|