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

useReviewCommentSelectionController.ts

139 lines4.1 KB

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