fix: app crashing due to invalid hex (#11542)

pull/10470/head^2
Nafees Nazik 2023-09-27 19:28:52 +05:30 committed by GitHub
parent c9b87bbf85
commit 01e2ba3fd9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 2 deletions

View File

@ -31,6 +31,15 @@ export function isValidHexCode(hexColor: string): boolean {
return isHexColor; return isHexColor;
} }
// credit : https://github.com/validatorjs/validator.js/blob/master/src/lib/isHexColor.js
const getValidHEX = (hex: string, defaultHex: string): string => {
const hexColor = /^#?([0-9A-F]{3}|[0-9A-F]{4}|[0-9A-F]{6}|[0-9A-F]{8})$/i.test(hex.replace("##", "#"));
if (hexColor) {
return hex;
}
return defaultHex;
};
function hexToRgb(hex: string): Rgb { function hexToRgb(hex: string): Rgb {
const sanitizedHex = hex.replace("##", "#"); const sanitizedHex = hex.replace("##", "#");
const colorParts = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(sanitizedHex); const colorParts = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(sanitizedHex);
@ -174,8 +183,8 @@ const useGetBrandingColours = ({
darkVal = normalizeHexCode(darkVal, true); darkVal = normalizeHexCode(darkVal, true);
const lightColourMap = createColorMap(lightVal); const lightColourMap = createColorMap(getValidHEX(lightVal, BRAND_COLOR));
const darkColourMap = createColorMap(darkVal); const darkColourMap = createColorMap(getValidHEX(darkVal, DARK_BRAND_COLOR));
const theme = { const theme = {
light: { light: {