1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-01 00:08:27 +01:00
unleash.unleash/frontend/src/component/common/util.ts
Nuno Góis 4167a60588
feat: biome lint frontend (#4903)
Follows up on https://github.com/Unleash/unleash/pull/4853 to add Biome
to the frontend as well.


![image](https://github.com/Unleash/unleash/assets/14320932/1906faf1-fc29-4172-a4d4-b2716d72cd65)

Added a few `biome-ignore` to speed up the process but we may want to
check and fix them in the future.
2023-10-02 13:25:46 +01:00

169 lines
4.8 KiB
TypeScript

import { weightTypes } from 'constants/weightTypes';
import { IUiConfig } from 'interfaces/uiConfig';
import { INavigationMenuItem } from 'interfaces/route';
import { IFeatureVariant } from 'interfaces/featureToggle';
import { format, isValid } from 'date-fns';
import { IFeatureVariantEdit } from 'component/feature/FeatureView/FeatureVariants/FeatureEnvironmentVariants/EnvironmentVariantsModal/EnvironmentVariantsModal';
/**
* Handle feature flags and configuration for different plans.
*/
export const filterByConfig =
(config: IUiConfig) => (r: INavigationMenuItem) => {
if (r.flag) {
// Check if the route's `flag` is enabled in IUiConfig.flags.
const flags = config.flags as unknown as Record<string, boolean>;
return Boolean(flags[r.flag]);
}
if (r.notFlag) {
const flags = config.flags as unknown as Record<string, boolean>;
return !(flags[r.notFlag] === true);
}
if (r.configFlag) {
// Check if the route's `configFlag` is enabled in IUiConfig.
return Boolean(config[r.configFlag]);
}
return true;
};
export const scrollToTop = () => {
window.scrollTo(0, 0);
};
export const mapRouteLink = (route: INavigationMenuItem) => ({
...route,
path: route.path.replace('/*', ''),
route: route.path,
});
export const trim = (value: string): string => {
if (value?.trim) {
return value.trim();
} else {
return value;
}
};
export const parseDateValue = (value: string) => {
const date = new Date(value);
return `${format(date, 'yyyy-MM-dd')}T${format(date, 'HH:mm')}`;
};
export const parseValidDate = (value: string): Date | undefined => {
const parsed = new Date(value);
if (isValid(parsed)) {
return parsed;
}
};
export const calculateVariantWeight = (weight: number) => {
return weight / 10.0;
};
export function updateWeight(variants: IFeatureVariant[], totalWeight: number) {
if (variants.length === 0) {
return [];
}
const variantMetadata = variants.reduce(
({ remainingPercentage, variableVariantCount }, variant) => {
if (variant.weight && variant.weightType === weightTypes.FIX) {
remainingPercentage -= Number(variant.weight);
} else {
variableVariantCount += 1;
}
return {
remainingPercentage,
variableVariantCount,
};
},
{ remainingPercentage: totalWeight, variableVariantCount: 0 },
);
const { remainingPercentage, variableVariantCount } = variantMetadata;
if (remainingPercentage < 0) {
throw new Error('The traffic distribution total must equal 100%');
}
if (!variableVariantCount) {
throw new Error('There must be at least one variable variant');
}
const percentage = parseInt(
String(remainingPercentage / variableVariantCount),
);
return variants.map((variant) => {
if (variant.weightType !== weightTypes.FIX) {
variant.weight = percentage;
}
return variant;
});
}
export function updateWeightEdit(
variants: IFeatureVariantEdit[],
totalWeight: number,
) {
if (variants.length === 0) {
return [];
}
let { remainingPercentage, variableVariantCount } = variants.reduce(
({ remainingPercentage, variableVariantCount }, variant) => {
if (variant.weight && variant.weightType === weightTypes.FIX) {
remainingPercentage -= Number(variant.weight);
}
if (variant.weightType === weightTypes.VARIABLE) {
variableVariantCount += 1;
}
return {
remainingPercentage,
variableVariantCount,
};
},
{ remainingPercentage: totalWeight, variableVariantCount: 0 },
);
const getPercentage = () =>
Math.round(remainingPercentage / variableVariantCount);
return variants.map((variant) => {
if (variant.weightType !== weightTypes.FIX) {
const percentage = getPercentage(); // round "as we go" - clean best effort approach
remainingPercentage -= percentage;
variableVariantCount -= 1;
variant.weight = percentage;
}
return variant;
});
}
export const modalStyles = {
overlay: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
backgroundColor: 'rgba(0, 0, 0, 0.25)',
zIndex: 5,
},
content: {
width: '500px',
maxWidth: '90%',
margin: '0',
top: '50%',
left: '50%',
right: 'auto',
bottom: 'auto',
transform: 'translate(-50%, -50%)',
},
};