| 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 {TrackEventName} from 'isl-server/src/analytics/eventNames'; |
| b69ab31 | | | 9 | import type {CommitInfo} from '../../types'; |
| b69ab31 | | | 10 | |
| b69ab31 | | | 11 | import {Button} from 'isl-components/Button'; |
| b69ab31 | | | 12 | import {Tooltip} from 'isl-components/Tooltip'; |
| b69ab31 | | | 13 | import {useAtomValue, useSetAtom} from 'jotai'; |
| b69ab31 | | | 14 | import {tracker} from '../../analytics'; |
| b69ab31 | | | 15 | import {t} from '../../i18n'; |
| b69ab31 | | | 16 | import {uncommittedChangesWithPreviews} from '../../previews'; |
| b69ab31 | | | 17 | import {useConfirmUnsavedEditsBeforeSplit} from './ConfirmUnsavedEditsBeforeSplit'; |
| b69ab31 | | | 18 | import { |
| b69ab31 | | | 19 | bumpStackEditMetric, |
| b69ab31 | | | 20 | editingStackIntentionHashes, |
| b69ab31 | | | 21 | shouldAutoSplitState, |
| b69ab31 | | | 22 | } from './stackEditState'; |
| b69ab31 | | | 23 | |
| b69ab31 | | | 24 | export interface BaseSplitButtonProps { |
| b69ab31 | | | 25 | /** The commit to split */ |
| b69ab31 | | | 26 | commit: CommitInfo; |
| b69ab31 | | | 27 | /** The event name to track when the button is clicked */ |
| b69ab31 | | | 28 | trackerEventName: TrackEventName; |
| b69ab31 | | | 29 | /** Whether to automatically trigger AI split */ |
| b69ab31 | | | 30 | autoSplit?: boolean; |
| b69ab31 | | | 31 | /** Function to call after the split action is initiated */ |
| b69ab31 | | | 32 | onSplitInitiated?: () => void; |
| b69ab31 | | | 33 | /** Whether to bump the "splitFromSuggestion" metric */ |
| b69ab31 | | | 34 | bumpSplitFromSuggestion?: boolean; |
| b69ab31 | | | 35 | /** Children to render inside the button */ |
| b69ab31 | | | 36 | children: React.ReactNode; |
| b69ab31 | | | 37 | } |
| b69ab31 | | | 38 | |
| b69ab31 | | | 39 | /** Base button component for initiating split operations */ |
| b69ab31 | | | 40 | export function BaseSplitButton({ |
| b69ab31 | | | 41 | commit, |
| b69ab31 | | | 42 | trackerEventName, |
| b69ab31 | | | 43 | autoSplit = false, |
| b69ab31 | | | 44 | onSplitInitiated, |
| b69ab31 | | | 45 | bumpSplitFromSuggestion = false, |
| b69ab31 | | | 46 | children, |
| b69ab31 | | | 47 | ...buttonProps |
| b69ab31 | | | 48 | }: BaseSplitButtonProps & React.ComponentProps<typeof Button>) { |
| b69ab31 | | | 49 | const confirmUnsavedEditsBeforeSplit = useConfirmUnsavedEditsBeforeSplit(); |
| b69ab31 | | | 50 | const setEditStackIntentionHashes = useSetAtom(editingStackIntentionHashes); |
| b69ab31 | | | 51 | const setShouldAutoSplit = useSetAtom(shouldAutoSplitState); |
| b69ab31 | | | 52 | |
| b69ab31 | | | 53 | const uncommittedChanges = useAtomValue(uncommittedChangesWithPreviews); |
| b69ab31 | | | 54 | const hasUncommittedChanges = uncommittedChanges.length > 0; |
| b69ab31 | | | 55 | |
| b69ab31 | | | 56 | const onClick = async (e: React.MouseEvent) => { |
| b69ab31 | | | 57 | if (!(await confirmUnsavedEditsBeforeSplit([commit], 'split'))) { |
| b69ab31 | | | 58 | return; |
| b69ab31 | | | 59 | } |
| b69ab31 | | | 60 | setEditStackIntentionHashes(['split', new Set([commit.hash])]); |
| b69ab31 | | | 61 | if (autoSplit) { |
| b69ab31 | | | 62 | setShouldAutoSplit(true); |
| b69ab31 | | | 63 | } |
| b69ab31 | | | 64 | if (bumpSplitFromSuggestion) { |
| b69ab31 | | | 65 | bumpStackEditMetric('splitFromSuggestion'); |
| b69ab31 | | | 66 | } |
| b69ab31 | | | 67 | tracker.track(trackerEventName); |
| b69ab31 | | | 68 | if (onSplitInitiated) { |
| b69ab31 | | | 69 | onSplitInitiated(); |
| b69ab31 | | | 70 | } |
| b69ab31 | | | 71 | e.stopPropagation(); |
| b69ab31 | | | 72 | }; |
| b69ab31 | | | 73 | |
| b69ab31 | | | 74 | return ( |
| b69ab31 | | | 75 | <Tooltip |
| b69ab31 | | | 76 | title={hasUncommittedChanges ? t('Cannot currently split with uncommitted changes') : ''} |
| b69ab31 | | | 77 | trigger={hasUncommittedChanges ? 'hover' : 'disabled'}> |
| b69ab31 | | | 78 | <Button onClick={onClick} disabled={hasUncommittedChanges} {...buttonProps}> |
| b69ab31 | | | 79 | {children} |
| b69ab31 | | | 80 | </Button> |
| b69ab31 | | | 81 | </Tooltip> |
| b69ab31 | | | 82 | ); |
| b69ab31 | | | 83 | } |