@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 158 lines 4.44 KB
Raw
1 import { useLocale } from "gt-react-native";
2 import { useEffect } from "react";
3
4 import { getCachedNativeReviewDiffData } from "./nativeReviewDiffAdapter";
5 import type { ReviewSectionItem } from "./reviewModel";
6 import {
7 getCachedReviewParsedDiff,
8 getReviewParsedDiffSourceCharacterCount,
9 MAX_CACHED_REVIEW_DIFFS,
10 MAX_CACHED_REVIEW_SOURCE_CHARACTERS,
11 } from "./reviewState";
12
13 interface IdleDeadlineLike {
14 readonly didTimeout: boolean;
15 timeRemaining(): number;
16 }
17
18 type IdleCallback = (deadline: IdleDeadlineLike) => void;
19
20 function scheduleIdle(callback: IdleCallback): number {
21 if (typeof globalThis.requestIdleCallback === "function") {
22 return globalThis.requestIdleCallback(callback, { timeout: 2_000 });
23 }
24
25 return setTimeout(
26 () => callback({ didTimeout: true, timeRemaining: () => 0 }),
27 100,
28 ) as unknown as number;
29 }
30
31 function cancelIdle(handle: number): void {
32 if (typeof globalThis.cancelIdleCallback === "function") {
33 globalThis.cancelIdleCallback(handle);
34 return;
35 }
36 clearTimeout(handle);
37 }
38
39 function prewarmReviewDiffSection(input: {
40 readonly threadKey: string;
41 readonly section: ReviewSectionItem;
42 readonly locale: string;
43 }): void {
44 const { section, threadKey, locale } = input;
45 if (section.diff === null || section.diff.length > MAX_CACHED_REVIEW_SOURCE_CHARACTERS) {
46 return;
47 }
48
49 const parsedDiff = getCachedReviewParsedDiff({
50 threadKey,
51 sectionId: section.id,
52 diff: section.diff,
53 locale,
54 });
55 getCachedNativeReviewDiffData({ parsedDiff, comments: [] });
56 }
57
58 function getReviewDiffPrewarmSections(input: {
59 readonly threadKey: string;
60 readonly sections: ReadonlyArray<ReviewSectionItem>;
61 readonly selectedSectionId: string | null;
62 }): ReadonlyArray<ReviewSectionItem> {
63 const { threadKey, sections, selectedSectionId } = input;
64 const selectedIndex = sections.findIndex((section) => section.id === selectedSectionId);
65 const selectedSection = sections[selectedIndex];
66 if (!selectedSection) {
67 return [];
68 }
69
70 let sourceCharacterCount = getReviewParsedDiffSourceCharacterCount({
71 threadKey,
72 sectionId: selectedSection.id,
73 diff: selectedSection.diff,
74 });
75 if (sourceCharacterCount > MAX_CACHED_REVIEW_SOURCE_CHARACTERS) {
76 return [];
77 }
78
79 const pendingSections: ReviewSectionItem[] = [];
80 for (let distance = 1; distance < sections.length; distance += 1) {
81 for (const index of [selectedIndex - distance, selectedIndex + distance]) {
82 if (pendingSections.length >= MAX_CACHED_REVIEW_DIFFS - 1) {
83 return pendingSections;
84 }
85 const section = sections[index];
86 if (!section || section.diff === null) {
87 continue;
88 }
89 const sectionCharacterCount = getReviewParsedDiffSourceCharacterCount({
90 threadKey,
91 sectionId: section.id,
92 diff: section.diff,
93 });
94 if (sourceCharacterCount + sectionCharacterCount > MAX_CACHED_REVIEW_SOURCE_CHARACTERS) {
95 continue;
96 }
97 pendingSections.push(section);
98 sourceCharacterCount += sectionCharacterCount;
99 }
100 }
101 return pendingSections;
102 }
103
104 export function useReviewDiffPrewarming(input: {
105 readonly threadKey: string | null;
106 readonly sections: ReadonlyArray<ReviewSectionItem>;
107 readonly selectedSectionId: string | null;
108 }): void {
109 const { sections, selectedSectionId, threadKey } = input;
110 const locale = useLocale();
111
112 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- prewarms pending review diff sections in idle callbacks and cancels the idle handle on cleanup
113 useEffect(() => {
114 if (!threadKey) {
115 return;
116 }
117
118 const pendingSections = getReviewDiffPrewarmSections({
119 threadKey,
120 sections,
121 selectedSectionId,
122 });
123 if (pendingSections.length === 0) {
124 return;
125 }
126
127 let cancelled = false;
128 let idleHandle: number | null = null;
129 let nextSectionIndex = 0;
130
131 const scheduleNext = () => {
132 idleHandle = scheduleIdle(() => {
133 if (cancelled) {
134 return;
135 }
136
137 const section = pendingSections[nextSectionIndex];
138 if (!section) {
139 return;
140 }
141 nextSectionIndex += 1;
142 prewarmReviewDiffSection({ threadKey, section, locale });
143
144 if (nextSectionIndex < pendingSections.length) {
145 scheduleNext();
146 }
147 });
148 };
149
150 scheduleNext();
151 return () => {
152 cancelled = true;
153 if (idleHandle !== null) {
154 cancelIdle(idleHandle);
155 }
156 };
157 }, [sections, selectedSectionId, threadKey, locale]);
158 }