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
| 1 | import { msg, useLocale, useMessages } from "gt-react-native"; |
| 2 | import { useCallback, useContext, useEffect, useMemo, useState } from "react"; |
| 3 | import { formatDiffCount } from "@t3tools/client-runtime/diff-count"; |
| 4 | |
| 5 | import { parseReviewInlineComments } from "./reviewCommentSelection"; |
| 6 | import { getCachedNativeReviewDiffData } from "./nativeReviewDiffAdapter"; |
| 7 | import { getCachedReviewParsedDiff } from "./reviewState"; |
| 8 | import { |
| 9 | applyReviewDiffMetadata, |
| 10 | buildReviewParsedDiff, |
| 11 | type ReviewParsedDiff, |
| 12 | type ReviewRenderableFile, |
| 13 | type ReviewSectionItem, |
| 14 | } from "./reviewModel"; |
| 15 | |
| 16 | import type { |
| 17 | EnvironmentId, |
| 18 | OrchestrationMessageContext, |
| 19 | ReviewDiffFileStat, |
| 20 | ReviewDiffPreviewSource, |
| 21 | } from "@t3tools/contracts"; |
| 22 | import { RegistryContext, useAtomValue } from "@effect/atom-react"; |
| 23 | import * as AsyncResult from "effect/reactivity/AsyncResult"; |
| 24 | import * as Atom from "effect/reactivity/Atom"; |
| 25 | import { reviewEnvironment } from "../../state/review"; |
| 26 | import type { MessageResolver } from "../../i18n/messages"; |
| 27 | |
| 28 | const EMPTY_INLINE_REVIEW_COMMENTS = Object.freeze([]); |
| 29 | type ParsedFilePatch = AsyncResult.AsyncResult< |
| 30 | { source: ReviewDiffPreviewSource; parsed: ReviewParsedDiff }, |
| 31 | unknown |
| 32 | >; |
| 33 | const normalizedFiles = new WeakMap< |
| 34 | ParsedFilePatch, |
| 35 | { |
| 36 | stat: ReviewDiffFileStat; |
| 37 | diffHash: string; |
| 38 | locale: string; |
| 39 | file: ReviewRenderableFile; |
| 40 | } |
| 41 | >(); |
| 42 | |
| 43 | function getCachedReviewFile( |
| 44 | stat: ReviewDiffFileStat, |
| 45 | diffHash: string, |
| 46 | patch: ParsedFilePatch | null | undefined, |
| 47 | resolve: MessageResolver, |
| 48 | locale: string, |
| 49 | ): ReviewRenderableFile { |
| 50 | const cached = patch && normalizedFiles.get(patch); |
| 51 | if (cached && cached.stat === stat && cached.diffHash === diffHash && cached.locale === locale) |
| 52 | return cached.file; |
| 53 | const parsed = patch?._tag === "Success" ? patch.value.parsed : null; |
| 54 | const sourceFiles = patch?._tag === "Success" ? patch.value.source.files : undefined; |
| 55 | const loaded = |
| 56 | parsed?.kind === "files" |
| 57 | ? (parsed.files.find((file) => file.path === stat.path) ?? |
| 58 | (parsed.files.length === 1 && |
| 59 | sourceFiles?.length === 1 && |
| 60 | sourceFiles[0]?.path === stat.path |
| 61 | ? parsed.files[0] |
| 62 | : undefined)) |
| 63 | : undefined; |
| 64 | const file: ReviewRenderableFile = { |
| 65 | ...(loaded ?? { |
| 66 | path: stat.path, |
| 67 | previousPath: stat.previousPath, |
| 68 | changeType: stat.changeType ?? ("change" as const), |
| 69 | languageHint: null, |
| 70 | additionLines: [], |
| 71 | deletionLines: [], |
| 72 | rows: [], |
| 73 | cacheKey: `${diffHash}:${stat.path}`, |
| 74 | }), |
| 75 | id: stat.path, |
| 76 | path: stat.path, |
| 77 | previousPath: stat.previousPath, |
| 78 | additions: stat.additions, |
| 79 | deletions: stat.deletions, |
| 80 | ...(patch?._tag === "Success" |
| 81 | ? patch.value.source.truncated |
| 82 | ? { |
| 83 | notice: resolve( |
| 84 | msg("File preview exceeds the size limit. Counts include all changes."), |
| 85 | ), |
| 86 | } |
| 87 | : loaded |
| 88 | ? {} |
| 89 | : { notice: resolve(msg("Could not display file preview.")) } |
| 90 | : { |
| 91 | notice: |
| 92 | patch?._tag === "Failure" |
| 93 | ? resolve(msg("Could not load diff. Select the file to retry.")) |
| 94 | : resolve(msg("Loading diff…")), |
| 95 | }), |
| 96 | }; |
| 97 | if (patch) normalizedFiles.set(patch, { stat, diffHash, locale, file }); |
| 98 | return file; |
| 99 | } |
| 100 | |
| 101 | function isReviewDiffDebugLoggingEnabled(): boolean { |
| 102 | return typeof __DEV__ !== "undefined" ? __DEV__ : false; |
| 103 | } |
| 104 | |
| 105 | function logReviewDiffDiagnostic(message: string, details?: Record<string, unknown>): void { |
| 106 | if (!isReviewDiffDebugLoggingEnabled()) { |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | if (details) { |
| 111 | console.log(`[review-sheet] ${message}`, details); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | console.log(`[review-sheet] ${message}`); |
| 116 | } |
| 117 | |
| 118 | function formatHeaderDiffSummary( |
| 119 | parsedDiff: ReviewParsedDiff, |
| 120 | files?: ReviewSectionItem["files"], |
| 121 | ): { |
| 122 | readonly additions: string | null; |
| 123 | readonly deletions: string | null; |
| 124 | } { |
| 125 | if (files) { |
| 126 | return { |
| 127 | additions: `+${formatDiffCount(files.reduce((total, file) => total + file.additions, 0))}`, |
| 128 | deletions: `-${formatDiffCount(files.reduce((total, file) => total + file.deletions, 0))}`, |
| 129 | }; |
| 130 | } |
| 131 | if (parsedDiff.kind !== "files") return { additions: null, deletions: null }; |
| 132 | return { |
| 133 | additions: `+${formatDiffCount(parsedDiff.additions)}`, |
| 134 | deletions: `-${formatDiffCount(parsedDiff.deletions)}`, |
| 135 | }; |
| 136 | } |
| 137 | |
| 138 | export function useReviewDiffData(input: { |
| 139 | readonly threadKey: string | null; |
| 140 | readonly environmentId: EnvironmentId | undefined; |
| 141 | readonly cwd: string | null; |
| 142 | readonly selectedSection: ReviewSectionItem | null; |
| 143 | readonly draftMessage: string; |
| 144 | readonly draftContext: OrchestrationMessageContext | undefined; |
| 145 | }) { |
| 146 | const { draftMessage, draftContext, selectedSection, threadKey } = input; |
| 147 | const messages = useMessages(); |
| 148 | const locale = useLocale(); |
| 149 | const selectedSectionId = selectedSection?.id ?? null; |
| 150 | const source = selectedSection?.source; |
| 151 | const lazySource = source?.truncated && source.files ? source : null; |
| 152 | const previewDiff: ReviewParsedDiff = lazySource |
| 153 | ? { kind: "empty" } |
| 154 | : getCachedReviewParsedDiff({ |
| 155 | threadKey, |
| 156 | sectionId: selectedSection?.id ?? null, |
| 157 | diff: selectedSection?.diff, |
| 158 | resolve: messages, |
| 159 | locale, |
| 160 | }); |
| 161 | const registry = useContext(RegistryContext); |
| 162 | const { environmentId, cwd } = input; |
| 163 | const scope = JSON.stringify([environmentId, cwd, lazySource?.kind, source?.baseRef]); |
| 164 | const [requested, setRequested] = useState({ scope, indices: [0, 1, 2] }); |
| 165 | const indices = useMemo( |
| 166 | () => new Set(requested.scope === scope ? requested.indices : [0, 1, 2]), |
| 167 | [requested, scope], |
| 168 | ); |
| 169 | const queries = useMemo( |
| 170 | () => |
| 171 | !environmentId || !cwd || !lazySource |
| 172 | ? [] |
| 173 | : (lazySource.files ?? []).map((file, index) => |
| 174 | indices.has(index) |
| 175 | ? reviewEnvironment.diffFilePatch({ |
| 176 | environmentId, |
| 177 | input: { |
| 178 | cacheKey: scope, |
| 179 | // Newer preview revisions refresh patches read under an older one. |
| 180 | preview: { cwd }, |
| 181 | request: { |
| 182 | cwd, |
| 183 | ...(lazySource.baseRef ? { baseRef: lazySource.baseRef } : {}), |
| 184 | file: { |
| 185 | path: file.path, |
| 186 | previousPath: file.previousPath, |
| 187 | sourceKind: lazySource.kind, |
| 188 | }, |
| 189 | }, |
| 190 | }, |
| 191 | }) |
| 192 | : null, |
| 193 | ), |
| 194 | [environmentId, cwd, lazySource, indices, scope], |
| 195 | ); |
| 196 | const parsedQuery = useMemo( |
| 197 | () => |
| 198 | Atom.family((query: ReturnType<typeof reviewEnvironment.diffFilePatch>) => |
| 199 | Atom.map(query, (result) => |
| 200 | AsyncResult.map(result, (source) => ({ |
| 201 | source, |
| 202 | parsed: buildReviewParsedDiff(source.diff, source.diffHash, messages), |
| 203 | })), |
| 204 | ), |
| 205 | ), |
| 206 | [messages], |
| 207 | ); |
| 208 | const patches = useAtomValue( |
| 209 | useMemo( |
| 210 | () => Atom.make((get) => queries.map((query) => (query ? get(parsedQuery(query)) : null))), |
| 211 | [queries, parsedQuery], |
| 212 | ), |
| 213 | ); |
| 214 | const loadVisibleFile = useCallback( |
| 215 | (fileId: string | null, retry = false) => { |
| 216 | const index = |
| 217 | fileId === null ? 0 : (lazySource?.files?.findIndex((file) => file.path === fileId) ?? -1); |
| 218 | if (index < 0) return; |
| 219 | setRequested((current) => { |
| 220 | const previous = current.scope === scope ? current.indices : [0, 1, 2]; |
| 221 | const added = [index, index + 1, index + 2].filter((next) => !previous.includes(next)); |
| 222 | return added.length === 0 ? current : { scope, indices: [...previous, ...added] }; |
| 223 | }); |
| 224 | if (retry && patches[index]?._tag === "Failure" && queries[index]) |
| 225 | registry.refresh(queries[index]); |
| 226 | }, |
| 227 | [lazySource, scope, patches, queries, registry], |
| 228 | ); |
| 229 | const parsedDiff = useMemo<ReviewParsedDiff>(() => { |
| 230 | if (!lazySource?.files) return applyReviewDiffMetadata(previewDiff, selectedSection, messages); |
| 231 | const files = lazySource.files.map((stat, index) => |
| 232 | getCachedReviewFile(stat, lazySource.diffHash, patches[index], messages, locale), |
| 233 | ); |
| 234 | return { |
| 235 | kind: "files", |
| 236 | files, |
| 237 | fileCount: files.length, |
| 238 | additions: files.reduce((total, file) => total + file.additions, 0), |
| 239 | deletions: files.reduce((total, file) => total + file.deletions, 0), |
| 240 | notice: null, |
| 241 | }; |
| 242 | }, [lazySource, previewDiff, selectedSection, patches, messages, locale]); |
| 243 | const headerDiffSummary = useMemo( |
| 244 | () => formatHeaderDiffSummary(parsedDiff, selectedSection?.files), |
| 245 | [parsedDiff, selectedSection?.files], |
| 246 | ); |
| 247 | const inlineReviewComments = parseReviewInlineComments(draftMessage, draftContext, messages); |
| 248 | const selectedSectionInlineComments = useMemo(() => { |
| 249 | if (!selectedSectionId || inlineReviewComments.length === 0) { |
| 250 | return EMPTY_INLINE_REVIEW_COMMENTS; |
| 251 | } |
| 252 | return inlineReviewComments.filter((comment) => comment.sectionId === selectedSectionId); |
| 253 | }, [inlineReviewComments, selectedSectionId]); |
| 254 | const nativeReviewDiffData = getCachedNativeReviewDiffData({ |
| 255 | parsedDiff, |
| 256 | comments: selectedSectionInlineComments, |
| 257 | }); |
| 258 | const pendingReviewCommentCount = inlineReviewComments.length; |
| 259 | |
| 260 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- records review perf marks and diagnostic logs once a parsed diff has committed |
| 261 | useEffect(() => { |
| 262 | if (parsedDiff.kind !== "files") { |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | logReviewDiffDiagnostic("parsed diff files", { |
| 267 | selectedSectionId: selectedSection?.id ?? null, |
| 268 | fileCount: parsedDiff.fileCount, |
| 269 | renderableFileCount: parsedDiff.files.length, |
| 270 | }); |
| 271 | }, [parsedDiff, selectedSection?.id]); |
| 272 | |
| 273 | return { |
| 274 | parsedDiff, |
| 275 | loadVisibleFile, |
| 276 | isPending: patches.some((patch) => patch?._tag === "Initial" || patch?.waiting), |
| 277 | headerDiffSummary, |
| 278 | nativeReviewDiffData, |
| 279 | pendingReviewCommentCount, |
| 280 | }; |
| 281 | } |