All files / components/workspace search-chat.tsx

80.95% Statements 17/21
75% Branches 3/4
55.55% Functions 5/9
80.95% Lines 17/21

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    3x 3x 3x   3x 3x 3x 3x 3x 3x                       4x 4x             1x       1x 1x             4x                                                                                               4x         8x                                                                                  
"use client";
 
import { createPortal } from "react-dom";
import { SIDEBAR_COPY } from "@/constants/app";
import { cn } from "@/utils/class-name";
import type { ChatThread } from "@/types/thread";
import { XMarkIcon } from "@/components/icons/XMark";
import { PlusIcon } from "@/components/icons/Plus";
import { MessageBubbleIcon } from "@/components/icons/MessageBubble";
import { useSearchChat } from "@/hooks/use-search-chat";
import { TrashIcon } from "@/components/icons/Trash";
import { formatTimestamp } from "@/utils/date";
 
type SearchChatModalProps = {
  allThreads: ChatThread[];
  activeThreadId: string;
  disableNewChat: boolean;
  onSelect: (id: string) => void;
  onClose: () => void;
  onCreateThread: () => void;
  onDeleteThread: (id: string) => void;
};
 
export function SearchChatModal({ allThreads, activeThreadId, disableNewChat, onSelect, onClose, onCreateThread, onDeleteThread }: SearchChatModalProps) {
  const { query, setQuery, threadGroups, inputRef } = useSearchChat(allThreads, onClose);
 
  function handleQueryChange(e: React.ChangeEvent<HTMLInputElement>) {
    setQuery(e.target.value);
  }
 
  function handleModalClick(e: React.MouseEvent<HTMLDivElement>) {
    e.stopPropagation();
  }
 
  function handleNewChat() {
    onCreateThread();
    onClose();
  }
 
  function handleSelectThread(id: string) {
    onSelect(id);
  }
 
  return createPortal(
    <div
      data-testid="search-backdrop"
      className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 p-4 backdrop-blur-sm"
      onClick={onClose}
    >
      <div
        className="w-full max-w-md overflow-hidden rounded-2xl border border-white/12 bg-modal-bg shadow-navy-xl light:border-slate-200 light:bg-white"
        onClick={handleModalClick}
      >
        <div className="flex items-center gap-3 border-b border-white/8 px-4 py-4 light:border-slate-200">
          <input
            ref={inputRef}
            type="text"
            value={query}
            onChange={handleQueryChange}
            placeholder={SIDEBAR_COPY.searchPlaceholder}
            className="flex-1 bg-transparent text-sm text-white/90 placeholder:text-white/35 outline-none light:text-slate-800 light:placeholder:text-slate-400"
          />
          <button
            type="button"
            aria-label="Close search"
            onClick={onClose}
            className="grid h-6 w-6 shrink-0 place-items-center rounded-md text-white/40 transition hover:text-white/80 light:text-slate-400 light:hover:text-slate-700"
          >
            <XMarkIcon className="size-3.5" />
          </button>
        </div>
 
        <div className="max-h-[60vh] overflow-y-auto pb-2">
          <button
            type="button"
            disabled={disableNewChat}
            onClick={handleNewChat}
            className="flex w-full items-center gap-3 px-4 py-3 text-left transition hover:bg-white/6 disabled:cursor-not-allowed disabled:opacity-40 light:hover:bg-slate-50"
          >
            <span className="grid size-5 shrink-0 place-items-center rounded-full border border-white/20 light:border-slate-300">
              <PlusIcon className="size-3 text-white/60 light:text-slate-500" />
            </span>
            <span className="text-sm text-white/85 light:text-slate-800">{SIDEBAR_COPY.newChatLabel}</span>
          </button>
 
          {threadGroups.length === 0 ? (
            <p className="px-4 py-4 text-sm text-white/40 light:text-slate-400">
              {SIDEBAR_COPY.searchNoResults}
            </p>
          ) : (
            threadGroups.map((group) => (
              <div key={group.label}>
                <p className="px-4 pb-1 pt-3 text-[10px] font-semibold uppercase tracking-[0.12em] text-white/35 light:text-slate-400">
                  {group.label}
                </p>
                {group.threads.map((thread) => (
                  <div
                    key={thread.id}
                    className={cn(
                      "group flex w-full items-center gap-3 px-4 py-2.5 transition hover:bg-white/6 light:hover:bg-slate-50",
                      thread.id === activeThreadId && "bg-white/5 light:bg-slate-100",
                    )}
                  >
                    <button
                      type="button"
                      onClick={() => handleSelectThread(thread.id)}
                      className="flex min-w-0 flex-1 items-center gap-3 text-left"
                    >
                      <MessageBubbleIcon className="size-5 shrink-0 text-white/30 light:text-slate-400" />
                      <span className="min-w-0 flex-1">
                        <span className="block truncate text-sm text-white/85 light:text-slate-800">
                          {thread.title}
                        </span>
                        <span className="block text-[11px] text-white/35 light:text-slate-400" suppressHydrationWarning>
                          {formatTimestamp(thread.updatedAt)}
                        </span>
                      </span>
                    </button>
                    <button
                      type="button"
                      aria-label="Delete chat"
                      onClick={() => onDeleteThread(thread.id)}
                      className="grid h-6 w-6 shrink-0 place-items-center rounded-md text-white/30 transition hover:text-rose-300 light:text-slate-400 light:hover:text-rose-500"
                    >
                      <TrashIcon className="size-3.5" />
                    </button>
                  </div>
                ))}
              </div>
            ))
          )}
        </div>
      </div>
    </div>,
    document.body,
  );
}