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

100% Statements 17/17
90.9% Branches 10/11
100% Functions 9/9
100% Lines 17/17

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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142                              16x                       24x 12x 24x   19x   6x                                                 16x                 22x 22x                         4x     3x                           2x                   16x   16x   16x                   16x 833x                                    
import {
  DropdownMenu,
  DropdownMenuCheckboxItem,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuLabel,
  DropdownMenuSeparator,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import type { UploadedFileRef } from "@/services/api/routers/FileRouter";
import { parseServerDate } from "@/util/time";
import { FileText, FolderOpen, Paperclip, Upload } from "lucide-react";
import React from "react";
 
/** How many of the library's newest documents the menu offers. */
export const RECENT_LIMIT = 5;
 
/**
 * The documents the menu offers to attach again: the workspace library's
 * newest, read and not already on this message. A document whose text is
 * still being read, or never will be, is not offered — a chat refuses it.
 */
export function recentDocuments(
  documents: UploadedFileRef[],
  attachedIds: ReadonlySet<string>,
  limit: number = RECENT_LIMIT,
): UploadedFileRef[] {
  const time = (doc: UploadedFileRef) =>
    doc.created_at ? parseServerDate(doc.created_at).getTime() : 0;
  return documents
    .filter(
      (doc) => (doc.status ?? "ready") === "ready" && !attachedIds.has(doc.id),
    )
    .sort((a, b) => time(b) - time(a))
    .slice(0, limit);
}
 
export interface AttachItemsProps {
  /** Open the file picker. */
  onUpload: () => void;
  /** False where the chat takes no uploads: the item is shown, disabled. */
  canUpload: boolean;
  /** The library's documents, and those on this message already. */
  documents: UploadedFileRef[];
  attachedIds: ReadonlySet<string>;
  /** Put one on this message. */
  onAttach: (doc: UploadedFileRef) => void;
  /** The documents library is open above the message box. */
  documentsOpen: boolean;
  onToggleDocuments: () => void;
}
 
/**
 * What attaching offers, as menu items: a file from the computer, one of
 * the library's newest documents — any document uploaded in this workspace,
 * in any conversation — or the whole library. Shared by the paperclip's
 * menu and the narrow composer's (`ComposerMenu`), so the two never differ.
 */
export const AttachItems: React.FC<AttachItemsProps> = ({
  onUpload,
  canUpload,
  documents,
  attachedIds,
  onAttach,
  documentsOpen,
  onToggleDocuments,
}) => {
  const recent = recentDocuments(documents, attachedIds);
  return (
    <>
      <DropdownMenuItem inset disabled={!canUpload} onSelect={onUpload}>
        <Upload />
        Upload from computer
      </DropdownMenuItem>
      {recent.length > 0 && (
        <>
          <DropdownMenuSeparator />
          <DropdownMenuLabel className="text-xs font-medium text-muted-foreground">
            Recent
          </DropdownMenuLabel>
          {recent.map((doc) => (
            <DropdownMenuItem
              key={doc.id}
              inset
              onSelect={() => onAttach(doc)}
              title={`Attach ${doc.name}`}
            >
              <FileText />
              <span className="truncate">{doc.name}</span>
            </DropdownMenuItem>
          ))}
        </>
      )}
      <DropdownMenuSeparator />
      {/* A checkbox item draws its icon in the text colour; an item dims
          it. Dimmed here too, so the list reads as one. */}
      <DropdownMenuCheckboxItem
        checked={documentsOpen}
        onCheckedChange={() => onToggleDocuments()}
      >
        <FolderOpen className="text-muted-foreground" />
        All documents
      </DropdownMenuCheckboxItem>
    </>
  );
};
 
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 paperclip beside the message box: upload a file, attach one of the
 * library's recent documents again, or open the library. It replaced a
 * paperclip that only uploaded and a separate library button.
 *
 * The menu portals to <body>, so it carries z-[70] to open over full screen
 * (z-[60]), as the narrow composer's does.
 */
const AttachMenu: React.FC<AttachItemsProps> = (props) => (
  <DropdownMenu>
    <DropdownMenuTrigger asChild>
      <button
        type="button"
        aria-label="Attach"
        title="Attach"
        className={`${TRIGGER} ${props.documentsOpen ? TRIGGER_ACTIVE : TRIGGER_IDLE}`}
      >
        <Paperclip className="w-4 h-4" />
      </button>
    </DropdownMenuTrigger>
    <DropdownMenuContent side="top" align="start" className="z-[70] w-64">
      <AttachItems {...props} />
    </DropdownMenuContent>
  </DropdownMenu>
);
 
export default AttachMenu;