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