github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/ui/dashboard/src/hooks/useDashboardVersionCheck.ts (about)

     1  import { IDashboardContext } from "../types";
     2  import { useEffect } from "react";
     3  
     4  const useDashboardVersionCheck = (state: IDashboardContext) => {
     5    useEffect(() => {
     6      let cliVersion: string | null = "";
     7      let uiVersion: string | null = "";
     8      let mismatchedVersions = false;
     9      if (state.versionMismatchCheck) {
    10        const cliVersionRaw = state.metadata?.cli?.version;
    11        const uiVersionRaw = process.env.REACT_APP_VERSION;
    12        const hasVersionsSet = !!cliVersionRaw && !!uiVersionRaw;
    13        cliVersion = !!cliVersionRaw
    14          ? cliVersionRaw.startsWith("v")
    15            ? cliVersionRaw.substring(1)
    16            : cliVersionRaw
    17          : null;
    18        uiVersion = !!uiVersionRaw
    19          ? uiVersionRaw.startsWith("v")
    20            ? uiVersionRaw.substring(1)
    21            : uiVersionRaw
    22          : null;
    23        mismatchedVersions = hasVersionsSet && cliVersion !== uiVersion;
    24  
    25        const searchParams = new URLSearchParams(window.location.search);
    26  
    27        // Add a version to force a reload with the new version to get the correct assets
    28        if (mismatchedVersions && cliVersionRaw) {
    29          searchParams.set("version", cliVersionRaw);
    30          window.location.replace(`${window.location.origin}?${searchParams}`);
    31        }
    32      }
    33    }, [state]);
    34  };
    35  
    36  export default useDashboardVersionCheck;