@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

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