All files / src/components/chat/ChatView ChatViewFrame.tsx

100% Statements 8/8
87.5% Branches 7/8
100% Functions 2/2
100% Lines 8/8

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                          11x           147x                 11x           81x   75x   75x                 6x 6x            
import type { ChatFrame } from "@/components/chat/ChatLayout/ChatLayout";
import SubPage from "@/components/page/SubPage/SubPage";
import React from "react";
 
/**
 * What a chat view (`AgentChatView`, `ModelChatView`) is drawn inside, by
 * frame: on its own page, the page's title above it; in a panel (the chat
 * dock), nothing — the panel has its own header.
 *
 * Never `SubPage` in a panel: `SubPage` names the page (`usePageMeta`: the
 * header's title, the back button, `document.title`) and clears it again on
 * unmount, so a chat in the dock would rename whatever page it is open over.
 */
export const ChatViewFrame: React.FC<{
  frame: ChatFrame;
  title: React.ReactNode;
  icon?: React.ReactNode;
  children: React.ReactNode;
}> = ({ frame, title, icon, children }) =>
  frame === "page" ? (
    <SubPage title={title} title_icon={icon}>
      {children}
    </SubPage>
  ) : (
    <>{children}</>
  );
 
/** A chat view whose agent or model is still loading, or could not be. */
export const ChatViewStatus: React.FC<
  { frame: ChatFrame } & (
    | { state: "loading"; noun: "agent" | "model" }
    | { state: "error"; message: string }
  )
> = (props) => {
  if (props.state === "loading") {
    const bar = (
      <div className="animate-pulse h-8 w-48 rounded bg-gray-200 dark:bg-gray-700" />
    );
    return props.frame === "page" ? (
      <SubPage title={`Loading ${props.noun}…`}>{bar}</SubPage>
    ) : (
      <div aria-busy="true" className="flex flex-col gap-3 p-1">
        {bar}
        <div className="animate-pulse flex-1 min-h-40 rounded-2xl bg-gray-100 dark:bg-gray-800" />
      </div>
    );
  }
  const message = <p className="text-red-500 text-sm">{props.message}</p>;
  return props.frame === "page" ? (
    <SubPage title="Error">{message}</SubPage>
  ) : (
    <div className="p-1">{message}</div>
  );
};