Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

5.5 KB
Raw Blame
import { createEffect, createSignal, on } from 'solid-js';
import {
  presentationPolicyIsReadOnly,
  sessionPresentationPolicyResolved,
} from '@/stores/sessionPresentationPolicy';
import { resolveCanonicalSelfHostedBillingHref } from '@/utils/pricingHandoff';
import {
  buildInfrastructureAgentDoctorPath,
  buildInfrastructureWorkspacePath,
  deriveAddStepFromLocation,
  deriveAgentDoctorScopeFromLocation,
  isLegacyAgentDoctorLocation,
} from './infrastructureWorkspaceModel';
import {
  EXTERNAL_AGENT_SETUP_PATH,
  SETTINGS_API_ACCESS_PATH,
  SETTINGS_PULSE_INTELLIGENCE_AGENTS_PATH,
  isExternalAgentSetupHash,
} from '@/routing/resourceLinks';
import {
  DEFAULT_SETTINGS_TAB,
  deriveTabFromPath,
  deriveTabFromQuery,
  isAISettingsOAuthCallbackQuery,
  PULSE_INTELLIGENCE_LEGACY_ASSISTANT_PATH,
  resolveCanonicalSettingsPath,
  settingsTabPath,
  type AgentKey,
  type SettingsTab,
} from './settingsNavigationModel';

type SettingsLocation = {
  hash: string;
  pathname: string;
  search: string;
};

type SettingsNavigate = (
  to: string,
  options?: {
    replace?: boolean;
    scroll?: boolean;
  },
) => void;

interface UseSettingsNavigationParams {
  navigate: SettingsNavigate;
  location: SettingsLocation;
}

export function useSettingsNavigation({ navigate, location }: UseSettingsNavigationParams) {
  const [currentTab, setCurrentTab] = createSignal<SettingsTab>(
    deriveTabFromPath(location.pathname),
  );
  const resolveTabPath = (tab: SettingsTab): string => settingsTabPath(tab);
  const activeTab = (): SettingsTab => currentTab();

  const [selectedAgent, setSelectedAgent] = createSignal<AgentKey>('pve');

  const handleSelectAgent = (agent: AgentKey) => {
    setSelectedAgent(agent);
  };

  const setActiveTab = (tab: SettingsTab) => {
    if (currentTab() !== tab) {
      setCurrentTab(tab);
    }
    const targetPath = resolveTabPath(tab);
    if (location.pathname !== targetPath) {
      navigate(targetPath, { scroll: false });
    }
  };

  createEffect(
    on(
      () => [location.pathname, location.search, location.hash] as const,
      ([path, search, hash]) => {
        if (path === '/settings' || path === '/settings/') {
          const queryTab = deriveTabFromQuery(search);
          const resolvedTab = queryTab ?? DEFAULT_SETTINGS_TAB;
          const target = resolveTabPath(resolvedTab);
          const shouldPreserveQuery =
            resolvedTab === 'system-ai' && isAISettingsOAuthCallbackQuery(search);
          // The hash is a deep-link anchor on the target page (for example
          // `?tab=assistant#external-agent-setup` opening connector setup).
          const targetHref = shouldPreserveQuery ? `${target}${search}${hash}` : `${target}${hash}`;
          const currentHref = `${path}${search}${hash}`;

          if (targetHref !== currentHref) {
            navigate(targetHref, { replace: true, scroll: false });
            return;
          }

          if (currentTab() !== resolvedTab) {
            setCurrentTab(resolvedTab);
          }
          return;
        }

        const currentHref = `${path}${search}${hash}`;
        const canonicalBillingHref = resolveCanonicalSelfHostedBillingHref(path, search, hash);
        if (canonicalBillingHref && canonicalBillingHref !== currentHref) {
          navigate(canonicalBillingHref, {
            replace: true,
            scroll: false,
          });
          return;
        }

        // Settings > Assistant retired with the Assistant chat. Its links,
        // including the external-agent setup anchor, land on External agents.
        if (path.replace(/\/+$/, '') === PULSE_INTELLIGENCE_LEGACY_ASSISTANT_PATH) {
          navigate(`${SETTINGS_PULSE_INTELLIGENCE_AGENTS_PATH}${search}${hash}`, {
            replace: true,
            scroll: false,
          });
          return;
        }

        if (path === SETTINGS_API_ACCESS_PATH && isExternalAgentSetupHash(hash)) {
          navigate(EXTERNAL_AGENT_SETUP_PATH, {
            replace: true,
            scroll: false,
          });
          return;
        }

        if (
          sessionPresentationPolicyResolved() &&
          presentationPolicyIsReadOnly() &&
          path.startsWith('/settings/infrastructure')
        ) {
          navigate(buildInfrastructureWorkspacePath(), {
            replace: true,
            scroll: false,
          });
          return;
        }

        // Pre-route Agent Doctor links (?agentDoctor=1 / ?agentUpdates=1 on the
        // workspace path) canonicalize onto the routed page, preserving scope.
        // An add-flow query wins if both are somehow present, matching the
        // workspace's own precedence.
        if (
          isLegacyAgentDoctorLocation(path, search) &&
          deriveAddStepFromLocation(path, search) === null
        ) {
          navigate(
            buildInfrastructureAgentDoctorPath(deriveAgentDoctorScopeFromLocation(path, search)),
            {
              replace: true,
              scroll: false,
            },
          );
          return;
        }

        const canonicalPath = resolveCanonicalSettingsPath(path);
        if (canonicalPath && canonicalPath !== path) {
          navigate(canonicalPath, {
            replace: true,
            scroll: false,
          });
          return;
        }

        const effectivePath = canonicalPath ?? path;
        const resolved = deriveTabFromPath(effectivePath);
        if (resolved !== currentTab()) {
          setCurrentTab(resolved);
        }
      },
    ),
  );

  return {
    currentTab,
    activeTab,
    selectedAgent,
    setSelectedAgent,
    setActiveTab,
    handleSelectAgent,
  };
}