| 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 {ContextMenuItem} from 'shared/ContextMenu'; |
| b69ab31 | | | 9 | import type {RepoRelativePath} from './types'; |
| b69ab31 | | | 10 | |
| b69ab31 | | | 11 | import {Button} from 'isl-components/Button'; |
| b69ab31 | | | 12 | import {Icon} from 'isl-components/Icon'; |
| b69ab31 | | | 13 | import {Subtle} from 'isl-components/Subtle'; |
| b69ab31 | | | 14 | import {atom, useAtomValue} from 'jotai'; |
| b69ab31 | | | 15 | import {useContextMenu} from 'shared/ContextMenu'; |
| b69ab31 | | | 16 | import {minimalDisambiguousPaths} from 'shared/minimalDisambiguousPaths'; |
| b69ab31 | | | 17 | import serverAPI from './ClientToServerAPI'; |
| b69ab31 | | | 18 | import {Column, Row} from './ComponentUtils'; |
| b69ab31 | | | 19 | import {availableCwds} from './CwdSelector'; |
| b69ab31 | | | 20 | import {T, t} from './i18n'; |
| b69ab31 | | | 21 | import {readAtom, writeAtom} from './jotaiUtils'; |
| b69ab31 | | | 22 | import platform from './platform'; |
| b69ab31 | | | 23 | import {showModal} from './useModal'; |
| b69ab31 | | | 24 | import {registerCleanup, registerDisposable} from './utils'; |
| b69ab31 | | | 25 | |
| b69ab31 | | | 26 | /** |
| b69ab31 | | | 27 | * A list of files for this repo that are unsaved in the IDE. |
| b69ab31 | | | 28 | * This is always `[]` for unsupported platforms like browser. |
| b69ab31 | | | 29 | */ |
| b69ab31 | | | 30 | export const unsavedFiles = atom<Array<{path: RepoRelativePath; uri: string}>>([]); |
| b69ab31 | | | 31 | registerCleanup( |
| b69ab31 | | | 32 | availableCwds, |
| b69ab31 | | | 33 | serverAPI.onConnectOrReconnect(() => { |
| b69ab31 | | | 34 | serverAPI.postMessage({ |
| b69ab31 | | | 35 | type: 'platform/subscribeToUnsavedFiles', |
| b69ab31 | | | 36 | }); |
| b69ab31 | | | 37 | }), |
| b69ab31 | | | 38 | import.meta.hot, |
| b69ab31 | | | 39 | ); |
| b69ab31 | | | 40 | registerDisposable( |
| b69ab31 | | | 41 | availableCwds, |
| b69ab31 | | | 42 | serverAPI.onMessageOfType('platform/unsavedFiles', event => |
| b69ab31 | | | 43 | writeAtom(unsavedFiles, event.unsaved), |
| b69ab31 | | | 44 | ), |
| b69ab31 | | | 45 | import.meta.hot, |
| b69ab31 | | | 46 | ); |
| b69ab31 | | | 47 | |
| b69ab31 | | | 48 | export function UnsavedFilesCount() { |
| b69ab31 | | | 49 | const unsaved = useAtomValue(unsavedFiles); |
| b69ab31 | | | 50 | |
| b69ab31 | | | 51 | const menu = useContextMenu(() => { |
| b69ab31 | | | 52 | const fullPaths = unsaved.map(({path}) => path); |
| b69ab31 | | | 53 | const disambiguated = minimalDisambiguousPaths(fullPaths); |
| b69ab31 | | | 54 | const options: Array<ContextMenuItem> = disambiguated.map((name, i) => ({ |
| b69ab31 | | | 55 | label: t('Open $name', {replace: {$name: name}}), |
| b69ab31 | | | 56 | onClick: () => { |
| b69ab31 | | | 57 | platform.openFile(fullPaths[i]); |
| b69ab31 | | | 58 | }, |
| b69ab31 | | | 59 | })); |
| b69ab31 | | | 60 | options.push({type: 'divider'}); |
| b69ab31 | | | 61 | options.push({ |
| b69ab31 | | | 62 | label: t('Save All'), |
| b69ab31 | | | 63 | onClick: () => serverAPI.postMessage({type: 'platform/saveAllUnsavedFiles'}), |
| b69ab31 | | | 64 | }); |
| b69ab31 | | | 65 | return options; |
| b69ab31 | | | 66 | }); |
| b69ab31 | | | 67 | |
| b69ab31 | | | 68 | if (unsaved.length === 0) { |
| b69ab31 | | | 69 | return null; |
| b69ab31 | | | 70 | } |
| b69ab31 | | | 71 | return ( |
| b69ab31 | | | 72 | <Subtle> |
| b69ab31 | | | 73 | <Row> |
| b69ab31 | | | 74 | <T count={unsaved.length}>unsavedFileCount</T> |
| b69ab31 | | | 75 | <Button icon> |
| b69ab31 | | | 76 | <Icon icon="ellipsis" onClick={menu} /> |
| b69ab31 | | | 77 | </Button> |
| b69ab31 | | | 78 | </Row> |
| b69ab31 | | | 79 | </Subtle> |
| b69ab31 | | | 80 | ); |
| b69ab31 | | | 81 | } |
| b69ab31 | | | 82 | |
| b69ab31 | | | 83 | /** |
| b69ab31 | | | 84 | * If there are unsaved files, ask the user if they want to save them. |
| b69ab31 | | | 85 | * Returns true if the user wants to continue with the operation (after possibly having saved the files), |
| b69ab31 | | | 86 | * false if they cancelled. |
| b69ab31 | | | 87 | */ |
| b69ab31 | | | 88 | export async function confirmUnsavedFiles(): Promise<boolean> { |
| b69ab31 | | | 89 | const unsaved = readAtom(unsavedFiles); |
| b69ab31 | | | 90 | if (unsaved.length === 0) { |
| b69ab31 | | | 91 | return true; |
| b69ab31 | | | 92 | } |
| b69ab31 | | | 93 | |
| b69ab31 | | | 94 | const buttons = [ |
| b69ab31 | | | 95 | t('Cancel'), |
| b69ab31 | | | 96 | t('Continue Without Saving'), |
| b69ab31 | | | 97 | {label: t('Save All and Continue'), primary: true}, |
| b69ab31 | | | 98 | ]; |
| b69ab31 | | | 99 | const answer = await showModal({ |
| b69ab31 | | | 100 | type: 'confirm', |
| b69ab31 | | | 101 | buttons, |
| b69ab31 | | | 102 | title: <T count={unsaved.length}>confirmUnsavedFileCount</T>, |
| b69ab31 | | | 103 | message: ( |
| b69ab31 | | | 104 | <Column alignStart> |
| b69ab31 | | | 105 | <Column alignStart> |
| b69ab31 | | | 106 | {unsaved.map(({path}) => ( |
| b69ab31 | | | 107 | <Row key={path}>{path}</Row> |
| b69ab31 | | | 108 | ))} |
| b69ab31 | | | 109 | </Column> |
| b69ab31 | | | 110 | <Row> |
| b69ab31 | | | 111 | <T count={unsaved.length}>doYouWantToSaveThem</T> |
| b69ab31 | | | 112 | </Row> |
| b69ab31 | | | 113 | </Column> |
| b69ab31 | | | 114 | ), |
| b69ab31 | | | 115 | }); |
| b69ab31 | | | 116 | |
| b69ab31 | | | 117 | if (answer === buttons[2]) { |
| b69ab31 | | | 118 | serverAPI.postMessage({type: 'platform/saveAllUnsavedFiles'}); |
| b69ab31 | | | 119 | const message = await serverAPI.nextMessageMatching( |
| b69ab31 | | | 120 | 'platform/savedAllUnsavedFiles', |
| b69ab31 | | | 121 | () => true, |
| b69ab31 | | | 122 | ); |
| b69ab31 | | | 123 | return message.success; |
| b69ab31 | | | 124 | } |
| b69ab31 | | | 125 | |
| b69ab31 | | | 126 | return answer === buttons[1]; |
| b69ab31 | | | 127 | } |