From 3da614bc92f56f700fe0cc8766d50231647b51a7 Mon Sep 17 00:00:00 2001 From: Justin Williams Date: Wed, 25 Nov 2026 17:26:17 -0800 Subject: [PATCH 1/3] refactor: one helper for a message's Ref Commands take a message's author and sequence number alone. refOf builds that in one place, instead of five { author, seq } literals. Co-Authored-By: Claude Opus 5.5 --- ui/src/components/Chat.tsx | 15 ++++++--------- ui/src/store.ts | 2 +- ui/src/transcript.test.ts | 7 +++++++ ui/src/transcript.ts | 3 +++ 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/ui/src/components/Chat.tsx b/ui/src/components/Chat.tsx index be77a81..0710ab0 100644 --- a/ui/src/components/Chat.tsx +++ b/ui/src/components/Chat.tsx @@ -71,7 +71,7 @@ import { store, unstageFile, } from '@/store' -import { indexOf, messageKey, targetOf } from '@/transcript' +import { indexOf, messageKey, refOf, targetOf } from '@/transcript' /** The reactions on offer. */ const EMOJI = ['👍', '❤️', '😂', '😮', '😢', '🎉'] @@ -407,9 +407,7 @@ const Row = memo(function Row({ const myReaction = message.reactions.find((r) => r.author === myId)?.emoji // Picking my current reaction again takes it back. const react = (emoji: string) => - void run( - api.react({ author: message.author, seq: message.seq }, emoji === myReaction ? '' : emoji), - ) + void run(api.react(refOf(message), emoji === myReaction ? '' : emoji)) const footer = groupEnd && ( {when.toLocaleTimeString([], { hour: 'numeric', minute: '2-digit' })} @@ -834,7 +832,7 @@ function Composer() { return } const saved = await run( - api.editMessage({ author: of.author, seq: of.seq }, value).then(() => true), + api.editMessage(refOf(of), value).then(() => true), 'composer', ) if (saved) setEditing(null) @@ -864,7 +862,7 @@ function Composer() { setSending(true) for (const [i, s] of store.state.staged.entries()) { const first = i === 0 - const to = first && reply ? { author: reply.author, seq: reply.seq } : undefined + const to = first && reply ? refOf(reply) : undefined const withText = first ? caption : '' const sent = await run( 'pasted' in s.source @@ -894,7 +892,7 @@ function Composer() { reset() setReplyingTo(null) typingSentRef.current = 0 // the next message signals typing right away - const to = reply ? { author: reply.author, seq: reply.seq } : undefined + const to = reply ? refOf(reply) : undefined const sent = await run(api.sendMessage(value, to, vanish), 'composer') if (sent) { addMessage(sent) @@ -1192,8 +1190,7 @@ function DeleteDialog() { onClick={() => { // AlertDialogAction is a plain button: it doesn't close the dialog itself. setDeleting(null) - if (deleting) - void run(api.deleteMessage({ author: deleting.author, seq: deleting.seq })) + if (deleting) void run(api.deleteMessage(refOf(deleting))) }} > Delete diff --git a/ui/src/store.ts b/ui/src/store.ts index 7811eca..0033360 100644 --- a/ui/src/store.ts +++ b/ui/src/store.ts @@ -256,7 +256,7 @@ export async function reveal(target: Cursor) { // Against the latest transcript, in case a page of older messages arrived meanwhile. store.setState((s) => ({ ...s, transcript: transcript.fill(s.transcript, missing ?? []) })) } - const at = { author: target.author, seq: target.seq } + const at = transcript.refOf(target) store.setState((s) => ({ ...s, jump: { at, n: ++jumps } })) } diff --git a/ui/src/transcript.test.ts b/ui/src/transcript.test.ts index f348ecf..6c325e7 100644 --- a/ui/src/transcript.test.ts +++ b/ui/src/transcript.test.ts @@ -13,6 +13,7 @@ import { targetOf, messageKey, prepend, + refOf, update, } from './transcript' @@ -59,6 +60,12 @@ describe('display order', () => { }) }) +describe('refs', () => { + test('carry only the author and number, as commands take them', () => { + expect(refOf(msg('a', 7, 3, { text: 'hi' }))).toEqual({ author: 'a', seq: 7 }) + }) +}) + describe('live messages', () => { test('go in display order, usually last', () => { let t = fromPage({ messages: [msg('a', 1, 10), msg('b', 1, 30)], has_more: false }) diff --git a/ui/src/transcript.ts b/ui/src/transcript.ts index fc981b3..bfc6dd1 100644 --- a/ui/src/transcript.ts +++ b/ui/src/transcript.ts @@ -19,6 +19,9 @@ export const empty: Transcript = { messages: [], keys: new Set(), hasOlder: fals export const messageKey = (m: Ref) => `${m.author}:${m.seq}` +/** Just the author and sequence number, as commands take them. */ +export const refOf = (m: Ref): Ref => ({ author: m.author, seq: m.seq }) + export const cursorOf = (m: Message): Cursor => ({ hlc: m.hlc, author: m.author, seq: m.seq }) /** Display order. The UI's one copy of the log's SQL `ORDER BY hlc, author, seq`. */ From f1eb37b47b430db863ed9f7a70bc6df131a411d2 Mon Sep 17 00:00:00 2001 From: Justin Williams Date: Wed, 25 Nov 2026 17:28:00 -0800 Subject: [PATCH 2/3] refactor: one module for how a message reads ui/src/reading.ts turns a message, or a reply's quote of one, into what it reads as: a placeholder (deleted, vanished, not on this computer), an action, or what was said with its file. The transcript lays that out; a quote shows its summary. The wording stays as it was, with one edge brought in line with the bubble: a quoted file whose caption starts with /me reads as its caption, not as an action. Adds Action and Summary to the glossary. Notifications stay worded in Rust, with a pointer to the UI's rules. Co-Authored-By: Claude Opus 5.5 --- GLOSSARY.md | 8 +++ README.md | 1 + src-tauri/src/desktop.rs | 3 +- ui/src/components/Chat.tsx | 26 +++----- ui/src/components/Files.tsx | 5 +- ui/src/reading.test.ts | 117 ++++++++++++++++++++++++++++++++++++ ui/src/reading.ts | 53 ++++++++++++++++ 7 files changed, 189 insertions(+), 24 deletions(-) create mode 100644 ui/src/reading.test.ts create mode 100644 ui/src/reading.ts diff --git a/GLOSSARY.md b/GLOSSARY.md index 155bb40..61db358 100644 --- a/GLOSSARY.md +++ b/GLOSSARY.md @@ -44,6 +44,14 @@ _Avoid_: ignored entry, unknown entry What the transcript shows for a message entry: its current text with later edits, deletes and reactions folded in. _Avoid_: entry (when meaning the shown result), post +**Action**: +A message whose text starts with `/me`, shown as what its author does ("Alex waves") instead of as something said. A message with a file is never an action. +_Avoid_: emote, me-message + +**Summary**: +A message in one line, where it's quoted or listed rather than shown in full: its text or caption, what its file is, or that it's gone. +_Avoid_: preview (that's a picture's), excerpt, snippet (that's a search match's) + **Transcript**: The messages shown, in display order, as far back as they're loaded. Older ones load as the reader scrolls up, or when a search hit or a reply's quote reaches back to them. _Avoid_: chat log, history (for what's shown), feed diff --git a/README.md b/README.md index 5f749fc..6e5d1e4 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ ui/src/api.ts typed wrappers for the Rust commands, plus the shared ui/src/store.ts app state (TanStack Store), fed by events from Rust; history paging ui/src/transcript.ts the transcript: what a page, a live message or an update does to it ui/src/people.ts whose name each message shows +ui/src/reading.ts how a message reads: placeholders, actions, its summary ui/src/commands.ts slash commands ui/src/components/ the screens: Setup, Pair, Chat, Search, Files, PairDialog, Notice ui/src/components/ui/ shadcn/ui components (generated; update with the shadcn CLI) diff --git a/src-tauri/src/desktop.rs b/src-tauri/src/desktop.rs index 3118f7d..244e9b1 100644 --- a/src-tauri/src/desktop.rs +++ b/src-tauri/src/desktop.rs @@ -108,7 +108,8 @@ pub fn on_message(app: &AppHandle, message: &Message) { let unread = desktop.unread.fetch_add(1, Ordering::Relaxed) + 1; show_unread(app, &window, unread); if desktop.notifications() && !core.dnd() && !recently_notified(&desktop) { - // A vanishing message's text would outlive it in the notification center. + // A vanishing message's text would outlive it in the notification center. The + // rest follows how the UI words a message (`ui/src/reading.ts`). let text = match message.text.strip_prefix("/me ") { _ if message.vanish.is_some() => "Vanishing message".to_string(), Some(action) => format!("{} {action}", title(core.peer_name(), "They")), diff --git a/ui/src/components/Chat.tsx b/ui/src/components/Chat.tsx index 0710ab0..8433ae0 100644 --- a/ui/src/components/Chat.tsx +++ b/ui/src/components/Chat.tsx @@ -16,7 +16,7 @@ import { memo, useEffect, useMemo, useRef, useState } from 'react' import { api, type Message as ChatMessage, type Quote, type Reaction } from '@/api' import { type Command, parse, suggest } from '@/commands' -import { describeFile, FileView, StagedFiles, Viewer } from '@/components/Files' +import { FileView, StagedFiles, Viewer } from '@/components/Files' import { Linkified } from '@/components/Linkified' import { NoteCard } from '@/components/Notice' import { SearchBar, SearchResults } from '@/components/Search' @@ -54,6 +54,7 @@ import { Spinner } from '@/components/ui/spinner' import { Textarea } from '@/components/ui/textarea' import { formatDuration, formatUntil } from '@/lib/duration' import { namer, peopleKey } from '@/people' +import { describeFile, goneText, readingOf, summaryOf } from '@/reading' import { addMessage, clearHistory, @@ -415,7 +416,8 @@ const Row = memo(function Row({ ) - if (message.deleted) { + const reading = readingOf(message) + if (reading.kind === 'gone') { return (
{newDay && } @@ -423,11 +425,7 @@ const Row = memo(function Row({ - {message.vanished - ? 'Message vanished' - : mine - ? 'You deleted this message' - : 'Message deleted'} + {goneText(reading.why, mine)} {footer} @@ -438,7 +436,7 @@ const Row = memo(function Row({ } // `/me waves`: an action line, "Alex waves", instead of a bubble. - const action = message.text.startsWith('/me ') && !message.file ? message.text.slice(4) : null + const action = reading.kind === 'action' ? reading.action : null const { file } = message const words = ( <> @@ -591,17 +589,7 @@ function QuoteButton({ onClick={() => onJump(quote)} > {name} - - {quote.vanished - ? 'Message vanished' - : quote.deleted - ? 'Deleted message' - : quote.file && !quote.text - ? describeFile(quote.file) - : quote.text?.startsWith('/me ') - ? `* ${quote.text.slice(4)}` - : (quote.text ?? 'A message that isn’t on this computer')} - + {summaryOf(quote)} ) } diff --git a/ui/src/components/Files.tsx b/ui/src/components/Files.tsx index f72b512..408fbd6 100644 --- a/ui/src/components/Files.tsx +++ b/ui/src/components/Files.tsx @@ -6,7 +6,7 @@ import { useSelector } from '@tanstack/react-store' import { DownloadIcon, ExternalLinkIcon, FileIcon, FolderOpenIcon, XIcon } from 'lucide-react' import { useState } from 'react' -import { api, type Attachment, type FileInfo } from '@/api' +import { api, type Attachment } from '@/api' import { Button } from '@/components/ui/button' import { fitWithin, formatBytes } from '@/lib/files' import { run, setViewing, store, unstageFile } from '@/store' @@ -18,9 +18,6 @@ const onMac = navigator.userAgent.includes('Mac') const SHOW_IN_FOLDER = onMac ? 'Show in Finder' : 'Show in folder' const SAVE_A_COPY = 'Save a copy…' -/** A picture, or a file's name: for quotes, the reply bar, and notifications. */ -export const describeFile = (file: FileInfo) => (file.width ? 'Picture' : file.name) - /** A message's file: the picture itself, or a card for any other file. */ export function FileView({ file, peer }: { file: Attachment; peer: string }) { // The other side can't always show what the sender could, like HEIC on Windows. diff --git a/ui/src/reading.test.ts b/ui/src/reading.test.ts new file mode 100644 index 0000000..8439b4c --- /dev/null +++ b/ui/src/reading.test.ts @@ -0,0 +1,117 @@ +import { describe, expect, test } from 'bun:test' + +import type { Attachment, Message, Quote } from './api' +import { actionOf, goneText, readingOf, summaryOf } from './reading' + +function msg(more: Partial = {}): Message { + return { + author: 'a', + seq: 1, + ts: 1, + hlc: 1, + text: 'hello', + edited: false, + deleted: false, + file: null, + vanish: null, + expires: null, + vanished: false, + reply_to: null, + reactions: [], + ...more, + } +} + +function quote(more: Partial = {}): Quote { + return { + author: 'a', + seq: 1, + hlc: 1, + text: 'hello', + deleted: false, + vanished: false, + file: null, + ...more, + } +} + +const pdf: Attachment = { + hash: 'h', + name: 'Report.pdf', + mime: 'application/pdf', + size: 10, + state: 'done', + openable: true, +} +const picture: Attachment = { ...pdf, name: 'beach.jpg', mime: 'image/jpeg', width: 4, height: 3 } + +const deleted = { text: '', deleted: true } +const vanished = { text: '', deleted: true, vanished: true } + +describe('what a message reads as', () => { + const cases: [string, Message | Quote, ReturnType][] = [ + ['text', msg(), { kind: 'said', text: 'hello', file: null }], + ['an action', msg({ text: '/me waves' }), { kind: 'action', action: 'waves' }], + ['"/me" alone, not an action', msg({ text: '/me' }), { kind: 'said', text: '/me', file: null }], + [ + 'a caption that starts with /me, not an action', + msg({ text: '/me waves', file: picture }), + { kind: 'said', text: '/me waves', file: picture }, + ], + ['a file alone', msg({ text: '', file: pdf }), { kind: 'said', text: '', file: pdf }], + ['deleted', msg(deleted), { kind: 'gone', why: 'deleted' }], + ['vanished', msg(vanished), { kind: 'gone', why: 'vanished' }], + [ + 'a quote of a deleted one', + quote({ text: null, deleted: true }), + { kind: 'gone', why: 'deleted' }, + ], + [ + 'a quote of a vanished one', + quote({ text: null, deleted: true, vanished: true }), + { kind: 'gone', why: 'vanished' }, + ], + [ + 'a quote of one not on this computer', + quote({ text: null }), + { kind: 'gone', why: 'missing' }, + ], + ] + for (const [name, m, reading] of cases) { + test(name, () => expect(readingOf(m)).toEqual(reading)) + } +}) + +describe('placeholders', () => { + test('say whose delete it was, and nothing else', () => { + expect(goneText('deleted', true)).toBe('You deleted this message') + expect(goneText('deleted', false)).toBe('Message deleted') + expect(goneText('vanished', true)).toBe('Message vanished') + expect(goneText('missing', false)).toBe('A message that isn’t on this computer') + }) +}) + +describe('a message in one line', () => { + const cases: [string, Message | Quote, string][] = [ + ['text', quote(), 'hello'], + ['an action', quote({ text: '/me waves' }), '* waves'], + ['a caption', quote({ text: 'look', file: picture }), 'look'], + ['a picture alone', quote({ text: '', file: picture }), 'Picture'], + ['a file alone', quote({ text: '', file: pdf }), 'Report.pdf'], + ['deleted', quote({ text: null, deleted: true }), 'Deleted message'], + ['vanished', quote({ text: null, deleted: true, vanished: true }), 'Message vanished'], + ['not on this computer', quote({ text: null }), 'A message that isn’t on this computer'], + ] + for (const [name, m, line] of cases) { + test(name, () => expect(summaryOf(m)).toBe(line)) + } +}) + +describe('actions', () => { + test('are text that starts with "/me "', () => { + expect(actionOf('/me waves')).toBe('waves') + expect(actionOf('/me')).toBeNull() + expect(actionOf('/men are here')).toBeNull() + expect(actionOf('hi /me waves')).toBeNull() + }) +}) diff --git a/ui/src/reading.ts b/ui/src/reading.ts new file mode 100644 index 0000000..0046277 --- /dev/null +++ b/ui/src/reading.ts @@ -0,0 +1,53 @@ +// How a message reads: the one place that decides its wording, wherever it's shown. The +// transcript lays out a message's reading; quotes and the reply bar show its summary. +// +// Notifications are worded in Rust (`desktop::on_message`). Keep the two in step. + +import type { FileInfo, Message, Quote } from './api' + +/** What a message reads as. */ +export type Reading = + /** Deleted, vanished, or not on this computer: a placeholder. */ + | { kind: 'gone'; why: Gone } + /** An action, like `/me waves`: shown as "Alex waves". */ + | { kind: 'action'; action: string } + /** Anything else: its text, and its file if it has one (the text is then its caption). */ + | { kind: 'said'; text: string; file: FileInfo | null } + +export type Gone = 'deleted' | 'vanished' | 'missing' + +const ACTION = '/me ' + +/** The action in `/me waves` (`waves`), or `null` if the text isn't one. */ +export function actionOf(text: string): string | null { + return text.startsWith(ACTION) ? text.slice(ACTION.length) : null +} + +/** What a message, or a reply's quote of one, reads as. */ +export function readingOf(m: Message | Quote): Reading { + if (m.vanished) return { kind: 'gone', why: 'vanished' } + if (m.deleted) return { kind: 'gone', why: 'deleted' } + if (m.text === null) return { kind: 'gone', why: 'missing' } + // With a file, the text is a caption, never an action. + const action = m.file ? null : actionOf(m.text) + if (action !== null) return { kind: 'action', action } + return { kind: 'said', text: m.text, file: m.file } +} + +/** The placeholder for a message that's gone. `mine`: I wrote it. */ +export function goneText(why: Gone, mine: boolean) { + if (why === 'vanished') return 'Message vanished' + if (why === 'missing') return 'A message that isn’t on this computer' + return mine ? 'You deleted this message' : 'Message deleted' +} + +/** A file, in a word: "Picture", or its name. */ +export const describeFile = (file: FileInfo) => (file.width ? 'Picture' : file.name) + +/** A message in one line, for a reply's quote. */ +export function summaryOf(m: Message | Quote): string { + const r = readingOf(m) + if (r.kind === 'gone') return r.why === 'deleted' ? 'Deleted message' : goneText(r.why, false) + if (r.kind === 'action') return `* ${r.action}` + return r.text || (r.file ? describeFile(r.file) : '') +} From 9fd64cf3122250ba091d461188b96dd29cc2bfbc Mon Sep 17 00:00:00 2001 From: Justin Williams Date: Wed, 25 Nov 2026 17:30:01 -0800 Subject: [PATCH 3/3] fix: show /me the same way in replies, quotes and search An action's summary is now "Alex waves", as the transcript shows it, instead of "* waves" in a quote and the raw "/me waves" in the reply bar. A search match on an action reads the same way. A quote of a deleted message says "Message deleted", as the message itself does. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 1 + ui/src/components/Chat.tsx | 17 ++++++++++++----- ui/src/components/Search.tsx | 19 +++++++++++++------ ui/src/reading.test.ts | 34 +++++++++++++++++++++++++++++----- ui/src/reading.ts | 24 ++++++++++++++++++------ 5 files changed, 73 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d1b817f..bfdbc3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ Changes people using Tightbeam will notice. Add a line under **Unreleased** as t ## Unreleased +- Replies, quotes and search show /me messages the way the conversation does ("Alex waves"), instead of the raw text. A quote of a deleted message says "Message deleted", as the message itself does. - Send any file, not just pictures: pick files with the paperclip, or drop them on the window. Files up to 1 GB go straight from disk, so big ones don't slow the app down. Changing or moving the original afterwards doesn't affect what was sent. The other side needs version 0.7 or later. - More kinds of received files open in their app with one click: PDFs, text and Office documents, audio and video, as well as pictures. Programs, scripts, web pages and archives can only be shown in their folder. - After unpairing and pairing with someone new, they get only the messages written from then on. Before, they also received every message sent to the previous person. Editing or deleting those older messages stays on your computer. diff --git a/ui/src/components/Chat.tsx b/ui/src/components/Chat.tsx index 8433ae0..1909d53 100644 --- a/ui/src/components/Chat.tsx +++ b/ui/src/components/Chat.tsx @@ -54,7 +54,7 @@ import { Spinner } from '@/components/ui/spinner' import { Textarea } from '@/components/ui/textarea' import { formatDuration, formatUntil } from '@/lib/duration' import { namer, peopleKey } from '@/people' -import { describeFile, goneText, readingOf, summaryOf } from '@/reading' +import { goneText, readingOf, summaryOf } from '@/reading' import { addMessage, clearHistory, @@ -406,6 +406,8 @@ const Row = memo(function Row({ const mine = receipt !== null const align = mine ? 'end' : 'start' const myReaction = message.reactions.find((r) => r.author === myId)?.emoji + // Who does an action: my name, not "You", so "Alex waves" reads as a sentence. + const actorOf = (author: string) => (author === myId ? myName : nameOf(author)) // Picking my current reaction again takes it back. const react = (emoji: string) => void run(api.react(refOf(message), emoji === myReaction ? '' : emoji)) @@ -444,7 +446,7 @@ const Row = memo(function Row({ ) : ( - {mine ? myName : nameOf(message.author)}{' '} + {actorOf(message.author)}{' '} )} @@ -475,6 +477,7 @@ const Row = memo(function Row({ )} @@ -576,10 +579,13 @@ function jumpToQuote(quote: Quote) { function QuoteButton({ quote, name, + actor, onJump, }: { quote: Quote name: string + /** Its author as an action names them: my name rather than "You". */ + actor: string onJump: (quote: Quote) => void }) { return ( @@ -589,7 +595,7 @@ function QuoteButton({ onClick={() => onJump(quote)} > {name} - {summaryOf(quote)} + {summaryOf(quote, actor)} ) } @@ -726,7 +732,7 @@ function Composer() { const textRef = useRef(null) const typingSentRef = useRef(0) const { scrollToEnd } = useMessageScroller() - const { replyingTo, editing, peerName, myId, away, dndUntil, staged, hint } = useSelector( + const { replyingTo, editing, peerName, myId, myName, away, dndUntil, staged, hint } = useSelector( store, (s) => ({ hint: s.hint, @@ -734,6 +740,7 @@ function Composer() { editing: s.editing, peerName: s.status?.peer?.name || 'them', myId: s.status?.me.id ?? '', + myName: s.status?.me.name || 'You', away: s.status?.away ? (s.status.away.note ?? '') : null, dndUntil: s.status?.dnd_until ?? null, staged: s.staged.length, @@ -1001,7 +1008,7 @@ function Composer() { Replying to {replyingTo.author === myId ? 'yourself' : peerName}
- {replyingTo.text || (replyingTo.file && describeFile(replyingTo.file))} + {summaryOf(replyingTo, replyingTo.author === myId ? myName : peerName)}
))} @@ -141,11 +143,16 @@ function when(ts: number) { return `${date}, ${d.toLocaleTimeString([], { hour: 'numeric', minute: '2-digit' })}` } -/** A snippet from Rust, with the matches (between \u0002 and \u0003) highlighted. */ -function Snippet({ text }: { text: string }) { - const [before, ...rest] = text.split('\u0002') +/** + * A match's snippet from Rust, with the matches (between \u0002 and \u0003) highlighted. + * An action reads as the transcript shows it: "Alex waves", with `actor` as the name. + */ +function Snippet({ hit, actor }: { hit: Hit; actor: string }) { + const action = actionInSnippet(hit) + const [before, ...rest] = (action ?? hit.snippet).split('\u0002') return ( -

+

+ {action !== null && {actor} } {before} {rest.map((part, i) => { const [match, after = ''] = part.split('\u0003') diff --git a/ui/src/reading.test.ts b/ui/src/reading.test.ts index 8439b4c..5478b01 100644 --- a/ui/src/reading.test.ts +++ b/ui/src/reading.test.ts @@ -1,7 +1,7 @@ import { describe, expect, test } from 'bun:test' -import type { Attachment, Message, Quote } from './api' -import { actionOf, goneText, readingOf, summaryOf } from './reading' +import type { Attachment, Hit, Message, Quote } from './api' +import { actionInSnippet, actionOf, goneText, readingOf, summaryOf } from './reading' function msg(more: Partial = {}): Message { return { @@ -94,19 +94,43 @@ describe('placeholders', () => { describe('a message in one line', () => { const cases: [string, Message | Quote, string][] = [ ['text', quote(), 'hello'], - ['an action', quote({ text: '/me waves' }), '* waves'], + ['an action, as the transcript says it', quote({ text: '/me waves' }), 'Alex waves'], + ['an action in the reply bar', msg({ text: '/me waves' }), 'Alex waves'], ['a caption', quote({ text: 'look', file: picture }), 'look'], + ['a caption that starts with /me', msg({ text: '/me waves', file: picture }), '/me waves'], ['a picture alone', quote({ text: '', file: picture }), 'Picture'], ['a file alone', quote({ text: '', file: pdf }), 'Report.pdf'], - ['deleted', quote({ text: null, deleted: true }), 'Deleted message'], + ['deleted', quote({ text: null, deleted: true }), 'Message deleted'], + ['deleted, in the reply bar', msg(deleted), 'Message deleted'], ['vanished', quote({ text: null, deleted: true, vanished: true }), 'Message vanished'], ['not on this computer', quote({ text: null }), 'A message that isn’t on this computer'], ] for (const [name, m, line] of cases) { - test(name, () => expect(summaryOf(m)).toBe(line)) + test(name, () => expect(summaryOf(m, 'Alex')).toBe(line)) } }) +/** A match on `/me waves at everyone`, unless `more` says otherwise. */ +const hit = (snippet: string, more: Partial = {}): Hit => ({ + message: msg({ text: '/me waves at everyone', ...more }), + snippet, +}) + +describe('a search match', () => { + test('on an action loses the /me, keeping its highlights', () => { + expect(actionInSnippet(hit('/me \u0002waves\u0003 at everyone'))).toBe( + '\u0002waves\u0003 at everyone', + ) + }) + test('that starts mid-message stays as it is', () => { + expect(actionInSnippet(hit('…\u0002waves\u0003 at everyone'))).toBeNull() + }) + test('on anything else stays as it is', () => { + expect(actionInSnippet(hit('/me \u0002waves\u0003', { file: picture }))).toBeNull() + expect(actionInSnippet(hit('\u0002hello\u0003', { text: 'hello' }))).toBeNull() + }) +}) + describe('actions', () => { test('are text that starts with "/me "', () => { expect(actionOf('/me waves')).toBe('waves') diff --git a/ui/src/reading.ts b/ui/src/reading.ts index 0046277..fb41174 100644 --- a/ui/src/reading.ts +++ b/ui/src/reading.ts @@ -1,9 +1,10 @@ // How a message reads: the one place that decides its wording, wherever it's shown. The -// transcript lays out a message's reading; quotes and the reply bar show its summary. +// transcript lays out a message's reading; quotes, the reply bar and search show its +// summary. // // Notifications are worded in Rust (`desktop::on_message`). Keep the two in step. -import type { FileInfo, Message, Quote } from './api' +import type { FileInfo, Hit, Message, Quote } from './api' /** What a message reads as. */ export type Reading = @@ -44,10 +45,21 @@ export function goneText(why: Gone, mine: boolean) { /** A file, in a word: "Picture", or its name. */ export const describeFile = (file: FileInfo) => (file.width ? 'Picture' : file.name) -/** A message in one line, for a reply's quote. */ -export function summaryOf(m: Message | Quote): string { +/** + * A message in one line, for a reply's quote or the reply bar. `actor`: its author's name, + * for an action ("Alex waves"). + */ +export function summaryOf(m: Message | Quote, actor: string): string { const r = readingOf(m) - if (r.kind === 'gone') return r.why === 'deleted' ? 'Deleted message' : goneText(r.why, false) - if (r.kind === 'action') return `* ${r.action}` + if (r.kind === 'gone') return goneText(r.why, false) + if (r.kind === 'action') return `${actor} ${r.action}` return r.text || (r.file ? describeFile(r.file) : '') } + +/** + * A search match on an action: its snippet without the `/me `, to show after its author's + * name. `null` for anything else, and for a snippet that starts mid-message. + */ +export function actionInSnippet(hit: Hit): string | null { + return readingOf(hit.message).kind === 'action' ? actionOf(hit.snippet) : null +}