| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {Atom, Getter} from 'jotai'; |
| b69ab31 | | | 9 | import type {Loadable} from 'jotai/vanilla/utils/loadable'; |
| b69ab31 | | | 10 | import type {CommitInfo, RepoRelativePath, SlocInfo} from '../types'; |
| b69ab31 | | | 11 | |
| b69ab31 | | | 12 | import {atom, useAtomValue} from 'jotai'; |
| b69ab31 | | | 13 | import {loadable} from 'jotai/utils'; |
| b69ab31 | | | 14 | import {useEffect, useMemo, useRef, useState} from 'react'; |
| b69ab31 | | | 15 | import serverAPI from '../ClientToServerAPI'; |
| b69ab31 | | | 16 | import {commitInfoViewCurrentCommits} from '../CommitInfoView/CommitInfoState'; |
| b69ab31 | | | 17 | import {getGeneratedFilesFrom} from '../GeneratedFile'; |
| b69ab31 | | | 18 | import {pageVisibility} from '../codeReview/CodeReviewInfo'; |
| b69ab31 | | | 19 | import {atomFamilyWeak, lazyAtom} from '../jotaiUtils'; |
| b69ab31 | | | 20 | import {isFullyOrPartiallySelected} from '../partialSelection'; |
| b69ab31 | | | 21 | import {uncommittedChangesWithPreviews} from '../previews'; |
| b69ab31 | | | 22 | import {commitByHash} from '../serverAPIState'; |
| b69ab31 | | | 23 | import {GeneratedStatus} from '../types'; |
| b69ab31 | | | 24 | import {arraysEqual} from '../utils'; |
| b69ab31 | | | 25 | import {MAX_FILES_ALLOWED_FOR_DIFF_STAT} from './diffStatConstants'; |
| b69ab31 | | | 26 | |
| b69ab31 | | | 27 | const isPageHiddenAtom = atom(get => get(pageVisibility) === 'hidden'); |
| b69ab31 | | | 28 | |
| b69ab31 | | | 29 | const getGeneratedFiles = (files: ReadonlyArray<RepoRelativePath>): Array<RepoRelativePath> => { |
| b69ab31 | | | 30 | const generatedStatuses = getGeneratedFilesFrom(files); |
| b69ab31 | | | 31 | |
| b69ab31 | | | 32 | return files.reduce<string[]>((filtered, path) => { |
| b69ab31 | | | 33 | // check if the file should be excluded |
| b69ab31 | | | 34 | // the __generated__ pattern is included in the exclusions, so we don't need to include it here |
| b69ab31 | | | 35 | if (path.match(/__generated__/) || generatedStatuses[path] === GeneratedStatus.Generated) { |
| b69ab31 | | | 36 | filtered.push(path); |
| b69ab31 | | | 37 | } |
| b69ab31 | | | 38 | |
| b69ab31 | | | 39 | return filtered; |
| b69ab31 | | | 40 | }, []); |
| b69ab31 | | | 41 | }; |
| b69ab31 | | | 42 | |
| b69ab31 | | | 43 | const filterGeneratedFiles = (files: ReadonlyArray<RepoRelativePath>): Array<RepoRelativePath> => { |
| b69ab31 | | | 44 | const generatedStatuses = getGeneratedFilesFrom(files); |
| b69ab31 | | | 45 | |
| b69ab31 | | | 46 | return files.filter( |
| b69ab31 | | | 47 | path => !path.match(/__generated__/) && generatedStatuses[path] !== GeneratedStatus.Generated, |
| b69ab31 | | | 48 | ); |
| b69ab31 | | | 49 | }; |
| b69ab31 | | | 50 | |
| b69ab31 | | | 51 | async function fetchSignificantLinesOfCode( |
| b69ab31 | | | 52 | commit: Readonly<CommitInfo>, |
| b69ab31 | | | 53 | additionalFilesToExclude: ReadonlyArray<RepoRelativePath> = [], |
| b69ab31 | | | 54 | getExcludedFiles: ( |
| b69ab31 | | | 55 | files: ReadonlyArray<RepoRelativePath>, |
| b69ab31 | | | 56 | ) => Array<RepoRelativePath> = getGeneratedFiles, |
| b69ab31 | | | 57 | ): Promise<SlocInfo> { |
| b69ab31 | | | 58 | const filesToQueryGeneratedStatus = commit.filePathsSample; |
| b69ab31 | | | 59 | const excludedFiles = getExcludedFiles(filesToQueryGeneratedStatus); |
| b69ab31 | | | 60 | |
| b69ab31 | | | 61 | serverAPI.postMessage({ |
| b69ab31 | | | 62 | type: 'fetchSignificantLinesOfCode', |
| b69ab31 | | | 63 | hash: commit.hash, |
| b69ab31 | | | 64 | excludedFiles: [...excludedFiles, ...additionalFilesToExclude], |
| b69ab31 | | | 65 | }); |
| b69ab31 | | | 66 | |
| b69ab31 | | | 67 | const slocData = await serverAPI |
| b69ab31 | | | 68 | .nextMessageMatching('fetchedSignificantLinesOfCode', message => message.hash === commit.hash) |
| b69ab31 | | | 69 | .then(result => ({ |
| b69ab31 | | | 70 | sloc: result.result.value, |
| b69ab31 | | | 71 | })); |
| b69ab31 | | | 72 | |
| b69ab31 | | | 73 | return slocData; |
| b69ab31 | | | 74 | } |
| b69ab31 | | | 75 | |
| b69ab31 | | | 76 | const commitSlocFamily = atomFamilyWeak((hash: string) => { |
| b69ab31 | | | 77 | return lazyAtom(async get => { |
| b69ab31 | | | 78 | const commit = get(commitByHash(hash)); |
| b69ab31 | | | 79 | if (commit == null) { |
| b69ab31 | | | 80 | return undefined; |
| b69ab31 | | | 81 | } |
| b69ab31 | | | 82 | if (commit.totalFileCount > MAX_FILES_ALLOWED_FOR_DIFF_STAT) { |
| b69ab31 | | | 83 | return undefined; |
| b69ab31 | | | 84 | } |
| b69ab31 | | | 85 | if (commit.optimisticRevset != null) { |
| b69ab31 | | | 86 | return undefined; |
| b69ab31 | | | 87 | } |
| b69ab31 | | | 88 | const sloc = await fetchSignificantLinesOfCode(commit); |
| b69ab31 | | | 89 | return sloc; |
| b69ab31 | | | 90 | }, undefined); |
| b69ab31 | | | 91 | }); |
| b69ab31 | | | 92 | |
| b69ab31 | | | 93 | let previouslySelectedFiles: string[] = []; |
| b69ab31 | | | 94 | |
| b69ab31 | | | 95 | const selectedFilesAtom = atom(get => { |
| b69ab31 | | | 96 | const isPathFullorPartiallySelected = get(isFullyOrPartiallySelected); |
| b69ab31 | | | 97 | |
| b69ab31 | | | 98 | const uncommittedChanges = get(uncommittedChangesWithPreviews); |
| b69ab31 | | | 99 | const selectedFiles = uncommittedChanges.reduce((selected, f) => { |
| b69ab31 | | | 100 | if (!f.path.match(/__generated__/) && isPathFullorPartiallySelected(f.path)) { |
| b69ab31 | | | 101 | selected.push(f.path); |
| b69ab31 | | | 102 | } |
| b69ab31 | | | 103 | return selected; |
| b69ab31 | | | 104 | }, [] as string[]); |
| b69ab31 | | | 105 | |
| b69ab31 | | | 106 | if (!arraysEqual(previouslySelectedFiles, selectedFiles)) { |
| b69ab31 | | | 107 | previouslySelectedFiles = selectedFiles; |
| b69ab31 | | | 108 | } |
| b69ab31 | | | 109 | return previouslySelectedFiles; |
| b69ab31 | | | 110 | }); |
| b69ab31 | | | 111 | |
| b69ab31 | | | 112 | /** |
| b69ab31 | | | 113 | * FETCH PENDING AMEND SLOC |
| b69ab31 | | | 114 | */ |
| b69ab31 | | | 115 | const fetchPendingAmendSloc = async ( |
| b69ab31 | | | 116 | get: Getter, |
| b69ab31 | | | 117 | includedFiles: string[], |
| b69ab31 | | | 118 | requestId: number, |
| b69ab31 | | | 119 | ): Promise<SlocInfo | undefined> => { |
| b69ab31 | | | 120 | const commits = get(commitInfoViewCurrentCommits); |
| b69ab31 | | | 121 | if (commits == null || commits.length > 1) { |
| b69ab31 | | | 122 | return undefined; |
| b69ab31 | | | 123 | } |
| b69ab31 | | | 124 | const [commit] = commits; |
| b69ab31 | | | 125 | if (commit.totalFileCount > MAX_FILES_ALLOWED_FOR_DIFF_STAT || commit.optimisticRevset != null) { |
| b69ab31 | | | 126 | return undefined; |
| b69ab31 | | | 127 | } |
| b69ab31 | | | 128 | |
| b69ab31 | | | 129 | const filteredFiles = filterGeneratedFiles(includedFiles); |
| b69ab31 | | | 130 | if (filteredFiles.length > MAX_FILES_ALLOWED_FOR_DIFF_STAT) { |
| b69ab31 | | | 131 | return undefined; |
| b69ab31 | | | 132 | } |
| b69ab31 | | | 133 | |
| b69ab31 | | | 134 | if (filteredFiles.length === 0) { |
| b69ab31 | | | 135 | return {sloc: 0}; |
| b69ab31 | | | 136 | } |
| b69ab31 | | | 137 | |
| b69ab31 | | | 138 | //the calculation here is a bit tricky but in nutshell it is: |
| b69ab31 | | | 139 | // SLOC for unselected committed files |
| b69ab31 | | | 140 | // + SLOC for selected files (to be amended) in the commit |
| b69ab31 | | | 141 | // --------------------------------------------------------------------------------------------------- |
| b69ab31 | | | 142 | // => What SLOC would be after you do the amend. |
| b69ab31 | | | 143 | // this way we won't show the split suggestions when the net effect of the amend will actually reduce SLOC (reverting for example) |
| b69ab31 | | | 144 | |
| b69ab31 | | | 145 | //pass in the selected files to be excluded. |
| b69ab31 | | | 146 | const unselectedCommittedSlocInfo = await fetchSignificantLinesOfCode(commit, includedFiles); |
| b69ab31 | | | 147 | |
| b69ab31 | | | 148 | serverAPI.postMessage({ |
| b69ab31 | | | 149 | type: 'fetchPendingAmendSignificantLinesOfCode', |
| b69ab31 | | | 150 | hash: commit.hash, |
| b69ab31 | | | 151 | includedFiles: filteredFiles, |
| b69ab31 | | | 152 | requestId, |
| b69ab31 | | | 153 | }); |
| b69ab31 | | | 154 | |
| b69ab31 | | | 155 | const pendingLoc = await serverAPI |
| b69ab31 | | | 156 | .nextMessageMatching( |
| b69ab31 | | | 157 | 'fetchedPendingAmendSignificantLinesOfCode', |
| b69ab31 | | | 158 | message => message.requestId === requestId && message.hash === commit.hash, |
| b69ab31 | | | 159 | ) |
| b69ab31 | | | 160 | .then(result => ({ |
| b69ab31 | | | 161 | sloc: result.result.value, |
| b69ab31 | | | 162 | })); |
| b69ab31 | | | 163 | |
| b69ab31 | | | 164 | if (unselectedCommittedSlocInfo === undefined) { |
| b69ab31 | | | 165 | return pendingLoc; |
| b69ab31 | | | 166 | } |
| b69ab31 | | | 167 | |
| b69ab31 | | | 168 | if (pendingLoc === undefined) { |
| b69ab31 | | | 169 | return unselectedCommittedSlocInfo; |
| b69ab31 | | | 170 | } |
| b69ab31 | | | 171 | |
| b69ab31 | | | 172 | const slocInfo = { |
| b69ab31 | | | 173 | sloc: (unselectedCommittedSlocInfo.sloc ?? 0) + (pendingLoc.sloc ?? 0), |
| b69ab31 | | | 174 | }; |
| b69ab31 | | | 175 | |
| b69ab31 | | | 176 | return slocInfo; |
| b69ab31 | | | 177 | }; |
| b69ab31 | | | 178 | |
| b69ab31 | | | 179 | /** |
| b69ab31 | | | 180 | * FETCH PENDING SLOC |
| b69ab31 | | | 181 | */ |
| b69ab31 | | | 182 | const fetchPendingSloc = async ( |
| b69ab31 | | | 183 | get: Getter, |
| b69ab31 | | | 184 | includedFiles: string[], |
| b69ab31 | | | 185 | requestId: number, |
| b69ab31 | | | 186 | ): Promise<SlocInfo | undefined> => { |
| b69ab31 | | | 187 | // this atom makes use of the fact that jotai will only use the most recently created request (ignoring older requests) |
| b69ab31 | | | 188 | // to avoid race conditions when the response from an older request is sent after a newer one |
| b69ab31 | | | 189 | // so for example: |
| b69ab31 | | | 190 | // pendingRequestId A (slow) => Server (sleeps 5 sec) |
| b69ab31 | | | 191 | // pendingRequestId B (fast) => Server responds immediately, client updates |
| b69ab31 | | | 192 | // pendingRequestId A (slow) => Server responds, client ignores |
| b69ab31 | | | 193 | |
| b69ab31 | | | 194 | // We don't want to fetch the pending changes if the page is hidden |
| b69ab31 | | | 195 | // Use isPageHiddenAtom instead of pageVisibility directly to avoid |
| b69ab31 | | | 196 | // re-triggering on focus/blur events (transitions between 'focused' and 'visible') |
| b69ab31 | | | 197 | const pageIsHidden = get(isPageHiddenAtom); |
| b69ab31 | | | 198 | const commits = get(commitInfoViewCurrentCommits); |
| b69ab31 | | | 199 | |
| b69ab31 | | | 200 | if (pageIsHidden || commits == null || commits.length > 1) { |
| b69ab31 | | | 201 | return undefined; |
| b69ab31 | | | 202 | } |
| b69ab31 | | | 203 | |
| b69ab31 | | | 204 | const [commit] = commits; |
| b69ab31 | | | 205 | if (commit.totalFileCount > MAX_FILES_ALLOWED_FOR_DIFF_STAT) { |
| b69ab31 | | | 206 | return undefined; |
| b69ab31 | | | 207 | } |
| b69ab31 | | | 208 | |
| b69ab31 | | | 209 | const filteredFiles = filterGeneratedFiles(includedFiles); |
| b69ab31 | | | 210 | if (filteredFiles.length > MAX_FILES_ALLOWED_FOR_DIFF_STAT) { |
| b69ab31 | | | 211 | return undefined; |
| b69ab31 | | | 212 | } |
| b69ab31 | | | 213 | |
| b69ab31 | | | 214 | if (filteredFiles.length === 0) { |
| b69ab31 | | | 215 | return {sloc: 0}; |
| b69ab31 | | | 216 | } |
| b69ab31 | | | 217 | |
| b69ab31 | | | 218 | serverAPI.postMessage({ |
| b69ab31 | | | 219 | type: 'fetchPendingSignificantLinesOfCode', |
| b69ab31 | | | 220 | hash: commit.hash, |
| b69ab31 | | | 221 | includedFiles: filteredFiles, |
| b69ab31 | | | 222 | requestId, |
| b69ab31 | | | 223 | }); |
| b69ab31 | | | 224 | |
| b69ab31 | | | 225 | const pendingLocData = await serverAPI |
| b69ab31 | | | 226 | .nextMessageMatching( |
| b69ab31 | | | 227 | 'fetchedPendingSignificantLinesOfCode', |
| b69ab31 | | | 228 | message => message.requestId === requestId && message.hash === commit.hash, |
| b69ab31 | | | 229 | ) |
| b69ab31 | | | 230 | .then(result => ({ |
| b69ab31 | | | 231 | sloc: result.result.value, |
| b69ab31 | | | 232 | })); |
| b69ab31 | | | 233 | |
| b69ab31 | | | 234 | return pendingLocData; |
| b69ab31 | | | 235 | }; |
| b69ab31 | | | 236 | |
| b69ab31 | | | 237 | function useFetchWithPrevious(atom: Atom<Loadable<Promise<SlocInfo | undefined>>>): { |
| b69ab31 | | | 238 | slocInfo: SlocInfo | undefined; |
| b69ab31 | | | 239 | isLoading: boolean; |
| b69ab31 | | | 240 | } { |
| b69ab31 | | | 241 | const previous = useRef<SlocInfo | undefined>(undefined); |
| b69ab31 | | | 242 | const results = useAtomValue(atom); |
| b69ab31 | | | 243 | if (results.state === 'hasError') { |
| b69ab31 | | | 244 | throw results.error; |
| b69ab31 | | | 245 | } |
| b69ab31 | | | 246 | if (results.state === 'loading') { |
| b69ab31 | | | 247 | //using the previous value in the loading state to avoid flickering / jankiness in the UI |
| b69ab31 | | | 248 | return {slocInfo: previous.current, isLoading: true}; |
| b69ab31 | | | 249 | } |
| b69ab31 | | | 250 | |
| b69ab31 | | | 251 | previous.current = results.data; |
| b69ab31 | | | 252 | |
| b69ab31 | | | 253 | return {slocInfo: results.data, isLoading: false}; |
| b69ab31 | | | 254 | } |
| b69ab31 | | | 255 | |
| b69ab31 | | | 256 | export function useFetchSignificantLinesOfCode(commit: CommitInfo) { |
| b69ab31 | | | 257 | const loadableAtom = loadable(commitSlocFamily(commit.hash)); |
| b69ab31 | | | 258 | const result = useAtomValue(loadableAtom); |
| b69ab31 | | | 259 | |
| b69ab31 | | | 260 | if (result.state === 'hasError') { |
| b69ab31 | | | 261 | throw result.error; |
| b69ab31 | | | 262 | } |
| b69ab31 | | | 263 | |
| b69ab31 | | | 264 | if (result.state === 'loading') { |
| b69ab31 | | | 265 | return {slocInfo: undefined, isLoading: true}; |
| b69ab31 | | | 266 | } |
| b69ab31 | | | 267 | |
| b69ab31 | | | 268 | return {slocInfo: result.data, isLoading: false}; |
| b69ab31 | | | 269 | } |
| b69ab31 | | | 270 | |
| b69ab31 | | | 271 | // Debounce delay for SLOC requests to prevent spamming when many files are selected quickly |
| b69ab31 | | | 272 | const DEBOUNCE_DELAY_MS = 300; |
| b69ab31 | | | 273 | |
| b69ab31 | | | 274 | // Hook that debounces an atom value |
| b69ab31 | | | 275 | function useDebouncedAtomValue<T>(sourceAtom: Atom<T>, debounceMs: number): T { |
| b69ab31 | | | 276 | const currentValue = useAtomValue(sourceAtom); |
| b69ab31 | | | 277 | const [debouncedValue, setDebouncedValue] = useState(currentValue); |
| b69ab31 | | | 278 | const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null); |
| b69ab31 | | | 279 | |
| b69ab31 | | | 280 | useEffect(() => { |
| b69ab31 | | | 281 | if (timeoutRef.current) { |
| b69ab31 | | | 282 | clearTimeout(timeoutRef.current); |
| b69ab31 | | | 283 | } |
| b69ab31 | | | 284 | |
| b69ab31 | | | 285 | timeoutRef.current = setTimeout(() => { |
| b69ab31 | | | 286 | setDebouncedValue(currentValue); |
| b69ab31 | | | 287 | }, debounceMs); |
| b69ab31 | | | 288 | |
| b69ab31 | | | 289 | return () => { |
| b69ab31 | | | 290 | if (timeoutRef.current) { |
| b69ab31 | | | 291 | clearTimeout(timeoutRef.current); |
| b69ab31 | | | 292 | } |
| b69ab31 | | | 293 | }; |
| b69ab31 | | | 294 | }, [currentValue, debounceMs]); |
| b69ab31 | | | 295 | |
| b69ab31 | | | 296 | return debouncedValue; |
| b69ab31 | | | 297 | } |
| b69ab31 | | | 298 | |
| b69ab31 | | | 299 | let pendingRequestId = 0; |
| b69ab31 | | | 300 | export function useFetchPendingSignificantLinesOfCode() { |
| b69ab31 | | | 301 | // Debounce selected files to prevent spamming SLOC requests |
| b69ab31 | | | 302 | const debouncedSelectedFiles = useDebouncedAtomValue(selectedFilesAtom, DEBOUNCE_DELAY_MS); |
| b69ab31 | | | 303 | |
| b69ab31 | | | 304 | // Use a derived atom that depends on the debounced value |
| b69ab31 | | | 305 | const debouncedAtom = useMemo(() => { |
| b69ab31 | | | 306 | return atom(get => { |
| b69ab31 | | | 307 | // Force the atom to use the debounced value by creating a dependency |
| b69ab31 | | | 308 | // Note: we can't pass debouncedSelectedFiles directly to the atom, |
| b69ab31 | | | 309 | // so we create a new fetch call with it |
| b69ab31 | | | 310 | return fetchPendingSloc(get, debouncedSelectedFiles, pendingRequestId++); |
| b69ab31 | | | 311 | }); |
| b69ab31 | | | 312 | }, [debouncedSelectedFiles]); |
| b69ab31 | | | 313 | |
| b69ab31 | | | 314 | const loadableAtom = loadable(debouncedAtom); |
| b69ab31 | | | 315 | return useFetchWithPrevious(loadableAtom); |
| b69ab31 | | | 316 | } |
| b69ab31 | | | 317 | |
| b69ab31 | | | 318 | let pendingAmendRequestId = 0; |
| b69ab31 | | | 319 | export function useFetchPendingAmendSignificantLinesOfCode() { |
| b69ab31 | | | 320 | // Debounce selected files to prevent spamming SLOC requests |
| b69ab31 | | | 321 | const debouncedSelectedFiles = useDebouncedAtomValue(selectedFilesAtom, DEBOUNCE_DELAY_MS); |
| b69ab31 | | | 322 | |
| b69ab31 | | | 323 | // Use a derived atom that depends on the debounced value |
| b69ab31 | | | 324 | const debouncedAtom = useMemo(() => { |
| b69ab31 | | | 325 | return atom(get => { |
| b69ab31 | | | 326 | return fetchPendingAmendSloc(get, debouncedSelectedFiles, pendingAmendRequestId++); |
| b69ab31 | | | 327 | }); |
| b69ab31 | | | 328 | }, [debouncedSelectedFiles]); |
| b69ab31 | | | 329 | |
| b69ab31 | | | 330 | const loadableAtom = loadable(debouncedAtom); |
| b69ab31 | | | 331 | return useFetchWithPrevious(loadableAtom); |
| b69ab31 | | | 332 | } |