Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 53 additions & 57 deletions app/src/components/channels/composer/composer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,9 @@ function stagedMimeType(attachment: Attachment): string {

const MAX_HEIGHT_PX = 220;
/**
* Two lines: the compact composer's message box opens two lines tall and grows from there. 22.5px
* lines at 15px; 24px lines at the 16px a touch screen gets (see `inputClassName`).
*/
const COMPACT_MIN_HEIGHT_PX = 45;
const COMPACT_TOUCH_MIN_HEIGHT_PX = 48;
/**
* One 24px line: a one-line box opens at this height. Given to PromptArea itself, whose own
* minimum (80px) is an inline style no class can beat.
* One 24px line: a one-line box opens at this height, the compact composer's included, and grows
* as the words wrap. Given to PromptArea itself, whose own minimum (80px) is an inline style no
* class can beat.
*/
const ONE_LINE_HEIGHT_PX = 24;
const COMPACT_MAX_HEIGHT_PX = 160;
Expand Down Expand Up @@ -350,7 +345,7 @@ export type ComposerProps = {
spinsWhileSending?: boolean;
/**
* True draws the words and the controls in one row, so the empty box is one line tall and grows
* as the words wrap (Routines). Only the full-size box has it; a compact box keeps its two rows.
* as the words wrap (Routines). A compact box is always one row.
*/
oneLine?: boolean;
};
Expand Down Expand Up @@ -2032,10 +2027,10 @@ export function Composer({
* internally at COMPACT_MAX_HEIGHT_PX: padding inside that box would scroll away with
* the text, so the first visible line would touch the top edge on a long message.
*
* A column: the attachment strip across the top, the message box under it, and the
* controls row at the bottom.
* A column: the attachment strip across the top, then one row of the controls and the
* words between them.
*/
"flex flex-col px-3 py-3",
"flex flex-col p-2",
MESSAGE_BOX_FRAME,
className,
)}
Expand All @@ -2049,54 +2044,55 @@ export function Composer({
onRemove={discardAttachment}
/>
{/*
* TWO ROWS: the message box across the full width, then the controls under it. Attach
* and the model on the left, the microphone and send on the right, so the controls
* keep their place however long the message grows.
* ONE ROW: Attach on the left, the words in the middle, the model, the microphone and
* Send on the right. Empty, the box is one line tall, as tall as its buttons. The words
* grow upward as they wrap, and the controls stay on the bottom line, where they were.
*/}
<div className="min-w-0" data-slot="composer-input">
<PromptArea
aria-label={label}
className={inputClassName}
disabled={disabled}
maxHeight={COMPACT_MAX_HEIGHT_PX}
minHeight={
touchTyping
? COMPACT_TOUCH_MIN_HEIGHT_PX
: COMPACT_MIN_HEIGHT_PX
}
onChange={handleChange}
onImagePaste={canAttach ? stagePastedImage : undefined}
onKeyDown={handleEditorKeyDown}
onSubmit={submitDraft}
placeholder={placeholder}
ref={promptAreaRef}
submitOnEnter={!touchTyping}
triggers={triggers}
value={value}
/>
</div>
<div className="mt-2 flex items-center justify-between gap-2">
<div className="flex min-w-0 items-center gap-1">
{attachmentsEnabled ? (
<Button
aria-label="Attach a file"
data-slot="composer-attach"
// The button stays, and stays labelled: a conversation that cannot take another
// message has not lost the ability to attach, it has lost the message.
disabled={disabled}
onClick={() => fileInputRef.current?.click()}
size="icon"
type="button"
variant="ghost"
>
<IconPlus className="size-5" />
</Button>
) : null}
{/* No attachments and nothing else behind a plus, so no plus: a disabled one that
explained nothing read as broken. */}
{modelPicker}
<div className="flex items-end gap-1">
{attachmentsEnabled ? (
<Button
aria-label="Attach a file"
data-slot="composer-attach"
// The button stays, and stays labelled: a conversation that cannot take another
// message has not lost the ability to attach, it has lost the message.
disabled={disabled}
onClick={() => fileInputRef.current?.click()}
size="icon"
type="button"
variant="ghost"
>
<IconPlus className="size-5" />
</Button>
) : null}
{/* No attachments and nothing else behind a plus, so no plus: a disabled one that
explained nothing read as broken. */}
{/* The 4px above and below a 24px line make it as tall as the 32px buttons beside it. */}
<div
className={cn(
"min-w-0 grow py-1",
!attachmentsEnabled && "pl-1",
)}
data-slot="composer-input"
>
<PromptArea
aria-label={label}
className={inputClassName}
disabled={disabled}
maxHeight={COMPACT_MAX_HEIGHT_PX}
minHeight={ONE_LINE_HEIGHT_PX}
onChange={handleChange}
onImagePaste={canAttach ? stagePastedImage : undefined}
onKeyDown={handleEditorKeyDown}
onSubmit={submitDraft}
placeholder={placeholder}
ref={promptAreaRef}
submitOnEnter={!touchTyping}
triggers={triggers}
value={value}
/>
</div>
<div className="flex shrink-0 items-center gap-1">
{modelPicker}
{dictates ? (
<DictationButton
dictation={dictation}
Expand Down
18 changes: 12 additions & 6 deletions app/src/components/channels/composer/model-picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -571,14 +571,20 @@ function Picker({
aria-hidden
className="size-4 shrink-0 text-destructive"
/>
) : (
) : showValue ? (
<IconCpu aria-hidden className="size-4 shrink-0" />
)}
{showValue ? (
<span className="min-w-0 flex-1 truncate text-left">
{shown}
</span>
) : null}
{/* In the message box, the model by its name, cut short when long: a chip icon
alone didn't say which model the next message goes to. The thinking setting is
in the name a screen reader hears and in the tooltip. */}
<span
className={cn(
"min-w-0 truncate text-left",
showValue ? "flex-1" : "max-w-28",
)}
>
{showValue ? shown : (selected?.label ?? "Model")}
</span>
<IconChevronDown aria-hidden className="size-3.5 shrink-0" />
</ComboboxPrimitive.Trigger>
}
Expand Down
30 changes: 25 additions & 5 deletions app/src/lib/auth/greeting.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
import type { AuthenticatedUser } from "./queries";

/** Keep the welcome out of message history, and never guess a first name. */
/**
* The first word of the name the person saved: "Sam Julien" is greeted as Sam. Undefined when there
* is no name to go by (none saved, one still to be asked for, or the email standing in for it):
* a first name is never guessed from an email.
*/
export function firstName(
user: AuthenticatedUser | null | undefined,
): string | undefined {
const name = user?.name?.trim();
if (!name || user?.needsName || name === user?.email) return undefined;
return name.split(/\s+/)[0];
}

/** What Home says when nobody's name is known. */
const NO_NAME = "What would you like to work on?";

/**
* Home's heading: the person by their first name, asked what to work on the first time, welcomed
* back after that. Kept out of message history.
*/
export function firstConversationGreeting(
user: AuthenticatedUser | null | undefined,
noConversations: boolean,
): string {
const name = user?.name?.trim();
if (!noConversations || !name || user?.needsName || name === user?.email)
return "Start a conversation";
return `Hi ${name}, what would you like to work on?`;
const name = firstName(user);
if (!name) return NO_NAME;
return noConversations
? `Hi ${name}, what would you like to work on?`
: `Welcome back, ${name}`;
}
3 changes: 2 additions & 1 deletion app/src/routes/_authed/_app/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ function Greeting() {
// not yet shown.
if (greeting === undefined && (!channels.isPending || waited))
setGreeting(choose());
const heading = "text-2xl font-semibold tracking-tight text-center";
// The title step, at a lighter weight than a page title: a greeting, not a heading to scan.
const heading = "text-2xl font-medium tracking-tight text-center";
if (!waits) return <h1 className={heading}>{greeting}</h1>;
const could = [
...new Set([
Expand Down
62 changes: 62 additions & 0 deletions app/tests/composer-one-row.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { afterAll, afterEach, beforeAll, expect, test } from "bun:test";
import { GlobalRegistrator } from "@happy-dom/global-registrator";
import { cleanup, render } from "@testing-library/react";
import { Composer } from "@/components/channels/composer/composer";
import { settleReactWork } from "./settle-react-work";

/**
* The message box in a conversation (the compact composer) is one row: Attach, the words, then the
* model, the microphone and Send. Empty, it is one line tall. It was two rows, the words over the
* controls, about 114px tall before anything was typed.
*
* The harness is this repository's: see composer-attachments-ui.test.tsx.
*/
const originalFetch = global.fetch;
beforeAll(() => {
GlobalRegistrator.register({ url: "http://localhost/" });
// Nothing here reads the server: whatever the box asks for is not found.
global.fetch = (async () =>
new Response("{}", { status: 404 })) as unknown as typeof fetch;
});
afterEach(cleanup);
afterAll(async () => {
await settleReactWork();
global.fetch = originalFetch;
GlobalRegistrator.unregister();
});

test("a conversation's box is one row: Attach, the words and Send side by side", () => {
const view = render(
<Composer
channelId="channel-1"
compact
modelPicker={<button type="button">Model</button>}
onSubmit={() => {}}
/>,
);
const input = view.container.querySelector("[data-slot=composer-input]");
const row = input?.parentElement;
expect(row?.className).toContain("flex");
// Every control keeps its name, in the same row as the words.
const attach = view.getByRole("button", { name: "Attach a file" });
const send = view.getByRole("button", { name: "Send message" });
const model = view.getByRole("button", { name: "Model" });
for (const control of [attach, send, model])
expect(row?.contains(control)).toBe(true);
expect(view.getByRole("textbox", { name: "Message" })).toBeTruthy();
// Attach first, the words, then the model before Send.
const order = [attach, input, model, send].map((element) =>
[...(row?.querySelectorAll("*") ?? [])].indexOf(element as Element),
);
expect(order).toEqual([...order].sort((a, b) => a - b));
});

test("empty, the words take one 24px line, not two", () => {
const view = render(
<Composer channelId="channel-1" compact onSubmit={() => {}} />,
);
const editor = view.container.querySelector(
"[data-slot=composer-input] [style*=min-height]",
) as HTMLElement | null;
expect(editor?.style.minHeight).toBe("24px");
});
21 changes: 15 additions & 6 deletions app/tests/first-conversation-greeting.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from "bun:test";
import { firstConversationGreeting } from "../src/lib/auth/greeting";
import { firstConversationGreeting, firstName } from "../src/lib/auth/greeting";
import type { AuthenticatedUser } from "../src/lib/auth/queries";

const user: AuthenticatedUser = {
Expand All @@ -10,20 +10,29 @@ const user: AuthenticatedUser = {
onboarding: null,
needsName: false,
};
test("greets by the saved name only with a confirmed empty conversation list", () => {
test("greets by first name: asked what to work on the first time, welcomed back after", () => {
expect(firstConversationGreeting(user, true)).toBe(
"Hi Sam Julien, what would you like to work on?",
"Hi Sam, what would you like to work on?",
);
expect(firstConversationGreeting(user, false)).toBe("Start a conversation");
expect(firstConversationGreeting(user, false)).toBe("Welcome back, Sam");
});
test("does not greet with a missing or placeholder name", () => {
test("a first name is the first word of the saved name", () => {
expect(firstName({ ...user, name: " Ana María Ruiz " })).toBe("Ana");
expect(firstName({ ...user, name: "Cher" })).toBe("Cher");
});
test("with a missing or placeholder name, a neutral line", () => {
for (const value of [
undefined,
null,
{ ...user, name: "" },
{ ...user, name: " " },
{ ...user, needsName: true },
{ ...user, name: user.email },
]) {
expect(firstConversationGreeting(value, true)).toBe("Start a conversation");
for (const first of [true, false])
expect(firstConversationGreeting(value, first)).toBe(
"What would you like to work on?",
);
expect(firstName(value)).toBeUndefined();
}
});
7 changes: 3 additions & 4 deletions app/tests/home-holds-still.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ const ANSWER_AFTER_MS = 400;
/** How long a test waits for the page to ask for both lists and have them answered. */
const ANSWERED_WITHIN_MS = 10_000;
/** The person the fixture signs in, and what Home says to them before their first conversation. */
const FIRST_CONVERSATION_GREETING =
"Hi Sam Rivera, what would you like to work on?";
const FIRST_CONVERSATION_GREETING = "Hi Sam, what would you like to work on?";

const APP = join(import.meta.dir, "..");
const ORIGIN = "http://openbot.test";
Expand Down Expand Up @@ -542,7 +541,7 @@ describe.skipIf(!asked)("Home while it loads", () => {
}

for (const engine of engines) {
test(`a conversation list that fails leaves the plain greeting, drawn once, in ${engine}`, async () => {
test(`a conversation list that fails leaves the returning greeting, drawn once, in ${engine}`, async () => {
const { page, frames } = await loaded(
engine,
"light",
Expand All @@ -551,7 +550,7 @@ describe.skipIf(!asked)("Home while it loads", () => {
{ bots: false, conversations: false, conversationsFail: true },
);
try {
expect(greetingsOf(frames)).toEqual(["Start a conversation"]);
expect(greetingsOf(frames)).toEqual(["Welcome back, Sam"]);
} finally {
await page.close();
}
Expand Down
11 changes: 7 additions & 4 deletions app/tests/model-picker.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ function thinking(popup: HTMLElement) {
return within(popup).queryByRole("group", { name: "Thinking" });
}

test("the trigger is a CPU icon, and names a model that takes no effort on its own", () => {
test("the trigger says the model by its name, and names a model that takes no effort on its own", () => {
const { getByRole } = render(
<ModelPicker
catalogue={catalogue}
Expand All @@ -201,10 +201,13 @@ test("the trigger is a CPU icon, and names a model that takes no effort on its o
/>,
);
const trigger = getByRole("combobox", { name: "Model: GPT-4o" });
// No name and no maker's mark in the row: the CPU icon and a chevron, named for screen readers.
expect(trigger.textContent).toBe("");
// The model's name and a chevron, cut short when long. No maker's mark and no chip icon.
expect(trigger.textContent).toBe("GPT-4o");
expect(trigger.querySelector("img")).toBeNull();
expect(trigger.firstElementChild?.classList.contains("icon-cpu")).toBe(true);
expect(trigger.querySelector(".icon-cpu")).toBeNull();
expect(trigger.querySelector("span")?.classList.contains("truncate")).toBe(
true,
);
});

/** The open popup of a picker drawn on its own. */
Expand Down
Loading