3.1 KB97 lines
Blame
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
8import {Button} from 'isl-components/Button';
9import {Icon} from 'isl-components/Icon';
10import {DOCUMENTATION_DELAY, Tooltip} from 'isl-components/Tooltip';
11import {atom, useAtomValue} from 'jotai';
12import {Suspense} from 'react';
13import serverAPI from './ClientToServerAPI';
14import {commitCloudEnabledAtom} from './CommitCloud';
15import {t, T} from './i18n';
16import {writeAtom} from './jotaiUtils';
17import {CommitCloudSyncOperation} from './operations/CommitCloudSyncOperation';
18import {useRunOperation} from './operationsState';
19import {useIsOperationRunningOrQueued} from './previews';
20import {commitsShownRange, isFetchingAdditionalCommits} from './serverAPIState';
21
22export function FetchingAdditionalCommitsRow() {
23 return (
24 <Suspense>
25 <div className="fetch-additional-commits-row">
26 <FetchingAdditionalCommitsButton />
27 <FetchingAdditionalCommitsIndicator />
28 </div>
29 </Suspense>
30 );
31}
32
33const hasSyncedFromCloudAtom = atom(false);
34
35function FetchingAdditionalCommitsIndicator() {
36 const isFetching = useAtomValue(isFetchingAdditionalCommits);
37 return isFetching ? <Icon icon="loading" /> : null;
38}
39
40function 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
70function 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