| 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 {ReactNode} from 'react'; |
| b69ab31 | | | 9 | import type {Comparison} from 'shared/Comparison'; |
| b69ab31 | | | 10 | |
| b69ab31 | | | 11 | import {Button} from 'isl-components/Button'; |
| b69ab31 | | | 12 | import {Icon} from 'isl-components/Icon'; |
| b69ab31 | | | 13 | import {ComparisonType} from 'shared/Comparison'; |
| b69ab31 | | | 14 | import {T, t} from '../i18n'; |
| b69ab31 | | | 15 | import {short} from '../utils'; |
| b69ab31 | | | 16 | import {showComparison} from './atoms'; |
| b69ab31 | | | 17 | |
| b69ab31 | | | 18 | export function OpenComparisonViewButton({ |
| b69ab31 | | | 19 | comparison, |
| b69ab31 | | | 20 | buttonText, |
| b69ab31 | | | 21 | onClick, |
| b69ab31 | | | 22 | }: { |
| b69ab31 | | | 23 | comparison: Comparison; |
| b69ab31 | | | 24 | buttonText?: ReactNode; |
| b69ab31 | | | 25 | onClick?: () => unknown; |
| b69ab31 | | | 26 | }) { |
| b69ab31 | | | 27 | const isFake = |
| b69ab31 | | | 28 | comparison.type === ComparisonType.Committed && comparison.hash.startsWith('OPTIMISTIC'); |
| b69ab31 | | | 29 | return ( |
| b69ab31 | | | 30 | <Button |
| b69ab31 | | | 31 | data-testid={`open-comparison-view-button-${comparison.type}`} |
| b69ab31 | | | 32 | icon |
| b69ab31 | | | 33 | disabled={isFake} |
| b69ab31 | | | 34 | onClick={() => { |
| b69ab31 | | | 35 | onClick?.(); |
| b69ab31 | | | 36 | showComparison(comparison); |
| b69ab31 | | | 37 | }}> |
| b69ab31 | | | 38 | <Icon icon="files" slot="start" /> |
| b69ab31 | | | 39 | {isFake ? <T>View Changes</T> : (buttonText ?? buttonLabelForComparison(comparison))} |
| b69ab31 | | | 40 | </Button> |
| b69ab31 | | | 41 | ); |
| b69ab31 | | | 42 | } |
| b69ab31 | | | 43 | |
| b69ab31 | | | 44 | function buttonLabelForComparison(comparison: Comparison): string { |
| b69ab31 | | | 45 | switch (comparison.type) { |
| b69ab31 | | | 46 | case ComparisonType.UncommittedChanges: |
| b69ab31 | | | 47 | return t('View Changes'); |
| b69ab31 | | | 48 | case ComparisonType.HeadChanges: |
| b69ab31 | | | 49 | return t('View Head Changes'); |
| b69ab31 | | | 50 | case ComparisonType.StackChanges: |
| b69ab31 | | | 51 | return t('View Stack Changes'); |
| b69ab31 | | | 52 | case ComparisonType.Committed: |
| b69ab31 | | | 53 | return t('View Changes in $hash', {replace: {$hash: short(comparison.hash)}}); |
| b69ab31 | | | 54 | case ComparisonType.SinceLastCodeReviewSubmit: |
| b69ab31 | | | 55 | return t('Compare $hash with remote', {replace: {$hash: short(comparison.hash)}}); |
| b69ab31 | | | 56 | } |
| b69ab31 | | | 57 | } |