All files / src/components/EnterpriseSettings/Branding/hooks useThemeSettings.ts

41.66% Statements 15/36
42.85% Branches 15/35
60% Functions 3/5
41.66% Lines 15/36

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                          48x 2183x 2183x 2183x 2183x   2183x 626x           626x 437x               189x                                                                               189x                 626x                                                                                     626x   626x                   2183x        
import { useThemeContext } from "../Provider/CustomThemeProvider";
import { useAppTheme } from "@theme/v2/Provider";
import { CustomPalette } from "@theme/v2/palette.interface";
import { checkPortals } from "@utils/routing";
import { useMemo } from "react";
 
type TActiveGradientColor = {
  light: string;
  dark: string;
  id: number;
  name: string;
};
 
const useThemeSettings = () => {
  const { isDarkMode, customThemeV2 } = useThemeContext();
  const { palette } = useAppTheme();
  const { isAcquirerPortal } = checkPortals();
  const { activeGradientColor } = customThemeV2 || {};
 
  const themeSettings = useMemo(() => {
    const getColor = (
      isDarkMode: boolean,
      palette: CustomPalette,
      activeGradientColor: TActiveGradientColor,
      isAcquirerPortal: boolean,
    ) => {
      if (isAcquirerPortal) {
        return {
          bgColorDefault: palette.gradient["ocean-blue"]?.default,
          bgColorHighlight: palette.gradient["ocean-blue"]?.highlight,
          textColor: palette.primitive?.blue["100"],
          isGradientText: false,
        };
      }
 
      switch (activeGradientColor?.name) {
        case "ocean_blue":
          return {
            bgColorDefault: palette.gradient["ocean-blue"]?.default,
            bgColorHighlight: palette.gradient["ocean-blue"]?.highlight,
            textColor: palette.primitive?.blue["100"],
            isGradientText: false,
          };
        case "citrus_peel":
          return {
            bgColorDefault: palette.gradient["citrus-peel"]?.default,
            bgColorHighlight: palette.gradient["citrus-peel"]?.highlight,
            textColor: palette.primitive?.["citrus-peel"]["100"],
            isGradientText: false,
          };
        case "moon_purple":
          return {
            bgColorDefault: palette.gradient["moon-purple"]?.default,
            bgColorHighlight: palette.gradient["moon-purple"]?.highlight,
            textColor: palette.primitive?.["moon-purple"]["100"],
            isGradientText: false,
          };
        case "Custom": {
          const textColor = isDarkMode
            ? activeGradientColor.dark
            : activeGradientColor.light;
 
          const bgColor = getBGColor(
            isDarkMode ? activeGradientColor.dark : activeGradientColor.light,
          );
 
          return {
            bgColorDefault: bgColor,
            bgColorHighlight: bgColor,
            textColor: textColor,
            colors: extractGradientColors(textColor), // We are using this colors only for Phosphor icons as we don't have the functionality implemented to directly apply the gradient color to the icon
            isGradientText: textColor.includes("linear-gradient"),
          };
        }
        default:
          return {
            bgColorDefault: "",
            bgColorHighlight: "",
            textColor: "",
            isGradientText: false,
          };
      }
    };
 
    const getBGColor = (color: string): string => {
      let result: string;
      if (!color) {
        result = "rgba(0, 0, 0, 0.8)";
      } else if (color.toLowerCase().startsWith("linear-gradient")) {
        result = color.replace(
          /rgba?\((\d+),\s*(\d+),\s*(\d+),?\s*[\d.]*\)/gi,
          "rgba($1, $2, $3, 0.2)",
        );
      } else if (color.toLowerCase().startsWith("rgb(")) {
        result = color.replace(
          /rgb\((\d+),\s*(\d+),\s*(\d+)\)/,
          "rgba($1, $2, $3, 0.2)",
        );
      } else if (color.toLowerCase().startsWith("rgba(")) {
        result = color.replace(
          /rgba\((\d+),\s*(\d+),\s*(\d+),?\s*[\d.]*\)/,
          "rgba($1, $2, $3, 0.2)",
        );
      } else {
        result = color;
      }
      return result;
    };
 
    function extractGradientColors(gradient: string) {
      const colorRegex = /(rgba?\([\d\s,]+\))/gi;
 
      const colors = gradient.toString().match(colorRegex);
 
      if (!colors || colors.length < 2) {
        return [];
      }
 
      return colors.slice(0, 2);
    }
 
    const {
      bgColorDefault,
      bgColorHighlight,
      textColor,
      colors,
      isGradientText,
    } = getColor(isDarkMode, palette, activeGradientColor, isAcquirerPortal);
 
    return {
      bgColorDefault: bgColorDefault || "",
      bgColorHighlight: bgColorHighlight || "",
      textColor: textColor || "",
      colors: colors || [],
      isGradientText: isGradientText || false,
      isCustomColor: activeGradientColor?.name === "Custom",
    };
  }, [isDarkMode, palette, activeGradientColor, isAcquirerPortal]);
 
  return themeSettings;
};
 
export default useThemeSettings;