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