| 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 {ErrorNotice} from 'isl-components/ErrorNotice'; |
| b69ab31 | | | 10 | import {Icon} from 'isl-components/Icon'; |
| b69ab31 | | | 11 | import {Kbd} from 'isl-components/Kbd'; |
| b69ab31 | | | 12 | import {KeyCode, Modifier} from 'isl-components/KeyboardShortcuts'; |
| b69ab31 | | | 13 | import {Subtle} from 'isl-components/Subtle'; |
| b69ab31 | | | 14 | import {Tooltip} from 'isl-components/Tooltip'; |
| b69ab31 | | | 15 | import {useAtom} from 'jotai'; |
| b69ab31 | | | 16 | import {useEffect} from 'react'; |
| b69ab31 | | | 17 | import {ComparisonType} from 'shared/Comparison'; |
| b69ab31 | | | 18 | import serverAPI from './ClientToServerAPI'; |
| b69ab31 | | | 19 | import {OpenComparisonViewButton} from './ComparisonView/OpenComparisonViewButton'; |
| b69ab31 | | | 20 | import {FlexSpacer, Row} from './ComponentUtils'; |
| b69ab31 | | | 21 | import {DropdownFields} from './DropdownFields'; |
| b69ab31 | | | 22 | import {EmptyState} from './EmptyState'; |
| b69ab31 | | | 23 | import {useCommandEvent} from './ISLShortcuts'; |
| b69ab31 | | | 24 | import {OperationDisabledButton} from './OperationDisabledButton'; |
| b69ab31 | | | 25 | import {ChangedFiles} from './UncommittedChanges'; |
| b69ab31 | | | 26 | import {T, t} from './i18n'; |
| b69ab31 | | | 27 | import {atomLoadableWithRefresh} from './jotaiUtils'; |
| b69ab31 | | | 28 | import {DeleteShelveOperation} from './operations/DeleteShelveOperation'; |
| b69ab31 | | | 29 | import {UnshelveOperation} from './operations/UnshelveOperation'; |
| b69ab31 | | | 30 | import {RelativeDate} from './relativeDate'; |
| b69ab31 | | | 31 | |
| b69ab31 | | | 32 | import './ShelvedChanges.css'; |
| b69ab31 | | | 33 | |
| b69ab31 | | | 34 | const shelvedChangesState = atomLoadableWithRefresh(async _get => { |
| b69ab31 | | | 35 | serverAPI.postMessage({ |
| b69ab31 | | | 36 | type: 'fetchShelvedChanges', |
| b69ab31 | | | 37 | }); |
| b69ab31 | | | 38 | |
| b69ab31 | | | 39 | const result = await serverAPI.nextMessageMatching('fetchedShelvedChanges', () => true); |
| b69ab31 | | | 40 | if (result.shelvedChanges.error != null) { |
| b69ab31 | | | 41 | throw new Error(result.shelvedChanges.error.toString()); |
| b69ab31 | | | 42 | } |
| b69ab31 | | | 43 | return result.shelvedChanges.value; |
| b69ab31 | | | 44 | }); |
| b69ab31 | | | 45 | |
| b69ab31 | | | 46 | export function ShelvedChangesMenu() { |
| b69ab31 | | | 47 | const additionalToggles = useCommandEvent('ToggleShelvedChangesDropdown'); |
| b69ab31 | | | 48 | return ( |
| b69ab31 | | | 49 | <Tooltip |
| b69ab31 | | | 50 | component={dismiss => <ShelvedChangesList dismiss={dismiss} />} |
| b69ab31 | | | 51 | trigger="click" |
| b69ab31 | | | 52 | placement="bottom" |
| b69ab31 | | | 53 | additionalToggles={additionalToggles.asEventTarget()} |
| b69ab31 | | | 54 | group="topbar" |
| b69ab31 | | | 55 | title={ |
| b69ab31 | | | 56 | <T replace={{$shortcut: <Kbd keycode={KeyCode.S} modifiers={[Modifier.ALT]} />}}> |
| b69ab31 | | | 57 | Shelved Changes ($shortcut) |
| b69ab31 | | | 58 | </T> |
| b69ab31 | | | 59 | }> |
| b69ab31 | | | 60 | <Button icon data-testid="shelved-changes-button"> |
| b69ab31 | | | 61 | <Icon icon="archive" /> |
| b69ab31 | | | 62 | </Button> |
| b69ab31 | | | 63 | </Tooltip> |
| b69ab31 | | | 64 | ); |
| b69ab31 | | | 65 | } |
| b69ab31 | | | 66 | |
| b69ab31 | | | 67 | function ShelvedChangesList({dismiss}: {dismiss: () => void}) { |
| b69ab31 | | | 68 | const [shelvedChanges, refresh] = useAtom(shelvedChangesState); |
| b69ab31 | | | 69 | useEffect(() => { |
| b69ab31 | | | 70 | // make sure we fetch whenever loading the shelved changes list |
| b69ab31 | | | 71 | refresh(); |
| b69ab31 | | | 72 | }, [refresh]); |
| b69ab31 | | | 73 | return ( |
| b69ab31 | | | 74 | <DropdownFields |
| b69ab31 | | | 75 | title={ |
| b69ab31 | | | 76 | <Row> |
| b69ab31 | | | 77 | <T>Shelved Changes</T>{' '} |
| b69ab31 | | | 78 | <Tooltip |
| b69ab31 | | | 79 | title={t( |
| b69ab31 | | | 80 | 'You can Shelve a set of uncommitted changes to save them for later, via the Shelve button in the list of uncommitted changes.\n\nHere you can view and Unshelve previously shelved changes.', |
| b69ab31 | | | 81 | )}> |
| b69ab31 | | | 82 | <Icon icon="info" /> |
| b69ab31 | | | 83 | </Tooltip> |
| b69ab31 | | | 84 | </Row> |
| b69ab31 | | | 85 | } |
| b69ab31 | | | 86 | icon="archive" |
| b69ab31 | | | 87 | className="shelved-changes-dropdown" |
| b69ab31 | | | 88 | data-testid="shelved-changes-dropdown"> |
| b69ab31 | | | 89 | {shelvedChanges.state === 'loading' ? ( |
| b69ab31 | | | 90 | <Icon icon="loading" /> |
| b69ab31 | | | 91 | ) : shelvedChanges.state === 'hasError' ? ( |
| b69ab31 | | | 92 | <ErrorNotice |
| b69ab31 | | | 93 | title="Could not fetch shelved changes" |
| b69ab31 | | | 94 | error={shelvedChanges.error as Error} |
| b69ab31 | | | 95 | /> |
| b69ab31 | | | 96 | ) : shelvedChanges.data.length === 0 ? ( |
| b69ab31 | | | 97 | <EmptyState small> |
| b69ab31 | | | 98 | <T>No shelved changes</T> |
| b69ab31 | | | 99 | </EmptyState> |
| b69ab31 | | | 100 | ) : ( |
| b69ab31 | | | 101 | <div className="shelved-changes-list"> |
| b69ab31 | | | 102 | {shelvedChanges.data.map(change => { |
| b69ab31 | | | 103 | const comparison = { |
| b69ab31 | | | 104 | type: ComparisonType.Committed, |
| b69ab31 | | | 105 | hash: change.hash, |
| b69ab31 | | | 106 | }; |
| b69ab31 | | | 107 | return ( |
| b69ab31 | | | 108 | <div key={change.hash} className="shelved-changes-item"> |
| b69ab31 | | | 109 | <div className="shelved-changes-item-row"> |
| b69ab31 | | | 110 | <span className="shelve-name">{change.name}</span> |
| b69ab31 | | | 111 | <Subtle> |
| b69ab31 | | | 112 | <RelativeDate date={change.date} useShortVariant /> |
| b69ab31 | | | 113 | </Subtle> |
| b69ab31 | | | 114 | <FlexSpacer /> |
| b69ab31 | | | 115 | <Tooltip title={t('Remove from the list of shelved changes')}> |
| b69ab31 | | | 116 | <OperationDisabledButton |
| b69ab31 | | | 117 | kind="icon" |
| b69ab31 | | | 118 | contextKey={`delete-shelve-${change.hash}`} |
| b69ab31 | | | 119 | data-testid={`delete-shelve-${change.hash}`} |
| b69ab31 | | | 120 | className="unshelve-button" |
| b69ab31 | | | 121 | runOperation={() => { |
| b69ab31 | | | 122 | dismiss(); |
| b69ab31 | | | 123 | return new DeleteShelveOperation(change); |
| b69ab31 | | | 124 | }} |
| b69ab31 | | | 125 | icon={<Icon icon="trash" />}></OperationDisabledButton> |
| b69ab31 | | | 126 | </Tooltip> |
| b69ab31 | | | 127 | <Tooltip |
| b69ab31 | | | 128 | title={t( |
| b69ab31 | | | 129 | 'Apply these changes without removing this from your list of shelved changes', |
| b69ab31 | | | 130 | )}> |
| b69ab31 | | | 131 | <OperationDisabledButton |
| b69ab31 | | | 132 | kind="icon" |
| b69ab31 | | | 133 | contextKey={`unshelve-keep-${change.hash}`} |
| b69ab31 | | | 134 | className="unshelve-button" |
| b69ab31 | | | 135 | runOperation={() => { |
| b69ab31 | | | 136 | dismiss(); |
| b69ab31 | | | 137 | return new UnshelveOperation(change, true); |
| b69ab31 | | | 138 | }} |
| b69ab31 | | | 139 | icon={<Icon icon="layers-active" slot="start" />}> |
| b69ab31 | | | 140 | <T>Apply</T> |
| b69ab31 | | | 141 | </OperationDisabledButton> |
| b69ab31 | | | 142 | </Tooltip> |
| b69ab31 | | | 143 | <Tooltip |
| b69ab31 | | | 144 | title={t( |
| b69ab31 | | | 145 | 'Apply these changes and remove this from your list of shelved changes', |
| b69ab31 | | | 146 | )}> |
| b69ab31 | | | 147 | <OperationDisabledButton |
| b69ab31 | | | 148 | contextKey={`unshelve-${change.hash}`} |
| b69ab31 | | | 149 | className="unshelve-button" |
| b69ab31 | | | 150 | runOperation={() => { |
| b69ab31 | | | 151 | dismiss(); |
| b69ab31 | | | 152 | return new UnshelveOperation(change, false); |
| b69ab31 | | | 153 | }} |
| b69ab31 | | | 154 | icon={<Icon icon="layers-active" slot="start" />}> |
| b69ab31 | | | 155 | <T>Unshelve</T> |
| b69ab31 | | | 156 | </OperationDisabledButton> |
| b69ab31 | | | 157 | </Tooltip> |
| b69ab31 | | | 158 | </div> |
| b69ab31 | | | 159 | <OpenComparisonViewButton |
| b69ab31 | | | 160 | comparison={comparison} |
| b69ab31 | | | 161 | buttonText={<T>View Changes</T>} |
| b69ab31 | | | 162 | onClick={dismiss} |
| b69ab31 | | | 163 | /> |
| b69ab31 | | | 164 | <div className="shelved-changes-item-row"> |
| b69ab31 | | | 165 | <ChangedFiles |
| b69ab31 | | | 166 | filesSubset={change.filesSample} |
| b69ab31 | | | 167 | totalFiles={change.totalFileCount} |
| b69ab31 | | | 168 | comparison={comparison} |
| b69ab31 | | | 169 | /> |
| b69ab31 | | | 170 | </div> |
| b69ab31 | | | 171 | </div> |
| b69ab31 | | | 172 | ); |
| b69ab31 | | | 173 | })} |
| b69ab31 | | | 174 | </div> |
| b69ab31 | | | 175 | )} |
| b69ab31 | | | 176 | </DropdownFields> |
| b69ab31 | | | 177 | ); |
| b69ab31 | | | 178 | } |