Skip to content

Commit 7f11433

Browse files
committed
fix(review): save pending comments to GitHub and simplify workspace tabs
1 parent c0f849f commit 7f11433

12 files changed

Lines changed: 314 additions & 80 deletions

‎apps/mobile/src/features/pull-requests/PullRequestInlineCommentScreen.tsx‎

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,17 @@ export function PullRequestInlineCommentScreen({ route }: StaticScreenProps<Inli
3434
const params = route.params;
3535
const navigation = useNavigation();
3636
const store = usePullRequestReviewDraft(params);
37-
const saved = store.comments.find((draft) => draft.id === params.draftId);
37+
const [commentId] = useState(() => params.draftId ?? uuidv4());
38+
const saved = store.comments.find((draft) => draft.id === commentId);
3839
const [editedBody, setBody] = useState<string | null>(null);
3940
const body = editedBody ?? saved?.body ?? params.body ?? "";
4041
const pendingActions = usePendingReviewCommentActions(params);
4142
const updateComment = useAtomCommand(composerPullRequests.updateComment, {
4243
reportFailure: false,
4344
});
45+
const saveDraftComment = useAtomCommand(composerPullRequests.saveDraftComment, {
46+
reportFailure: false,
47+
});
4448
const detail = useEnvironmentQuery(composerPullRequests.detail(pullRequestTarget(params)));
4549
const { refresh: refreshDetail } = detail;
4650
useFocusEffect(() => refreshDetail());
@@ -92,28 +96,42 @@ export function PullRequestInlineCommentScreen({ route }: StaticScreenProps<Inli
9296
});
9397
if (result._tag === "Failure") throw squashAtomCommandFailure(result);
9498
}
99+
const comment = {
100+
id: commentId,
101+
path: params.path,
102+
...(params.oldPath ? { oldPath: params.oldPath } : {}),
103+
position: params.position,
104+
headSha: params.headSha,
105+
code: params.code,
106+
body,
107+
...(hostedComment ? { hostedComment } : {}),
108+
};
95109
store.update((draft) => ({
96110
...draft,
97-
comments: [
98-
...draft.comments.filter((comment) => comment.id !== params.draftId),
99-
{
100-
id: params.draftId ?? uuidv4(),
101-
path: params.path,
102-
...(params.oldPath ? { oldPath: params.oldPath } : {}),
103-
position: params.position,
104-
headSha: params.headSha,
105-
code: params.code,
106-
body,
107-
...(hostedComment ? { hostedComment } : {}),
108-
},
109-
],
111+
comments: [...draft.comments.filter((comment) => comment.id !== commentId), comment],
110112
}));
111113
await store.flush();
114+
if (!hostedComment && detail.data?.capabilities.review.draftComment === true) {
115+
const target = pullRequestTarget(params);
116+
const result = await saveDraftComment({
117+
...target,
118+
input: { ...target.input, comment },
119+
});
120+
if (result._tag === "Failure") throw squashAtomCommandFailure(result);
121+
store.update((draft) => ({
122+
...draft,
123+
comments: draft.comments.map((entry) =>
124+
entry.id === comment.id ? { ...entry, hostedComment: result.value } : entry,
125+
),
126+
}));
127+
await store.flush();
128+
}
129+
detail.refresh();
112130
navigation.goBack();
113131
} catch (cause) {
114132
Alert.alert(
115133
"Could not save the review comment",
116-
cause instanceof Error ? cause.message : "Your draft has been kept. Try again.",
134+
`${cause instanceof Error ? cause.message : "Saving failed."}\nYour draft is kept in T3. Try saving again.`,
117135
);
118136
} finally {
119137
if (locked) setReviewDraftBusy(params, false);

‎apps/mobile/src/features/workspace/ThreadWorkspaceScreen.tsx‎

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ import { EnvironmentId, ThreadId } from "@t3tools/contracts";
33
import { useNavigation, type StaticScreenProps } from "@react-navigation/native";
44
import { useState } from "react";
55
import { View } from "react-native";
6+
import { AppActivityIndicator } from "../../components/AppActivityIndicator";
7+
import {
8+
readWorkspacePane,
9+
rememberWorkspacePane,
10+
resolveWorkspacePane,
11+
type WorkspacePane,
12+
} from "./workspacePaneMemory";
613
import { SegmentedControl } from "../../components/SegmentedControl";
714
import { useEnvironmentQuery } from "../../state/query";
815
import { useEnvironmentPresentation } from "../../state/presentation";
@@ -24,9 +31,16 @@ type Props = StaticScreenProps<{
2431
readonly reviewWorkingTreeScope?: "staged" | "unstaged";
2532
}>;
2633

27-
function ThreadWorkspaceScreen(props: Props & { readonly initialTab: "files" | "git" }) {
34+
function ThreadWorkspaceScreen(props: Props) {
2835
const navigation = useNavigation();
29-
const [tab, setTab] = useState(props.initialTab);
36+
const paneKey = JSON.stringify([props.route.params.environmentId, props.route.params.threadId]);
37+
const [tab, setTab] = useState<WorkspacePane | null>(() =>
38+
props.route.params.path
39+
? "files"
40+
: props.route.params.reviewSectionId
41+
? "git"
42+
: readWorkspacePane(paneKey),
43+
);
3044
const environmentId = EnvironmentId.make(props.route.params.environmentId);
3145
const threadId = ThreadId.make(props.route.params.threadId);
3246
const { selectedThreadCwd: cwd } = useSelectedThreadWorktree();
@@ -46,16 +60,28 @@ function ThreadWorkspaceScreen(props: Props & { readonly initialTab: "files" | "
4660
// Status counts each changed path once, including edits that cancel between
4761
// the index and working tree, and updates through the existing Git subscription.
4862
const count = status.data?.workingTree?.files.length;
63+
const statusUnavailable =
64+
!access.isPending && (!access.canReadFiles || cwd === null || status.error !== null);
65+
const selectedTab = resolveWorkspacePane(tab, count ?? (statusUnavailable ? 0 : undefined));
66+
if (selectedTab !== null && selectedTab !== tab) {
67+
setTab(selectedTab);
68+
rememberWorkspacePane(paneKey, selectedTab);
69+
}
70+
const selectTab = (pane: WorkspacePane) => {
71+
setTab(pane);
72+
rememberWorkspacePane(paneKey, pane);
73+
};
4974
const tabs = (
50-
<View className="border-b border-border-subtle bg-sheet px-3 py-2">
75+
<View className="w-full border-b border-border-subtle bg-sheet px-3 py-2">
5176
<SegmentedControl
5277
size="compact"
78+
className="w-full"
5379
options={[
54-
{ value: "files" as const, label: "Files" },
5580
{ value: "git" as const, label: "Git", ...(count === undefined ? {} : { badge: count }) },
81+
{ value: "files" as const, label: "Files" },
5682
]}
57-
selected={tab}
58-
onSelect={setTab}
83+
selected={selectedTab ?? "files"}
84+
onSelect={selectTab}
5985
role="tab"
6086
/>
6187
</View>
@@ -64,7 +90,7 @@ function ThreadWorkspaceScreen(props: Props & { readonly initialTab: "files" | "
6490
<ReviewHeader
6591
environmentId={environmentId}
6692
threadId={threadId}
67-
title={tab === "files" ? "Files" : "Git"}
93+
title={selectedTab === "git" ? "Git" : "Files"}
6894
subtitle=""
6995
androidSubtitle=""
7096
selectedThreadCwd={cwd}
@@ -73,7 +99,17 @@ function ThreadWorkspaceScreen(props: Props & { readonly initialTab: "files" | "
7399
onReturnToThread={() => navigation.goBack()}
74100
/>
75101
);
76-
return tab === "files" ? (
102+
if (selectedTab === null)
103+
return (
104+
<View className="flex-1 bg-sheet">
105+
{workspaceHeader}
106+
{tabs}
107+
<View className="flex-1 items-center justify-center">
108+
<AppActivityIndicator accessibilityLabel="Loading workspace" />
109+
</View>
110+
</View>
111+
);
112+
return selectedTab === "files" ? (
77113
props.route.params.path ? (
78114
<ThreadFileScreen
79115
route={{ params: { ...props.route.params, path: props.route.params.path } }}
@@ -110,7 +146,6 @@ export function ThreadFilesWorkspaceScreen(props: Props) {
110146
<ThreadWorkspaceScreen
111147
key={`${props.route.params.environmentId}:${props.route.params.threadId}`}
112148
{...props}
113-
initialTab="files"
114149
/>
115150
);
116151
}
@@ -120,7 +155,6 @@ export function ThreadGitWorkspaceScreen(props: Props) {
120155
<ThreadWorkspaceScreen
121156
key={`${props.route.params.environmentId}:${props.route.params.threadId}`}
122157
{...props}
123-
initialTab="git"
124158
/>
125159
);
126160
}
@@ -132,7 +166,6 @@ export function ThreadFileWorkspaceScreen(props: Parameters<typeof ThreadFileScr
132166
<ThreadWorkspaceScreen
133167
key={`${props.route.params.environmentId}:${threadId}`}
134168
route={{ params: { ...props.route.params, threadId } }}
135-
initialTab="files"
136169
/>
137170
);
138171
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { expect, it } from "vite-plus/test";
2+
import {
3+
readWorkspacePane,
4+
rememberWorkspacePane,
5+
resolveWorkspacePane,
6+
} from "./workspacePaneMemory";
7+
8+
it("waits for the initial status and chooses Git for changed files or Files for a clean tree", () => {
9+
expect(resolveWorkspacePane(null, undefined)).toBeNull();
10+
expect(resolveWorkspacePane(null, 4)).toBe("git");
11+
expect(resolveWorkspacePane(null, 0)).toBe("files");
12+
});
13+
14+
it("keeps the selected pane across staging updates and thread changes", () => {
15+
const first = "mobile-pane-thread-one";
16+
const second = "mobile-pane-thread-two";
17+
expect(readWorkspacePane(first)).toBeNull();
18+
const initial = resolveWorkspacePane(null, 3)!;
19+
rememberWorkspacePane(first, initial);
20+
expect(resolveWorkspacePane(readWorkspacePane(first), 0)).toBe("git");
21+
rememberWorkspacePane(first, "files");
22+
rememberWorkspacePane(second, "git");
23+
expect(resolveWorkspacePane(readWorkspacePane(first), 7)).toBe("files");
24+
expect(readWorkspacePane(second)).toBe("git");
25+
expect(readWorkspacePane(first)).toBe("files");
26+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Atom } from "effect/reactivity";
2+
import { appAtomRegistry } from "../../state/atom-registry";
3+
4+
export type WorkspacePane = "git" | "files";
5+
const rememberedPane = Atom.family((key: string) =>
6+
Atom.make<WorkspacePane | null>(null).pipe(
7+
Atom.keepAlive,
8+
Atom.withLabel(`mobile:workspace:pane:${key}`),
9+
),
10+
);
11+
12+
export function readWorkspacePane(key: string) {
13+
return appAtomRegistry.get(rememberedPane(key));
14+
}
15+
export function rememberWorkspacePane(key: string, pane: WorkspacePane) {
16+
appAtomRegistry.set(rememberedPane(key), pane);
17+
}
18+
19+
/** The first ready status chooses a pane once; later Git updates preserve that choice. */
20+
export function resolveWorkspacePane(
21+
current: WorkspacePane | null,
22+
changedFiles: number | undefined,
23+
): WorkspacePane | null {
24+
if (current !== null) return current;
25+
return changedFiles === undefined ? null : changedFiles > 0 ? "git" : "files";
26+
}

‎apps/web/src/components/diffs/WorkingChangesReview.tsx‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export function WorkingChangesReview(props: WorkingReviewProps) {
160160
const [generation, setGeneration] = useState(0);
161161
const navigationKey = props.navigationKey ?? JSON.stringify([environmentId, composerDraftTarget]);
162162
const remembered = workingNavigation.get(navigationKey);
163-
const [view, setView] = useState<"files" | "git">(remembered?.view ?? "git");
163+
const [chosenView, setView] = useState<"files" | "git" | null>(remembered?.view ?? null);
164164
const [selection, setSelection] = useState<WorkingSelection>(
165165
remembered?.selection ?? { scope: "unstaged", path: null },
166166
);
@@ -184,14 +184,22 @@ export function WorkingChangesReview(props: WorkingReviewProps) {
184184
const [workspacePath, setWorkspacePath] = useState<string | null>(
185185
remembered?.workspacePath ?? null,
186186
);
187-
useLayoutEffect(() => {
188-
workingNavigation.set(navigationKey, { view, selection, folded, message, workspacePath });
189-
}, [navigationKey, view, selection, folded, message, workspacePath]);
190187
const [generatingMessage, setGeneratingMessage] = useState(false);
191188
const [reverting, setReverting] = useState<{ paths: string[]; label: string } | null>(null);
192189
const all = useWorkingChanges(props, undefined, generation);
193190
const staged = useWorkingChanges(props, "staged", generation);
194191
const unstaged = useWorkingChanges(props, "unstaged", generation);
192+
const initialGitStatusReady = staged.query.data !== null && unstaged.query.data !== null;
193+
const initialChangedCount =
194+
(staged.source?.files?.length ?? staged.files.length) +
195+
(unstaged.source?.files?.length ?? unstaged.files.length);
196+
const view = chosenView ?? (initialGitStatusReady && initialChangedCount === 0 ? "files" : "git");
197+
// Choose once after the initial read. Later writes keep the current reader open.
198+
if (chosenView === null && initialGitStatusReady) setView(view);
199+
useLayoutEffect(() => {
200+
workingNavigation.set(navigationKey, { view, selection, folded, message, workspacePath });
201+
}, [navigationKey, view, selection, folded, message, workspacePath]);
202+
195203
const [optimistic, setOptimistic] = useState<{
196204
state: WorkingChangesState;
197205
confirmed: boolean;
@@ -575,6 +583,7 @@ export function WorkingChangesReview(props: WorkingReviewProps) {
575583
};
576584
const viewTabs = (
577585
<ToggleGroup
586+
className="w-full"
578587
value={[view]}
579588
onValueChange={(values) => {
580589
const next = values[0];
@@ -585,17 +594,17 @@ export function WorkingChangesReview(props: WorkingReviewProps) {
585594
}}
586595
aria-label="Workspace navigation"
587596
>
588-
<Toggle value="files">
589-
<MorphIcon icon={Files} />
590-
Files
591-
</Toggle>
592-
<Toggle value="git">
597+
<Toggle value="git" className="flex-1">
593598
<MorphIcon icon={GitBranch} />
594599
Git
595600
<Badge size="sm" variant="info" aria-label={`${changedCount} changed files`}>
596601
{changedCount}
597602
</Badge>
598603
</Toggle>
604+
<Toggle value="files" className="flex-1">
605+
<MorphIcon icon={Files} />
606+
Files
607+
</Toggle>
599608
</ToggleGroup>
600609
);
601610
return (

‎apps/web/src/components/files/FileBrowserPanel.tsx‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ import { serializeComposerFileLink } from "@t3tools/shared/composerTrigger";
1111
import * as AsyncResult from "effect/reactivity/AsyncResult";
1212
import * as Cause from "effect/Cause";
1313
import { ChevronsDownUp, ChevronsUpDown } from "lucide";
14-
import { useEffect, useRef, useState } from "react";
14+
import { useEffect, useMemo, useRef, useState } from "react";
1515

1616
import { Button } from "~/components/ui/button";
1717
import { InputGroup, InputGroupInput } from "~/components/ui/input-group";
18-
import { MorphIcon } from "~/components/MorphIcon";
1918
import { toastManager } from "~/components/ui/toast";
2019
import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
2120
import { useComposerHandleContext } from "~/composerHandleContext";
@@ -30,7 +29,6 @@ import { PIERRE_TREE_UNSAFE_CSS, pierreTreeStyle } from "~/pierre-tree-theme";
3029

3130
import { joinHostPath } from "./filePath";
3231
import { createFileTreeDragMentionController } from "./fileTreeDragMention";
33-
import { areAllDirectoriesExpanded, setAllDirectoriesExpanded } from "./fileTreeExpansion";
3432
import { buildFileTreePathUpdates } from "./fileTreePathReconciliation";
3533
import { loadFileAncestors } from "./loadFileAncestors";
3634
import { useDirectoryEntries } from "./useDirectoryEntries";
@@ -374,7 +372,6 @@ export default function FileBrowserPanel({
374372
void load(path.replace(/\/$/, ""));
375373
}
376374
} else {
377-
if (item?.isDirectory() && expandedPathsRef.current.has(path)) setExpandAll(false);
378375
expandedPathsRef.current.delete(path);
379376
}
380377
}

‎apps/web/src/components/pullRequest/PullRequestCodeTab.tsx‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ import {
131131
nextPendingReviewCommentId,
132132
pullRequestReviewKey,
133133
usePendingReviewComments,
134-
usePullRequestReviewStore,
135134
type PendingReviewComment,
136135
} from "./pullRequestReviewStore";
136+
import { usePendingReviewCommentActions } from "./usePendingReviewCommentActions";
137137

138138
interface ReviewAnnotationGroup {
139139
readonly threads: ReadonlyArray<PullRequestReviewThread>;
@@ -462,9 +462,8 @@ function PullRequestCodeTab({
462462
input: { ...reference, ...(commit === null ? {} : { commit }) },
463463
}),
464464
);
465-
const reviewKey = referenceKey;
466465
const pendingComments = usePendingReviewComments(reference, detail.reviewThreads);
467-
const addComment = usePullRequestReviewStore((store) => store.addComment);
466+
const { add: addComment } = usePendingReviewCommentActions(environmentId, reference);
468467
const replyToThread = useAtomCommand(pullRequestEnvironment.replyToThread, {
469468
reportFailure: false,
470469
});
@@ -1464,13 +1463,16 @@ function PullRequestCodeTab({
14641463
}
14651464
return;
14661465
}
1467-
addComment(reviewKey, {
1466+
addComment(
1467+
{
14681468
id: nextPendingReviewCommentId(),
14691469
path: draft.path,
14701470
...(draft.oldPath === null ? {} : { oldPath: draft.oldPath }),
14711471
position: draft.position,
14721472
body,
1473-
});
1473+
},
1474+
detail.capabilities.review.draftComment === true,
1475+
);
14741476
setDraft(null);
14751477
setSelectedLines(null);
14761478
}}
@@ -1488,7 +1490,6 @@ function PullRequestCodeTab({
14881490
onAddToAgentSelection,
14891491
onAddPendingToComposer,
14901492
renderThreadCard,
1491-
reviewKey,
14921493
],
14931494
);
14941495

0 commit comments

Comments
 (0)