@xo-union/tk-component-header-nav
Version:
28 lines (26 loc) • 1.18 kB
JavaScript
import _Set from "@babel/runtime-corejs3/core-js/set";
import { useState } from "react";
import { useLayoutEffect } from "../../utils/use-isomorphic-layout-effect.js";
const DO_NOT_RENDER_IN_PAGES = new _Set(['', 'dashboard', 'paper', 'knotrewards']);
const DISMISSED_SESSION_NAME = 'tk-header-rewards-bar-dismissed';
const useRewardsBarVisibility = _ref => {
let {
member,
location
} = _ref;
const [isEnabled, setIsEnabled] = useState(false);
useLayoutEffect(() => {
// Member's data
const isLoggedIn = !!member;
const hasRewardsProgram = member === null || member === void 0 ? void 0 : member.in_rewards_program;
// Page validation
const pathname = (location === null || location === void 0 ? void 0 : location.pathname) ?? '';
const [, currentPageSlug] = pathname.split('/', 2);
const isPageValid = !DO_NOT_RENDER_IN_PAGES.has(currentPageSlug);
// Session storage validation
const isDismissed = sessionStorage.getItem(DISMISSED_SESSION_NAME) === 'true';
setIsEnabled(!isDismissed && isPageValid && isLoggedIn && !!hasRewardsProgram);
}, [member, location]);
return isEnabled;
};
export default useRewardsBarVisibility;