UNPKG

@hhgtech/hhg-components

Version:
89 lines (86 loc) 4.43 kB
import React__default, { createContext, useContext, useRef } from 'react'; import { deleteCookie } from './miscCookieHelper.js'; import { u as useSSOV2Store, I as IS_SSOV2_ENABLED } from './store-994a3f4d.js'; import { T as TogetherComponentGlobalContext, j as getUrlToSignUp } from './utils-40e61585.js'; import { B as BEARER_TOKEN_COOKIE } from './constants-f4091ce6.js'; import './tslib.es6-ea4dfe68.js'; import 'zustand'; import './index-235eabf2.js'; import './constantsIsProduction.js'; import './normalizeLink-593b397a.js'; import './constantsDomainLocales.js'; import './Locale-f270bd9d.js'; import './index-5e947517.js'; import 'date-fns/locale'; import './index-90813715.js'; import './constantsSite.js'; import './constantsRiskScreener.js'; import './index-963a1701.js'; import 'slugify'; import 'string-format'; import './togetherApiPaths.js'; const TogetherAuthConfigContext = createContext({}); const TogetherAuthConfigContextProvider = ({ children, defaultParams, }) => { return (React__default.createElement(TogetherAuthConfigContext.Provider, { value: { defaultParams, } }, children)); }; const useTogetherAuthRequiredAction = (propsParams) => { const { triggerLogin } = useSSOV2Store(); const { data: { userInfo, env: { togetherBasePath }, }, } = useContext(TogetherComponentGlobalContext); const { defaultParams } = useContext(TogetherAuthConfigContext) || {}; const loggedInRef = useRef(!!(userInfo === null || userInfo === void 0 ? void 0 : userInfo.id)); loggedInRef.current = !!(userInfo === null || userInfo === void 0 ? void 0 : userInfo.id); const isFirstInteractRef = useRef(userInfo && typeof userInfo.communities_interacted === 'number' && userInfo.communities_interacted === 0); isFirstInteractRef.current = userInfo && typeof userInfo.communities_interacted === 'number' && userInfo.communities_interacted === 0; const authActionWrapper = (func, extraParams) => { const params = Object.assign(Object.assign(Object.assign({}, defaultParams), propsParams), extraParams); if (!loggedInRef.current || isFirstInteractRef.current) { if (!loggedInRef.current) { deleteCookie(BEARER_TOKEN_COOKIE); } if (params === null || params === void 0 ? void 0 : params._joinCommunityIds) { localStorage.setItem('to_join_community_ids', JSON.stringify(Array.from(new Set((JSON.parse(localStorage.getItem('to_join_community_ids') || '[]') || []) .concat(params._joinCommunityIds) .filter(Boolean))))); params === null || params === void 0 ? true : delete params._joinCommunityIds; } if (params === null || params === void 0 ? void 0 : params._joinTopicIds) { localStorage.setItem('to_join_topic_ids', JSON.stringify(Array.from(new Set((JSON.parse(localStorage.getItem('to_join_topic_ids') || '[]') || []) .concat(params._joinTopicIds) .filter(Boolean))))); params === null || params === void 0 ? true : delete params._joinTopicIds; } const currentLocation = (params === null || params === void 0 ? void 0 : params._location) || window.location.href; params === null || params === void 0 ? true : delete params._location; const redirectUrl = getUrlToSignUp(currentLocation, params, togetherBasePath); if (IS_SSOV2_ENABLED()) { const returnUrlParam = redirectUrl.slice(redirectUrl.indexOf('returnUrl=') + 10); if (returnUrlParam) { const originalRedirectUrl = decodeURIComponent(returnUrlParam); triggerLogin({ returnUrl: originalRedirectUrl, source: 'communities', }); } } else { window.location.href = redirectUrl; } return null; } else { return func && func(); } }; return { authActionWrapper, params: Object.assign(Object.assign({}, defaultParams), propsParams), }; }; export { TogetherAuthConfigContext, TogetherAuthConfigContextProvider, useTogetherAuthRequiredAction };