@estiva-app/ui 0.105.0 β†’ 0.106.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,7 @@ import type { Meta, StoryObj } from '@storybook/react-vite'
2
2
  import { useState } from 'react'
3
3
  import { userEvent, within } from 'storybook/test'
4
4
  import { ConversationCard, type ConversationCardProps } from './ConversationCard'
5
- import { AT, Column, EditBox, Files, LONG, Mentions, ONE, SOME, THREE, TWO, WithBlock, WithCard, WORDS, YOU, useReactions } from './conversation.examples'
5
+ import { AT, Column, EditBox, Files, LONG, Mentions, ONE, RELAY_SAYS, SOME, THREE, TWO, WithBlock, WithCard, WORDS, YOU, useReactions, type RelaySays } from './conversation.examples'
6
6
  import { RichText } from './RichText'
7
7
  import { MenuItem } from './Menu'
8
8
  import type { ReactionCount } from './ReactionRow'
@@ -47,6 +47,14 @@ function Live({ start, resolvedAt, deletable = true, ...props }: ConversationCar
47
47
  )
48
48
  }
49
49
 
50
+ type CardWithRefusal = ConversationCardProps & { refusal: RelaySays }
51
+
52
+ /** A card whose every reaction the relay refuses, for `refusal`. */
53
+ function RefusingReactions({ refusal, ...props }: CardWithRefusal) {
54
+ const reacting = useReactions(SOME)
55
+ return <ConversationCard {...props} reactions={{ ...reacting, onToggle: () => new Promise((resolve) => setTimeout(() => resolve({ accepted: false, reason: RELAY_SAYS[refusal] }), 400)) }} />
56
+ }
57
+
50
58
  /** Someone's comment. */
51
59
  export const Comment: Story = { render: (args) => <Live {...args} /> }
52
60
 
