| 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 * as stylex from '@stylexjs/stylex'; |
| b69ab31 | | | 9 | import {Button, buttonStyles} from 'isl-components/Button'; |
| b69ab31 | | | 10 | import {ButtonDropdown, styles} from 'isl-components/ButtonDropdown'; |
| b69ab31 | | | 11 | import {Row} from 'isl-components/Flex'; |
| b69ab31 | | | 12 | import {Icon} from 'isl-components/Icon'; |
| b69ab31 | | | 13 | import {Tooltip} from 'isl-components/Tooltip'; |
| b69ab31 | | | 14 | import {getZoomLevel} from 'isl-components/zoom'; |
| b69ab31 | | | 15 | import {atom, useAtomValue} from 'jotai'; |
| b69ab31 | | | 16 | import {loadable} from 'jotai/utils'; |
| b69ab31 | | | 17 | import {useEffect, useMemo, useRef, useState} from 'react'; |
| b69ab31 | | | 18 | import {useContextMenu} from 'shared/ContextMenu'; |
| b69ab31 | | | 19 | import {tracker} from '../analytics'; |
| b69ab31 | | | 20 | import serverAPI from '../ClientToServerAPI'; |
| b69ab31 | | | 21 | import {bulkFetchFeatureFlags, useFeatureFlagSync} from '../featureFlags'; |
| b69ab31 | | | 22 | import {t} from '../i18n'; |
| b69ab31 | | | 23 | import {Internal} from '../Internal'; |
| b69ab31 | | | 24 | import platform from '../platform'; |
| b69ab31 | | | 25 | import {optimisticMergeConflicts} from '../previews'; |
| b69ab31 | | | 26 | import {repositoryInfo} from '../serverAPIState'; |
| b69ab31 | | | 27 | import type {CommitInfo, PlatformSpecificClientToServerMessages} from '../types'; |
| b69ab31 | | | 28 | import {bumpSmartAction, useSortedActions} from './smartActionsOrdering'; |
| b69ab31 | | | 29 | import type {ActionContext, ActionMenuItem, SmartActionConfig} from './types'; |
| b69ab31 | | | 30 | |
| b69ab31 | | | 31 | const smartActionsConfig = [ |
| b69ab31 | | | 32 | // Internal actions |
| b69ab31 | | | 33 | ...(Internal.smartActions?.smartActionsConfig ?? []), |
| b69ab31 | | | 34 | // Public actions |
| b69ab31 | | | 35 | // TODO: Add public actions here |
| b69ab31 | | | 36 | ] satisfies SmartActionConfig[]; |
| b69ab31 | | | 37 | |
| b69ab31 | | | 38 | const smartActionFeatureFlagsAtom = atom<Promise<Record<string, boolean>>>(async () => { |
| b69ab31 | | | 39 | const flags: Record<string, boolean> = {}; |
| b69ab31 | | | 40 | |
| b69ab31 | | | 41 | const flagNames: string[] = []; |
| b69ab31 | | | 42 | for (const config of smartActionsConfig) { |
| b69ab31 | | | 43 | if (config.featureFlag && Internal.featureFlags?.[config.featureFlag]) { |
| b69ab31 | | | 44 | flagNames.push(Internal.featureFlags[config.featureFlag]); |
| b69ab31 | | | 45 | } |
| b69ab31 | | | 46 | } |
| b69ab31 | | | 47 | |
| b69ab31 | | | 48 | if (flagNames.length === 0) { |
| b69ab31 | | | 49 | return flags; |
| b69ab31 | | | 50 | } |
| b69ab31 | | | 51 | |
| b69ab31 | | | 52 | const results = await bulkFetchFeatureFlags(flagNames); |
| b69ab31 | | | 53 | |
| b69ab31 | | | 54 | // Map back from flag names to flag keys |
| b69ab31 | | | 55 | for (const config of smartActionsConfig) { |
| b69ab31 | | | 56 | if (config.featureFlag && Internal.featureFlags?.[config.featureFlag]) { |
| b69ab31 | | | 57 | const flagName = Internal.featureFlags[config.featureFlag]; |
| b69ab31 | | | 58 | flags[config.featureFlag as string] = results[flagName] ?? false; |
| b69ab31 | | | 59 | } |
| b69ab31 | | | 60 | } |
| b69ab31 | | | 61 | |
| b69ab31 | | | 62 | return flags; |
| b69ab31 | | | 63 | }); |
| b69ab31 | | | 64 | |
| b69ab31 | | | 65 | const loadableFeatureFlagsAtom = loadable(smartActionFeatureFlagsAtom); |
| b69ab31 | | | 66 | |
| b69ab31 | | | 67 | export function SmartActionsDropdown({commit}: {commit?: CommitInfo}) { |
| b69ab31 | | | 68 | const smartActionsMenuEnabled = useFeatureFlagSync(Internal.featureFlags?.SmartActionsMenu); |
| b69ab31 | | | 69 | const repo = useAtomValue(repositoryInfo); |
| b69ab31 | | | 70 | const conflicts = useAtomValue(optimisticMergeConflicts); |
| b69ab31 | | | 71 | const featureFlagsLoadable = useAtomValue(loadableFeatureFlagsAtom); |
| b69ab31 | | | 72 | const dropdownButtonRef = useRef<HTMLButtonElement>(null); |
| b69ab31 | | | 73 | |
| b69ab31 | | | 74 | const context: ActionContext = useMemo( |
| b69ab31 | | | 75 | () => ({ |
| b69ab31 | | | 76 | commit, |
| b69ab31 | | | 77 | repoPath: repo?.repoRoot, |
| b69ab31 | | | 78 | conflicts, |
| b69ab31 | | | 79 | }), |
| b69ab31 | | | 80 | [commit, repo?.repoRoot, conflicts], |
| b69ab31 | | | 81 | ); |
| b69ab31 | | | 82 | |
| b69ab31 | | | 83 | const availableActionItems = useMemo(() => { |
| b69ab31 | | | 84 | const featureFlagResults = |
| b69ab31 | | | 85 | featureFlagsLoadable.state === 'hasData' ? featureFlagsLoadable.data : {}; |
| b69ab31 | | | 86 | const items: ActionMenuItem[] = []; |
| b69ab31 | | | 87 | |
| b69ab31 | | | 88 | if (featureFlagsLoadable.state === 'hasData') { |
| b69ab31 | | | 89 | for (const config of smartActionsConfig) { |
| b69ab31 | | | 90 | if ( |
| b69ab31 | | | 91 | shouldShowSmartAction( |
| b69ab31 | | | 92 | config, |
| b69ab31 | | | 93 | context, |
| b69ab31 | | | 94 | config.featureFlag ? featureFlagResults[config.featureFlag as string] : true, |
| b69ab31 | | | 95 | ) |
| b69ab31 | | | 96 | ) { |
| b69ab31 | | | 97 | items.push({ |
| b69ab31 | | | 98 | id: config.id, |
| b69ab31 | | | 99 | label: config.label, |
| b69ab31 | | | 100 | config, |
| b69ab31 | | | 101 | }); |
| b69ab31 | | | 102 | } |
| b69ab31 | | | 103 | } |
| b69ab31 | | | 104 | } |
| b69ab31 | | | 105 | |
| b69ab31 | | | 106 | return items; |
| b69ab31 | | | 107 | }, [featureFlagsLoadable, context]); |
| b69ab31 | | | 108 | |
| b69ab31 | | | 109 | const sortedActionItems = useSortedActions(availableActionItems); |
| b69ab31 | | | 110 | |
| b69ab31 | | | 111 | const [selectedAction, setSelectedAction] = useState<ActionMenuItem | undefined>(undefined); |
| b69ab31 | | | 112 | |
| b69ab31 | | | 113 | useEffect(() => { |
| b69ab31 | | | 114 | if ( |
| b69ab31 | | | 115 | !selectedAction || // No action selected |
| b69ab31 | | | 116 | !sortedActionItems.find(item => item.id === selectedAction.id) // Selected action is no longer available |
| b69ab31 | | | 117 | ) { |
| b69ab31 | | | 118 | setSelectedAction(sortedActionItems[0]); |
| b69ab31 | | | 119 | } |
| b69ab31 | | | 120 | }, [selectedAction, sortedActionItems]); |
| b69ab31 | | | 121 | |
| b69ab31 | | | 122 | const contextMenu = useContextMenu(() => |
| b69ab31 | | | 123 | sortedActionItems.map(actionItem => ({ |
| b69ab31 | | | 124 | label: ( |
| b69ab31 | | | 125 | // Mark the current action as selected |
| b69ab31 | | | 126 | <Row> |
| b69ab31 | | | 127 | <Icon icon={actionItem.id === selectedAction?.id ? 'check' : 'blank'} /> |
| b69ab31 | | | 128 | {actionItem.label} |
| b69ab31 | | | 129 | </Row> |
| b69ab31 | | | 130 | ), |
| b69ab31 | | | 131 | onClick: () => { |
| b69ab31 | | | 132 | setSelectedAction(actionItem); |
| b69ab31 | | | 133 | // Run the action immediately on click instead of requiring a second click |
| b69ab31 | | | 134 | runSmartAction(actionItem.config, context); |
| b69ab31 | | | 135 | bumpSmartAction(actionItem.id); |
| b69ab31 | | | 136 | }, |
| b69ab31 | | | 137 | tooltip: actionItem.config.description ? t(actionItem.config.description) : undefined, |
| b69ab31 | | | 138 | })), |
| b69ab31 | | | 139 | ); |
| b69ab31 | | | 140 | |
| b69ab31 | | | 141 | if (featureFlagsLoadable.state !== 'hasData') { |
| b69ab31 | | | 142 | return null; |
| b69ab31 | | | 143 | } |
| b69ab31 | | | 144 | |
| b69ab31 | | | 145 | if ( |
| b69ab31 | | | 146 | !smartActionsMenuEnabled || |
| b69ab31 | | | 147 | !Internal.smartActions?.showSmartActions || |
| b69ab31 | | | 148 | sortedActionItems.length === 0 || |
| b69ab31 | | | 149 | !selectedAction |
| b69ab31 | | | 150 | ) { |
| b69ab31 | | | 151 | return null; |
| b69ab31 | | | 152 | } |
| b69ab31 | | | 153 | |
| b69ab31 | | | 154 | let buttonComponent; |
| b69ab31 | | | 155 | |
| b69ab31 | | | 156 | const description = selectedAction.config.description |
| b69ab31 | | | 157 | ? t(selectedAction.config.description) |
| b69ab31 | | | 158 | : undefined; |
| b69ab31 | | | 159 | const tooltip = |
| b69ab31 | | | 160 | description != null && Internal.smartActions?.renderModifierContextTooltip != null |
| b69ab31 | | | 161 | ? Internal.smartActions.renderModifierContextTooltip(description) |
| b69ab31 | | | 162 | : description; |
| b69ab31 | | | 163 | |
| b69ab31 | | | 164 | if (sortedActionItems.length === 1) { |
| b69ab31 | | | 165 | const singleAction = sortedActionItems[0]; |
| b69ab31 | | | 166 | buttonComponent = ( |
| b69ab31 | | | 167 | <SmartActionWithContext config={singleAction.config} context={context} tooltip={tooltip}> |
| b69ab31 | | | 168 | <Button |
| b69ab31 | | | 169 | kind="icon" |
| b69ab31 | | | 170 | onClick={e => { |
| b69ab31 | | | 171 | if (e.altKey) { |
| b69ab31 | | | 172 | return; |
| b69ab31 | | | 173 | } |
| b69ab31 | | | 174 | e.stopPropagation(); |
| b69ab31 | | | 175 | runSmartAction(singleAction.config, context); |
| b69ab31 | | | 176 | bumpSmartAction(singleAction.id); |
| b69ab31 | | | 177 | }}> |
| b69ab31 | | | 178 | <Icon icon="lightbulb-sparkle" /> |
| b69ab31 | | | 179 | {singleAction.label} |
| b69ab31 | | | 180 | </Button> |
| b69ab31 | | | 181 | </SmartActionWithContext> |
| b69ab31 | | | 182 | ); |
| b69ab31 | | | 183 | } else { |
| b69ab31 | | | 184 | buttonComponent = ( |
| b69ab31 | | | 185 | <SmartActionWithContext config={selectedAction.config} context={context} tooltip={tooltip}> |
| b69ab31 | | | 186 | <ButtonDropdown |
| b69ab31 | | | 187 | kind="icon" |
| b69ab31 | | | 188 | options={[]} |
| b69ab31 | | | 189 | selected={selectedAction} |
| b69ab31 | | | 190 | icon={<Icon icon="lightbulb-sparkle" />} |
| b69ab31 | | | 191 | onClick={(action, e) => { |
| b69ab31 | | | 192 | if (e.altKey) { |
| b69ab31 | | | 193 | return; |
| b69ab31 | | | 194 | } |
| b69ab31 | | | 195 | e.stopPropagation(); |
| b69ab31 | | | 196 | runSmartAction(action.config, context); |
| b69ab31 | | | 197 | // Update the cache with the most recent action |
| b69ab31 | | | 198 | bumpSmartAction(action.id); |
| b69ab31 | | | 199 | }} |
| b69ab31 | | | 200 | onChangeSelected={() => {}} |
| b69ab31 | | | 201 | customSelectComponent={ |
| b69ab31 | | | 202 | <Button |
| b69ab31 | | | 203 | {...stylex.props(styles.select, buttonStyles.icon, styles.iconSelect)} |
| b69ab31 | | | 204 | onClick={e => { |
| b69ab31 | | | 205 | if (dropdownButtonRef.current) { |
| b69ab31 | | | 206 | const rect = dropdownButtonRef.current.getBoundingClientRect(); |
| b69ab31 | | | 207 | const zoom = getZoomLevel(); |
| b69ab31 | | | 208 | const xOffset = 4 * zoom; |
| b69ab31 | | | 209 | const centerX = rect.left + rect.width / 2 - xOffset; |
| b69ab31 | | | 210 | // Position arrow at the top or bottom edge of button depending on which half of screen we're in |
| b69ab31 | | | 211 | const isTopHalf = |
| b69ab31 | | | 212 | (rect.top + rect.height / 2) / zoom <= window.innerHeight / zoom / 2; |
| b69ab31 | | | 213 | const yOffset = 5 * zoom; |
| b69ab31 | | | 214 | const edgeY = isTopHalf ? rect.bottom - yOffset : rect.top + yOffset; |
| b69ab31 | | | 215 | Object.defineProperty(e, 'clientX', {value: centerX, configurable: true}); |
| b69ab31 | | | 216 | Object.defineProperty(e, 'clientY', {value: edgeY, configurable: true}); |
| b69ab31 | | | 217 | } |
| b69ab31 | | | 218 | contextMenu(e); |
| b69ab31 | | | 219 | e.stopPropagation(); |
| b69ab31 | | | 220 | }} |
| b69ab31 | | | 221 | ref={dropdownButtonRef} |
| b69ab31 | | | 222 | /> |
| b69ab31 | | | 223 | } |
| b69ab31 | | | 224 | /> |
| b69ab31 | | | 225 | </SmartActionWithContext> |
| b69ab31 | | | 226 | ); |
| b69ab31 | | | 227 | } |
| b69ab31 | | | 228 | |
| b69ab31 | | | 229 | return buttonComponent; |
| b69ab31 | | | 230 | } |
| b69ab31 | | | 231 | |
| b69ab31 | | | 232 | function SmartActionWithContext({ |
| b69ab31 | | | 233 | config, |
| b69ab31 | | | 234 | context, |
| b69ab31 | | | 235 | tooltip, |
| b69ab31 | | | 236 | children, |
| b69ab31 | | | 237 | }: { |
| b69ab31 | | | 238 | config: SmartActionConfig; |
| b69ab31 | | | 239 | context: ActionContext; |
| b69ab31 | | | 240 | tooltip?: React.ReactNode; |
| b69ab31 | | | 241 | children: React.ReactNode; |
| b69ab31 | | | 242 | }) { |
| b69ab31 | | | 243 | const ContextInput = Internal.smartActions?.ContextInput; |
| b69ab31 | | | 244 | |
| b69ab31 | | | 245 | if (!ContextInput) { |
| b69ab31 | | | 246 | if (tooltip) { |
| b69ab31 | | | 247 | return <Tooltip title={tooltip}>{children}</Tooltip>; |
| b69ab31 | | | 248 | } |
| b69ab31 | | | 249 | return <>{children}</>; |
| b69ab31 | | | 250 | } |
| b69ab31 | | | 251 | |
| b69ab31 | | | 252 | return ( |
| b69ab31 | | | 253 | <Tooltip |
| b69ab31 | | | 254 | trigger="click" |
| b69ab31 | | | 255 | component={dismiss => ( |
| b69ab31 | | | 256 | <ContextInput |
| b69ab31 | | | 257 | onSubmit={(userContext: string) => { |
| b69ab31 | | | 258 | runSmartAction(config, {...context, userContext}); |
| b69ab31 | | | 259 | bumpSmartAction(config.id); |
| b69ab31 | | | 260 | dismiss(); |
| b69ab31 | | | 261 | }} |
| b69ab31 | | | 262 | /> |
| b69ab31 | | | 263 | )} |
| b69ab31 | | | 264 | title={tooltip} |
| b69ab31 | | | 265 | group="smart-action-context-input"> |
| b69ab31 | | | 266 | {children} |
| b69ab31 | | | 267 | </Tooltip> |
| b69ab31 | | | 268 | ); |
| b69ab31 | | | 269 | } |
| b69ab31 | | | 270 | |
| b69ab31 | | | 271 | function shouldShowSmartAction( |
| b69ab31 | | | 272 | config: SmartActionConfig, |
| b69ab31 | | | 273 | context: ActionContext, |
| b69ab31 | | | 274 | passesFeatureFlag: boolean, |
| b69ab31 | | | 275 | ): boolean { |
| b69ab31 | | | 276 | if (!passesFeatureFlag) { |
| b69ab31 | | | 277 | return false; |
| b69ab31 | | | 278 | } |
| b69ab31 | | | 279 | |
| b69ab31 | | | 280 | if (config.platformRestriction && !config.platformRestriction?.includes(platform.platformName)) { |
| b69ab31 | | | 281 | return false; |
| b69ab31 | | | 282 | } |
| b69ab31 | | | 283 | |
| b69ab31 | | | 284 | return config.shouldShow?.(context) ?? true; |
| b69ab31 | | | 285 | } |
| b69ab31 | | | 286 | |
| b69ab31 | | | 287 | function runSmartAction(config: SmartActionConfig, context: ActionContext): void { |
| b69ab31 | | | 288 | tracker.track('SmartActionClicked', { |
| b69ab31 | | | 289 | extras: {action: config.trackEventName, withUserContext: context.userContext != null}, |
| b69ab31 | | | 290 | }); |
| b69ab31 | | | 291 | if (config.getMessagePayload) { |
| b69ab31 | | | 292 | const payload = config.getMessagePayload(context); |
| b69ab31 | | | 293 | serverAPI.postMessage({ |
| b69ab31 | | | 294 | ...payload, |
| b69ab31 | | | 295 | } as PlatformSpecificClientToServerMessages); |
| b69ab31 | | | 296 | } |
| b69ab31 | | | 297 | } |