Skip to content

The agent that grows with you

fix(webapp): drop a reason's period before the next item in the drop warning

unsupportedpastels86130453a936
diff --git a/apps/desktop/src/app/chat/hooks/drop-warning.test.ts b/apps/desktop/src/app/chat/hooks/drop-warning.test.ts
new file mode 100644
index 00000000000..0a7825ce435
--- /dev/null
+++ b/apps/desktop/src/app/chat/hooks/drop-warning.test.ts
@@ -0,0 +1,45 @@
+import { describe, expect, it } from 'vitest'
+
+import { dropWarning } from './drop-warning'
+
+const more = (count: number) => `and ${count} more`
+const folder = "Folders can't be attached by dropping them. Use Attach → Folder… instead."
+const limit = 'The file is larger than 16 MiB, the largest file the browser can attach.'
+
+// Reasons are sentences; joined with '; ' their periods used to read '…instead.; Could not attach…'.
+describe('dropWarning', () => {
+ it('drops the period of a reason another item follows, and keeps the last one', () => {
+ const { detail, message } = dropWarning(
+ [
+ { name: 'somefolder', reason: folder },
+ { name: 'huge.zip', reason: limit }
+ ],
+ more
+ )
+
+ expect(message).toBe(
+ `Could not attach somefolder: ${folder.slice(0, -1)}; Could not attach huge.zip: ${limit}`
+ )
+ expect(message).not.toContain('.;')
+ expect(detail).toBeUndefined()
+ })
+
+ it('drops the period before the count of the rest, and keeps each reason whole in the detail', () => {
+ const failures = [
+ { name: 'somefolder', reason: folder },
+ ...Array.from({ length: 5 }, (_, index) => ({ name: `${'z'.repeat(135)}-${index}.zip`, reason: limit }))
+ ]
+
+ const { detail, message } = dropWarning(failures, more)
+
+ expect(message).toMatch(/[^.]; and \d+ more$/)
+ expect(message).not.toContain('.;')
+ expect(detail?.split('\n')).toEqual(failures.map(({ name, reason }) => `Could not attach ${name}: ${reason}`))
+ })
+
+ it('keeps a lone reason as it is, period included, with no detail', () => {
+ expect(dropWarning([{ name: 'huge.zip', reason: limit }], more)).toEqual({
+ message: `Could not attach huge.zip: ${limit}`
+ })
+ })
+})
diff --git a/apps/desktop/src/app/chat/hooks/drop-warning.ts b/apps/desktop/src/app/chat/hooks/drop-warning.ts
index d67cb9d186e..4eb3c0e7f3a 100644
--- a/apps/desktop/src/app/chat/hooks/drop-warning.ts
+++ b/apps/desktop/src/app/chat/hooks/drop-warning.ts
@@ -23,34 +23,44 @@ function middleEllipsis(text: string, max: number): string {
return `${text.slice(0, head)}…${text.slice(text.length - (max - 1 - head))}`
}
-function failureText({ name, reason }: DropFailure, maxName = Infinity): string {
+function failureText({ name, reason }: DropFailure, maxName = Infinity, joined = false): string {
const label = `Could not attach ${middleEllipsis(name, maxName)}`
+ // A reason ends with a period, which reads '….;' before the '; ' that joins the next item.
+ const text = joined ? reason?.replace(/\.$/, '') : reason
- return reason ? `${label}: ${reason}` : label
+ return text ? `${label}: ${text}` : label
+}
+
+/** The failures joined into one line, then the count of the rest when there is one. */
+function summaryText(failures: DropFailure[], maxName: number, rest?: string): string {
+ const items = failures.map((failure, index) =>
+ failureText(failure, maxName, rest !== undefined || index < failures.length - 1)
+ )
+
+ return (rest === undefined ? items : [...items, rest]).join('; ')
}
/** One bounded warning for a drop's failures: shortened names, a few of them, and a count of the
* rest, so a large drop or long paths can't grow the toast off screen. Whenever anything is
- * shortened or left out, the full list rides in the collapsible detail. */
+ * shortened or left out, the full list, each reason whole, rides in the collapsible detail. */
export function dropWarning(
failures: DropFailure[],
more: (count: number) => string
): { detail?: string; message: string } {
- const shown: string[] = []
+ const shown: DropFailure[] = []
for (const failure of failures.slice(0, DROP_FAILURES_SHOWN)) {
- const text = failureText(failure, DROP_FAILURE_NAME_CHARS)
-
- if (shown.length && [...shown, text].join('; ').length > DROP_WARNING_CHARS) {
+ if (shown.length && summaryText([...shown, failure], DROP_FAILURE_NAME_CHARS).length > DROP_WARNING_CHARS) {
break
}
- shown.push(text)
+ shown.push(failure)
}
const hidden = failures.length - shown.length
- const message = hidden > 0 ? `${shown.join('; ')}; ${more(hidden)}` : shown.join('; ')
- const full = failures.map(failure => failureText(failure))
+ const message = summaryText(shown, DROP_FAILURE_NAME_CHARS, hidden > 0 ? more(hidden) : undefined)
- return message === full.join('; ') ? { message } : { detail: full.join('\n'), message }
+ return message === summaryText(failures, Infinity)
+ ? { message }
+ : { detail: failures.map(failure => failureText(failure)).join('\n'), message }
}
diff --git a/apps/desktop/src/components/assistant-ui/thread/user-message-edit-drop-staging.test.tsx b/apps/desktop/src/components/assistant-ui/thread/user-message-edit-drop-staging.test.tsx
index 3f3578c1266..fbca5c6ca7b 100644
--- a/apps/desktop/src/components/assistant-ui/thread/user-message-edit-drop-staging.test.tsx
+++ b/apps/desktop/src/components/assistant-ui/thread/user-message-edit-drop-staging.test.tsx
@@ -127,7 +127,8 @@ describe('edit composer drop staging failures', () => {
})
expect(warning.kind).toBe('error')
- expect(warning.message).toContain(`Could not attach ${'a'.repeat(24)}…${'a'.repeat(19)}.zip: ${limit}`)
+ // A reason another item follows loses its period; the detail keeps it whole.
+ expect(warning.message).toContain(`Could not attach ${'a'.repeat(24)}…${'a'.repeat(19)}.zip: ${limit.slice(0, -1)}; `)
expect(warning.message).toContain(`Could not attach big.zip: ${limit}`)
expect(warning.message).not.toContain(longName)
expect(warning.message).not.toContain('notes.txt')