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

useReviewCommentSelectionController.ts

148 lines4.4 KB

1import { useGT, useMessages } from "gt-react-native";
2import { useState } from "react";
3import type { NativeSyntheticEvent } from "react-native";
4import { useNavigation } from "@react-navigation/native";
5import * as Arr from "effect/Array";
6import { pipe } from "effect/Function";
7import * as Result from "effect/Result";
8​
9import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
10import { scopedThreadKey } from "../../lib/scopedEntities";
11import {
12 buildReviewCommentTarget,
13 clearReviewCommentTarget,
14 completeReviewCommentRange,
15 formatReviewSelectedRangeLabel,
16 reviewCommentRangeText,
17 getSelectedReviewCommentLines,
18 nextReviewSheetId,
19 reviewCommentSelectionScope,
20 setReviewCommentTarget,
21 startReviewCommentRange,
22 useReviewCommentSelection,
23} from "./reviewCommentSelection";
24import type { NativeReviewDiffData } from "./nativeReviewDiffAdapter";
25import type { ReviewSectionItem } from "./reviewModel";
26​
27export function useReviewCommentSelectionController(input: {
28 readonly environmentId?: EnvironmentId;
29 readonly threadId?: ThreadId;
30 readonly selectedSection: ReviewSectionItem | null;
31 readonly nativeReviewDiffData: NativeReviewDiffData;
32}) {
33 const { environmentId, nativeReviewDiffData, selectedSection, threadId } = input;
34 const gt = useGT();
35 const messages = useMessages();
36 const navigation = useNavigation();
37 const [sheetId] = useState(nextReviewSheetId);
38 // Scoping the selection to this sheet, thread, section and diff drops it when the sheet
39 // reopens, either changes, or a section comes back with different lines.
40 const scope = reviewCommentSelectionScope(
41 sheetId,
42 environmentId && threadId ? scopedThreadKey(environmentId, threadId) : null,
43 selectedSection === null
44 ? null
45 : {
46 id: selectedSection.id,
47 content: selectedSection.source?.diffHash ?? selectedSection.diff,
48 },
49 );
50 const { target: activeCommentTarget, pending: pendingRange } = useReviewCommentSelection(scope);
51 const hasRangeTarget =
52 activeCommentTarget !== null && activeCommentTarget.startIndex !== activeCommentTarget.endIndex;
53​
54 const openReviewCommentSheet = () => {
55 if (!environmentId || !threadId) {
56 return;
57 }
58​
59 navigation.navigate("ThreadReviewComment", {
60 environmentId,
61 threadId,
62 });
63 };
64​
65 const selectedRowIds = hasRangeTarget
66 ? pipe(
67 getSelectedReviewCommentLines(activeCommentTarget),
68 Arr.filterMap((line) => {
69 const rowId = nativeReviewDiffData.rowIdByCommentLineId.get(line.id);
70 return rowId ? Result.succeed(rowId) : Result.failVoid;
71 }),
72 )
73 : pendingRange
74 ? [pendingRange.rowId]
75 : [];
76​
77 const selectionAction = hasRangeTarget
78 ? {
79 title: gt`Comment on ${{
80 range: reviewCommentRangeText(
81 formatReviewSelectedRangeLabel(activeCommentTarget),
82 messages,
83 ),
84 }}`,
85 onOpenComment: openReviewCommentSheet,
86 }
87 : pendingRange
88 ? { title: gt`Select range end`, onOpenComment: null }
89 : null;
90​
91 const onPressLine = (
92 event: NativeSyntheticEvent<{
93 readonly rowId?: string;
94 readonly gesture?: "tap" | "longPress";
95 }>,
96 ) => {
97 if (!selectedSection || scope === null) {
98 return;
99 }
100​
101 const { rowId, gesture } = event.nativeEvent;
102 if (!rowId) {
103 return;
104 }
105​
106 const target = nativeReviewDiffData.commentTargetsByRowId.get(rowId);
107 if (!target) {
108 return;
109 }
110​
111 if (gesture === "longPress") {
112 startReviewCommentRange(scope, {
113 sectionId: selectedSection.id,
114 sectionTitle: selectedSection.title,
115 filePath: target.filePath,
116 lines: target.lines,
117 lineIndex: target.lineIndex,
118 rowId,
119 });
120 return;
121 }
122​
123 if (pendingRange && pendingRange.filePath === target.filePath) {
124 completeReviewCommentRange(
125 scope,
126 buildReviewCommentTarget(pendingRange, pendingRange.lineIndex, target.lineIndex),
127 );
128 return;
129 }
130​
131 setReviewCommentTarget(scope, {
132 sectionTitle: selectedSection.title,
133 sectionId: selectedSection.id,
134 filePath: target.filePath,
135 lines: target.lines,
136 startIndex: target.lineIndex,
137 endIndex: target.lineIndex,
138 });
139 openReviewCommentSheet();
140 };
141​
142 return {
143 selectedRowIds,
144 selectionAction,
145 onPressLine,
146 clearSelection: clearReviewCommentTarget,
147 };