| 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 {ChangedFile, CommitInfo, FilesSample, Hash, Result} from './types'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | import {Button} from 'isl-components/Button'; |
| b69ab31 | | | 11 | import {Tooltip} from 'isl-components/Tooltip'; |
| b69ab31 | | | 12 | import {useEffect, useState} from 'react'; |
| b69ab31 | | | 13 | import {CancellationToken} from 'shared/CancellationToken'; |
| b69ab31 | | | 14 | import {ComparisonType} from 'shared/Comparison'; |
| b69ab31 | | | 15 | import {LRU} from 'shared/LRU'; |
| b69ab31 | | | 16 | import serverAPI from './ClientToServerAPI'; |
| b69ab31 | | | 17 | import {ChangedFiles} from './UncommittedChanges'; |
| b69ab31 | | | 18 | import {t, T} from './i18n'; |
| b69ab31 | | | 19 | |
| b69ab31 | | | 20 | // Cache fetches in progress so we don't double fetch |
| b69ab31 | | | 21 | const commitFilesCache = new LRU<Hash, Promise<Result<FilesSample>>>(10); |
| b69ab31 | | | 22 | export const __TEST__ = {commitFilesCache}; |
| b69ab31 | | | 23 | |
| b69ab31 | | | 24 | /** |
| b69ab31 | | | 25 | * The basic CommitInfo we fetch in bulk only contains the first 25 files, |
| b69ab31 | | | 26 | * and is missing file statuses. |
| b69ab31 | | | 27 | * But we want to be able to scroll through pages of files, |
| b69ab31 | | | 28 | * and also see their statuses (added, removed, etc). |
| b69ab31 | | | 29 | * So all files for the currently selected commit, |
| b69ab31 | | | 30 | * to augment the subset we already have. |
| b69ab31 | | | 31 | * Public commits don't show changed files by default for performance, |
| b69ab31 | | | 32 | * but instead show a button used to fetch all the files. |
| b69ab31 | | | 33 | */ |
| b69ab31 | | | 34 | export function ChangedFilesWithFetching({commit}: {commit: CommitInfo}) { |
| b69ab31 | | | 35 | const [fetchedAllFiles, setFetchedAllFiles] = useState<FilesSample | undefined>(undefined); |
| b69ab31 | | | 36 | |
| b69ab31 | | | 37 | const [showingPublicWarning, setShowPublicWarning] = useState(commit.phase === 'public'); |
| b69ab31 | | | 38 | useEffect(() => { |
| b69ab31 | | | 39 | setShowPublicWarning(commit.phase === 'public'); |
| b69ab31 | | | 40 | }, [commit.hash, commit.phase]); |
| b69ab31 | | | 41 | |
| b69ab31 | | | 42 | useEffect(() => { |
| b69ab31 | | | 43 | if (showingPublicWarning === true) { |
| b69ab31 | | | 44 | return; |
| b69ab31 | | | 45 | } |
| b69ab31 | | | 46 | |
| b69ab31 | | | 47 | setFetchedAllFiles(undefined); |
| b69ab31 | | | 48 | const cancel = new CancellationToken(); |
| b69ab31 | | | 49 | getChangedFilesForHash(commit.hash, undefined).then(result => { |
| b69ab31 | | | 50 | if (cancel.isCancelled) { |
| b69ab31 | | | 51 | return; |
| b69ab31 | | | 52 | } |
| b69ab31 | | | 53 | if (result.value != null) { |
| b69ab31 | | | 54 | setFetchedAllFiles(result.value); |
| b69ab31 | | | 55 | } |
| b69ab31 | | | 56 | }); |
| b69ab31 | | | 57 | return () => { |
| b69ab31 | | | 58 | cancel.cancel(); |
| b69ab31 | | | 59 | }; |
| b69ab31 | | | 60 | }, [commit.hash, showingPublicWarning]); |
| b69ab31 | | | 61 | |
| b69ab31 | | | 62 | if (showingPublicWarning) { |
| b69ab31 | | | 63 | return ( |
| b69ab31 | | | 64 | <Tooltip |
| b69ab31 | | | 65 | title={t( |
| b69ab31 | | | 66 | 'Changed files are not loaded for public commits by default, for performance. Click to load changed files.', |
| b69ab31 | | | 67 | )}> |
| b69ab31 | | | 68 | <Button onClick={() => setShowPublicWarning(false)}> |
| b69ab31 | | | 69 | <T>Load changed files</T> |
| b69ab31 | | | 70 | </Button> |
| b69ab31 | | | 71 | </Tooltip> |
| b69ab31 | | | 72 | ); |
| b69ab31 | | | 73 | } |
| b69ab31 | | | 74 | |
| b69ab31 | | | 75 | return ( |
| b69ab31 | | | 76 | <ChangedFiles |
| b69ab31 | | | 77 | filesSubset={ |
| b69ab31 | | | 78 | fetchedAllFiles?.filesSample ?? |
| b69ab31 | | | 79 | commit.filePathsSample.map( |
| b69ab31 | | | 80 | (filePath): ChangedFile => ({ |
| b69ab31 | | | 81 | path: filePath, |
| b69ab31 | | | 82 | // default to 'modified' as a best guess. |
| b69ab31 | | | 83 | // TODO: should this be a special loading status that shows a spinner? |
| b69ab31 | | | 84 | status: 'M' as const, |
| b69ab31 | | | 85 | }), |
| b69ab31 | | | 86 | ) |
| b69ab31 | | | 87 | } |
| b69ab31 | | | 88 | totalFiles={fetchedAllFiles?.totalFileCount ?? commit.totalFileCount} |
| b69ab31 | | | 89 | comparison={ |
| b69ab31 | | | 90 | commit.isDot |
| b69ab31 | | | 91 | ? {type: ComparisonType.HeadChanges} |
| b69ab31 | | | 92 | : { |
| b69ab31 | | | 93 | type: ComparisonType.Committed, |
| b69ab31 | | | 94 | hash: commit.hash, |
| b69ab31 | | | 95 | } |
| b69ab31 | | | 96 | } |
| b69ab31 | | | 97 | /> |
| b69ab31 | | | 98 | ); |
| b69ab31 | | | 99 | } |
| b69ab31 | | | 100 | |
| b69ab31 | | | 101 | /** |
| b69ab31 | | | 102 | * Get changed files in a given commit. |
| b69ab31 | | | 103 | * A small subset of the files may have already been fetched, |
| b69ab31 | | | 104 | * or in some cases no files may be cached yet and all files need to be fetched asynchronously. */ |
| b69ab31 | | | 105 | export function getChangedFilesForHash( |
| b69ab31 | | | 106 | hash: Hash, |
| b69ab31 | | | 107 | limit?: number | undefined, |
| b69ab31 | | | 108 | ): Promise<Result<FilesSample>> { |
| b69ab31 | | | 109 | const foundPromise = commitFilesCache.get(hash); |
| b69ab31 | | | 110 | if (foundPromise != null) { |
| b69ab31 | | | 111 | return foundPromise; |
| b69ab31 | | | 112 | } |
| b69ab31 | | | 113 | serverAPI.postMessage({ |
| b69ab31 | | | 114 | type: 'fetchCommitChangedFiles', |
| b69ab31 | | | 115 | hash, |
| b69ab31 | | | 116 | limit, |
| b69ab31 | | | 117 | }); |
| b69ab31 | | | 118 | |
| b69ab31 | | | 119 | const resultPromise = serverAPI |
| b69ab31 | | | 120 | .nextMessageMatching('fetchedCommitChangedFiles', message => message.hash === hash) |
| b69ab31 | | | 121 | .then(result => result.result); |
| b69ab31 | | | 122 | commitFilesCache.set(hash, resultPromise); |
| b69ab31 | | | 123 | |
| b69ab31 | | | 124 | return resultPromise; |
| b69ab31 | | | 125 | } |