Open History to see the last commit
1import { settlePromise } from "@t3tools/client-runtime/state/runtime";
2import { AsyncResult } from "effect/reactivity";
3import * as Dialog from "../../components/ui/Dialog";
4import { useFocusEffect, useNavigation, type StaticScreenProps } from "@react-navigation/native";
5import { useRef, useState } from "react";
6import { View } from "react-native";
7import type { PullRequestReviewPosition } from "@t3tools/contracts";
8import { AppText as Text, AppTextInput as TextInput } from "../../components/ui/AppText";
9import { Button } from "../../components/ui/Button";
10import { uuidv4 } from "../../lib/uuid";
11import { composerPullRequests } from "../../state/pull-requests";
12import { useEnvironmentQuery } from "../../state/query";
13import { PullRequestSheetContent } from "./PullRequestSheetContent";
14import { usePullRequestReviewDraft } from "./usePullRequestReviewDraft";
15import { reviewPositionLabel } from "./pullRequestDiffComments";
16import { pullRequestTarget, type PullRequestParams } from "./usePullRequestDiff";
17import { usePullRequestPermissions } from "./usePullRequestPermissions";
18import { PullRequestCompletionSuggestions } from "./PullRequestCompletionSuggestions";
19import { usePullRequestCompletions } from "./usePullRequestCompletions";
20
21export type InlineCommentParams = PullRequestParams & {
22 readonly draftId?: string;
23 readonly path: string;
24 readonly oldPath?: string;
25 readonly position: PullRequestReviewPosition;
26 readonly headSha: string;
27 readonly code: string;
28};
29export function PullRequestInlineCommentScreen({ route }: StaticScreenProps<InlineCommentParams>) {
30 const params = route.params;
31 const navigation = useNavigation();
32 const store = usePullRequestReviewDraft(params);
33 const saved = store.draft.comments.find((draft) => draft.id === params.draftId);
34 const [editedBody, setBody] = useState<string | null>(null);
35 const body = editedBody ?? saved?.body ?? "";
36 const detail = useEnvironmentQuery(composerPullRequests.detail(pullRequestTarget(params)));
37 const { refresh: refreshDetail } = detail;
38 const completions = usePullRequestCompletions({
39 environmentId: params.environmentId,
40 projectId: params.projectId,
41 value: body,
42 onChangeValue: setBody,
43 support: detail.data?.capabilities ?? null,
44 });
45 useFocusEffect(refreshDetail);
46 const permissions = usePullRequestPermissions(params, detail.data);
47 const stale = detail.data && detail.data.headSha !== params.headSha;
48 const pending = useRef(false);
49 const [busy, setBusy] = useState(false);
50 const save = async () => {
51 if (
52 pending.current ||
53 !store.ready ||
54 !body.trim() ||
55 detail.isPending ||
56 detail.error ||
57 stale ||
58 !permissions.canInlineComment
59 )
60 return;
61 pending.current = true;
62 setBusy(true);
63 store.update((draft) => ({
64 ...draft,
65 comments: [
66 ...draft.comments.filter((comment) => comment.id !== params.draftId),
67 {
68 id: params.draftId ?? uuidv4(),
69 path: params.path,
70 ...(params.oldPath ? { oldPath: params.oldPath } : {}),
71 position: params.position,
72 headSha: params.headSha,
73 code: params.code,
74 body,
75 },
76 ],
77 }));
78 // flush reports write failures through the draft store's error field.
79 const result = await settlePromise(() => store.flush());
80 pending.current = false;
81 setBusy(false);
82 if (AsyncResult.isSuccess(result)) navigation.goBack();
83 };
84 return (
85 <PullRequestSheetContent
86 busy={busy}
87 title={saved ? "Edit review comment" : "Review comment"}
88 subtitle={`#${params.number} · ${reviewPositionLabel(params.position)}`}
89 footer={
90 <Button
91 fullWidth
92 tone="contrast"
93 label={busy ? "Saving…" : "Save draft"}
94 loading={busy}
95 disabled={
96 !store.ready ||
97 !body.trim() ||
98 detail.isPending ||
99 detail.error !== null ||
100 Boolean(stale) ||
101 !permissions.canInlineComment
102 }
103 onPress={() => void save()}
104 />
105 }
106 >
107 <View className="gap-2 rounded-[20px] bg-grouped-card p-4">
108 <Text className="font-mono text-xs text-foreground-muted">{params.path}</Text>
109 <Text selectable className="font-mono text-xs">
110 {params.code}
111 </Text>
112 </View>
113 <PullRequestCompletionSuggestions completions={completions} />
114 <TextInput
115 multiline
116 selection={completions.selection}
117 onSelectionChange={completions.onSelectionChange}
118 onBlur={completions.onBlur}
119 editable={store.ready && !busy}
120 autoFocus
121 accessibilityLabel="Inline review comment"
122 placeholder="Write feedback on this line…"
123 placeholderTextColorClassName="accent-foreground-muted"
124 maxLength={65536}
125 value={body}
126 onChangeText={setBody}
127 className="min-h-32 rounded-[20px] bg-grouped-card p-4 text-base text-foreground"
128 style={{ textAlignVertical: "top" }}
129 />
130 <Text className="text-xs text-foreground-muted">
131 Saved comments are private until you submit your review.
132 </Text>
133 {stale ? (
134 <Text className="text-sm text-danger-foreground">
135 The branch changed. Return to the current diff and select the line again.
136 </Text>
137 ) : null}
138 {store.error || detail.error ? (
139 <Text className="text-sm text-danger-foreground">{store.error ?? detail.error}</Text>
140 ) : null}
141 {saved ? (
142 <Button
143 label="Delete draft"
144 onPress={() =>
145 Dialog.alert("Delete this draft?", "Your other review comments will be kept.", [
146 { text: "Cancel", style: "cancel" },
147 {
148 text: "Delete",
149 style: "destructive",
150 onPress: () => {
151 store.update((draft) => ({
152 ...draft,
153 comments: draft.comments.filter((comment) => comment.id !== saved.id),
154 }));
155 navigation.goBack();
156 },
157 },
158 ])
159 }
160 />
161 ) : null}
162 </PullRequestSheetContent>
163 );
164}