addons/isl/src/FetchAdditionalCommitsButton.tsxblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import {Button} from 'isl-components/Button';
b69ab319import {Icon} from 'isl-components/Icon';
b69ab3110import {DOCUMENTATION_DELAY, Tooltip} from 'isl-components/Tooltip';
b69ab3111import {atom, useAtomValue} from 'jotai';
b69ab3112import {Suspense} from 'react';
b69ab3113import serverAPI from './ClientToServerAPI';
b69ab3114import {commitCloudEnabledAtom} from './CommitCloud';
b69ab3115import {t, T} from './i18n';
b69ab3116import {writeAtom} from './jotaiUtils';
b69ab3117import {CommitCloudSyncOperation} from './operations/CommitCloudSyncOperation';
b69ab3118import {useRunOperation} from './operationsState';
b69ab3119import {useIsOperationRunningOrQueued} from './previews';
b69ab3120import {commitsShownRange, isFetchingAdditionalCommits} from './serverAPIState';
b69ab3121
b69ab3122export function FetchingAdditionalCommitsRow() {
b69ab3123 return (
b69ab3124 <Suspense>
b69ab3125 <div className="fetch-additional-commits-row">
b69ab3126 <FetchingAdditionalCommitsButton />
b69ab3127 <FetchingAdditionalCommitsIndicator />
b69ab3128 </div>
b69ab3129 </Suspense>
b69ab3130 );
b69ab3131}
b69ab3132
b69ab3133const hasSyncedFromCloudAtom = atom(false);
b69ab3134
b69ab3135function FetchingAdditionalCommitsIndicator() {
b69ab3136 const isFetching = useAtomValue(isFetchingAdditionalCommits);
b69ab3137 return isFetching ? <Icon icon="loading" /> : null;
b69ab3138}
b69ab3139
b69ab3140function FetchingAdditionalCommitsButton() {
b69ab3141 const shownRange = useAtomValue(commitsShownRange);
b69ab3142 const isLoading = useAtomValue(isFetchingAdditionalCommits);
b69ab3143 const hasAlreadySynced = useAtomValue(hasSyncedFromCloudAtom);
b69ab3144 if (shownRange === undefined && hasAlreadySynced) {
b69ab3145 return null;
b69ab3146 }
b69ab3147 const fetchFromCloudNext = shownRange == null;
b69ab3148 if (fetchFromCloudNext) {
b69ab3149 return <LoadMoreFromCloudButton />;
b69ab3150 }
b69ab3151 const commitsShownMessage = t('Showing commits from the last $numDays days', {
b69ab3152 replace: {$numDays: shownRange.toString()},
b69ab3153 });
b69ab3154 return (
b69ab3155 <Tooltip placement="top" delayMs={DOCUMENTATION_DELAY} title={commitsShownMessage}>
b69ab3156 <Button
b69ab3157 disabled={isLoading}
b69ab3158 onClick={() => {
b69ab3159 serverAPI.postMessage({
b69ab3160 type: 'loadMoreCommits',
b69ab3161 });
b69ab3162 }}
b69ab3163 icon>
b69ab3164 <T>Load more commits</T>
b69ab3165 </Button>
b69ab3166 </Tooltip>
b69ab3167 );
b69ab3168}
b69ab3169
b69ab3170function LoadMoreFromCloudButton() {
b69ab3171 const runOperation = useRunOperation();
b69ab3172 const isRunning = useIsOperationRunningOrQueued(CommitCloudSyncOperation) != null;
b69ab3173 const isFetching = useAtomValue(isFetchingAdditionalCommits);
b69ab3174 const isLoading = isRunning || isFetching;
b69ab3175 const isCloudEnabled = useAtomValue(commitCloudEnabledAtom);
b69ab3176 if (!isCloudEnabled) {
b69ab3177 return null;
b69ab3178 }
b69ab3179 return (
b69ab3180 <Tooltip
b69ab3181 placement="top"
b69ab3182 delayMs={DOCUMENTATION_DELAY}
b69ab3183 title={t('Showing full commit history. Click to fetch all commits from Commit Cloud')}>
b69ab3184 <Button
b69ab3185 disabled={isLoading}
b69ab3186 onClick={() => {
b69ab3187 runOperation(new CommitCloudSyncOperation(/* full */ true)).then(() =>
b69ab3188 writeAtom(hasSyncedFromCloudAtom, true),
b69ab3189 );
b69ab3190 }}
b69ab3191 icon>
b69ab3192 <Icon icon={isLoading ? 'spinner' : 'cloud-download'} /> Fetch all cloud commits
b69ab3193 </Button>
b69ab3194 </Tooltip>
b69ab3195 );
b69ab3196}