| 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 {CommitCloudSyncState, Hash, Result} from './types'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | import {Button} from 'isl-components/Button'; |
| b69ab31 | | | 11 | import {Dropdown} from 'isl-components/Dropdown'; |
| b69ab31 | | | 12 | import {ErrorNotice, InlineErrorBadge} from 'isl-components/ErrorNotice'; |
| b69ab31 | | | 13 | import {Icon} from 'isl-components/Icon'; |
| b69ab31 | | | 14 | import {Subtle} from 'isl-components/Subtle'; |
| b69ab31 | | | 15 | import {TextField} from 'isl-components/TextField'; |
| b69ab31 | | | 16 | import {Tooltip} from 'isl-components/Tooltip'; |
| b69ab31 | | | 17 | import {atom, useAtom, useAtomValue} from 'jotai'; |
| b69ab31 | | | 18 | import {useCallback, useEffect, useRef, useState} from 'react'; |
| b69ab31 | | | 19 | import {notEmpty} from 'shared/utils'; |
| b69ab31 | | | 20 | import serverAPI from './ClientToServerAPI'; |
| b69ab31 | | | 21 | import {Commit} from './Commit'; |
| b69ab31 | | | 22 | import {FlexSpacer} from './ComponentUtils'; |
| b69ab31 | | | 23 | import {EducationInfoTip} from './Education'; |
| b69ab31 | | | 24 | import {T, t} from './i18n'; |
| b69ab31 | | | 25 | import {writeAtom} from './jotaiUtils'; |
| b69ab31 | | | 26 | import {CommitCloudChangeWorkspaceOperation} from './operations/CommitCloudChangeWorkspaceOperation'; |
| b69ab31 | | | 27 | import {CommitCloudCreateWorkspaceOperation} from './operations/CommitCloudCreateWorkspaceOperation'; |
| b69ab31 | | | 28 | import {CommitCloudSyncOperation} from './operations/CommitCloudSyncOperation'; |
| b69ab31 | | | 29 | import {useRunOperation} from './operationsState'; |
| b69ab31 | | | 30 | import {CommitPreview, dagWithPreviews, useMostRecentPendingOperation} from './previews'; |
| b69ab31 | | | 31 | import {RelativeDate} from './relativeDate'; |
| b69ab31 | | | 32 | import {repoRootAndCwd} from './repositoryData'; |
| b69ab31 | | | 33 | import {CommitCloudBackupStatus} from './types'; |
| b69ab31 | | | 34 | import {registerDisposable} from './utils'; |
| b69ab31 | | | 35 | |
| b69ab31 | | | 36 | import './CommitCloud.css'; |
| b69ab31 | | | 37 | |
| b69ab31 | | | 38 | export const commitCloudEnabledAtom = atom(async (get): Promise<boolean> => { |
| b69ab31 | | | 39 | // depend on the cwd so we recheck when the cwd changes |
| b69ab31 | | | 40 | get(repoRootAndCwd); |
| b69ab31 | | | 41 | |
| b69ab31 | | | 42 | serverAPI.postMessage({ |
| b69ab31 | | | 43 | type: 'getConfig', |
| b69ab31 | | | 44 | name: 'extensions.commitcloud', |
| b69ab31 | | | 45 | }); |
| b69ab31 | | | 46 | const message = await serverAPI.nextMessageMatching('gotConfig', message => { |
| b69ab31 | | | 47 | return message.name === 'extensions.commitcloud'; |
| b69ab31 | | | 48 | }); |
| b69ab31 | | | 49 | const enabled = message.value != null && message.value !== '!'; |
| b69ab31 | | | 50 | return enabled; |
| b69ab31 | | | 51 | }); |
| b69ab31 | | | 52 | |
| b69ab31 | | | 53 | const cloudSyncStateAtom = atom<Result<CommitCloudSyncState> | null>(null); |
| b69ab31 | | | 54 | |
| b69ab31 | | | 55 | registerDisposable( |
| b69ab31 | | | 56 | cloudSyncStateAtom, |
| b69ab31 | | | 57 | serverAPI.onMessageOfType('fetchedCommitCloudState', event => { |
| b69ab31 | | | 58 | writeAtom(cloudSyncStateAtom, event.state); |
| b69ab31 | | | 59 | }), |
| b69ab31 | | | 60 | import.meta.hot, |
| b69ab31 | | | 61 | ); |
| b69ab31 | | | 62 | |
| b69ab31 | | | 63 | const REFRESH_INTERVAL = 30 * 1000; |
| b69ab31 | | | 64 | |
| b69ab31 | | | 65 | export function CommitCloudInfo() { |
| b69ab31 | | | 66 | const [cloudSyncState, setCloudSyncState] = useAtom(cloudSyncStateAtom); |
| b69ab31 | | | 67 | const runOperation = useRunOperation(); |
| b69ab31 | | | 68 | const pendingOperation = useMostRecentPendingOperation(); |
| b69ab31 | | | 69 | const isRunningSync = pendingOperation?.trackEventName === 'CommitCloudSyncOperation'; |
| b69ab31 | | | 70 | const isLoading = cloudSyncState?.value?.isFetching === true; |
| b69ab31 | | | 71 | const [enteredWorkspaceName, setEnteredWorkspaceName] = useState<null | string>(null); |
| b69ab31 | | | 72 | |
| b69ab31 | | | 73 | const refreshCommitCloudStatus = useCallback(() => { |
| b69ab31 | | | 74 | setCloudSyncState(old => |
| b69ab31 | | | 75 | old?.value != null ? {value: {...old.value, isFetching: true}} : old, |
| b69ab31 | | | 76 | ); |
| b69ab31 | | | 77 | serverAPI.postMessage({ |
| b69ab31 | | | 78 | type: 'fetchCommitCloudState', |
| b69ab31 | | | 79 | }); |
| b69ab31 | | | 80 | }, [setCloudSyncState]); |
| b69ab31 | | | 81 | |
| b69ab31 | | | 82 | useEffect(() => { |
| b69ab31 | | | 83 | const interval = setInterval(refreshCommitCloudStatus, REFRESH_INTERVAL); |
| b69ab31 | | | 84 | // also call immediately on mount |
| b69ab31 | | | 85 | refreshCommitCloudStatus(); |
| b69ab31 | | | 86 | return () => clearInterval(interval); |
| b69ab31 | | | 87 | }, [refreshCommitCloudStatus]); |
| b69ab31 | | | 88 | |
| b69ab31 | | | 89 | const isMakingWorkspace = enteredWorkspaceName != null; |
| b69ab31 | | | 90 | const newWorkspaceNameRef = useRef(null); |
| b69ab31 | | | 91 | useEffect(() => { |
| b69ab31 | | | 92 | if (isMakingWorkspace && newWorkspaceNameRef.current != null) { |
| b69ab31 | | | 93 | (newWorkspaceNameRef.current as HTMLInputElement).focus(); |
| b69ab31 | | | 94 | } |
| b69ab31 | | | 95 | }, [newWorkspaceNameRef, isMakingWorkspace]); |
| b69ab31 | | | 96 | |
| b69ab31 | | | 97 | return ( |
| b69ab31 | | | 98 | <div className="commit-cloud-info"> |
| b69ab31 | | | 99 | <div className="dropdown-fields-header commit-cloud-header"> |
| b69ab31 | | | 100 | <Icon icon="cloud" size="M" /> |
| b69ab31 | | | 101 | <strong role="heading">{<T>Commit Cloud</T>}</strong> |
| b69ab31 | | | 102 | <EducationInfoTip> |
| b69ab31 | | | 103 | <T>Commit Cloud backs up your draft commits automatically across all your devices.</T> |
| b69ab31 | | | 104 | </EducationInfoTip> |
| b69ab31 | | | 105 | {isLoading && <Icon icon="loading" />} |
| b69ab31 | | | 106 | </div> |
| b69ab31 | | | 107 | |
| b69ab31 | | | 108 | {cloudSyncState?.value?.isDisabled !== true ? null : ( |
| b69ab31 | | | 109 | <div className="commit-cloud-row"> |
| b69ab31 | | | 110 | <Subtle> |
| b69ab31 | | | 111 | <T>Commit Cloud is disabled in this repository</T> |
| b69ab31 | | | 112 | </Subtle> |
| b69ab31 | | | 113 | </div> |
| b69ab31 | | | 114 | )} |
| b69ab31 | | | 115 | |
| b69ab31 | | | 116 | {cloudSyncState?.value?.syncError == null ? null : ( |
| b69ab31 | | | 117 | <div className="commit-cloud-row"> |
| b69ab31 | | | 118 | <InlineErrorBadge error={cloudSyncState?.value?.syncError}> |
| b69ab31 | | | 119 | <T>Failed to fetch commit cloud backup statuses</T> |
| b69ab31 | | | 120 | </InlineErrorBadge> |
| b69ab31 | | | 121 | </div> |
| b69ab31 | | | 122 | )} |
| b69ab31 | | | 123 | {cloudSyncState?.value?.workspaceError == null ? null : ( |
| b69ab31 | | | 124 | <div className="commit-cloud-row"> |
| b69ab31 | | | 125 | <InlineErrorBadge error={cloudSyncState?.value?.workspaceError}> |
| b69ab31 | | | 126 | <T>Failed to fetch commit cloud status</T> |
| b69ab31 | | | 127 | </InlineErrorBadge> |
| b69ab31 | | | 128 | </div> |
| b69ab31 | | | 129 | )} |
| b69ab31 | | | 130 | <div className="commit-cloud-row"> |
| b69ab31 | | | 131 | {cloudSyncState == null ? ( |
| b69ab31 | | | 132 | <Icon icon="loading" /> |
| b69ab31 | | | 133 | ) : cloudSyncState.error != null ? ( |
| b69ab31 | | | 134 | <ErrorNotice |
| b69ab31 | | | 135 | error={cloudSyncState.error} |
| b69ab31 | | | 136 | title={t('Failed to check Commit Cloud state')} |
| b69ab31 | | | 137 | /> |
| b69ab31 | | | 138 | ) : cloudSyncState.value.lastBackup == null ? null : ( |
| b69ab31 | | | 139 | <> |
| b69ab31 | | | 140 | <Subtle> |
| b69ab31 | | | 141 | <T |
| b69ab31 | | | 142 | replace={{ |
| b69ab31 | | | 143 | $relTimeAgo: ( |
| b69ab31 | | | 144 | <Tooltip title={cloudSyncState.value.lastBackup.toLocaleString()}> |
| b69ab31 | | | 145 | <RelativeDate date={cloudSyncState.value.lastBackup} /> |
| b69ab31 | | | 146 | </Tooltip> |
| b69ab31 | | | 147 | ), |
| b69ab31 | | | 148 | }}> |
| b69ab31 | | | 149 | Last meaningful sync: $relTimeAgo |
| b69ab31 | | | 150 | </T> |
| b69ab31 | | | 151 | </Subtle> |
| b69ab31 | | | 152 | <FlexSpacer /> |
| b69ab31 | | | 153 | <Button |
| b69ab31 | | | 154 | onClick={() => { |
| b69ab31 | | | 155 | runOperation(new CommitCloudSyncOperation()).then(() => { |
| b69ab31 | | | 156 | refreshCommitCloudStatus(); |
| b69ab31 | | | 157 | }); |
| b69ab31 | | | 158 | }} |
| b69ab31 | | | 159 | disabled={isRunningSync} |
| b69ab31 | | | 160 | icon> |
| b69ab31 | | | 161 | {isRunningSync ? ( |
| b69ab31 | | | 162 | <Icon icon="loading" slot="start" /> |
| b69ab31 | | | 163 | ) : ( |
| b69ab31 | | | 164 | <Icon icon="sync" slot="start" /> |
| b69ab31 | | | 165 | )} |
| b69ab31 | | | 166 | <T>Sync now</T> |
| b69ab31 | | | 167 | </Button> |
| b69ab31 | | | 168 | </> |
| b69ab31 | | | 169 | )} |
| b69ab31 | | | 170 | </div> |
| b69ab31 | | | 171 | |
| b69ab31 | | | 172 | {cloudSyncState?.value?.commitStatuses == null ? null : ( |
| b69ab31 | | | 173 | <CommitCloudSyncStatusBadge statuses={cloudSyncState?.value?.commitStatuses} /> |
| b69ab31 | | | 174 | )} |
| b69ab31 | | | 175 | |
| b69ab31 | | | 176 | <div className="commit-cloud-row"> |
| b69ab31 | | | 177 | {cloudSyncState?.value?.currentWorkspace == null ? null : ( |
| b69ab31 | | | 178 | <div className="commit-cloud-dropdown-container"> |
| b69ab31 | | | 179 | <label htmlFor="stack-file-dropdown"> |
| b69ab31 | | | 180 | <T>Commit Cloud Workspace</T> |
| b69ab31 | | | 181 | </label> |
| b69ab31 | | | 182 | <div className="commit-cloud-workspace-actions"> |
| b69ab31 | | | 183 | <Dropdown |
| b69ab31 | | | 184 | value={cloudSyncState?.value.currentWorkspace} |
| b69ab31 | | | 185 | disabled={ |
| b69ab31 | | | 186 | pendingOperation?.trackEventName === 'CommitCloudChangeWorkspaceOperation' || |
| b69ab31 | | | 187 | pendingOperation?.trackEventName === 'CommitCloudCreateWorkspaceOperation' |
| b69ab31 | | | 188 | } |
| b69ab31 | | | 189 | onChange={event => { |
| b69ab31 | | | 190 | const newChoice = event.currentTarget.value; |
| b69ab31 | | | 191 | runOperation(new CommitCloudChangeWorkspaceOperation(newChoice)).then(() => { |
| b69ab31 | | | 192 | refreshCommitCloudStatus(); |
| b69ab31 | | | 193 | }); |
| b69ab31 | | | 194 | if (cloudSyncState?.value) { |
| b69ab31 | | | 195 | // optimistically set the workspace choice |
| b69ab31 | | | 196 | setCloudSyncState({ |
| b69ab31 | | | 197 | value: {...cloudSyncState?.value, currentWorkspace: newChoice}, |
| b69ab31 | | | 198 | }); |
| b69ab31 | | | 199 | } |
| b69ab31 | | | 200 | }} |
| b69ab31 | | | 201 | options={cloudSyncState?.value.workspaceChoices ?? []} |
| b69ab31 | | | 202 | /> |
| b69ab31 | | | 203 | {enteredWorkspaceName == null ? ( |
| b69ab31 | | | 204 | <Button |
| b69ab31 | | | 205 | icon |
| b69ab31 | | | 206 | onClick={e => { |
| b69ab31 | | | 207 | setEnteredWorkspaceName(''); |
| b69ab31 | | | 208 | e.preventDefault(); |
| b69ab31 | | | 209 | e.stopPropagation(); |
| b69ab31 | | | 210 | }}> |
| b69ab31 | | | 211 | <Icon icon="plus" slot="start" /> |
| b69ab31 | | | 212 | <T>Add Workspace</T> |
| b69ab31 | | | 213 | </Button> |
| b69ab31 | | | 214 | ) : ( |
| b69ab31 | | | 215 | <div className="commit-cloud-new-workspace-input"> |
| b69ab31 | | | 216 | <TextField |
| b69ab31 | | | 217 | ref={newWorkspaceNameRef as React.MutableRefObject<null>} |
| b69ab31 | | | 218 | onInput={e => setEnteredWorkspaceName((e.target as HTMLInputElement).value)}> |
| b69ab31 | | | 219 | <T>New Workspace Name</T> |
| b69ab31 | | | 220 | </TextField> |
| b69ab31 | | | 221 | <Button |
| b69ab31 | | | 222 | onClick={e => { |
| b69ab31 | | | 223 | setEnteredWorkspaceName(null); |
| b69ab31 | | | 224 | e.preventDefault(); |
| b69ab31 | | | 225 | e.stopPropagation(); |
| b69ab31 | | | 226 | }}> |
| b69ab31 | | | 227 | <T>Cancel</T> |
| b69ab31 | | | 228 | </Button> |
| b69ab31 | | | 229 | <Button |
| b69ab31 | | | 230 | primary |
| b69ab31 | | | 231 | disabled={!enteredWorkspaceName} |
| b69ab31 | | | 232 | onClick={e => { |
| b69ab31 | | | 233 | if (!enteredWorkspaceName) { |
| b69ab31 | | | 234 | return; |
| b69ab31 | | | 235 | } |
| b69ab31 | | | 236 | const name = enteredWorkspaceName.trim().replace(' ', '_'); |
| b69ab31 | | | 237 | // optimistically update the dropdown |
| b69ab31 | | | 238 | setCloudSyncState(old => |
| b69ab31 | | | 239 | old?.value != null |
| b69ab31 | | | 240 | ? { |
| b69ab31 | | | 241 | value: { |
| b69ab31 | | | 242 | ...old.value, |
| b69ab31 | | | 243 | workspaceChoices: [...(old.value.workspaceChoices ?? []), name], |
| b69ab31 | | | 244 | currentWorkspace: name, |
| b69ab31 | | | 245 | }, |
| b69ab31 | | | 246 | } |
| b69ab31 | | | 247 | : old, |
| b69ab31 | | | 248 | ); |
| b69ab31 | | | 249 | runOperation(new CommitCloudCreateWorkspaceOperation(name)).then(() => { |
| b69ab31 | | | 250 | refreshCommitCloudStatus(); |
| b69ab31 | | | 251 | }); |
| b69ab31 | | | 252 | setEnteredWorkspaceName(null); |
| b69ab31 | | | 253 | e.preventDefault(); |
| b69ab31 | | | 254 | e.stopPropagation(); |
| b69ab31 | | | 255 | }}> |
| b69ab31 | | | 256 | <T>Create</T> |
| b69ab31 | | | 257 | </Button> |
| b69ab31 | | | 258 | </div> |
| b69ab31 | | | 259 | )} |
| b69ab31 | | | 260 | </div> |
| b69ab31 | | | 261 | </div> |
| b69ab31 | | | 262 | )} |
| b69ab31 | | | 263 | </div> |
| b69ab31 | | | 264 | </div> |
| b69ab31 | | | 265 | ); |
| b69ab31 | | | 266 | } |
| b69ab31 | | | 267 | |
| b69ab31 | | | 268 | function CommitCloudSyncStatusBadge({statuses}: {statuses: Map<Hash, CommitCloudBackupStatus>}) { |
| b69ab31 | | | 269 | const statusValues = [...statuses.entries()]; |
| b69ab31 | | | 270 | const pending = statusValues.filter( |
| b69ab31 | | | 271 | ([_hash, status]) => |
| b69ab31 | | | 272 | status === CommitCloudBackupStatus.Pending || status === CommitCloudBackupStatus.InProgress, |
| b69ab31 | | | 273 | ); |
| b69ab31 | | | 274 | const failed = statusValues.filter( |
| b69ab31 | | | 275 | ([_hash, status]) => status === CommitCloudBackupStatus.Failed, |
| b69ab31 | | | 276 | ); |
| b69ab31 | | | 277 | |
| b69ab31 | | | 278 | let icon; |
| b69ab31 | | | 279 | let content; |
| b69ab31 | | | 280 | let renderTooltip; |
| b69ab31 | | | 281 | if (pending.length > 0) { |
| b69ab31 | | | 282 | icon = 'sync'; |
| b69ab31 | | | 283 | content = <T count={pending.length}>commitsBeingBackedUp</T>; |
| b69ab31 | | | 284 | renderTooltip = () => <BackupList commits={pending.map(([hash]) => hash)} />; |
| b69ab31 | | | 285 | } else if (failed.length > 0) { |
| b69ab31 | | | 286 | icon = 'sync'; |
| b69ab31 | | | 287 | content = ( |
| b69ab31 | | | 288 | <div className="inline-error-badge"> |
| b69ab31 | | | 289 | <span> |
| b69ab31 | | | 290 | <Icon icon="error" slot="start" /> |
| b69ab31 | | | 291 | <T count={failed.length}>commitsFailedBackingUp</T> |
| b69ab31 | | | 292 | </span> |
| b69ab31 | | | 293 | </div> |
| b69ab31 | | | 294 | ); |
| b69ab31 | | | 295 | renderTooltip = () => <BackupList commits={failed.map(([hash]) => hash)} />; |
| b69ab31 | | | 296 | } else { |
| b69ab31 | | | 297 | // Empty means all commits were backed up, since we don't fetch successfully backed up hashes. |
| b69ab31 | | | 298 | // Note: this does mean we can't tell the difference between a commit we don't know about and a commit that is backed up. |
| b69ab31 | | | 299 | icon = 'check'; |
| b69ab31 | | | 300 | content = <T>All commits backed up</T>; |
| b69ab31 | | | 301 | } |
| b69ab31 | | | 302 | |
| b69ab31 | | | 303 | return ( |
| b69ab31 | | | 304 | <div className="commit-cloud-row commit-cloud-sync-status-badge"> |
| b69ab31 | | | 305 | {renderTooltip == null ? ( |
| b69ab31 | | | 306 | <div> |
| b69ab31 | | | 307 | <Icon icon={icon} /> |
| b69ab31 | | | 308 | {content} |
| b69ab31 | | | 309 | </div> |
| b69ab31 | | | 310 | ) : ( |
| b69ab31 | | | 311 | <Tooltip component={renderTooltip}> |
| b69ab31 | | | 312 | <Icon icon={icon} /> |
| b69ab31 | | | 313 | {content} |
| b69ab31 | | | 314 | </Tooltip> |
| b69ab31 | | | 315 | )} |
| b69ab31 | | | 316 | </div> |
| b69ab31 | | | 317 | ); |
| b69ab31 | | | 318 | } |
| b69ab31 | | | 319 | |
| b69ab31 | | | 320 | function BackupList({commits}: {commits: Array<Hash>}) { |
| b69ab31 | | | 321 | const dag = useAtomValue(dagWithPreviews); |
| b69ab31 | | | 322 | const infos = commits.map(hash => dag.get(hash)).filter(notEmpty); |
| b69ab31 | | | 323 | return ( |
| b69ab31 | | | 324 | <div className="commit-cloud-backup-list"> |
| b69ab31 | | | 325 | {infos.map(commit => |
| b69ab31 | | | 326 | typeof commit === 'string' ? ( |
| b69ab31 | | | 327 | <div>{commit}</div> |
| b69ab31 | | | 328 | ) : ( |
| b69ab31 | | | 329 | <Commit |
| b69ab31 | | | 330 | commit={commit} |
| b69ab31 | | | 331 | key={commit.hash} |
| b69ab31 | | | 332 | hasChildren={false} |
| b69ab31 | | | 333 | previewType={CommitPreview.NON_ACTIONABLE_COMMIT} |
| b69ab31 | | | 334 | /> |
| b69ab31 | | | 335 | ), |
| b69ab31 | | | 336 | )} |
| b69ab31 | | | 337 | </div> |
| b69ab31 | | | 338 | ); |
| b69ab31 | | | 339 | } |