Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

3.2 KB
Raw Blame
import { useMemo } from "react";
import { useTranslation } from "react-i18next";
import { useSettingsNav as useCoreSettingsNav } from "@editor-core/components/settings/useSettingsNav";
import type { SettingsNav } from "@editor/components/settings/settingsNavTypes";
import { usePortalAccessState } from "@editor/hooks/usePortalAccess";
import { useAuth } from "@editor/auth/context";
import { useAppConfig } from "@editor/contexts/AppConfigContext";
import { useRosterAvailable } from "@editor/hooks/useRosterAvailable";
import { mergeSettingsGroups } from "@editor/components/settings/mergeSettingsGroups";
import {
  buildPortalSettingsSections,
  PORTAL_SECTION_ALIASES,
  portalSupersededSectionKeys,
} from "@editor/components/settings/portalSettingsNav";

export type { SettingsNav };

/**
 * Self-hosted settings: the build's own sections, plus the processor's server
 * administration (Team / Infrastructure / Usage & Billing). Where the two
 * overlap the processor's view wins — it is a superset (roles, teams,
 * processor access, audit) — so the narrower section is dropped rather than
 * shown twice, and its key aliases across.
 *
 * Admins manage encryption; only the organization owner manages billing and
 * the Stirling account connection. Portal access alone grants neither.
 */
export function useSettingsNav(onLeave: () => void): SettingsNav {
  const { t } = useTranslation();
  const base = useCoreSettingsNav(onLeave);
  const { granted: portalAccess, settled: accessSettled } =
    usePortalAccessState();
  const { isAdmin, user, loading } = useAuth();
  const isOwner = isAdmin && !loading && user?.orgOwner === true;
  const { config } = useAppConfig();
  const rosterAvailable = useRosterAvailable();

  const portalSections = useMemo(
    () =>
      buildPortalSettingsSections(t, {
        // The roster is this build's only one, so it does not wait on processor
        // access the way the processor's own surfaces do.
        includeRoster:
          rosterAvailable &&
          config?.enableLogin === true &&
          isAdmin &&
          !loading,
        includeApiKeys: portalAccess,
        includeEncryption: portalAccess && isAdmin,
        includeBilling: portalAccess && isOwner,
        includeAccountLink: portalAccess && isOwner,
      }),
    [
      portalAccess,
      isAdmin,
      isOwner,
      loading,
      config?.enableLogin,
      rosterAvailable,
      t,
    ],
  );

  const sections = useMemo(
    () =>
      mergeSettingsGroups(base.sections, portalSections, [
        "plan",
        "adminPlan",
        ...portalSupersededSectionKeys(portalSections),
      ]),
    [base.sections, portalSections],
  );

  const portalAliases = { ...PORTAL_SECTION_ALIASES };
  if (
    !portalSections.some((group) =>
      group.items.some((item) => item.key === "users"),
    )
  ) {
    delete portalAliases.people;
    delete portalAliases.teams;
  }
  if (
    !portalSections.some((group) =>
      group.items.some((item) => item.key === "billing"),
    )
  ) {
    delete portalAliases.plan;
    delete portalAliases.adminPlan;
  }

  return {
    ...base,
    sections,
    pending: !accessSettled || loading,
    aliases:
      portalSections.length > 0
        ? { ...base.aliases, ...portalAliases }
        : base.aliases,
  };
}