All files / src/components/chat/ChatPanel ComposerMenu.tsx

100% Statements 15/15
93.33% Branches 14/15
100% Functions 4/4
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116                                                      13x   13x   13x                         13x                     27x 27x 27x   27x                                 9x 1x 1x 1x                 1x                     3x                                
import {
  DropdownMenu,
  DropdownMenuCheckboxItem,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuSeparator,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { MoreHorizontal, Settings2, Slash, Sparkles } from "lucide-react";
import React, { useRef } from "react";
import { AttachItems, type AttachItemsProps } from "./AttachMenu";
 
export interface ComposerMenuProps {
  /** What attaching offers: the paperclip's menu (`AttachMenu`). */
  attach: AttachItemsProps;
  /** Open the command palette; absent where the chat offers no commands. */
  onCommands?: () => void;
  settingsOpen: boolean;
  onToggleSettings: () => void;
  /** Thinking is on: Response settings shows it, as its button does. */
  thinking: boolean;
  /** A response format other than Markdown, named on the trigger and the
   *  item, since it changes what comes back while the menu is closed. */
  formatLabel?: string;
}
 
const TRIGGER =
  "shrink-0 w-8 h-8 p-0 flex items-center justify-center rounded-lg transition-colors mb-0.5";
const TRIGGER_ACTIVE =
  "text-indigo-600 dark:text-indigo-400 bg-indigo-100 dark:bg-indigo-900/40";
const TRIGGER_IDLE =
  "text-gray-400 hover:text-gray-600 dark:hover:text-gray-200 hover:bg-gray-200 dark:hover:bg-gray-700 data-[state=open]:bg-gray-200 dark:data-[state=open]:bg-gray-700";
 
/**
 * The composer's actions behind one button, for a chat too narrow to give
 * them a row of their own (`ChatPanel`'s `COMPACT_BELOW_PX`): what the
 * paperclip offers (`AttachItems`: upload, the library's recent documents,
 * the whole library), commands and Response settings — what the wide
 * composer draws beside the message box.
 *
 * The trigger is highlighted while a panel it opened is up or a format other
 * than Markdown is chosen, as the settings button is. The menu portals to
 * <body>, so it carries z-[70] to open over full screen (z-[60]).
 */
const ComposerMenu: React.FC<ComposerMenuProps> = ({
  attach,
  onCommands,
  settingsOpen,
  onToggleSettings,
  thinking,
  formatLabel,
}) => {
  // Commands puts the caret in the message box. Run once the menu has
  // closed: from inside it, the menu's focus trap would take the caret back,
  // and closing would hand it to the trigger.
  const commandsPending = useRef(false);
  const active = attach.documentsOpen || settingsOpen || !!formatLabel;
  const SettingsIcon = thinking ? Sparkles : Settings2;
 
  return (
    <DropdownMenu>
      <DropdownMenuTrigger asChild>
        <button
          type="button"
          aria-label="More actions"
          title={formatLabel ? `More actions · ${formatLabel}` : "More actions"}
          className={`${TRIGGER} ${active ? TRIGGER_ACTIVE : TRIGGER_IDLE}`}
        >
          <MoreHorizontal className="w-4 h-4" />
        </button>
      </DropdownMenuTrigger>
      <DropdownMenuContent
        side="top"
        align="start"
        className="z-[70] w-56"
        onCloseAutoFocus={(e) => {
          if (!commandsPending.current) return;
          commandsPending.current = false;
          e.preventDefault();
          onCommands?.();
        }}
      >
        <AttachItems {...attach} />
        <DropdownMenuSeparator />
        {onCommands && (
          <DropdownMenuItem
            inset
            onSelect={() => {
              commandsPending.current = true;
            }}
          >
            <Slash />
            Commands
          </DropdownMenuItem>
        )}
        {/* A checkbox item draws its icons in the text colour; an item dims
            them. Dimmed here too, so the list reads as one. */}
        <DropdownMenuCheckboxItem
          checked={settingsOpen}
          onCheckedChange={() => onToggleSettings()}
        >
          <SettingsIcon className="text-muted-foreground" />
          Response settings
          {formatLabel && (
            <span className="ml-auto text-xs text-muted-foreground">
              {formatLabel}
            </span>
          )}
        </DropdownMenuCheckboxItem>
      </DropdownMenuContent>
    </DropdownMenu>
  );
};
 
export default ComposerMenu;