Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

3.6 KB
Raw Blame
/**
 * Sidebar panel that lists pages linking *into* the current page.
 *
 * Reads ``page.metadata.backlinks`` populated by the backend's
 * interlinking post-processor. Empty/missing metadata renders nothing
 * (graceful fallback for pages generated before interlinking shipped).
 */

import * as React from "react";
import { getPageTypeIcon, getPageTypeLabel } from "../lib/page-types";
import type { Backlink } from "./wiki-links-types";

export interface BacklinksPanelProps {
  /** ``page.metadata.backlinks`` from the page response. */
  backlinks: Backlink[];
  /** Repo ID for routing the source-page links. */
  repoId: string;
  /** Maximum entries to show before "+N more". */
  limit?: number;
  /** Optional URL builder; defaults to ``/repos/{repoId}/wiki/{pageId}``. */
  buildHref?: (repoId: string, pageId: string) => string;
  /**
   * Render-prop for the link element so the host can plug Next's `<Link>`
   * (or any router link) without dragging a framework dep into ui/.
   * Defaults to a plain `<a>`.
   */
  renderLink?: (props: {
    href: string;
    className: string;
    title: string;
    children: React.ReactNode;
  }) => React.ReactNode;
}

const DEFAULT_LIMIT = 6;

function defaultHref(repoId: string, pageId: string): string {
  // PageIds are like ``file_page:src/foo.py``. The router resolves them
  // via the catch-all slug parameter.
  return `/repos/${repoId}/wiki/${encodeURIComponent(pageId)}`;
}

const LINK_CLASS =
  "flex items-start gap-1.5 text-[var(--color-text-secondary)] hover:text-[var(--color-accent-primary)] transition-colors";

export function BacklinksPanel({
  backlinks,
  repoId,
  limit = DEFAULT_LIMIT,
  buildHref = defaultHref,
  renderLink,
}: BacklinksPanelProps) {
  if (!backlinks || backlinks.length === 0) {
    return null;
  }

  const visible = backlinks.slice(0, limit);
  const extra = backlinks.length - visible.length;

  return (
    <div>
      <p className="text-xs font-medium text-[var(--color-text-tertiary)] uppercase tracking-wider mb-2">
        Linked by ({backlinks.length})
      </p>
      <ul className="space-y-1.5">
        {visible.map((bl) => {
          const Icon = getPageTypeIcon(bl.source_page_type);
          const href = buildHref(repoId, bl.source_page_id);
          // The page's own name, not its kind. The kind is already the icon
          // beside it, and a tooltip reading "File" over a list of files told
          // the reader nothing about which page they were about to open.
          const title = bl.source_title;
          const children = (
            <>
              <Icon
                aria-label={getPageTypeLabel(bl.source_page_type)}
                className="h-3 w-3 mt-0.5 shrink-0 text-[var(--color-text-tertiary)]"
              />
              {/* Wraps rather than truncates, like the Related list. In a
                  ~260px rail one-line ellipsis cut page titles at the path
                  segment that identifies them. */}
              <span className="min-w-0 break-words leading-snug">{bl.source_title}</span>
            </>
          );
          return (
            <li key={bl.source_page_id} className="text-xs">
              {renderLink ? (
                renderLink({ href, className: LINK_CLASS, title, children })
              ) : (
                <a href={href} className={LINK_CLASS} title={title}>
                  {children}
                </a>
              )}
            </li>
          );
        })}
      </ul>
      {extra > 0 && (
        <p className="text-[10px] text-[var(--color-text-tertiary)] mt-1.5">
          + {extra} more
        </p>
      )}
    </div>
  );
}