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/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 be77a81..1909d53 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 { goneText, readingOf, summaryOf } from '@/reading' import { addMessage, clearHistory, @@ -71,7 +72,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 = ['👍', '❤️', '😂', '😮', '😢', '🎉'] @@ -405,11 +406,11 @@ 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({ 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' })} @@ -417,7 +418,8 @@ const Row = memo(function Row({ ) - if (message.deleted) { + const reading = readingOf(message) + if (reading.kind === 'gone') { return (
{newDay && } @@ -425,11 +427,7 @@ const Row = memo(function Row({ - {message.vanished - ? 'Message vanished' - : mine - ? 'You deleted this message' - : 'Message deleted'} + {goneText(reading.why, mine)} {footer} @@ -440,7 +438,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 = ( <> @@ -448,7 +446,7 @@ const Row = memo(function Row({ ) : ( - {mine ? myName : nameOf(message.author)}{' '} + {actorOf(message.author)}{' '} )} @@ -479,6 +477,7 @@ const Row = memo(function Row({ )} @@ -580,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 ( @@ -593,17 +595,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, actor)} ) } @@ -740,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, @@ -748,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, @@ -834,7 +827,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 +857,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 +887,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) @@ -1015,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 new file mode 100644 index 0000000..5478b01 --- /dev/null +++ b/ui/src/reading.test.ts @@ -0,0 +1,141 @@ +import { describe, expect, test } from 'bun:test' + +import type { Attachment, Hit, Message, Quote } from './api' +import { actionInSnippet, 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, 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 }), '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, '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') + 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..fb41174 --- /dev/null +++ b/ui/src/reading.ts @@ -0,0 +1,65 @@ +// How a message reads: the one place that decides its wording, wherever it's shown. The +// 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, Hit, 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 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 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 +} 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`. */