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

100% Statements 21/21
100% Branches 14/14
100% Functions 5/5
100% Lines 19/19

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                14x     14x               14x     19x 19x 19x   19x 2x 2x 2x       2x   2x       19x     19x 19x 6x             13x 13x       2x                                                
import type { AttachmentRef } from "@/objects/Model";
import { useApiHandler } from "@/services/useApiHandler";
import { openAttachment } from "@/util/chatAttachments";
import { Loader2, Paperclip } from "lucide-react";
import React, { useState } from "react";
 
/** What a chip says of a document that could not be opened: deleted from
 *  the library, or uploaded before CHAIN-R kept originals. */
export const UNAVAILABLE = "This document is no longer available to open.";
 
const CHIP =
  "inline-flex items-center gap-1 px-2 py-0.5 rounded-full bg-indigo-100 dark:bg-indigo-900/40 border border-indigo-200 dark:border-indigo-800 text-indigo-700 dark:text-indigo-300 text-xs";
 
/**
 * The documents a sent message attached, as chips. One the message names by
 * id (`meta.attachments`) opens as it was uploaded: a PDF or an image in a
 * new tab, anything else saved. One from a message stored before then is
 * named only.
 */
const SentAttachments: React.FC<{ attachments: AttachmentRef[] }> = ({
  attachments,
}) => {
  const { getApiHandler } = useApiHandler();
  const [opening, setOpening] = useState<string | null>(null);
  const [unavailable, setUnavailable] = useState<ReadonlySet<string>>(new Set());
 
  const open = async (id: string, ref: AttachmentRef) => {
    setOpening(id);
    try {
      const opened = await openAttachment(getApiHandler().file_router, {
        ...ref,
        id,
      });
      if (!opened) setUnavailable((prev) => new Set(prev).add(id));
    } finally {
      setOpening(null);
    }
  };
 
  return (
    <div className="flex flex-wrap gap-1.5 justify-end mb-1.5">
      {attachments.map((a, i) => {
        const id = a.id;
        if (!id) {
          return (
            <span key={`${a.name}-${i}`} className={CHIP}>
              <Paperclip className="w-3 h-3 shrink-0" />
              {a.name}
            </span>
          );
        }
        const gone = unavailable.has(id);
        return (
          <button
            key={id}
            type="button"
            onClick={() => open(id, a)}
            disabled={gone || opening === id}
            title={gone ? UNAVAILABLE : `Open ${a.name}`}
            className={`${CHIP} hover:bg-indigo-200 dark:hover:bg-indigo-900/70 disabled:cursor-default disabled:hover:bg-indigo-100 ${gone ? "opacity-60 line-through" : ""}`}
          >
            {opening === id ? (
              <Loader2 className="w-3 h-3 shrink-0 animate-spin" />
            ) : (
              <Paperclip className="w-3 h-3 shrink-0" />
            )}
            {a.name}
          </button>
        );
      })}
      {unavailable.size > 0 && (
        <p className="basis-full text-right text-xs text-gray-500 dark:text-gray-400">
          {UNAVAILABLE}
        </p>
      )}
    </div>
  );
};
 
export default SentAttachments;