import { useState } from "react"; import { UserType } from "../providers"; import { getCookie, setCookie } from "../lib"; import { USER_TYPE_COOKIE_NAME } from "../vars"; type UseUserOutput = { isJuridicalUser: boolean; changeUserType: (value: UserType) => void; }; /** * Hook return user type and callback for change that * Using in useUser context */ export const useUserType = (): UseUserOutput => { const [userType, setUserType] = useState( (getCookie(USER_TYPE_COOKIE_NAME) as UserType) || UserType.Individual, ); const isJuridicalUser = userType === UserType.JuridicalPerson; const changeUserType = (value: UserType): void => { const { hostname } = window?.location; const regex = /(\.\S+\.ru)/g; const domain = hostname?.match(regex)?.[0] setUserType(value); if (domain) { setCookie(USER_TYPE_COOKIE_NAME, value, { domain: `${domain}`, }); } }; return { isJuridicalUser, changeUserType, }; };