UNPKG

@octopusdeploy/design-system-components

Version:
51 lines (50 loc) 2.16 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.OctopusRoutingProvider = OctopusRoutingProvider; exports.useOctopusLinkComponent = useOctopusLinkComponent; exports.useIsUrlActive = useIsUrlActive; exports.useDispatchLinkAnalytics = useDispatchLinkAnalytics; const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = __importDefault(require("react")); const OctopusRoutingContext = react_1.default.createContext(undefined); function OctopusRoutingProvider({ Link, useIsUrlActive, useDispatchLinkAnalytics, children }) { const contextValue = react_1.default.useMemo(() => ({ Link, useIsUrlActive, useDispatchLinkAnalytics, }), [Link, useIsUrlActive, useDispatchLinkAnalytics]); return (0, jsx_runtime_1.jsx)(OctopusRoutingContext.Provider, { value: contextValue, children: children }); } function useOctopusLinkComponent() { const contextValue = react_1.default.useContext(OctopusRoutingContext); if (contextValue === undefined) { throw new Error("Context value not provided via a OctopusRoutingProvider"); } return contextValue.Link; } function useIsUrlActive() { const contextValue = react_1.default.useContext(OctopusRoutingContext); if (contextValue === undefined) { throw new Error("Context value not provided via a OctopusRoutingProvider"); } const isUrlActive = contextValue.useIsUrlActive(); return (path, showLinkAsActive) => { if (showLinkAsActive === "never") { return false; } if (showLinkAsActive === "always") { return true; } return isUrlActive(path, showLinkAsActive === "if path matches exactly"); }; } function useDispatchLinkAnalytics() { const contextValue = react_1.default.useContext(OctopusRoutingContext); if (contextValue === undefined) { throw new Error("Context value not provided via a OctopusRoutingProvider"); } return contextValue.useDispatchLinkAnalytics(); }