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.

5.2 KB
Raw Blame
'use client';

import { isDesktop } from '@lobechat/const';
import { RENDERER_HANDLED_LINK_ATTR } from '@lobechat/desktop-bridge';
import { Button, cssVar } from '@lobehub/ui';
import type { LucideIcon } from 'lucide-react';
import type { MouseEvent, ReactNode } from 'react';
import { memo, useCallback } from 'react';

import { useWorkspaces } from '@/business/client/hooks/useWorkspaces';
import { taskDetailPath } from '@/features/AgentTasks/shared/taskDetailPath';
import { parseInternalLink } from '@/features/EntityLink';
import { useWorkspaceAwareNavigate } from '@/features/Workspace/useWorkspaceAwareNavigate';
import { useChatStore } from '@/store/chat';
import { useGlobalStore } from '@/store/global';

interface BriefActionLinkProps {
  /** Agent owning the brief's task β€” keeps the task detail path agent-scoped. */
  agentId?: string | null;
  children: ReactNode;
  className?: string;
  /** Leading glyph, for the quiet navigation variant. */
  icon?: LucideIcon;
  /** Renders the filled primary variant used for a card's leading action. */
  primary?: boolean;
  taskId?: string | null;
  taskIdentifier?: string | null;
  url?: string;
  /**
   * `quiet`: a small secondary text button, the same as the card's "View run"
   * shortcut β€” for a link that navigates beside the answers rather than being one.
   */
  variant?: 'quiet';
}

/**
 * A `type: 'link'` brief action.
 *
 * A bare `<a href>` is a document navigation: on the web it throws away the
 * whole SPA, and in Electron it hands `app://renderer/<path>` to a router that
 * never registered the target (acceptance is web-only β€” see
 * `desktopRouter.config.tsx`), so the button reads as dead. Internal
 * destinations therefore route through React Router instead, and acceptance β€”
 * which has no standalone desktop route at all β€” opens in the task-agent panel
 * beside its task, the same surface `TaskAcceptance` / `RunVerifyTag` use.
 *
 * The `href` stays on the anchor so middle-click / copy-link still work, and
 * `RENDERER_HANDLED_LINK_ATTR` tells the desktop preload interceptor this
 * click is already claimed.
 */
export const BriefActionLink = memo<BriefActionLinkProps>(
  ({ agentId, children, className, icon, primary, taskId, taskIdentifier, url, variant }) => {
    const navigate = useWorkspaceAwareNavigate();
    const workspaces = useWorkspaces();
    const openAcceptance = useChatStore((s) => s.openAcceptance);
    const toggleTaskAgentPanel = useGlobalStore((s) => s.toggleTaskAgentPanel);

    const reference = parseInternalLink(
      url,
      typeof window === 'undefined' ? undefined : window.location.origin,
      workspaces.map((workspace) => workspace.slug),
    );
    const isTasklessDesktopAcceptance = isDesktop && reference?.type === 'acceptance' && !taskId;
    const isRendererHandled = !!reference && !isTasklessDesktopAcceptance;

    const handleClick = useCallback(
      (event: MouseEvent<HTMLAnchorElement | HTMLButtonElement>) => {
        // External destinations (billing, docs) stay plain anchors.
        if (!reference) return;
        // Electron has no standalone acceptance route. Without a task there is
        // no panel destination either, so leave the link to the preload layer.
        if (isTasklessDesktopAcceptance) return;
        if (event.button !== 0) return;

        // On the web a modifier-click means "open in a new tab", so let the
        // browser handle it. Desktop has no tabs for it to open.
        const modifierClick = event.metaKey || event.ctrlKey || event.shiftKey || event.altKey;
        if (!isDesktop && modifierClick) return;

        event.preventDefault();

        if (reference.type === 'acceptance' && taskId) {
          // Arm the panel before navigating, the same handoff
          // `Home/InputArea/useSend` performs β€” the panel mounts with the task
          // page and reads this state on its first render.
          toggleTaskAgentPanel(true);
          openAcceptance(reference.acceptanceId);
          navigate(taskDetailPath(taskIdentifier ?? taskId, agentId ?? undefined));
          return;
        }

        // A path that already carries its workspace slug must not be prefixed twice.
        navigate(
          reference.pathname,
          'workspaceSlug' in reference && reference.workspaceSlug ? { escape: true } : undefined,
        );
      },
      [
        agentId,
        isTasklessDesktopAcceptance,
        navigate,
        openAcceptance,
        reference,
        taskId,
        taskIdentifier,
        toggleTaskAgentPanel,
      ],
    );

    if (variant === 'quiet')
      return (
        <Button
          {...(isRendererHandled ? { [RENDERER_HANDLED_LINK_ATTR]: 'true' } : {})}
          className={className}
          href={url}
          icon={icon}
          size={'small'}
          style={{ color: cssVar.colorTextSecondary }}
          type={'text'}
          onClick={handleClick}
        >
          {children}
        </Button>
      );

    return (
      <Button
        {...(isRendererHandled ? { [RENDERER_HANDLED_LINK_ATTR]: 'true' } : {})}
        className={className}
        href={url}
        shape={'round'}
        type={primary ? 'primary' : 'default'}
        onClick={handleClick}
      >
        {children}
      </Button>
    );
  },
);

BriefActionLink.displayName = 'BriefActionLink';