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