| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import type {ReactNode} from 'react'; |
| 9 | import type {Comparison} from 'shared/Comparison'; |
| 10 | |
| 11 | import {Button} from 'isl-components/Button'; |
| 12 | import {Icon} from 'isl-components/Icon'; |
| 13 | import {ComparisonType} from 'shared/Comparison'; |
| 14 | import {T, t} from '../i18n'; |
| 15 | import {short} from '../utils'; |
| 16 | import {showComparison} from './atoms'; |
| 17 | |
| 18 | export function OpenComparisonViewButton({ |
| 19 | comparison, |
| 20 | buttonText, |
| 21 | onClick, |
| 22 | }: { |
| 23 | comparison: Comparison; |
| 24 | buttonText?: ReactNode; |
| 25 | onClick?: () => unknown; |
| 26 | }) { |
| 27 | const isFake = |
| 28 | comparison.type === ComparisonType.Committed && comparison.hash.startsWith('OPTIMISTIC'); |
| 29 | return ( |
| 30 | <Button |
| 31 | data-testid={`open-comparison-view-button-${comparison.type}`} |
| 32 | icon |
| 33 | disabled={isFake} |
| 34 | onClick={() => { |
| 35 | onClick?.(); |
| 36 | showComparison(comparison); |
| 37 | }}> |
| 38 | <Icon icon="files" slot="start" /> |
| 39 | {isFake ? <T>View Changes</T> : (buttonText ?? buttonLabelForComparison(comparison))} |
| 40 | </Button> |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | function buttonLabelForComparison(comparison: Comparison): string { |
| 45 | switch (comparison.type) { |
| 46 | case ComparisonType.UncommittedChanges: |
| 47 | return t('View Changes'); |
| 48 | case ComparisonType.HeadChanges: |
| 49 | return t('View Head Changes'); |
| 50 | case ComparisonType.StackChanges: |
| 51 | return t('View Stack Changes'); |
| 52 | case ComparisonType.Committed: |
| 53 | return t('View Changes in $hash', {replace: {$hash: short(comparison.hash)}}); |
| 54 | case ComparisonType.SinceLastCodeReviewSubmit: |
| 55 | return t('Compare $hash with remote', {replace: {$hash: short(comparison.hash)}}); |
| 56 | } |
| 57 | } |
| 58 | |