GeezerVison is showing github.com — fix(mobile): a held thread move retires when a snooze wakes, even wit… · pingdotgg/t3code@03be432
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/features/home/HomeScreen.tsx +3-24Lines changed: 3 additions & 24 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,7 +12,6 @@ import { | 12 | 12 | import { threadSearchMatchKey } from "@t3tools/client-runtime/state/thread-search"; | 13 | 13 | import { type EnvironmentId, type SidebarProjectGroupingMode } from "@t3tools/contracts"; | 14 | 14 | import { useAtomValue } from "@effect/atom-react"; | 15 | | -import { useFocusEffect } from "@react-navigation/native"; | 16 | 15 | import { use, useEffect, useRef, useState, type ComponentProps } from "react"; | 17 | 16 | import { | 18 | 17 | Platform, | @@ -51,6 +50,7 @@ import { useThreadSearch } from "../../state/queries"; | 51 | 50 | import { requestThreadMessageJump } from "../threads/threadMessageJump"; | 52 | 51 | import { useThreadListSearchMatches } from "../threads/useThreadListSearchMatches"; | 53 | 52 | import { useThreadListV2 } from "../threads/useThreadListV2"; | | 53 | +import { useThreadListNow } from "../threads/use-thread-list-now"; | 54 | 54 | import { useThreadJumpShortcuts } from "../keyboard/threadKeyboardShortcuts"; | 55 | 55 | import { usePendingThreadOrder } from "../../state/thread-order"; | 56 | 56 | import { threadListEnvironmentsAtom } from "../../state/server"; | @@ -423,15 +423,7 @@ export function HomeScreen(props: HomeScreenProps) { | 423 | 423 | toggleSnoozedShelf, | 424 | 424 | toggleWorkingShelf, | 425 | 425 | } = useThreadListV2ShelfPreferences(); | 426 | | - // Keep time in state; the compiler would cache a render-time new Date(). | 427 | | - const [now, setNow] = useState(() => new Date().toISOString()); | 428 | | - const nowMinute = now.slice(0, 16); | 429 | | - const [snoozeWakeTick, bumpSnoozeWakeTick] = useState(0); | 430 | | - useFocusEffect(() => { | 431 | | - setNow(new Date().toISOString()); | 432 | | - const id = setInterval(() => setNow(new Date().toISOString()), 60_000); | 433 | | - return () => clearInterval(id); | 434 | | - }); | | 426 | + const now = useThreadListNow(props.threads); | 435 | 427 | const listEnvironments = useAtomValue(threadListEnvironmentsAtom); | 436 | 428 | const { | 437 | 429 | providersByEnvironmentId, | @@ -445,7 +437,7 @@ export function HomeScreen(props: HomeScreenProps) { | 445 | 437 | titleRegenerationEnvironmentIds, | 446 | 438 | } = listEnvironments; | 447 | 439 | const resolveProviderInstance = useThreadRowProviderInstanceResolver(providersByEnvironmentId); | 448 | | - const pendingOrder = usePendingThreadOrder(nowMinute, snoozeWakeTick); | | 440 | + const pendingOrder = usePendingThreadOrder(); | 449 | 441 | const { | 450 | 442 | layout: threadListV2Layout, | 451 | 443 | items: threadListV2Items, | @@ -473,19 +465,6 @@ export function HomeScreen(props: HomeScreenProps) { | 473 | 465 | settledShelfExpanded, | 474 | 466 | selectedThreadKey, | 475 | 467 | }); | 476 | | - const nextSnoozeWakeAt = threadListV2Layout.nextSnoozeWakeAt; | 477 | | - useEffect(() => { | 478 | | - if (nextSnoozeWakeAt === null) return; | 479 | | - const wakeAtMs = Date.parse(nextSnoozeWakeAt); | 480 | | - if (Number.isNaN(wakeAtMs)) return; | 481 | | - const delayMs = Math.min(Math.max(0, wakeAtMs - Date.now()) + 50, 2_147_483_647); | 482 | | - const id = setTimeout(() => { | 483 | | - setNow(new Date().toISOString()); | 484 | | - bumpSnoozeWakeTick((tick) => tick + 1); | 485 | | - }, delayMs); | 486 | | - return () => clearTimeout(id); | 487 | | - // oxlint-disable-next-line react/exhaustive-effect-dependencies -- snoozeWakeTick re-arms clamped timers even when the wake date exceeds 32-bit setTimeout and stays unchanged. | 488 | | - }, [nextSnoozeWakeAt, snoozeWakeTick]); | 489 | 468 | // Called as `props.onSelectThread(...)`, the compiler would cache this handler on the whole | 490 | 469 | // props object, which is new on every render of Home's parent. | 491 | 470 | const { onSelectThread } = props; | |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/features/threads/ThreadArrangementSheet.tsx +2-15Lines changed: 2 additions & 15 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -36,6 +36,7 @@ import { | 36 | 36 | THREAD_LIST_V2_SETTLED_PAGE_COUNT, | 37 | 37 | threadListInboxReturns, | 38 | 38 | } from "./threadListV2"; | | 39 | +import { useThreadListNow } from "./use-thread-list-now"; | 39 | 40 | import { useThreadListV2ShelfPreferences } from "./use-thread-list-v2-shelf-preferences"; | 40 | 41 |
| 41 | 42 | const ROW_HEIGHT = 56; | @@ -168,7 +169,7 @@ export function ThreadArrangementSheet(props: { onClose: () => void }) { | 168 | 169 | const dropBusy = useAtomValue(threadDropBusyAtom); | 169 | 170 | const { moveThread } = useThreadListActions(); | 170 | 171 | const { workingShelfEnabled } = useThreadListV2ShelfPreferences(); | 171 | | - const [now, setNow] = useState(() => new Date().toISOString()); | | 172 | + const now = useThreadListNow(threads); | 172 | 173 | const [expanded, setExpanded] = useState({ snoozed: false, settled: false }); | 173 | 174 | const [settledVisibleCount, setSettledVisibleCount] = useState( | 174 | 175 | THREAD_LIST_V2_SETTLED_INITIAL_COUNT, | @@ -179,20 +180,6 @@ export function ThreadArrangementSheet(props: { onClose: () => void }) { | 179 | 180 | expanded: expanded.settled, | 180 | 181 | visibleCount: settledVisibleCount, | 181 | 182 | }); | 182 | | - useEffect(() => { | 183 | | - const wakeAt = Math.min( | 184 | | - ...threads.flatMap((thread) => { | 185 | | - const at = Date.parse(thread.snoozedUntil ?? ""); | 186 | | - return at > Date.parse(now) ? [at] : []; | 187 | | - }), | 188 | | - ); | 189 | | - if (!Number.isFinite(wakeAt)) return; | 190 | | - const timer = setTimeout( | 191 | | - () => setNow(new Date().toISOString()), | 192 | | - Math.min(Math.max(0, wakeAt - Date.now()) + 1, 2_147_483_647), | 193 | | - ); | 194 | | - return () => clearTimeout(timer); | 195 | | - }, [threads, now]); | 196 | 183 | const sections = useMemo(() => { | 197 | 184 | const shared = { | 198 | 185 | threads, | |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/features/threads/ThreadNavigationSidebar.tsx +3-24Lines changed: 3 additions & 24 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -30,6 +30,7 @@ import { scopedProjectKey, scopedThreadKey } from "../../lib/scopedEntities"; | 30 | 30 | import { useProjects, useNavigationThreadShells } from "../../state/entities"; | 31 | 31 | import { useThreadSearch } from "../../state/queries"; | 32 | 32 | import { useThreadListV2ShelfPreferences } from "./use-thread-list-v2-shelf-preferences"; | | 33 | +import { useThreadListNow } from "./use-thread-list-now"; | 33 | 34 | import { usePendingThreadOrder } from "../../state/thread-order"; | 34 | 35 | import { threadListEnvironmentsAtom } from "../../state/server"; | 35 | 36 | import { usePendingNewTasks, type PendingNewTask } from "../../state/use-pending-new-tasks"; | @@ -269,16 +270,7 @@ function ThreadNavigationSidebarPane( | 269 | 270 | toggleSnoozedShelf, | 270 | 271 | toggleWorkingShelf, | 271 | 272 | } = useThreadListV2ShelfPreferences(); | 272 | | - // Keep time in state; the compiler would cache a render-time new Date(). | 273 | | - const [now, setNow] = useState(() => new Date().toISOString()); | 274 | | - const nowMinute = now.slice(0, 16); | 275 | | - const [snoozeWakeTick, bumpSnoozeWakeTick] = useState(0); | 276 | | - useEffect(() => { | 277 | | - // oxlint-disable-next-line react/set-state-in-effect -- Refreshes the minute clock when the minute interval starts. | 278 | | - setNow(new Date().toISOString()); | 279 | | - const id = setInterval(() => setNow(new Date().toISOString()), 60_000); | 280 | | - return () => clearInterval(id); | 281 | | - }, []); | | 273 | + const now = useThreadListNow(threads); | 282 | 274 | const listEnvironments = useAtomValue(threadListEnvironmentsAtom); | 283 | 275 | const { | 284 | 276 | providersByEnvironmentId, | @@ -292,7 +284,7 @@ function ThreadNavigationSidebarPane( | 292 | 284 | titleRegenerationEnvironmentIds, | 293 | 285 | } = listEnvironments; | 294 | 286 | const resolveProviderInstance = useThreadRowProviderInstanceResolver(providersByEnvironmentId); | 295 | | - const pendingOrder = usePendingThreadOrder(nowMinute, snoozeWakeTick); | | 287 | + const pendingOrder = usePendingThreadOrder(); | 296 | 288 | const { | 297 | 289 | layout: threadListV2Layout, | 298 | 290 | items: threadListV2Items, | @@ -320,19 +312,6 @@ function ThreadNavigationSidebarPane( | 320 | 312 | settledShelfExpanded, | 321 | 313 | selectedThreadKey: props.selectedThreadKey ?? null, | 322 | 314 | }); | 323 | | - const nextSnoozeWakeAt = threadListV2Layout.nextSnoozeWakeAt; | 324 | | - useEffect(() => { | 325 | | - if (nextSnoozeWakeAt === null) return; | 326 | | - const wakeAtMs = Date.parse(nextSnoozeWakeAt); | 327 | | - if (Number.isNaN(wakeAtMs)) return; | 328 | | - const delayMs = Math.min(Math.max(0, wakeAtMs - Date.now()) + 50, 2_147_483_647); | 329 | | - const id = setTimeout(() => { | 330 | | - setNow(new Date().toISOString()); | 331 | | - bumpSnoozeWakeTick((tick) => tick + 1); | 332 | | - }, delayMs); | 333 | | - return () => clearTimeout(id); | 334 | | - // oxlint-disable-next-line react/exhaustive-effect-dependencies -- snoozeWakeTick re-arms clamped timers even when the wake date exceeds 32-bit setTimeout and stays unchanged. | 335 | | - }, [nextSnoozeWakeAt, snoozeWakeTick]); | 336 | 315 | const listItems: readonly SidebarListItem[] = | 337 | 316 | settledShelfExpanded && threadListV2Layout.hiddenSettledCount > 0 | 338 | 317 | ? [ | |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/features/threads/threadListV2.ts +14Lines changed: 14 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -140,6 +140,20 @@ export function resolveThreadListV2SnoozeGateExpiryMs( | 140 | 140 | return messageAtMs + QUEUED_TURN_START_GRACE_MS; | 141 | 141 | } | 142 | 142 |
| | 143 | +/** The earliest `snoozedUntil` strictly after `nowMs`, in epoch ms. */ | | 144 | +export function nextSnoozeWakeAfter( | | 145 | + threads: ReadonlyArray<{ readonly snoozedUntil?: string | null }>, | | 146 | + nowMs: number, | | 147 | +): number | null { | | 148 | + let next: number | null = null; | | 149 | + for (const thread of threads) { | | 150 | + if (thread.snoozedUntil == null) continue; | | 151 | + const wakeAtMs = Date.parse(thread.snoozedUntil); | | 152 | + if (wakeAtMs > nowMs && (next === null || wakeAtMs < next)) next = wakeAtMs; | | 153 | + } | | 154 | + return next; | | 155 | +} | | 156 | + | 143 | 157 | export const THREAD_LIST_V2_SETTLED_INITIAL_COUNT = 10; | 144 | 158 | export const THREAD_LIST_V2_SETTLED_PAGE_COUNT = 25; | 145 | 159 |
| |
Collapse file Copy file name to clipboard +32Lines changed: 32 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { act, useLayoutEffect } from "react"; | | 2 | +import { createRoot } from "react-dom/client"; | | 3 | +import { expect, it, vi } from "vite-plus/test"; | | 4 | + | | 5 | +import { useThreadListNow } from "./use-thread-list-now"; | | 6 | + | | 7 | +const MINUTE_MS = Date.UTC(2026, 9, 27, 12, 0); | | 8 | +const iso = (secondsIntoMinute: number) => | | 9 | + new Date(MINUTE_MS + secondsIntoMinute * 1000).toISOString(); | | 10 | + | | 11 | +it("wakes for each snooze that expires within the same minute", async () => { | | 12 | + using _timers = { [Symbol.dispose]: () => vi.useRealTimers() }; | | 13 | + vi.useFakeTimers({ now: MINUTE_MS + 10_000 }); | | 14 | + const threads = [{ snoozedUntil: iso(30) }, { snoozedUntil: iso(40) }]; | | 15 | + const seen: string[] = []; | | 16 | + function Probe() { | | 17 | + const now = useThreadListNow(threads); | | 18 | + useLayoutEffect(() => { | | 19 | + seen.push(now); | | 20 | + }); | | 21 | + return null; | | 22 | + } | | 23 | + | | 24 | + const root = createRoot(document.createElement("div")); | | 25 | + await act(async () => root.render(<Probe />)); | | 26 | + await act(async () => vi.advanceTimersByTime(20_000)); | | 27 | + expect(seen.at(-1)).toBe(iso(30)); | | 28 | + | | 29 | + await act(async () => vi.advanceTimersByTime(10_000)); | | 30 | + expect(seen.at(-1)).toBe(iso(40)); | | 31 | + act(() => root.unmount()); | | 32 | +}); |
Collapse file Copy file name to clipboard +24Lines changed: 24 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { useState } from "react"; | | 2 | + | | 3 | +import { useNowAfter, useNowMinuteMs } from "../../lib/clock"; | | 4 | +import { nextSnoozeWakeAfter } from "./threadListV2"; | | 5 | + | | 6 | +/** | | 7 | + * The thread list's clock as an ISO timestamp. It advances on each wall-clock | | 8 | + * minute and the moment any snooze expires, so the list re-partitions on time | | 9 | + * instead of on the next minute tick. Waking on every snoozed thread, not just | | 10 | + * the ones the list shows as snoozed, only costs an extra render. | | 11 | + */ | | 12 | +export function useThreadListNow( | | 13 | + threads: ReadonlyArray<{ readonly snoozedUntil?: string | null }>, | | 14 | +): string { | | 15 | + const minuteMs = useNowMinuteMs(); | | 16 | + // The latest time returned. The next wake is searched after it, not after | | 17 | + // the floored minute, so a second snooze expiring later in the same minute | | 18 | + // arms its own wake instead of waiting for the next minute tick. | | 19 | + const [latestMs, setLatestMs] = useState(minuteMs); | | 20 | + const afterWakeMs = useNowAfter(nextSnoozeWakeAfter(threads, Math.max(minuteMs, latestMs))); | | 21 | + const nowMs = Math.max(minuteMs, afterWakeMs, latestMs); | | 22 | + if (nowMs !== latestMs) setLatestMs(nowMs); | | 23 | + return new Date(nowMs).toISOString(); | | 24 | +} |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/lib/clock.ts +6-7Lines changed: 6 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,13 +13,12 @@ export function useDeadlinePassed(deadlineMs: number | null): boolean { | 13 | 13 | */ | 14 | 14 | export function useNowAfter(deadlineMs: number | null): number { | 15 | 15 | const passed = useDeadlinePassed(deadlineMs); | 16 | | - const [snapshot, setSnapshot] = useState(() => ({ deadlineMs, passed, nowMs: Date.now() })); | 17 | | - if (snapshot.deadlineMs !== deadlineMs || snapshot.passed !== passed) { | 18 | | - // oxlint-disable-next-line react/purity -- Re-reads the clock only in the render-phase adjustment that runs when the deadline changes or passes. | 19 | | - const next = { deadlineMs, passed, nowMs: Date.now() }; | 20 | | - setSnapshot(next); | 21 | | - return next.nowMs; | 22 | | - } | | 16 | + const readNow = () => ({ deadlineMs, passed, nowMs: Date.now() }); | | 17 | + const [snapshot, setSnapshot] = useState(readNow); | | 18 | + // The clock is read inside the updater: the compiler caches a bare | | 19 | + // `Date.now()` in render once per instance, so a later adjustment would reuse | | 20 | + // the first reading. React discards this render and re-runs with the update. | | 21 | + if (snapshot.deadlineMs !== deadlineMs || snapshot.passed !== passed) setSnapshot(readNow); | 23 | 22 | return snapshot.nowMs; | 24 | 23 | } | 25 | 24 |
| |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/state/thread-order.test.ts +51Lines changed: 51 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -11,6 +11,7 @@ import { | 11 | 11 | getPendingThreadOrder, | 12 | 12 | pendingThreadOrderAtom, | 13 | 13 | } from "./thread-order"; | | 14 | +import { environmentServerConfigsAtom } from "./server"; | 14 | 15 | import { environmentThreadShells } from "./threads"; | 15 | 16 |
| 16 | 17 | vi.mock("./atom-registry", async () => { | @@ -30,6 +31,9 @@ vi.mock("./use-thread-outbox", async () => { | 30 | 31 | return { queuedThreadKeysAtom: Atom.make(new Set<string>()).pipe(Atom.keepAlive) }; | 31 | 32 | }); | 32 | 33 |
| | 34 | +type ServerConfigs = Atom.Type<typeof environmentServerConfigsAtom>; | | 35 | +type ServerConfig = ServerConfigs extends ReadonlyMap<EnvironmentId, infer Config> ? Config : never; | | 36 | +const configsAtom = environmentServerConfigsAtom as Atom.Writable<ServerConfigs, ServerConfigs>; | 33 | 37 | const shellsAtom = environmentThreadShells.threadShellsAtom as Atom.Writable< | 34 | 38 | readonly EnvironmentThreadShell[], | 35 | 39 | readonly EnvironmentThreadShell[] | @@ -113,4 +117,51 @@ describe("shared mobile pending move", () => { | 113 | 117 | appAtomRegistry.set(shellsAtom, rows); | 114 | 118 | expect(getPendingThreadOrder()).toBeNull(); | 115 | 119 | }); | | 120 | + | | 121 | + it("retires the hold when a snooze expiry changes the section without a shell event", () => { | | 122 | + vi.useFakeTimers(); | | 123 | + try { | | 124 | + vi.setSystemTime(new Date("2026-06-01T03:00:00.000Z")); | | 125 | + const { rows, start } = fixture(); | | 126 | + // Only the snooze capability matters to the section filter; afterEach's | | 127 | + // registry reset restores the empty configs. | | 128 | + const config = { environment: { capabilities: { threadSnooze: true } } } as ServerConfig; | | 129 | + appAtomRegistry.set(configsAtom, new Map([[EnvironmentId.make("env"), config]])); | | 130 | + const snoozed = makeThreadShellFixture({ | | 131 | + id: ThreadId.make("c"), | | 132 | + environmentId: EnvironmentId.make("env"), | | 133 | + createdAt: "2026-06-01T00:00:00.000Z", | | 134 | + snoozedUntil: "2026-06-01T03:00:05.000Z", | | 135 | + }); | | 136 | + appAtomRegistry.set(shellsAtom, [...rows, snoozed]); | | 137 | + start(); | | 138 | + expect(getPendingThreadOrder()).not.toBeNull(); | | 139 | + | | 140 | + vi.advanceTimersByTime(5_000); | | 141 | + | | 142 | + expect(appAtomRegistry.get(pendingThreadOrderAtom)).toBeNull(); | | 143 | + } finally { | | 144 | + vi.useRealTimers(); | | 145 | + } | | 146 | + }); | | 147 | + | | 148 | + it("tears down a superseded hold's clocks when another hold begins", () => { | | 149 | + vi.useFakeTimers(); | | 150 | + try { | | 151 | + vi.setSystemTime(new Date("2026-06-01T03:00:00.000Z")); | | 152 | + const { rows, start } = fixture(); | | 153 | + const snoozed = makeThreadShellFixture({ | | 154 | + id: ThreadId.make("c"), | | 155 | + environmentId: EnvironmentId.make("env"), | | 156 | + createdAt: "2026-06-01T00:00:00.000Z", | | 157 | + snoozedUntil: "2026-06-01T04:00:00.000Z", | | 158 | + }); | | 159 | + appAtomRegistry.set(shellsAtom, [...rows, snoozed]); | | 160 | + start(); | | 161 | + start().cancel(); | | 162 | + expect(vi.getTimerCount()).toBe(0); | | 163 | + } finally { | | 164 | + vi.useRealTimers(); | | 165 | + } | | 166 | + }); | 116 | 167 | }); |
|