Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

1.2 KB
Raw Blame
import { AGENT_CHAT_URL } from '@lobechat/const';
import { useMemo } from 'react';

import { useActiveLocation } from '@/hooks/useActiveLocation';

// Sub-routes that are agent-scoped views (not tied to a specific topic/task id),
// safe to carry over when switching between agents from the sidebar switcher.
const PRESERVED_AGENT_SUB_PATHS = new Set(['topics', 'profile', 'channel']);

/**
 * When switching from an agent's sub-view (e.g. `/agent/A/topics`) to another
 * agent, preserve the sub-route on the destination so the user lands on the
 * same view. Topic / task ids belong to the previous agent and are
 * intentionally dropped.
 */
export const resolvePreservedAgentUrl = (pathname: string, agentId: string): string => {
  const match = pathname.match(/^\/agent\/[^/]+\/([^/]+)\/?$/);
  const subPath = match?.[1];
  if (subPath && PRESERVED_AGENT_SUB_PATHS.has(subPath)) {
    return `/agent/${agentId}/${subPath}`;
  }
  return AGENT_CHAT_URL(agentId, false);
};

export const usePreservedAgentUrl = (agentId: string): string => {
  const { pathname } = useActiveLocation();
  return useMemo(() => resolvePreservedAgentUrl(pathname, agentId), [agentId, pathname]);
};