@@ -146,14 +154,30 @@ export const Selected: Story = { render: (args) => <Live {...args} selected repl
146
154
  /** Being edited, in the kit's box. */
147
155
  export const Editing: Story = { render: (args) => <Live {...args} author={YOU} own editing /> }
148
156
 
149
- /** An edit that was refused: the box opens again, and says why. */
150
- export const EditRefused: Story = {
157
+ /** An edit that was refused: Save, and the box says why. Pick what the relay said in the controls. */
158
+ export const EditRefused: StoryObj<CardWithRefusal> = {
151
159
  name: 'Edit refused',
152
- render: (args) => <ConversationCard {...args} author={YOU} own editing editor={(done) => <EditBox text={WORDS} done={done} refuse />} />,
160
+ args: { refusal: 'Too long' },
161
+ argTypes: { refusal: { control: 'select', options: Object.keys(RELAY_SAYS) } },
162
+ render: ({ refusal, ...args }) => <ConversationCard {...args} author={YOU} own editing editor={(done) => <EditBox text={WORDS} done={done} refuse={refusal} />} />,
163
+ play: async ({ canvasElement }) => {
164
+ await userEvent.click(within(canvasElement).getByRole('button', { name: 'Save' }))
165
+ },
153
166
  }
154
167
 
155
- /** A reaction that was refused: said under the reactions. */
156
- export const ReactionRefused: Story = { name: 'Reaction refused', render: (args) => <Live {...args} start={SOME} reactionRefused="try again in a moment" /> }
168
+ /**
169
+ * A reaction that was refused: said under the reactions until the next reaction here, or until the card goes. Pick
170
+ * what the relay said in the controls; the story presses a reaction.
171
+ */
172
+ export const ReactionRefused: StoryObj<CardWithRefusal> = {
173
+ name: 'Reaction refused',
174
+ args: { refusal: 'Deleted, or no longer yours to write beside' },
175
+ argTypes: { refusal: { control: 'select', options: Object.keys(RELAY_SAYS) } },
176
+ render: ({ refusal, ...args }) => <RefusingReactions {...args} refusal={refusal} />,
177
+ play: async ({ canvasElement }) => {
178
+ await userEvent.click(within(canvasElement).getByRole('button', { name: 'Makes sense, 2' }))
179
+ },
180
+ }
157
181
 
158
182
  /** Nothing can be changed: no strip, reactions only shown. */
159
183
  export const ReadOnly: Story = { name: 'Read-only', render: (args) => <Live {...args} readOnly start={SOME} replies={{ count: 2, faces: [TWO, THREE], last: '12:02 PM' }} /> }
@@ -1,7 +1,7 @@
1
1
  // @vitest-environment jsdom
2
2
  import { useState } from 'react'
3
3
  import { afterEach, describe, expect, it, vi } from 'vitest'
4
- import { cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react'
4
+ import { act, cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react'
5
5
  import userEvent from '@testing-library/user-event'
6
6
  import { ConversationCard, ReplyCard } from './ConversationCard'
7
7
  import { PinnedComment } from './PinnedComment'
@@ -179,6 +179,68 @@ describe('ConversationCard', () => {
179
179
  })
180
180
  })
181
181
 
182
+ describe('a refused reaction (ONE-4)', () => {
183
+ const OPTIONS = [{ emoji: 'πŸ‘', label: 'Makes sense' }]
184
+ const row = (onToggle: (emoji: string) => Promise<{ accepted: true } | { accepted: false; reason: unknown }>) => ({
185
+ reactions: [{ emoji: 'πŸ‘', by: ['a', 'b'] }],
186
+ options: OPTIONS,
187
+ nameOf: () => 'Someone',
188
+ onToggle,
189
+ })
190
+ const settle = () => act(async () => {
191
+ for (let i = 0; i < 5; i++) await Promise.resolve()
192
+ })
193
+
194
+ it('says why in plain words under the reactions, never the relay’s', async () => {
195
+ render(
196
+ <ConversationCard author={ONE} time="11:08 AM" reactions={row(async () => ({ accepted: false, reason: 'invalid: reaction target event not found' }))}>
197
+ Words
198
+ </ConversationCard>,
199
+ )
200
+ fireEvent.click(screen.getByRole('button', { name: 'Makes sense, 2' }))
201
+ await settle()
202
+ expect(screen.getByText('Not reacted: this message was deleted, or you can no longer write here.')).toBeTruthy()
203
+ expect(screen.queryByText(/invalid:/)).toBeNull()
204
+ })
205
+
206
+ it('is gone on the next reaction, and a reply’s works the same', async () => {
207
+ const answers: ((a: { accepted: true } | { accepted: false; reason: unknown }) => void)[] = []
208
+ const onToggle = () => new Promise<{ accepted: true } | { accepted: false; reason: unknown }>((resolve) => answers.push(resolve))
209
+ render(
210
+ <ReplyCard author={ONE} time="11:40 AM" reactions={row(onToggle)}>
211
+ A reply
212
+ </ReplyCard>,
213
+ )
214
+ fireEvent.click(screen.getByRole('button', { name: 'Makes sense, 2' }))
215
+ answers[0]({ accepted: false, reason: 'rate-limited: slow down' })
216
+ await settle()
217
+ expect(screen.getByText('Not reacted: too many at once, try again in a moment.')).toBeTruthy()
218
+ fireEvent.click(screen.getByRole('button', { name: 'Makes sense, 2' }))
219
+ expect(screen.queryByText(/Not reacted/)).toBeNull()
220
+ answers[1]({ accepted: true })
221
+ await settle()
222
+ expect(screen.queryByText(/Not reacted/)).toBeNull()
223
+ })
224
+
225
+ it('says only the latest reaction’s answer: an earlier refusal arriving late does not replace it', async () => {
226
+ const answers: ((a: { accepted: true } | { accepted: false; reason: unknown }) => void)[] = []
227
+ const onToggle = () => new Promise<{ accepted: true } | { accepted: false; reason: unknown }>((resolve) => answers.push(resolve))
228
+ render(
229
+ <ConversationCard author={ONE} time="11:08 AM" reactions={row(onToggle)}>
230
+ Words
231
+ </ConversationCard>,
232
+ )
233
+ fireEvent.click(screen.getByRole('button', { name: 'Makes sense, 2' }))
234
+ fireEvent.click(screen.getByRole('button', { name: 'Makes sense, 2' }))
235
+ answers[1]({ accepted: false, reason: 'rate-limited: slow down' })
236
+ await settle()
237
+ answers[0]({ accepted: false, reason: 'invalid: reaction target event not found' })
238
+ await settle()
239
+ expect(screen.getByText('Not reacted: too many at once, try again in a moment.')).toBeTruthy()
240
+ expect(screen.queryByText(/this message was deleted/)).toBeNull()
241
+ })
242
+ })
243
+
182
244
  describe('ReplyCard', () => {
183
245
  it('asks before it deletes your reply', async () => {
184
246
  const user = userEvent.setup()
@@ -1,4 +1,5 @@
1
1
  import { useContext, useLayoutEffect, useRef, useState, type ReactNode, type RefObject } from 'react'
2
+ import { refusalReason } from '@estiva-app/conversation'
2
3
  import { IconArrowNarrowRight, IconCircleCheck, IconDotsVertical } from '@tabler/icons-react'
3
4
  import { Avatar } from './Avatar'
4
5
  import { Card } from './Card'
@@ -47,10 +48,11 @@ export interface ConversationEntryProps {
47
48
  about?: ReactNode
48
49
  /** Pictures and files: `AttachmentCard`s, laid out under the words. */
49
50
  files?: ReactNode
50
- /** Its reactions: the kit's `ReactionRow`. With `onToggle`, the strip offers React too. */
51
+ /**
52
+ * Its reactions: the kit's `ReactionRow`. With `onToggle`, the strip offers React too; when `onToggle` answers with a
53
+ * refusal, "Not reacted: <why>." is said under the reactions until the next reaction here, or until the card goes.
54
+ */
51
55
  reactions?: ReactionRowProps
52
- /** Why the last reaction was refused, in plain words: said under the reactions. */
53
- reactionRefused?: string
54
56
  /** Not read yet: the dot, or the urgent mark with `urgent`, and the card's edge in the theme's colour. */
55
57
  isNew?: boolean
56
58
  urgent?: boolean
@@ -253,6 +255,34 @@ function Words({ children }: { children: ReactNode }) {
253
255
  )
254
256
  }
255
257
 
258
+ /**
259
+ * The card holds why its last reaction was refused, so it lasts the same in every app: said from the answer until the
260
+ * next reaction here, or until the card goes. Only the latest reaction's answer is said.
261
+ */
262
+ function useReactionRefusal(reactions: ReactionRowProps | undefined): { row: ReactionRowProps | undefined; refused: string | undefined } {
263
+ const [refused, setRefused] = useState<string>()
264
+ const latest = useRef(0)
265
+ const toggle = reactions?.onToggle
266
+ if (!reactions || !toggle) return { row: reactions, refused }
267
+ const onToggle = (emoji: string) => {
268
+ const mine = ++latest.current
269
+ setRefused(undefined)
270
+ const say = (reason: unknown) => {
271
+ if (mine === latest.current) setRefused(refusalReason(reason))
272
+ }
273
+ const answer = toggle(emoji)
274
+ if (!answer) return
275
+ return answer.then((said) => {
276
+ if (said && !said.accepted) say(said.reason)
277
+ return said
278
+ }, (reason: unknown) => {
279
+ say(reason)
280
+ return { accepted: false as const, reason }
281
+ })
282
+ }
283
+ return { row: { ...reactions, onToggle }, refused }
284
+ }
285
+
256
286
  function Reactions({ reactions, refused, readOnly }: { reactions?: ReactionRowProps; refused?: string; readOnly: boolean }) {
257
287
  if ((!reactions || reactions.reactions.length === 0) && !refused) return null
258
288
  return (
@@ -347,7 +377,7 @@ function useEditing(editing: boolean | undefined, onEditingChange: ((editing: bo
347
377
  * The same card in every app, each in its own colours (the record that makes one conversation). Peek's, moved.
348
378
  */
349
379
  export function ConversationCard(props: ConversationCardProps) {
350
- const { author, about, files, reactions, reactionRefused, isNew = false, urgent = false, own = false, editor, deleted = false, replies, onReply, resolved, noun = 'comment', onResolve, resolveHint, resolveRefused, onReopen, selected, onClick, onDelete, deleteNote, deleteRefused, menu, menuAfter, menuLabel, editLabel, deleteLabel, moreLabel = 'More actions', className, children } = props
380
+ const { author, about, files, reactions, isNew = false, urgent = false, own = false, editor, deleted = false, replies, onReply, resolved, noun = 'comment', onResolve, resolveHint, resolveRefused, onReopen, selected, onClick, onDelete, deleteNote, deleteRefused, menu, menuAfter, menuLabel, editLabel, deleteLabel, moreLabel = 'More actions', className, children } = props
351
381
  const readOnly = (props.readOnly ?? false) || deleted
352
382
  const inThreadCard = useContext(InThreadCard)
353
383
 
@@ -378,7 +408,8 @@ export function ConversationCard(props: ConversationCardProps) {
378
408
  const showStrip = hovered && !isEditing && !readOnly
379
409
  const strip = useRef<HTMLDivElement>(null)
380
410
  const wordsEnd = useClearOfStrip(root, strip, showStrip)
381
- const react = reactions?.onToggle
411
+ const { row: reactionRow, refused: reactionRefused } = useReactionRefusal(reactions)
412
+ const react = reactionRow?.onToggle
382
413
  // Read-only with no replies, there is nothing to open: no box to write in, no thread to read (Peek's way, Katerina, 1 November).
383
414
  const press = onClick ?? (readOnly && !replies?.count ? undefined : item?.toggle)
384
415
  const reply = onReply ?? item?.show
@@ -459,7 +490,7 @@ export function ConversationCard(props: ConversationCardProps) {
459
490
  {!deleted && files && (
460
491
  <div className="relative flex w-full flex-wrap items-start gap-2 pb-2 pl-8 pr-2">{files}</div>
461
492
  )}
462
- <Reactions reactions={reactions} refused={reactionRefused} readOnly={readOnly} />
493
+ <Reactions reactions={reactionRow} refused={reactionRefused} readOnly={readOnly} />
463
494
  {resolved?.note && (
464
495
  // Who resolved it and the note, above the way into the replies; the check by the time says "Resolved" (Katerina, 1 November).
465
496
  <div className="relative w-full pb-1">
@@ -543,7 +574,8 @@ export function ConversationCard(props: ConversationCardProps) {
543
574
  * when it has one. Peek's, moved with the conversation.
544
575
  */
545
576
  export function ReplyCard(props: ReplyCardProps) {
546
- const { id, author, about, files, reactions, reactionRefused, isNew = false, urgent = false, own = false, editor, onDelete, deleteNote, deleteRefused, menu, menuAfter, menuLabel, editLabel, deleteLabel, moreLabel = 'More actions', noun = 'reply', className, children } = props
577
+ const { id, author, about, files, isNew = false, urgent = false, own = false, editor, onDelete, deleteNote, deleteRefused, menu, menuAfter, menuLabel, editLabel, deleteLabel, moreLabel = 'More actions', noun = 'reply', className, children } = props
578
+ const { row: reactions, refused: reactionRefused } = useReactionRefusal(props.reactions)
547
579
  const readOnly = props.readOnly ?? false
548
580
  const [hovered, setHovered] = useState(false)
549
581
  const [menuOpen, setMenuOpen] = useState(false)
@@ -92,11 +92,17 @@ export interface ReactionRowProps {
92
92
  nameOf: (id: string) => string
93
93
  /** Who is looking: their pills are lit, and "who reacted" says "you". */
94
94
  viewer?: string
95
- /** Your reaction on or off. Absent, the row only shows (read-only, signed out). */
96
- onToggle?: (emoji: string) => void
95
+ /**
96
+ * Your reaction on or off. Absent, the row only shows (read-only, signed out). In a `ConversationCard`, answering
97
+ * with what the relay said lets the card say why a refused one did not go through.
98
+ */
99
+ onToggle?: (emoji: string) => void | Promise<ReactionAnswer | void>
97
100
  className?: string
98
101
  }
99
102
 
103
+ /** What the relay said about a reaction; `reason` as it came, said in plain words by the card. */
104
+ export type ReactionAnswer = { accepted: true } | { accepted: false; reason: unknown }
105
+
100
106
  export function ReactionRow({ reactions, options, nameOf, viewer, onToggle, className }: ReactionRowProps) {
101
107
  const shown = reactions.filter((r) => r.by.length > 0)
102
108
  if (shown.length === 0) return null
@@ -99,25 +99,54 @@ export function WithBlock() {
99
99
  )
100
100
  }
101
101
 
102
+ /**
103
+ * What the relay (or Estiva ID, or the network) says for each cause of a refusal, as an app hands it over raw. The kit
104
+ * says each in `@estiva-app/conversation`'s plain words; a story picks one to show its sentence.
105
+ */
106
+ export const RELAY_SAYS = {
107
+ 'Too long': 'invalid: content exceeds maximum size of 262144 bytes',
108
+ 'Deleted, or no longer yours to write beside': 'invalid: edit target not found or not editable by you',
109
+ 'No longer a member': 'restricted: not a channel member',
110
+ Archived: 'restricted: channel is archived',
111
+ 'Too many at once': 'rate-limited: slow down',
112
+ 'Wrong clock': 'invalid: event timestamp too far in the future',
113
+ 'Signed out': 'auth-required: authentication required',
114
+ 'Anything else': 'error: internal error',
115
+ } as const
116
+ export type RelaySays = keyof typeof RELAY_SAYS
117
+
118
+ /** What the server says when it refuses a file, as protocol's upload hands it over. */
119
+ export const SERVER_SAYS = {
120
+ 'Too large': 'upload failed: 413 {"error":"file too large: 11534336 bytes (max 10485760)"}',
121
+ 'Not a type it takes': 'upload failed: 415 unsupported content type',
122
+ 'Could not be read': 'upload failed: 422 invalid image metadata',
123
+ 'Too many at once': 'upload failed: 429 rate limit exceeded',
124
+ 'Signed out': 'upload failed: 401 authentication failed',
125
+ 'No longer a member': 'upload failed: 403 forbidden',
126
+ 'Anything else': 'upload failed: 500 internal error',
127
+ } as const
128
+ export type ServerSays = keyof typeof SERVER_SAYS
129
+
102
130
  const saved = (): Promise<SendAnswer> => new Promise((resolve) => setTimeout(() => resolve({ accepted: true }), 400))
103
- const refused = (): Promise<SendAnswer> => new Promise((resolve) => setTimeout(() => resolve({ accepted: false, reason: 'try again in a moment' }), 600))
131
+ const refused = (reason: string): Promise<SendAnswer> => new Promise((resolve) => setTimeout(() => resolve({ accepted: false, reason }), 600))
104
132
  const asBody = (text: string): JSONContent => ({ type: 'doc', content: [{ type: 'paragraph', content: [{ type: 'text', text }] }] })
105
133
 
106
- /** The box a comment is edited in: the kit's CommentBox, as an app hands it to the card. */
107
- export function EditBox({ text, done, refuse = false }: { text: string; done: () => void; refuse?: boolean }) {
134
+ /** The box a comment is edited in: the kit's CommentBox, as an app hands it to the card. `refuse`: why the relay says no. */
135
+ export function EditBox({ text, done, refuse }: { text: string; done: () => void; refuse?: RelaySays }) {
136
+ const answer = () => (refuse ? refused(RELAY_SAYS[refuse]) : saved())
108
137
  return (
109
138
  <CommentBox
110
139
  people={[]}
111
140
  label="Edit"
112
141
  placeholder={null}
113
- send={refuse ? refused : saved}
142
+ send={answer}
114
143
  edit={{
115
144
  body: asBody(text),
116
145
  onCancel: done,
117
146
  onSave: async () => {
118
- const answer = await (refuse ? refused() : saved())
119
- if (answer.accepted) done()
120
- return answer
147
+ const said = await answer()
148
+ if (said.accepted) done()
149
+ return said
121
150
  },
122
151
  }}
123
152
  />