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

PullRequestInlineCommentScreen.tsx

151 lines5.4 KB

1import * as Dialog from "../../components/ui/Dialog";
2import { useFocusEffect, useNavigation, type StaticScreenProps } from "@react-navigation/native";
3import { useCallback, useRef, useState } from "react";
4import { View } from "react-native";
5import type { PullRequestReviewPosition } from "@t3tools/contracts";
6import { AppText as Text, AppTextInput as TextInput } from "../../components/ui/AppText";
7import { Button } from "../../components/ui/Button";
8import { uuidv4 } from "../../lib/uuid";
9import { composerPullRequests } from "../../state/pull-requests";
10import { useEnvironmentQuery } from "../../state/query";
11import { PullRequestSheetContent } from "./PullRequestSheetContent";
12import { usePullRequestReviewDraft } from "./usePullRequestReviewDraft";
13import { reviewPositionLabel } from "./pullRequestDiffComments";
14import { pullRequestTarget, type PullRequestParams } from "./usePullRequestDiff";
15import { usePullRequestPermissions } from "./usePullRequestPermissions";
16​
17export type InlineCommentParams = PullRequestParams & {
18 readonly draftId?: string;
19 readonly path: string;
20 readonly oldPath?: string;
21 readonly position: PullRequestReviewPosition;
22 readonly headSha: string;
23 readonly code: string;
24};
25export function PullRequestInlineCommentScreen({ route }: StaticScreenProps<InlineCommentParams>) {
26 const params = route.params;
27 const navigation = useNavigation();
28 const store = usePullRequestReviewDraft(params);
29 const saved = store.draft.comments.find((draft) => draft.id === params.draftId);
30 const [editedBody, setBody] = useState<string | null>(null);
31 const body = editedBody ?? saved?.body ?? "";
32 const detail = useEnvironmentQuery(composerPullRequests.detail(pullRequestTarget(params)));
33 const { refresh: refreshDetail } = detail;
34 useFocusEffect(useCallback(() => refreshDetail(), [refreshDetail]));
35 const permissions = usePullRequestPermissions(params, detail.data);
36 const stale = detail.data && detail.data.headSha !== params.headSha;
37 const pending = useRef(false);
38 const [busy, setBusy] = useState(false);
39 const save = async () => {
40 if (
41 pending.current ||
42 !store.ready ||
43 !body.trim() ||
44 detail.isPending ||
45 detail.error ||
46 stale ||
47 !permissions.canInlineComment
48 )
49 return;
50 pending.current = true;
51 setBusy(true);
52 store.update((draft) => ({
53 ...draft,
54 comments: [
55 ...draft.comments.filter((comment) => comment.id !== params.draftId),
56 {
57 id: params.draftId ?? uuidv4(),
58 path: params.path,
59 ...(params.oldPath ? { oldPath: params.oldPath } : {}),
60 position: params.position,
61 headSha: params.headSha,
62 code: params.code,
63 body,
64 },
65 ],
66 }));
67 try {
68 await store.flush();
69 navigation.goBack();
70 } finally {
71 pending.current = false;
72 setBusy(false);
73 }
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 );