| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import {Button} from 'isl-components/Button'; |
| 9 | import {Icon} from 'isl-components/Icon'; |
| 10 | import {DOCUMENTATION_DELAY, Tooltip} from 'isl-components/Tooltip'; |
| 11 | import {atom, useAtomValue} from 'jotai'; |
| 12 | import {Suspense} from 'react'; |
| 13 | import serverAPI from './ClientToServerAPI'; |
| 14 | import {commitCloudEnabledAtom} from './CommitCloud'; |
| 15 | import {t, T} from './i18n'; |
| 16 | import {writeAtom} from './jotaiUtils'; |
| 17 | import {CommitCloudSyncOperation} from './operations/CommitCloudSyncOperation'; |
| 18 | import {useRunOperation} from './operationsState'; |
| 19 | import {useIsOperationRunningOrQueued} from './previews'; |
| 20 | import {commitsShownRange, isFetchingAdditionalCommits} from './serverAPIState'; |
| 21 | |
| 22 | export function FetchingAdditionalCommitsRow() { |
| 23 | return ( |
| 24 | <Suspense> |
| 25 | <div className="fetch-additional-commits-row"> |
| 26 | <FetchingAdditionalCommitsButton /> |
| 27 | <FetchingAdditionalCommitsIndicator /> |
| 28 | </div> |
| 29 | </Suspense> |
| 30 | ); |
| 31 | } |
| 32 | |
| 33 | const hasSyncedFromCloudAtom = atom(false); |
| 34 | |
| 35 | function FetchingAdditionalCommitsIndicator() { |
| 36 | const isFetching = useAtomValue(isFetchingAdditionalCommits); |
| 37 | return isFetching ? <Icon icon="loading" /> : null; |
| 38 | } |
| 39 | |
| 40 | function FetchingAdditionalCommitsButton() { |
| 41 | const shownRange = useAtomValue(commitsShownRange); |
| 42 | const isLoading = useAtomValue(isFetchingAdditionalCommits); |
| 43 | const hasAlreadySynced = useAtomValue(hasSyncedFromCloudAtom); |
| 44 | if (shownRange === undefined && hasAlreadySynced) { |
| 45 | return null; |
| 46 | } |
| 47 | const fetchFromCloudNext = shownRange == null; |
| 48 | if (fetchFromCloudNext) { |
| 49 | return <LoadMoreFromCloudButton />; |
| 50 | } |
| 51 | const commitsShownMessage = t('Showing commits from the last $numDays days', { |
| 52 | replace: {$numDays: shownRange.toString()}, |
| 53 | }); |
| 54 | return ( |
| 55 | <Tooltip placement="top" delayMs={DOCUMENTATION_DELAY} title={commitsShownMessage}> |
| 56 | <Button |
| 57 | disabled={isLoading} |
| 58 | onClick={() => { |
| 59 | serverAPI.postMessage({ |
| 60 | type: 'loadMoreCommits', |
| 61 | }); |
| 62 | }} |
| 63 | icon> |
| 64 | <T>Load more commits</T> |
| 65 | </Button> |
| 66 | </Tooltip> |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | function LoadMoreFromCloudButton() { |
| 71 | const runOperation = useRunOperation(); |
| 72 | const isRunning = useIsOperationRunningOrQueued(CommitCloudSyncOperation) != null; |
| 73 | const isFetching = useAtomValue(isFetchingAdditionalCommits); |
| 74 | const isLoading = isRunning || isFetching; |
| 75 | const isCloudEnabled = useAtomValue(commitCloudEnabledAtom); |
| 76 | if (!isCloudEnabled) { |
| 77 | return null; |
| 78 | } |
| 79 | return ( |
| 80 | <Tooltip |
| 81 | placement="top" |
| 82 | delayMs={DOCUMENTATION_DELAY} |
| 83 | title={t('Showing full commit history. Click to fetch all commits from Commit Cloud')}> |
| 84 | <Button |
| 85 | disabled={isLoading} |
| 86 | onClick={() => { |
| 87 | runOperation(new CommitCloudSyncOperation(/* full */ true)).then(() => |
| 88 | writeAtom(hasSyncedFromCloudAtom, true), |
| 89 | ); |
| 90 | }} |
| 91 | icon> |
| 92 | <Icon icon={isLoading ? 'spinner' : 'cloud-download'} /> Fetch all cloud commits |
| 93 | </Button> |
| 94 | </Tooltip> |
| 95 | ); |
| 96 | } |
| 97 | |