Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

PullRequestInlineCommentScreen.tsx

151 lines5.5 KB

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