All files / src/utils/hooks useTheme.ts

68.42% Statements 13/19
43.75% Branches 7/16
100% Functions 4/4
68.42% Lines 13/19

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          66x               66x 65x   65x 15x 12x 10x 8x   8x     8x           8x             7x     65x    
import { ThemeInfoProps } from "@props/RecordProps";
import { getFaviconEl } from "@utils/ComponentUtils";
import { fetchDynamicThemeInfo } from "@utils/FetchUtils";
import { useEffect, useState } from "react";
 
export const defaultTheme = {
  title: "",
  favicon: "",
  backgroundImage: "",
  ssoEnable: false,
  ssoEnableLogic: ""
};
 
export const useTheme = (): ThemeInfoProps => {
  const [themeInfo, setThemeInfo] = useState<ThemeInfoProps>(defaultTheme);
 
  useEffect(() => {
    fetchDynamicThemeInfo().then((themeInfo: ThemeInfoProps): void => {
      setThemeInfo(themeInfo);
      Eif (themeInfo.title) {
        document.title = themeInfo.title;
      }
      Iif (themeInfo.backgroundImage && themeInfo.backgroundImage.length > 0) {
        document.body.style.backgroundImage = "url('" + themeInfo.backgroundImage + "')";
      }
      Iif (themeInfo.favicon && themeInfo.favicon.length > 0) {
        const faviconEl = getFaviconEl();
        if (faviconEl) {
          faviconEl.href = themeInfo.favicon;
        }
      }
      Iif (themeInfo.ssoEnableLogic && themeInfo.ssoEnableLogic?.length > 0) {
        //console.log(themeInfo.ssoEnableLogic, ' <--- themeInfo.ssoEnableLogic value');
        const ssoEnableEvalResult = eval(themeInfo.ssoEnableLogic);
        //console.log(ssoEnableEvalResult, ' <--- eval(themeInfo.ssoEnableLogic) value');
        themeInfo.ssoEnable = ssoEnableEvalResult;
      }
    }).catch((e: Error): void => {
      console.error(`Error fetching theme info: ${e}`);
    });
  }, []);
  return themeInfo;
};