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