| 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 {PrimitiveAtom} from 'jotai'; |
| b69ab31 | | | 9 | import type {ComponentProps} from 'react'; |
| b69ab31 | | | 10 | import type {Operation} from './operations/Operation'; |
| b69ab31 | | | 11 | |
| b69ab31 | | | 12 | import {Button} from 'isl-components/Button'; |
| b69ab31 | | | 13 | import {Icon} from 'isl-components/Icon'; |
| b69ab31 | | | 14 | import {atom, useAtom} from 'jotai'; |
| b69ab31 | | | 15 | import {isPromise} from 'shared/utils'; |
| b69ab31 | | | 16 | import {atomFamilyWeak} from './jotaiUtils'; |
| b69ab31 | | | 17 | import {useRunOperation} from './operationsState'; |
| b69ab31 | | | 18 | import {useMostRecentPendingOperation} from './previews'; |
| b69ab31 | | | 19 | |
| b69ab31 | | | 20 | /** |
| b69ab31 | | | 21 | * Wrapper around VSCodeButton intended for buttons which runOperations. |
| b69ab31 | | | 22 | * It remembers what Operation it spawns, and leaves the button disabled |
| b69ab31 | | | 23 | * if that operation is the most recent pending operation (queued or running). |
| b69ab31 | | | 24 | * If any further operations have been queued, then button will be re-enabled |
| b69ab31 | | | 25 | * (to allow queueing it again which may be valid). |
| b69ab31 | | | 26 | * |
| b69ab31 | | | 27 | * Note: do not use "useRunOperation" directly in the "runOperation", instead return the operation instance. |
| b69ab31 | | | 28 | * |
| b69ab31 | | | 29 | * runOperation may also return an Array of operations, if it queues multiple. |
| b69ab31 | | | 30 | * If the pending operation is ANY of those operations, the button will be disabled. |
| b69ab31 | | | 31 | * |
| b69ab31 | | | 32 | * Provide a `contextKey` to describe what this button is doing, to correlate with its resulting operation. |
| b69ab31 | | | 33 | * Generally this is just the name of the operation, but for operations that e.g. depend on a commit, |
| b69ab31 | | | 34 | * it may also include the commit hash so not every instance of this button is disabled. |
| b69ab31 | | | 35 | */ |
| b69ab31 | | | 36 | export function OperationDisabledButton({ |
| b69ab31 | | | 37 | contextKey, |
| b69ab31 | | | 38 | runOperation, |
| b69ab31 | | | 39 | disabled, |
| b69ab31 | | | 40 | children, |
| b69ab31 | | | 41 | icon, |
| b69ab31 | | | 42 | ...rest |
| b69ab31 | | | 43 | }: { |
| b69ab31 | | | 44 | contextKey: string; |
| b69ab31 | | | 45 | runOperation: () => |
| b69ab31 | | | 46 | | Operation |
| b69ab31 | | | 47 | | Array<Operation> |
| b69ab31 | | | 48 | | undefined |
| b69ab31 | | | 49 | | Promise<Operation | Array<Operation> | undefined>; |
| b69ab31 | | | 50 | children?: React.ReactNode; |
| b69ab31 | | | 51 | disabled?: boolean; |
| b69ab31 | | | 52 | icon?: React.ReactNode; |
| b69ab31 | | | 53 | className?: string; |
| b69ab31 | | | 54 | } & (Omit<ComponentProps<typeof Button>, 'icon' | 'primary'> & {kind?: string})) { |
| b69ab31 | | | 55 | const actuallyRunOperation = useRunOperation(); |
| b69ab31 | | | 56 | const pendingOperation = useMostRecentPendingOperation(); |
| b69ab31 | | | 57 | const [triggeredOperationId, setTriggeredOperationId] = useAtom( |
| b69ab31 | | | 58 | operationButtonDisableState(contextKey), |
| b69ab31 | | | 59 | ); |
| b69ab31 | | | 60 | const isRunningThisOperation = |
| b69ab31 | | | 61 | pendingOperation != null && triggeredOperationId?.includes(pendingOperation.id); |
| b69ab31 | | | 62 | |
| b69ab31 | | | 63 | return ( |
| b69ab31 | | | 64 | <Button |
| b69ab31 | | | 65 | {...rest} |
| b69ab31 | | | 66 | disabled={isRunningThisOperation || disabled} |
| b69ab31 | | | 67 | onClick={async () => { |
| b69ab31 | | | 68 | const opOrOpsResult = runOperation(); |
| b69ab31 | | | 69 | let opOrOps; |
| b69ab31 | | | 70 | if (isPromise(opOrOpsResult)) { |
| b69ab31 | | | 71 | opOrOps = await opOrOpsResult; |
| b69ab31 | | | 72 | } else { |
| b69ab31 | | | 73 | opOrOps = opOrOpsResult; |
| b69ab31 | | | 74 | } |
| b69ab31 | | | 75 | if (opOrOps == null) { |
| b69ab31 | | | 76 | return; |
| b69ab31 | | | 77 | } |
| b69ab31 | | | 78 | const ops = Array.isArray(opOrOps) ? opOrOps : [opOrOps]; |
| b69ab31 | | | 79 | for (const op of ops) { |
| b69ab31 | | | 80 | actuallyRunOperation(op); |
| b69ab31 | | | 81 | } |
| b69ab31 | | | 82 | setTriggeredOperationId(ops.map(op => op.id)); |
| b69ab31 | | | 83 | }}> |
| b69ab31 | | | 84 | {isRunningThisOperation ? <Icon icon="loading" slot="start" /> : (icon ?? null)} |
| b69ab31 | | | 85 | {children} |
| b69ab31 | | | 86 | </Button> |
| b69ab31 | | | 87 | ); |
| b69ab31 | | | 88 | } |
| b69ab31 | | | 89 | |
| b69ab31 | | | 90 | const operationButtonDisableState = atomFamilyWeak< |
| b69ab31 | | | 91 | string, |
| b69ab31 | | | 92 | PrimitiveAtom<Array<string> | undefined> |
| b69ab31 | | | 93 | >((_param: string | undefined) => atom<Array<string> | undefined>(undefined)); |