| 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 {UICodeReviewProvider} from './codeReview/UICodeReviewProvider'; |
| b69ab31 | | | 9 | import type {CommitInfo, DiffSummary, Hash} from './types'; |
| b69ab31 | | | 10 | |
| b69ab31 | | | 11 | import {Button} from 'isl-components/Button'; |
| b69ab31 | | | 12 | import {Icon} from 'isl-components/Icon'; |
| b69ab31 | | | 13 | import {Tooltip} from 'isl-components/Tooltip'; |
| b69ab31 | | | 14 | import {useAtomValue} from 'jotai'; |
| b69ab31 | | | 15 | import {nullthrows} from 'shared/utils'; |
| b69ab31 | | | 16 | import {OperationDisabledButton} from './OperationDisabledButton'; |
| b69ab31 | | | 17 | import {allDiffSummaries, codeReviewProvider} from './codeReview/CodeReviewInfo'; |
| b69ab31 | | | 18 | import {t, T} from './i18n'; |
| b69ab31 | | | 19 | import {HideOperation} from './operations/HideOperation'; |
| b69ab31 | | | 20 | import {useRunOperation} from './operationsState'; |
| b69ab31 | | | 21 | import {type Dag, dagWithPreviews} from './previews'; |
| b69ab31 | | | 22 | import {latestSuccessorUnlessExplicitlyObsolete} from './successionUtils'; |
| b69ab31 | | | 23 | |
| b69ab31 | | | 24 | export function isStackEligibleForCleanup( |
| b69ab31 | | | 25 | hash: Hash, |
| b69ab31 | | | 26 | dag: Dag, |
| b69ab31 | | | 27 | diffMap: Map<string, DiffSummary>, |
| b69ab31 | | | 28 | provider: UICodeReviewProvider, |
| b69ab31 | | | 29 | ): boolean { |
| b69ab31 | | | 30 | return dag |
| b69ab31 | | | 31 | .descendants(hash) |
| b69ab31 | | | 32 | .toSeq() |
| b69ab31 | | | 33 | .every(h => { |
| b69ab31 | | | 34 | const info = dag.get(h); |
| b69ab31 | | | 35 | // don't allow hiding a stack you're checked out on |
| b69ab31 | | | 36 | if (info == null || info.isDot) { |
| b69ab31 | | | 37 | return false; |
| b69ab31 | | | 38 | } |
| b69ab31 | | | 39 | // allow clean up obsoleted commits regardless of review state |
| b69ab31 | | | 40 | if (info.successorInfo != null) { |
| b69ab31 | | | 41 | return true; |
| b69ab31 | | | 42 | } |
| b69ab31 | | | 43 | // if not obsoleted, still allow cleanup for "Closed" diffs |
| b69ab31 | | | 44 | if (info.diffId != null) { |
| b69ab31 | | | 45 | const diffInfo = diffMap.get(info.diffId); |
| b69ab31 | | | 46 | if (diffInfo != null && provider.isDiffEligibleForCleanup(diffInfo)) { |
| b69ab31 | | | 47 | return true; |
| b69ab31 | | | 48 | } |
| b69ab31 | | | 49 | } |
| b69ab31 | | | 50 | // no cleanup otherwise |
| b69ab31 | | | 51 | return false; |
| b69ab31 | | | 52 | }); |
| b69ab31 | | | 53 | } |
| b69ab31 | | | 54 | |
| b69ab31 | | | 55 | export function CleanupButton({commit, hasChildren}: {commit: CommitInfo; hasChildren: boolean}) { |
| b69ab31 | | | 56 | const runOperation = useRunOperation(); |
| b69ab31 | | | 57 | return ( |
| b69ab31 | | | 58 | <Tooltip |
| b69ab31 | | | 59 | title={ |
| b69ab31 | | | 60 | hasChildren |
| b69ab31 | | | 61 | ? t('You can safely "clean up" by hiding this stack of commits.') |
| b69ab31 | | | 62 | : t('You can safely "clean up" by hiding this commit.') |
| b69ab31 | | | 63 | } |
| b69ab31 | | | 64 | placement="bottom"> |
| b69ab31 | | | 65 | <Button |
| b69ab31 | | | 66 | icon |
| b69ab31 | | | 67 | onClick={() => { |
| b69ab31 | | | 68 | runOperation(new HideOperation(latestSuccessorUnlessExplicitlyObsolete(commit))); |
| b69ab31 | | | 69 | }}> |
| b69ab31 | | | 70 | <Icon icon="eye-closed" slot="start" /> |
| b69ab31 | | | 71 | {hasChildren ? <T>Clean up stack</T> : <T>Clean up</T>} |
| b69ab31 | | | 72 | </Button> |
| b69ab31 | | | 73 | </Tooltip> |
| b69ab31 | | | 74 | ); |
| b69ab31 | | | 75 | } |
| b69ab31 | | | 76 | |
| b69ab31 | | | 77 | export function CleanupAllButton() { |
| b69ab31 | | | 78 | const dag = useAtomValue(dagWithPreviews); |
| b69ab31 | | | 79 | const reviewProvider = useAtomValue(codeReviewProvider); |
| b69ab31 | | | 80 | const diffMap = useAtomValue(allDiffSummaries)?.value; |
| b69ab31 | | | 81 | if (diffMap == null || reviewProvider == null) { |
| b69ab31 | | | 82 | return null; |
| b69ab31 | | | 83 | } |
| b69ab31 | | | 84 | |
| b69ab31 | | | 85 | const stackBases = dag.roots(dag.draft()).toArray(); |
| b69ab31 | | | 86 | const cleanableStacks = stackBases.filter(hash => |
| b69ab31 | | | 87 | isStackEligibleForCleanup(hash, dag, diffMap, reviewProvider), |
| b69ab31 | | | 88 | ); |
| b69ab31 | | | 89 | |
| b69ab31 | | | 90 | const disabled = cleanableStacks.length === 0; |
| b69ab31 | | | 91 | return ( |
| b69ab31 | | | 92 | <Tooltip |
| b69ab31 | | | 93 | title={ |
| b69ab31 | | | 94 | disabled |
| b69ab31 | | | 95 | ? t('No landed or closed commits to hide') |
| b69ab31 | | | 96 | : t('Hide all commits for landed or closed Diffs') |
| b69ab31 | | | 97 | }> |
| b69ab31 | | | 98 | <OperationDisabledButton |
| b69ab31 | | | 99 | contextKey="cleanup-all" |
| b69ab31 | | | 100 | runOperation={() => { |
| b69ab31 | | | 101 | return cleanableStacks.map(hash => { |
| b69ab31 | | | 102 | const info = nullthrows(dag.get(hash)); |
| b69ab31 | | | 103 | return new HideOperation(latestSuccessorUnlessExplicitlyObsolete(info)); |
| b69ab31 | | | 104 | }); |
| b69ab31 | | | 105 | }} |
| b69ab31 | | | 106 | icon={<Icon icon="eye-closed" slot="start" />} |
| b69ab31 | | | 107 | disabled={disabled}> |
| b69ab31 | | | 108 | <T>Clean up all</T> |
| b69ab31 | | | 109 | </OperationDisabledButton> |
| b69ab31 | | | 110 | </Tooltip> |
| b69ab31 | | | 111 | ); |
| b69ab31 | | | 112 | } |