Open History to see the last commit
1import { useCallback, useEffect, useMemo, useState } from "react";
2import type { NativeSyntheticEvent } from "react-native";
3import { useRouter } from "expo-router";
4import * as Arr from "effect/Array";
5import { pipe } from "effect/Function";
6import * as Result from "effect/Result";
7
8import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
9
10import {
11 buildReviewCommentTarget,
12 clearReviewCommentTarget,
13 formatReviewSelectedRangeLabel,
14 getSelectedReviewCommentLines,
15 setReviewCommentTarget,
16 useReviewCommentTarget,
17} from "./reviewCommentSelection";
18import type {
19 NativeReviewDiffData,
20 NativeReviewDiffCommentTarget,
21} from "./nativeReviewDiffAdapter";
22import type { ReviewSectionItem } from "./reviewModel";
23
24interface PendingNativeCommentSelection extends NativeReviewDiffCommentTarget {
25 readonly sectionId: string;
26 readonly sectionTitle: string;
27 readonly rowId: string;
28}
29
30export function useReviewCommentSelectionController(input: {
31 readonly environmentId?: EnvironmentId;
32 readonly threadId?: ThreadId;
33 readonly selectedSection: ReviewSectionItem | null;
34 readonly nativeReviewDiffData: NativeReviewDiffData;
35}) {
36 const { environmentId, nativeReviewDiffData, selectedSection, threadId } = input;
37 const { push } = useRouter();
38 const activeCommentTarget = useReviewCommentTarget();
39 const [pendingNativeCommentSelection, setPendingNativeCommentSelection] =
40 useState<PendingNativeCommentSelection | null>(null);
41
42 const openReviewCommentSheet = useCallback(() => {
43 if (!environmentId || !threadId) {
44 return;
45 }
46
47 push({
48 pathname: "/threads/[environmentId]/[threadId]/review-comment",
49 params: { environmentId, threadId },
50 });
51 }, [environmentId, push, threadId]);
52
53 const selectedRowIds = useMemo(() => {
54 if (
55 activeCommentTarget &&
56 activeCommentTarget.sectionTitle === selectedSection?.title &&
57 activeCommentTarget.startIndex !== activeCommentTarget.endIndex
58 ) {
59 return pipe(
60 getSelectedReviewCommentLines(activeCommentTarget),
61 Arr.filterMap((line) => {
62 const rowId = nativeReviewDiffData.rowIdByCommentLineId.get(line.id);
63 return rowId ? Result.succeed(rowId) : Result.failVoid;
64 }),
65 );
66 }
67
68 return pendingNativeCommentSelection ? [pendingNativeCommentSelection.rowId] : [];
69 }, [
70 activeCommentTarget,
71 nativeReviewDiffData.rowIdByCommentLineId,
72 pendingNativeCommentSelection,
73 selectedSection?.title,
74 ]);
75
76 const selectionAction = useMemo(() => {
77 if (
78 activeCommentTarget &&
79 activeCommentTarget.sectionTitle === selectedSection?.title &&
80 activeCommentTarget.startIndex !== activeCommentTarget.endIndex
81 ) {
82 return {
83 title: `Comment on ${formatReviewSelectedRangeLabel(activeCommentTarget)}`,
84 onOpenComment: openReviewCommentSheet,
85 };
86 }
87
88 if (
89 pendingNativeCommentSelection &&
90 pendingNativeCommentSelection.sectionTitle === selectedSection?.title
91 ) {
92 return {
93 title: "Select range end",
94 onOpenComment: null,
95 };
96 }
97
98 return null;
99 }, [
100 activeCommentTarget,
101 openReviewCommentSheet,
102 pendingNativeCommentSelection,
103 selectedSection?.title,
104 ]);
105
106 useEffect(() => {
107 clearReviewCommentTarget();
108 setPendingNativeCommentSelection(null);
109 }, [selectedSection?.id]);
110
111 useEffect(() => {
112 if (activeCommentTarget === null) {
113 setPendingNativeCommentSelection(null);
114 }
115 }, [activeCommentTarget]);
116
117 const onPressLine = useCallback(
118 (
119 event: NativeSyntheticEvent<{
120 readonly rowId?: string;
121 readonly gesture?: "tap" | "longPress";
122 }>,
123 ) => {
124 if (!selectedSection) {
125 return;
126 }
127
128 const { rowId, gesture } = event.nativeEvent;
129 if (!rowId) {
130 return;
131 }
132
133 const target = nativeReviewDiffData.commentTargetsByRowId.get(rowId);
134 if (!target) {
135 return;
136 }
137
138 if (gesture === "longPress") {
139 clearReviewCommentTarget();
140 setPendingNativeCommentSelection({
141 ...target,
142 sectionId: selectedSection.id,
143 sectionTitle: selectedSection.title,
144 rowId,
145 });
146 return;
147 }
148
149 if (
150 pendingNativeCommentSelection &&
151 pendingNativeCommentSelection.sectionTitle === selectedSection.title &&
152 pendingNativeCommentSelection.filePath === target.filePath
153 ) {
154 setReviewCommentTarget(
155 buildReviewCommentTarget(
156 {
157 sectionTitle: pendingNativeCommentSelection.sectionTitle,
158 sectionId: pendingNativeCommentSelection.sectionId,
159 filePath: pendingNativeCommentSelection.filePath,
160 lines: pendingNativeCommentSelection.lines,
161 },
162 pendingNativeCommentSelection.lineIndex,
163 target.lineIndex,
164 ),
165 );
166 return;
167 }
168
169 setPendingNativeCommentSelection(null);
170 setReviewCommentTarget({
171 sectionTitle: selectedSection.title,
172 sectionId: selectedSection.id,
173 filePath: target.filePath,
174 lines: target.lines,
175 startIndex: target.lineIndex,
176 endIndex: target.lineIndex,
177 });
178 openReviewCommentSheet();
179 },
180 [
181 nativeReviewDiffData.commentTargetsByRowId,
182 openReviewCommentSheet,
183 pendingNativeCommentSelection,
184 selectedSection,
185 ],
186 );
187
188 const clearSelection = useCallback(() => {
189 clearReviewCommentTarget();
190 setPendingNativeCommentSelection(null);
191 }, []);
192
193 return {
194 selectedRowIds,
195 selectionAction,
196 onPressLine,
197 clearSelection,
198 };
199}