@the-portland-company/shell 0.73.2 → 0.75.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1566,6 +1566,87 @@ function buildSpeculationRules(currentApp, registry) {
1566
1566
  };
1567
1567
  return JSON.stringify(payload);
1568
1568
  }
1569
+ var DESTRUCTIVE_LABEL_PATTERNS = ["sign out", "signout", "log out", "logout", "delete"];
1570
+ var DESTRUCTIVE_PATH_PATTERNS = ["/signout", "/sign-out", "/logout", "/log-out"];
1571
+ function isDestructive(item) {
1572
+ const label = item.label.toLowerCase();
1573
+ if (DESTRUCTIVE_LABEL_PATTERNS.some((p) => label.includes(p))) return true;
1574
+ const href = (item.href ?? "").toLowerCase();
1575
+ return DESTRUCTIVE_PATH_PATTERNS.some((p) => href.includes(p));
1576
+ }
1577
+ function collectSiblingAppUrls(items, currentOrigin) {
1578
+ const urls = /* @__PURE__ */ new Set();
1579
+ const walk = (list) => {
1580
+ for (const item of list) {
1581
+ if (item.kind === "section") continue;
1582
+ if (item.href && !isDestructive(item)) {
1583
+ try {
1584
+ const url = new URL(item.href, currentOrigin);
1585
+ if (url.origin !== currentOrigin && /^https?:$/.test(url.protocol)) {
1586
+ urls.add(url.href);
1587
+ }
1588
+ } catch {
1589
+ }
1590
+ }
1591
+ if (item.children?.length) walk(item.children);
1592
+ }
1593
+ };
1594
+ walk(items);
1595
+ return Array.from(urls);
1596
+ }
1597
+ function buildNavSpeculationRules(items, currentOrigin = typeof window !== "undefined" ? window.location.origin : "") {
1598
+ const urls = collectSiblingAppUrls(items, currentOrigin);
1599
+ if (urls.length === 0) return null;
1600
+ return {
1601
+ prerender: [{ source: "list", urls, eagerness: "moderate" }]
1602
+ };
1603
+ }
1604
+ var SCRIPT_ID = "politogy-speculation-rules";
1605
+ var PREFETCH_ATTR = "data-politogy-speculation-prefetch";
1606
+ function SpeculationRulesScript({ items }) {
1607
+ const origin = typeof window !== "undefined" ? window.location.origin : "";
1608
+ const rules = react$1.useMemo(() => buildNavSpeculationRules(items, origin), [items, origin]);
1609
+ const json = rules ? JSON.stringify(rules) : null;
1610
+ react$1.useEffect(() => {
1611
+ if (!json || typeof document === "undefined") return;
1612
+ let el = document.getElementById(SCRIPT_ID);
1613
+ if (!el) {
1614
+ el = document.createElement("script");
1615
+ el.id = SCRIPT_ID;
1616
+ el.type = "speculationrules";
1617
+ document.head.appendChild(el);
1618
+ }
1619
+ el.textContent = json;
1620
+ return () => {
1621
+ };
1622
+ }, [json]);
1623
+ react$1.useEffect(() => {
1624
+ if (typeof document === "undefined" || typeof window === "undefined") return;
1625
+ const supportsSpecRules = typeof HTMLScriptElement !== "undefined" && typeof HTMLScriptElement.supports === "function" && HTMLScriptElement.supports(
1626
+ "speculationrules"
1627
+ );
1628
+ if (supportsSpecRules) return;
1629
+ const urls = collectSiblingAppUrls(items, origin);
1630
+ if (urls.length === 0) return;
1631
+ const schedule = typeof window.requestIdleCallback === "function" ? window.requestIdleCallback : (cb) => setTimeout(cb, 2e3);
1632
+ const added = [];
1633
+ schedule(() => {
1634
+ for (const url of urls) {
1635
+ if (document.querySelector(`link[${PREFETCH_ATTR}][href="${url}"]`)) continue;
1636
+ const link = document.createElement("link");
1637
+ link.rel = "prefetch";
1638
+ link.href = url;
1639
+ link.setAttribute(PREFETCH_ATTR, "");
1640
+ document.head.appendChild(link);
1641
+ added.push(link);
1642
+ }
1643
+ });
1644
+ return () => {
1645
+ added.forEach((l) => l.remove());
1646
+ };
1647
+ }, [items, origin]);
1648
+ return null;
1649
+ }
1569
1650
 
1570
1651
  exports.BrandIcon = BrandIcon;
1571
1652
  exports.BrandLogo = BrandLogo;
@@ -1586,7 +1667,10 @@ exports.ShellOrgContext = ShellOrgContext;
1586
1667
  exports.ShellProvider = ShellProvider;
1587
1668
  exports.ShellRoleContext = ShellRoleContext;
1588
1669
  exports.ShellSupportProviderContext = ShellSupportProviderContext;
1670
+ exports.SpeculationRulesScript = SpeculationRulesScript;
1671
+ exports.buildNavSpeculationRules = buildNavSpeculationRules;
1589
1672
  exports.buildSpeculationRules = buildSpeculationRules;
1673
+ exports.collectSiblingAppUrls = collectSiblingAppUrls;
1590
1674
  exports.getOrganizationBadgeLetters = getOrganizationBadgeLetters;
1591
1675
  exports.getOrganizationBrandColor = getOrganizationBrandColor;
1592
1676
  exports.getUserDisplayName = getUserDisplayName;