fix: update generation key logic

This commit is contained in:
yassinedorbozgithub 2024-12-06 11:18:42 +01:00
parent 9580b4fe6c
commit 49e74fd8fa
3 changed files with 10 additions and 57 deletions

View File

@ -8,46 +8,33 @@
import { Avatar, Box } from "@mui/material";
import UiChatWidget from "hexabot-chat-widget/src/UiChatWidget";
import {
generateHashFromString,
getSettingValues,
} from "hexabot-chat-widget/src/utils/text";
import { usePathname } from "next/navigation";
import { useEffect, useState } from "react";
import { getAvatarSrc } from "@/components/inbox/helpers/mapMessages";
import { useLoadSettings } from "@/hooks/entities/auth-hooks";
import { useAuth } from "@/hooks/useAuth";
import { useConfig } from "@/hooks/useConfig";
import { useSetting } from "@/hooks/useSetting";
import i18n from "@/i18n/config";
import { EntityType, RouterType } from "@/services/types";
import { generateId } from "@/utils/generateId";
import { ChatWidgetHeader } from "./ChatWidgetHeader";
const SETTING_TYPE = "console_channel" as const;
export const ChatWidget = () => {
const pathname = usePathname();
const { apiUrl } = useConfig();
const { isAuthenticated } = useAuth();
const isVisualEditor = pathname === `/${RouterType.VISUAL_EDITOR}`;
const { data: settings } = useLoadSettings();
const [key, setKey] = useState("");
const allowedDomainsSetting = useSetting(SETTING_TYPE, "allowed_domains");
const themeColorSetting = useSetting(SETTING_TYPE, "theme_color");
const [key, setKey] = useState(generateId());
useEffect(() => {
const settingValues = getSettingValues(settings, [
{
group: "console_channel",
label: "allowed_domains",
selectedField: "value",
},
{
group: "console_channel",
label: "theme_color",
selectedField: "value",
},
]);
generateHashFromString(settingValues.join()).then((key) => setKey(key));
}, [settings]);
setKey(generateId());
}, [allowedDomainsSetting, themeColorSetting]);
return isAuthenticated ? (
<Box

View File

@ -20,7 +20,7 @@ import { SessionStorage } from "../utils/sessionStorage";
import { useSubscribe } from "./SocketProvider";
export type ChannelSettings = {
type ChannelSettings = {
menu: IMenuNode[];
secret: string;
allowed_domains: string;

View File

@ -6,8 +6,6 @@
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/
import { ChannelSettings } from "../providers/SettingsProvider";
export const truncate = (s: string, length = 100) => {
return s.length > length ? s.substr(0, length) + "..." : s;
};
@ -23,35 +21,3 @@ export const processContent = (s: string) => {
return result;
};
export const generateHashFromString = async (str: string) => {
const msgBuffer = new TextEncoder().encode(str);
const hashBuffer = await crypto.subtle.digest("SHA-256", msgBuffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
return hashHex;
};
type TChannel = "console_channel";
type TSetting = {
group: TChannel;
label: keyof ChannelSettings;
value?: string;
};
type TSettings = Record<string, TSetting[]>;
type TCriterion = {
group: TSetting["group"];
label: TSetting["label"];
selectedField: keyof TSetting;
};
export const getSettingValue = (settings: TSettings, criterion: TCriterion) =>
settings?.[criterion.group]?.find(
(setting) => setting.label === criterion.label,
)?.[criterion?.selectedField];
export const getSettingValues = (settings: TSettings, criteria: TCriterion[]) =>
criteria.map((criterion) => getSettingValue(settings, criterion));