Files
t3code/apps/mobile/src/features/threads/threadListV2.ts
T
Theo BrowneandClaude Fable 5 af880a2740 Address PR #4026 review findings
- settleThread inherits archive's running-turn guard on web and mobile:
  settling can never interrupt an active turn
- unsettleThread no-ops on auto-settled (unarchived) threads instead of
  sending an unarchive the server would reject
- Mobile v2 partition now receives per-row PR state (rows report up via
  onChangeRequestState, mirroring web) so merged/closed PRs auto-settle
- Mobile empty state accounts for archived-only workspaces when v2 is on
- Optimistic settled hold on web + mobile: the shell being settled is kept
  visible (marked archived) between the live stream dropping it and the
  archived snapshot returning it — no flicker, and holds are created only
  at settle time so deletes are never resurrected
- Hover-only settle buttons in web SidebarV2 get focus-visible:opacity-100
- v2 full swipe commits the advertised primary action (Settle/Un-settle),
  with the stretch animation following; delete remains a tap target only.
  v1 keeps full-swipe delete via the new fullSwipeAction prop default
- Android long-press on v2 cards works: PressableScaleCard is the menu's
  direct child and forwards the injected onLongPress to its Pressable

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-07-15 21:58:52 -07:00

116 lines
4.3 KiB
TypeScript

import { effectiveSettled } from "@t3tools/client-runtime/state/thread-settled";
import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
import type { EnvironmentId } from "@t3tools/contracts";
/**
* Thread List v2 model, ported from the web sidebar v2
* (apps/web/src/components/Sidebar.logic.ts + SidebarV2.tsx).
*
* Four visual states, three colors: color is reserved for "act now"
* (approval), "in motion" (working), and "broken" (failed). Ready is the
* unlabeled resting state.
*/
export type ThreadListV2Status = "approval" | "working" | "failed" | "ready";
export function resolveThreadListV2Status(
thread: Pick<EnvironmentThreadShell, "hasPendingApprovals" | "session">,
): ThreadListV2Status {
if (thread.hasPendingApprovals) {
return "approval";
}
if (thread.session?.status === "running" || thread.session?.status === "starting") {
return "working";
}
if (thread.session?.status === "error") {
return "failed";
}
return "ready";
}
/**
* v2 sort: static creation order, newest thread on top. Activity NEVER
* reorders the list — a row holds its position from open until settled, so
* the screen only moves at lifecycle transitions. Mirrors web's
* sortThreadsForSidebarV2.
*/
export function sortThreadsForListV2<T extends { readonly id: string; readonly createdAt: string }>(
threads: readonly T[],
): T[] {
// .sort() on a copy, not .toSorted(): Hermes doesn't ship the ES2023
// change-by-copy array methods.
return [...threads].sort(
(left, right) =>
Date.parse(right.createdAt) - Date.parse(left.createdAt) || left.id.localeCompare(right.id),
);
}
export interface ThreadListV2Item {
readonly thread: EnvironmentThreadShell;
readonly variant: "card" | "slim";
/** First settled row after the card block draws the SETTLED divider. */
readonly showSettledDivider: boolean;
readonly isLast: boolean;
}
/**
* Partitions visible threads into the active card block (creation order) and
* the settled recency tail, matching the web v2 list. `autoSettleAfterDays`
* mirrors the web default of 3 — mobile has no client-settings sync yet, so
* the default is fixed here rather than user-configurable.
*/
export function buildThreadListV2Items(input: {
readonly threads: ReadonlyArray<EnvironmentThreadShell>;
readonly environmentId: EnvironmentId | null;
readonly searchQuery: string;
/** Per-row PR state reported up by visible rows ("env:threadId" keys). */
readonly changeRequestStateByKey?: ReadonlyMap<string, "open" | "closed" | "merged">;
readonly autoSettleAfterDays?: number;
/** Injectable for tests; defaults to now. */
readonly now?: string;
}): ThreadListV2Item[] {
const now = input.now ?? new Date().toISOString();
const autoSettleAfterDays = input.autoSettleAfterDays ?? 3;
const query = input.searchQuery.trim().toLocaleLowerCase();
const active: EnvironmentThreadShell[] = [];
const settled: EnvironmentThreadShell[] = [];
for (const thread of input.threads) {
// Archived threads stay in the list: in the client-only settled model,
// archive IS settle, so they render as the settled tail.
if (input.environmentId !== null && thread.environmentId !== input.environmentId) continue;
if (query.length > 0 && !thread.title.toLocaleLowerCase().includes(query)) continue;
const changeRequestState =
input.changeRequestStateByKey?.get(`${thread.environmentId}:${thread.id}`) ?? null;
if (effectiveSettled(thread, { now, autoSettleAfterDays, changeRequestState })) {
settled.push(thread);
} else {
active.push(thread);
}
}
const orderedActive = sortThreadsForListV2(active);
const orderedSettled = [...settled].sort(
(left, right) =>
Date.parse(right.latestUserMessageAt ?? right.updatedAt) -
Date.parse(left.latestUserMessageAt ?? left.updatedAt),
);
const items: ThreadListV2Item[] = [];
for (const thread of orderedActive) {
items.push({ thread, variant: "card", showSettledDivider: false, isLast: false });
}
for (const [index, thread] of orderedSettled.entries()) {
items.push({
thread,
variant: "slim",
showSettledDivider: index === 0,
isLast: false,
});
}
const last = items.at(-1);
if (last) {
items[items.length - 1] = { ...last, isLast: true };
}
return items;
}