Skip to content

Commit 4faaad4

Browse files
t3dotggclaude
andcommitted
refactor(clients): share retained row status and select mobile snapshots
Web rows repeated the same identity-keyed retention block three times. Move it into one useRetainedValue hook in Sidebar.logic. Mobile rows read the shared snapshot map with a per-thread selector so a write for one row does not re-render every other row. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 51ebd8b commit 4faaad4

4 files changed

Lines changed: 39 additions & 60 deletions

File tree

‎apps/mobile/src/state/use-thread-pr.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
pullRequestDetailToVcsStatus,
77
} from "@t3tools/client-runtime/state/pull-requests";
88
import { Atom } from "effect/unstable/reactivity";
9-
import { useEffect, useMemo } from "react";
9+
import { useCallback, useEffect, useMemo } from "react";
1010

1111
import { connectionAtomRuntime } from "../connection/runtime";
1212
import { appAtomRegistry } from "./atom-registry";
@@ -50,8 +50,14 @@ export function useThreadPr(
5050
const snapshotIdentity = JSON.stringify(
5151
thread.linkedPullRequest ?? { branch: thread.branch, cwd },
5252
);
53-
const snapshots = useAtomValue(threadPrSnapshotsAtom);
54-
const snapshotEntry = snapshots.get(threadKey);
53+
// Select this row's entry so writes for other rows do not re-render it.
54+
const snapshotEntry = useAtomValue(
55+
threadPrSnapshotsAtom,
56+
useCallback(
57+
(current: ReadonlyMap<string, ThreadPrSnapshot>) => current.get(threadKey),
58+
[threadKey],
59+
),
60+
);
5561
const snapshot = snapshotEntry?.identity === snapshotIdentity ? snapshotEntry.presentation : null;
5662
const gitStatus = useEnvironmentQuery(
5763
thread.linkedPullRequest == null && thread.branch !== null && cwd !== null

‎apps/web/src/components/LegacySidebar.tsx‎

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ import {
185185
orderItemsByPreferredIds,
186186
shouldClearThreadSelectionOnMouseDown,
187187
sortProjectsForSidebar,
188+
useRetainedValue,
188189
useSidebarRowSubscriptionLease,
189190
useThreadJumpHintVisibility,
190191
ThreadStatusPill,
@@ -464,39 +465,16 @@ export const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThr
464465
leaseLiveStatus ? thread.environmentId : null,
465466
leaseLiveStatus ? thread.linkedPullRequest : null,
466467
);
467-
const gitStatusSnapshotKey = JSON.stringify([thread.environmentId, gitCwd]);
468-
const gitStatusSnapshotRef = useRef<{
469-
readonly key: string;
470-
readonly value: NonNullable<typeof gitStatus.data>;
471-
} | null>(null);
472-
const linkedPullRequestSnapshotKey =
468+
const visibleGitStatus = useRetainedValue(
469+
JSON.stringify([thread.environmentId, gitCwd]),
470+
gitStatus.data,
471+
);
472+
const visibleLinkedPullRequestStatus = useRetainedValue(
473473
thread.linkedPullRequest === null
474474
? null
475-
: JSON.stringify([thread.environmentId, thread.linkedPullRequest]);
476-
const linkedPullRequestStatusSnapshotRef = useRef<{
477-
readonly key: string;
478-
readonly value: NonNullable<typeof linkedPullRequestStatus>;
479-
} | null>(null);
480-
if (gitStatus.data !== null) {
481-
gitStatusSnapshotRef.current = { key: gitStatusSnapshotKey, value: gitStatus.data };
482-
}
483-
if (linkedPullRequestStatus !== null && linkedPullRequestSnapshotKey !== null) {
484-
linkedPullRequestStatusSnapshotRef.current = {
485-
key: linkedPullRequestSnapshotKey,
486-
value: linkedPullRequestStatus,
487-
};
488-
}
489-
const visibleGitStatus =
490-
gitStatus.data ??
491-
(gitStatusSnapshotRef.current?.key === gitStatusSnapshotKey
492-
? gitStatusSnapshotRef.current.value
493-
: null);
494-
const visibleLinkedPullRequestStatus =
495-
linkedPullRequestStatus ??
496-
(linkedPullRequestSnapshotKey !== null &&
497-
linkedPullRequestStatusSnapshotRef.current?.key === linkedPullRequestSnapshotKey
498-
? linkedPullRequestStatusSnapshotRef.current.value
499-
: null);
475+
: JSON.stringify([thread.environmentId, thread.linkedPullRequest]),
476+
linkedPullRequestStatus,
477+
);
500478
const pr =
501479
thread.linkedPullRequest == null
502480
? resolveThreadPr({ threadBranch: thread.branch, gitStatus: visibleGitStatus })

‎apps/web/src/components/Sidebar.logic.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ export function useSidebarRowSubscriptionLease(isActive: boolean): {
6363
};
6464
}
6565

66+
// A row keeps the last live value it rendered so a released lease never
67+
// blanks its badge. The value is bound to `key`, so a different worktree or
68+
// linked pull request cannot reuse the previous one.
69+
export function useRetainedValue<T>(key: string | null, value: T | null): T | null {
70+
const retained = React.useRef<{ readonly key: string; readonly value: T } | null>(null);
71+
if (key !== null && value !== null) {
72+
retained.current = { key, value };
73+
}
74+
if (value !== null) return value;
75+
return key !== null && retained.current?.key === key ? retained.current.value : null;
76+
}
77+
6678
// The list already reaches its destination through sortable transforms while
6779
// the pointer is down. dnd-kit's default also animates the committed DOM order
6880
// after release, replaying the same movement across every affected row.

‎apps/web/src/components/Sidebar.tsx‎

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ import {
143143
sortPinnedThreadsForSidebar,
144144
sortSettledThreadsForSidebar,
145145
sortThreadsForSidebar,
146+
useRetainedValue,
146147
useSidebarRowSubscriptionLease,
147148
useThreadJumpHintVisibility,
148149
} from "./Sidebar.logic";
@@ -813,19 +814,10 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: {
813814
})
814815
: null,
815816
);
816-
const gitStatusSnapshotKey = JSON.stringify([thread.environmentId, gitCwd]);
817-
const gitStatusSnapshotRef = useRef<{
818-
readonly key: string;
819-
readonly value: NonNullable<typeof gitStatus.data>;
820-
} | null>(null);
821-
if (gitStatus.data !== null) {
822-
gitStatusSnapshotRef.current = { key: gitStatusSnapshotKey, value: gitStatus.data };
823-
}
824-
const visibleGitStatus =
825-
gitStatus.data ??
826-
(gitStatusSnapshotRef.current?.key === gitStatusSnapshotKey
827-
? gitStatusSnapshotRef.current.value
828-
: null);
817+
const visibleGitStatus = useRetainedValue(
818+
JSON.stringify([thread.environmentId, gitCwd]),
819+
gitStatus.data,
820+
);
829821
const retainTerminalOnBranchMismatch = thread.worktreePath === null;
830822
const pr = resolveDisplayedThreadPr({
831823
threadBranch: thread.branch,
@@ -1666,19 +1658,10 @@ const SidebarSearchResultRow = memo(function SidebarSearchResultRow(props: {
16661658
})
16671659
: null,
16681660
);
1669-
const gitStatusSnapshotKey = JSON.stringify([thread.environmentId, gitCwd]);
1670-
const gitStatusSnapshotRef = useRef<{
1671-
readonly key: string;
1672-
readonly value: NonNullable<typeof gitStatus.data>;
1673-
} | null>(null);
1674-
if (gitStatus.data !== null) {
1675-
gitStatusSnapshotRef.current = { key: gitStatusSnapshotKey, value: gitStatus.data };
1676-
}
1677-
const visibleGitStatus =
1678-
gitStatus.data ??
1679-
(gitStatusSnapshotRef.current?.key === gitStatusSnapshotKey
1680-
? gitStatusSnapshotRef.current.value
1681-
: null);
1661+
const visibleGitStatus = useRetainedValue(
1662+
JSON.stringify([thread.environmentId, gitCwd]),
1663+
gitStatus.data,
1664+
);
16821665
const branchMismatch = resolveLocalCheckoutBranchMismatch({
16831666
effectiveEnvMode: thread.worktreePath === null ? "local" : "worktree",
16841667
activeWorktreePath: thread.worktreePath,

0 commit comments

Comments
 (0)