diff --git a/apps/mobile/src/features/threads/thread-list-v2-items.tsx b/apps/mobile/src/features/threads/thread-list-v2-items.tsx index aa12551f9ce1f..041cfa479a499 100644 --- a/apps/mobile/src/features/threads/thread-list-v2-items.tsx +++ b/apps/mobile/src/features/threads/thread-list-v2-items.tsx @@ -75,6 +75,7 @@ import { canTurnThreadIntoProject, turnThreadIntoProject } from "./turnThreadInt import { threadCanArchive } from "../home/threadArchive"; import { THREAD_LIST_V2_SETTLED_PAGE_COUNT, + canMarkThreadUnread, resolveThreadListV2SnoozeGateExpiryMs, resolveThreadListV2SnoozeMenuSelection, resolveThreadListV2SnoozePresets, @@ -800,6 +801,20 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: { const visitThread = useAtomCommand(threadEnvironment.visit, { reportFailure: false }); const markThreadUnread = useAtomCommand(threadEnvironment.markUnread, { reportFailure: false }); + const canMarkUnread = canMarkThreadUnread(thread, status); + const handleMarkUnread = useCallback(async () => { + if (!canMarkUnread) return; + const result = await markThreadUnread({ + environmentId: thread.environmentId, + input: { threadId: thread.id }, + }); + if (result._tag === "Failure") { + Dialog.alert( + "Could not mark thread unread", + "The thread could not be marked unread. Try again.", + ); + } + }, [canMarkUnread, markThreadUnread, thread.environmentId, thread.id]); const handleAcknowledgeWoke = useCallback(() => { if (props.wokeAt === null) return; void visitThread({ @@ -946,7 +961,9 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: { supported: props.titleRegenerationSupported, isRegenerating: isRegeneratingTitle, }), - { id: "mark-unread", title: "Mark unread", image: "envelope.badge" }, + ...(canMarkUnread + ? [{ id: "mark-unread", title: "Mark unread", image: "envelope.badge" }] + : []), ...(canTurnIntoProject ? [ { @@ -958,7 +975,13 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: { ] : []), ], - [canTurnIntoProject, props.titleRegenerationSupported, thread.runtime, isRegeneratingTitle], + [ + canMarkUnread, + canTurnIntoProject, + props.titleRegenerationSupported, + thread.runtime, + isRegeneratingTitle, + ], ); const menuActions = useMemo(() => { const nested = Platform.OS === "ios"; @@ -1060,13 +1083,8 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: { if (nativeEvent.event === "archive") handleArchive(); if (nativeEvent.event === "rename") handleRename(); if (nativeEvent.event === "regenerate-title") handleRegenerateTitle(); + if (nativeEvent.event === "mark-unread") void handleMarkUnread(); if (nativeEvent.event === "turn-into-project") void turnThreadIntoProject(thread); - if (nativeEvent.event === "mark-unread") { - void markThreadUnread({ - environmentId: thread.environmentId, - input: { threadId: thread.id }, - }); - } if (nativeEvent.event === "copy-thread") { const target = threadChipCopyTarget( { environmentId: thread.environmentId, threadId: thread.id }, @@ -1104,6 +1122,7 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: { thread, handleArchive, handleDelete, + handleMarkUnread, handleRegenerateTitle, handleRename, handleMoveDown, @@ -1115,7 +1134,6 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: { handleUnpin, handleUnsettle, handleUnsnooze, - markThreadUnread, setCustomSnoozeOpen, snoozePresets, props.snoozePresets, diff --git a/apps/mobile/src/features/threads/threadListV2.test.ts b/apps/mobile/src/features/threads/threadListV2.test.ts index 7324f039cd11e..03b526507fd87 100644 --- a/apps/mobile/src/features/threads/threadListV2.test.ts +++ b/apps/mobile/src/features/threads/threadListV2.test.ts @@ -46,6 +46,7 @@ import { threadListV2ListItemsAreEqual, threadListV2RowAccessibilityLabel, threadHasUnseenCompletion, + canMarkThreadUnread, type ThreadListV2ListItem, } from "./threadListV2"; @@ -114,7 +115,14 @@ describe("layoutThreadListV2RowMenuActions", () => { direct: actions("new-thread-on-branch", "copy-thread", "copy-thread-id", "settle", "snooze"), reorder: actions("arrange", "move-up", "move-down"), pin: actions("unpin"), - secondary: actions("rename", "regenerate-title", "turn-into-project", "archive", "delete"), + secondary: actions( + "rename", + "regenerate-title", + "mark-unread", + "turn-into-project", + "archive", + "delete", + ), }; const flat = layoutThreadListV2RowMenuActions({ ...full, nested: false }); const nested = layoutThreadListV2RowMenuActions({ ...full, nested: true }); @@ -132,6 +140,7 @@ describe("layoutThreadListV2RowMenuActions", () => { "more", ]); expect(nested.at(-1)?.subactions?.map((action) => action.id)).toEqual([ + "mark-unread", "arrange-menu", "rename", "regenerate-title", @@ -147,7 +156,12 @@ describe("layoutThreadListV2RowMenuActions", () => { reorder: actions("arrange"), nested: true, }); - expect(slim.at(-1)?.subactions?.[0]?.id).toBe("arrange"); + expect( + slim + .at(-1) + ?.subactions?.slice(0, 2) + .map((action) => action.id), + ).toEqual(["mark-unread", "arrange"]); }); }); @@ -228,6 +242,53 @@ describe("resolveThreadListV2Status", () => { ); }); +describe("canMarkThreadUnread", () => { + const completed = { status: "completed", completedAt: NOW } as const; + it.each([ + { + name: "a read completed run", + latestRun: completed, + lastVisitedAt: NOW, + status: "ready", + can: true, + }, + { + name: "an unread completed run", + latestRun: completed, + lastVisitedAt: "2026-06-01T00:00:00.000Z", + status: "ready", + can: false, + }, + { + name: "no completed run", + latestRun: undefined, + lastVisitedAt: NOW, + status: "ready", + can: false, + }, + { + name: "a server without visit tracking", + latestRun: completed, + lastVisitedAt: undefined, + status: "ready", + can: false, + }, + { + name: "a working thread", + latestRun: completed, + lastVisitedAt: NOW, + status: "working", + can: false, + }, + ] as const)("is $can for $name", ({ latestRun, lastVisitedAt, status, can }) => { + const thread = { latestRun, lastVisitedAt } as Pick< + EnvironmentThreadShell, + "latestRun" | "lastVisitedAt" + >; + expect(canMarkThreadUnread(thread, status)).toBe(can); + }); +}); + describe("threadListV2RowAccessibilityLabel", () => { const row = { title: "Fix login", diff --git a/apps/mobile/src/features/threads/threadListV2.ts b/apps/mobile/src/features/threads/threadListV2.ts index 61652bd0c3599..ab95c531d3a49 100644 --- a/apps/mobile/src/features/threads/threadListV2.ts +++ b/apps/mobile/src/features/threads/threadListV2.ts @@ -151,6 +151,7 @@ export function layoutThreadListV2RowMenuActions(input: { if (!input.nested) { return [...input.direct, ...input.reorder, ...input.pin, ...input.secondary]; } + const markUnread = input.secondary.find((action) => action.id === "mark-unread"); return [ ...input.direct, ...input.pin, @@ -159,6 +160,7 @@ export function layoutThreadListV2RowMenuActions(input: { title: "More", image: "ellipsis.circle", subactions: [ + ...(markUnread ? [markUnread] : []), ...(input.reorder.length > 1 ? [ { @@ -169,7 +171,7 @@ export function layoutThreadListV2RowMenuActions(input: { }, ] : input.reorder), - ...input.secondary, + ...input.secondary.filter((action) => action.id !== "mark-unread"), ], }, ]; @@ -246,6 +248,19 @@ export function threadHasUnseenCompletion( return completedAtMs > lastVisitedAtMs; } +/** The server rejects Mark unread without a completed run; older servers cannot persist it. */ +export function canMarkThreadUnread( + thread: Pick, + status: ThreadListV2Status, +): boolean { + return ( + status === "ready" && + thread.lastVisitedAt !== undefined && + thread.latestRun?.completedAt != null && + !threadHasUnseenCompletion(thread) + ); +} + export function resolveThreadListV2WokeAt( thread: EnvironmentThreadShell, options: { readonly now: string },