Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

4.6 KB
Raw Blame
import { memo, useRef, useEffect } from 'react';
import * as Ariakit from '@ariakit/react';
import type { RefObject } from 'react';
import type React from 'react';
import { useRowPullRequestQuery } from '~/data-provider/PullRequest';
import { TONE_DOT_CLASS, presentPullRequest } from './status';
import { summarizePullRequest } from './summary';
import useRowHover from './useRowHover';
import PullRequestPanel from './Panel';
import { useLocalize } from '~/hooks';
import PullRequestIcon from './Icon';
import { cn } from '~/utils';
import CiDot from './CiDot';

/** The row's own surface at each state, so the dot's ring blends into it. */
const RING_SELECTED = 'ring-surface-nav-selected';
const RING_IDLE = 'ring-surface-sidebar group-hover:ring-surface-nav-hover';

/**
 * The pull request a conversation opened, as a mark in its sidebar row: the state icon with the
 * CI dot on its corner. It renders nothing until a pull request is known, so a row without one
 * is exactly what it was. The row's title stays the conversation's own title; this only adds
 * the icon. The mark is a button beside the row's own, so Tab reaches it: focusing it opens the
 * card, which holds the GitHub link, and activating it must not also open the row.
 */
function PullRequestRowMark({
  conversationId,
  labelId,
  selected,
  onDescribed,
  rowRef,
}: {
  conversationId: string;
  /** The id the row lists in `aria-describedby`, so a screen reader hears what the colors say. */
  labelId: string;
  selected: boolean;
  /** Tells the row whether `labelId` is in the page, so it never points at text that is not. */
  onDescribed?: (described: boolean) => void;
  /** The row the mark sits in. Resting the pointer anywhere on it opens the card, not just on the icon. */
  rowRef?: RefObject<HTMLElement | null>;
}) {
  const localize = useLocalize();
  const store = Ariakit.useHovercardStore({
    placement: 'right-start',
    showTimeout: 100,
    hideTimeout: 150,
  });
  const open = Ariakit.useStoreState(store, 'open');
  const { data, isError, refetch } = useRowPullRequestQuery(conversationId);
  const pullRequest = data?.pullRequest;
  const described = pullRequest != null;

  /* The row is not focusable, so a card it opened hands focus back to the icon on close, and
     focus or a click on the icon makes the icon the anchor again. */
  const markRef = useRef<HTMLButtonElement>(null);
  const finalFocus = rowRef == null ? undefined : markRef;
  const anchorToMark = (event: React.SyntheticEvent<HTMLElement>) =>
    store.setAnchorElement(event.currentTarget);
  useRowHover(store, rowRef, described);

  useEffect(() => {
    onDescribed?.(described);
    return () => onDescribed?.(false);
  }, [described, onDescribed]);

  /* Discovery is automatic: only a known pull request can have a refresh warning. */
  if (pullRequest == null) return null;

  const view = presentPullRequest(pullRequest);
  const dotClass = view.dotTone == null ? null : TONE_DOT_CLASS[view.dotTone];
  const summary = summarizePullRequest(pullRequest, localize);

  return (
    <Ariakit.HovercardProvider store={store}>
      <Ariakit.HovercardAnchor
        render={<Ariakit.Button ref={markRef} />}
        aria-label={summary}
        aria-expanded={open}
        showOnHover={rowRef == null}
        data-testid="convo-pull-request"
        onFocus={(event: React.FocusEvent<HTMLElement>) => {
          anchorToMark(event);
          store.show();
        }}
        onClick={(event: React.MouseEvent<HTMLElement>) => {
          /* The row itself opens the conversation on click; this opens the card instead. */
          event.stopPropagation();
          anchorToMark(event);
          store.show();
        }}
        onKeyDown={(event: React.KeyboardEvent) => {
          /* Enter and Space belong to this button, not to the row's own shortcuts. */
          if (event.key === 'Enter' || event.key === ' ') event.stopPropagation();
        }}
        className={cn(
          'focus-visible:ring-text-primary relative me-1 flex size-4 shrink-0 items-center justify-center rounded-sm outline-hidden focus-visible:ring-2',
        )}
      >
        <PullRequestIcon icon={view.icon} tone={view.iconTone} className="size-4 shrink-0" />
        {dotClass != null && (
          <CiDot dotClass={dotClass} ringClassName={selected ? RING_SELECTED : RING_IDLE} />
        )}
      </Ariakit.HovercardAnchor>
      <span id={labelId} className="sr-only">
        {summary}
      </span>
      <PullRequestPanel
        store={store}
        pullRequest={pullRequest}
        refreshFailed={isError}
        onRetry={() => void refetch()}
        finalFocus={finalFocus}
      />
    </Ariakit.HovercardProvider>
  );
}

export default memo(PullRequestRowMark);