| 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 {ContextMenuItem} from 'shared/ContextMenu'; |
| b69ab31 | | | 9 | import type {InternalTypes} from './InternalTypes'; |
| b69ab31 | | | 10 | import type {CommitInfo} from './types'; |
| b69ab31 | | | 11 | |
| b69ab31 | | | 12 | import * as stylex from '@stylexjs/stylex'; |
| b69ab31 | | | 13 | import {Button} from 'isl-components/Button'; |
| b69ab31 | | | 14 | import {Column} from 'isl-components/Flex'; |
| b69ab31 | | | 15 | import {Icon} from 'isl-components/Icon'; |
| b69ab31 | | | 16 | import {Tag} from 'isl-components/Tag'; |
| b69ab31 | | | 17 | import {TextField} from 'isl-components/TextField'; |
| b69ab31 | | | 18 | import {Tooltip} from 'isl-components/Tooltip'; |
| b69ab31 | | | 19 | import {useAtomValue} from 'jotai'; |
| b69ab31 | | | 20 | import {useState} from 'react'; |
| b69ab31 | | | 21 | import {useContextMenu} from 'shared/ContextMenu'; |
| b69ab31 | | | 22 | import {spacing} from '../../components/theme/tokens.stylex'; |
| b69ab31 | | | 23 | import {tracker} from './analytics'; |
| b69ab31 | | | 24 | import { |
| b69ab31 | | | 25 | bookmarksDataStorage, |
| b69ab31 | | | 26 | recommendedBookmarksAtom, |
| b69ab31 | | | 27 | REMOTE_MASTER_BOOKMARK, |
| b69ab31 | | | 28 | } from './BookmarksData'; |
| b69ab31 | | | 29 | import {Row} from './ComponentUtils'; |
| b69ab31 | | | 30 | import {hiddenMasterFeatureAvailableAtom, shouldHideMasterAtom} from './HiddenMasterData'; |
| b69ab31 | | | 31 | import {T, t} from './i18n'; |
| b69ab31 | | | 32 | import {Internal} from './Internal'; |
| b69ab31 | | | 33 | import {BookmarkCreateOperation} from './operations/BookmarkCreateOperation'; |
| b69ab31 | | | 34 | import {BookmarkDeleteOperation} from './operations/BookmarkDeleteOperation'; |
| b69ab31 | | | 35 | import {useRunOperation} from './operationsState'; |
| b69ab31 | | | 36 | import {latestSuccessorUnlessExplicitlyObsolete} from './successionUtils'; |
| b69ab31 | | | 37 | import {showModal} from './useModal'; |
| b69ab31 | | | 38 | |
| b69ab31 | | | 39 | const styles = stylex.create({ |
| b69ab31 | | | 40 | stable: { |
| b69ab31 | | | 41 | backgroundColor: 'var(--list-hover-background)', |
| b69ab31 | | | 42 | color: 'var(--list-hover-foreground)', |
| b69ab31 | | | 43 | }, |
| b69ab31 | | | 44 | fullLength: { |
| b69ab31 | | | 45 | maxWidth: 'unset', |
| b69ab31 | | | 46 | }, |
| b69ab31 | | | 47 | bookmarkTag: { |
| b69ab31 | | | 48 | maxWidth: '300px', |
| b69ab31 | | | 49 | display: 'flex', |
| b69ab31 | | | 50 | alignItems: 'center', |
| b69ab31 | | | 51 | gap: spacing.quarter, |
| b69ab31 | | | 52 | }, |
| b69ab31 | | | 53 | modalButtonBar: { |
| b69ab31 | | | 54 | justifyContent: 'flex-end', |
| b69ab31 | | | 55 | }, |
| b69ab31 | | | 56 | }); |
| b69ab31 | | | 57 | |
| b69ab31 | | | 58 | export type BookmarkKind = 'remote' | 'local' | 'stable'; |
| b69ab31 | | | 59 | |
| b69ab31 | | | 60 | function useShouldShowBookmark(): (bookmarkValue: string) => boolean { |
| b69ab31 | | | 61 | const bookmarksData = useAtomValue(bookmarksDataStorage); |
| b69ab31 | | | 62 | const shouldHideMaster = useAtomValue(shouldHideMasterAtom); |
| b69ab31 | | | 63 | const hiddenMasterFeatureAvailable = useAtomValue(hiddenMasterFeatureAvailableAtom); |
| b69ab31 | | | 64 | return (bookmarkValue: string): boolean => { |
| b69ab31 | | | 65 | if (bookmarkValue === REMOTE_MASTER_BOOKMARK && hiddenMasterFeatureAvailable) { |
| b69ab31 | | | 66 | const visibility = bookmarksData.masterBookmarkVisibility; |
| b69ab31 | | | 67 | if (visibility === 'show') { |
| b69ab31 | | | 68 | return true; |
| b69ab31 | | | 69 | } |
| b69ab31 | | | 70 | if (visibility === 'hide') { |
| b69ab31 | | | 71 | return false; |
| b69ab31 | | | 72 | } |
| b69ab31 | | | 73 | // visibility === 'auto' or undefined - use sitevar config |
| b69ab31 | | | 74 | return !shouldHideMaster; |
| b69ab31 | | | 75 | } |
| b69ab31 | | | 76 | return !bookmarksData.hiddenRemoteBookmarks.includes(bookmarkValue); |
| b69ab31 | | | 77 | }; |
| b69ab31 | | | 78 | } |
| b69ab31 | | | 79 | |
| b69ab31 | | | 80 | const logged = new Set<string>(); |
| b69ab31 | | | 81 | function logExposureOncePerSession(location: string) { |
| b69ab31 | | | 82 | if (logged.has(location)) { |
| b69ab31 | | | 83 | return; |
| b69ab31 | | | 84 | } |
| b69ab31 | | | 85 | tracker.track('SawStableLocation', {extras: {location}}); |
| b69ab31 | | | 86 | logged.add(location); |
| b69ab31 | | | 87 | } |
| b69ab31 | | | 88 | |
| b69ab31 | | | 89 | export function getBookmarkAddons( |
| b69ab31 | | | 90 | name: string, |
| b69ab31 | | | 91 | showRecommendedIcon: boolean, |
| b69ab31 | | | 92 | showWarningOnMaster: boolean, |
| b69ab31 | | | 93 | tooltipOverride?: string, |
| b69ab31 | | | 94 | ): {icon: string | undefined; tooltip: React.ReactNode | undefined} { |
| b69ab31 | | | 95 | if (showRecommendedIcon) { |
| b69ab31 | | | 96 | return {icon: 'star-full', tooltip: tooltipOverride ?? Internal.RecommendedBookmarkInfo?.()}; |
| b69ab31 | | | 97 | } |
| b69ab31 | | | 98 | if (showWarningOnMaster && name === REMOTE_MASTER_BOOKMARK) { |
| b69ab31 | | | 99 | return {icon: 'warning', tooltip: tooltipOverride ?? Internal.MasterBookmarkInfo?.()}; |
| b69ab31 | | | 100 | } |
| b69ab31 | | | 101 | return {icon: undefined, tooltip: tooltipOverride}; |
| b69ab31 | | | 102 | } |
| b69ab31 | | | 103 | |
| b69ab31 | | | 104 | export function Bookmark({ |
| b69ab31 | | | 105 | children, |
| b69ab31 | | | 106 | kind, |
| b69ab31 | | | 107 | fullLength, |
| b69ab31 | | | 108 | tooltip, |
| b69ab31 | | | 109 | icon, |
| b69ab31 | | | 110 | }: { |
| b69ab31 | | | 111 | children: string; |
| b69ab31 | | | 112 | kind: BookmarkKind; |
| b69ab31 | | | 113 | fullLength?: boolean; |
| b69ab31 | | | 114 | tooltip?: string | React.ReactNode; |
| b69ab31 | | | 115 | icon?: string; |
| b69ab31 | | | 116 | }) { |
| b69ab31 | | | 117 | const bookmark = children; |
| b69ab31 | | | 118 | const contextMenu = useContextMenu(makeBookmarkContextMenuOptions); |
| b69ab31 | | | 119 | const runOperation = useRunOperation(); |
| b69ab31 | | | 120 | |
| b69ab31 | | | 121 | function makeBookmarkContextMenuOptions() { |
| b69ab31 | | | 122 | const items: Array<ContextMenuItem> = []; |
| b69ab31 | | | 123 | if (kind === 'local') { |
| b69ab31 | | | 124 | items.push({ |
| b69ab31 | | | 125 | label: <T replace={{$book: bookmark}}>Delete Bookmark "$book"</T>, |
| b69ab31 | | | 126 | onClick: () => { |
| b69ab31 | | | 127 | runOperation(new BookmarkDeleteOperation(bookmark)); |
| b69ab31 | | | 128 | }, |
| b69ab31 | | | 129 | }); |
| b69ab31 | | | 130 | } |
| b69ab31 | | | 131 | return items; |
| b69ab31 | | | 132 | } |
| b69ab31 | | | 133 | |
| b69ab31 | | | 134 | if (kind === 'stable') { |
| b69ab31 | | | 135 | logExposureOncePerSession(bookmark); |
| b69ab31 | | | 136 | } |
| b69ab31 | | | 137 | |
| b69ab31 | | | 138 | const inner = ( |
| b69ab31 | | | 139 | <Tag |
| b69ab31 | | | 140 | onContextMenu={contextMenu} |
| b69ab31 | | | 141 | xstyle={[ |
| b69ab31 | | | 142 | kind === 'stable' && styles.stable, |
| b69ab31 | | | 143 | styles.bookmarkTag, |
| b69ab31 | | | 144 | fullLength === true && styles.fullLength, |
| b69ab31 | | | 145 | ]}> |
| b69ab31 | | | 146 | {icon && <Icon icon={icon} size="XS" style={{display: 'flex', height: '12px'}} />} |
| b69ab31 | | | 147 | {bookmark} |
| b69ab31 | | | 148 | </Tag> |
| b69ab31 | | | 149 | ); |
| b69ab31 | | | 150 | return tooltip ? <Tooltip title={tooltip}>{inner}</Tooltip> : inner; |
| b69ab31 | | | 151 | } |
| b69ab31 | | | 152 | |
| b69ab31 | | | 153 | export function AllBookmarksTruncated({ |
| b69ab31 | | | 154 | stable, |
| b69ab31 | | | 155 | remote, |
| b69ab31 | | | 156 | local, |
| b69ab31 | | | 157 | fullRepoBranch, |
| b69ab31 | | | 158 | }: { |
| b69ab31 | | | 159 | stable: ReadonlyArray<string | {value: string; description: string; isRecommended?: boolean}>; |
| b69ab31 | | | 160 | remote: ReadonlyArray<string>; |
| b69ab31 | | | 161 | local: ReadonlyArray<string>; |
| b69ab31 | | | 162 | fullRepoBranch?: InternalTypes['FullRepoBranch'] | undefined; |
| b69ab31 | | | 163 | }) { |
| b69ab31 | | | 164 | const recommendedBookmarks = useAtomValue(recommendedBookmarksAtom); |
| b69ab31 | | | 165 | const showWarningOnMaster = Internal.shouldCheckRebase?.() ?? false; |
| b69ab31 | | | 166 | const shouldShowBookmark = useShouldShowBookmark(); |
| b69ab31 | | | 167 | |
| b69ab31 | | | 168 | const FullRepoBranchBookmark = Internal.FullRepoBranchBookmark; |
| b69ab31 | | | 169 | const compareFullRepoBranch = Internal.compareFullRepoBranch; |
| b69ab31 | | | 170 | |
| b69ab31 | | | 171 | const finalBookmarks = ( |
| b69ab31 | | | 172 | [ |
| b69ab31 | | | 173 | ['local', local], |
| b69ab31 | | | 174 | ['remote', remote], |
| b69ab31 | | | 175 | ['stable', stable], |
| b69ab31 | | | 176 | ] as const |
| b69ab31 | | | 177 | ) |
| b69ab31 | | | 178 | .map(([kind, bookmarks]) => |
| b69ab31 | | | 179 | bookmarks |
| b69ab31 | | | 180 | .filter(bookmark => |
| b69ab31 | | | 181 | shouldShowBookmark(typeof bookmark === 'string' ? bookmark : bookmark.value), |
| b69ab31 | | | 182 | ) |
| b69ab31 | | | 183 | .filter(bookmark => |
| b69ab31 | | | 184 | compareFullRepoBranch ? compareFullRepoBranch(fullRepoBranch, bookmark) : true, |
| b69ab31 | | | 185 | ) |
| b69ab31 | | | 186 | .map(bookmark => { |
| b69ab31 | | | 187 | const value = typeof bookmark === 'string' ? bookmark : bookmark.value; |
| b69ab31 | | | 188 | const isRecommended = |
| b69ab31 | | | 189 | recommendedBookmarks.has(value) || |
| b69ab31 | | | 190 | (typeof bookmark === 'object' && bookmark.isRecommended === true); |
| b69ab31 | | | 191 | const tooltipOverride = typeof bookmark === 'string' ? undefined : bookmark.description; |
| b69ab31 | | | 192 | const {icon, tooltip} = getBookmarkAddons( |
| b69ab31 | | | 193 | value, |
| b69ab31 | | | 194 | isRecommended, |
| b69ab31 | | | 195 | showWarningOnMaster, |
| b69ab31 | | | 196 | tooltipOverride, |
| b69ab31 | | | 197 | ); |
| b69ab31 | | | 198 | |
| b69ab31 | | | 199 | return {value, kind, tooltip, icon}; |
| b69ab31 | | | 200 | }), |
| b69ab31 | | | 201 | ) |
| b69ab31 | | | 202 | .flat(); |
| b69ab31 | | | 203 | const NUM_TO_SHOW = fullRepoBranch == null ? 3 : 2; |
| b69ab31 | | | 204 | const shownBookmarks = finalBookmarks.slice(0, NUM_TO_SHOW); |
| b69ab31 | | | 205 | const hiddenBookmarks = finalBookmarks.slice(NUM_TO_SHOW); |
| b69ab31 | | | 206 | const numTruncated = hiddenBookmarks.length; |
| b69ab31 | | | 207 | |
| b69ab31 | | | 208 | return ( |
| b69ab31 | | | 209 | <> |
| b69ab31 | | | 210 | {fullRepoBranch && FullRepoBranchBookmark && ( |
| b69ab31 | | | 211 | <FullRepoBranchBookmark branch={fullRepoBranch} /> |
| b69ab31 | | | 212 | )} |
| b69ab31 | | | 213 | {shownBookmarks.map(({value, kind, tooltip, icon}) => ( |
| b69ab31 | | | 214 | <Bookmark key={value} kind={kind} tooltip={tooltip} icon={icon}> |
| b69ab31 | | | 215 | {value} |
| b69ab31 | | | 216 | </Bookmark> |
| b69ab31 | | | 217 | ))} |
| b69ab31 | | | 218 | {numTruncated > 0 && ( |
| b69ab31 | | | 219 | <Tooltip |
| b69ab31 | | | 220 | component={() => ( |
| b69ab31 | | | 221 | <Column alignStart> |
| b69ab31 | | | 222 | {hiddenBookmarks.map(({value, kind, tooltip, icon}) => ( |
| b69ab31 | | | 223 | <Bookmark key={value} kind={kind} tooltip={tooltip} icon={icon} fullLength> |
| b69ab31 | | | 224 | {value} |
| b69ab31 | | | 225 | </Bookmark> |
| b69ab31 | | | 226 | ))} |
| b69ab31 | | | 227 | </Column> |
| b69ab31 | | | 228 | )}> |
| b69ab31 | | | 229 | <Tag> |
| b69ab31 | | | 230 | <T replace={{$n: numTruncated}}>+$n more</T> |
| b69ab31 | | | 231 | </Tag> |
| b69ab31 | | | 232 | </Tooltip> |
| b69ab31 | | | 233 | )} |
| b69ab31 | | | 234 | </> |
| b69ab31 | | | 235 | ); |
| b69ab31 | | | 236 | } |
| b69ab31 | | | 237 | |
| b69ab31 | | | 238 | export function Bookmarks({ |
| b69ab31 | | | 239 | bookmarks, |
| b69ab31 | | | 240 | kind, |
| b69ab31 | | | 241 | }: { |
| b69ab31 | | | 242 | bookmarks: ReadonlyArray<string | {value: string; description: string}>; |
| b69ab31 | | | 243 | kind: BookmarkKind; |
| b69ab31 | | | 244 | }) { |
| b69ab31 | | | 245 | const shouldShowBookmark = useShouldShowBookmark(); |
| b69ab31 | | | 246 | |
| b69ab31 | | | 247 | return ( |
| b69ab31 | | | 248 | <> |
| b69ab31 | | | 249 | {bookmarks |
| b69ab31 | | | 250 | .filter(bookmark => |
| b69ab31 | | | 251 | shouldShowBookmark(typeof bookmark === 'string' ? bookmark : bookmark.value), |
| b69ab31 | | | 252 | ) |
| b69ab31 | | | 253 | .map(bookmark => { |
| b69ab31 | | | 254 | const value = typeof bookmark === 'string' ? bookmark : bookmark.value; |
| b69ab31 | | | 255 | const tooltip = typeof bookmark === 'string' ? undefined : bookmark.description; |
| b69ab31 | | | 256 | return ( |
| b69ab31 | | | 257 | <Bookmark key={value} kind={kind} tooltip={tooltip}> |
| b69ab31 | | | 258 | {value} |
| b69ab31 | | | 259 | </Bookmark> |
| b69ab31 | | | 260 | ); |
| b69ab31 | | | 261 | })} |
| b69ab31 | | | 262 | </> |
| b69ab31 | | | 263 | ); |
| b69ab31 | | | 264 | } |
| b69ab31 | | | 265 | |
| b69ab31 | | | 266 | export async function createBookmarkAtCommit(commit: CommitInfo) { |
| b69ab31 | | | 267 | await showModal({ |
| b69ab31 | | | 268 | type: 'custom', |
| b69ab31 | | | 269 | title: <T>Create Bookmark</T>, |
| b69ab31 | | | 270 | component: ({returnResultAndDismiss}: {returnResultAndDismiss: (data?: undefined) => void}) => ( |
| b69ab31 | | | 271 | <CreateBookmarkAtCommitModal commit={commit} dismiss={returnResultAndDismiss} /> |
| b69ab31 | | | 272 | ), |
| b69ab31 | | | 273 | }); |
| b69ab31 | | | 274 | } |
| b69ab31 | | | 275 | |
| b69ab31 | | | 276 | function CreateBookmarkAtCommitModal({commit, dismiss}: {commit: CommitInfo; dismiss: () => void}) { |
| b69ab31 | | | 277 | const runOperation = useRunOperation(); |
| b69ab31 | | | 278 | const [bookmark, setBookmark] = useState(''); |
| b69ab31 | | | 279 | return ( |
| b69ab31 | | | 280 | <> |
| b69ab31 | | | 281 | <TextField |
| b69ab31 | | | 282 | autoFocus |
| b69ab31 | | | 283 | value={bookmark} |
| b69ab31 | | | 284 | onChange={e => setBookmark(e.currentTarget.value)} |
| b69ab31 | | | 285 | aria-label={t('Bookmark Name')} |
| b69ab31 | | | 286 | /> |
| b69ab31 | | | 287 | <Row {...stylex.props(styles.modalButtonBar)}> |
| b69ab31 | | | 288 | <Button |
| b69ab31 | | | 289 | onClick={() => { |
| b69ab31 | | | 290 | dismiss(); |
| b69ab31 | | | 291 | }}> |
| b69ab31 | | | 292 | <T>Cancel</T> |
| b69ab31 | | | 293 | </Button> |
| b69ab31 | | | 294 | <Button |
| b69ab31 | | | 295 | primary |
| b69ab31 | | | 296 | onClick={() => { |
| b69ab31 | | | 297 | runOperation( |
| b69ab31 | | | 298 | new BookmarkCreateOperation( |
| b69ab31 | | | 299 | latestSuccessorUnlessExplicitlyObsolete(commit), |
| b69ab31 | | | 300 | bookmark, |
| b69ab31 | | | 301 | ), |
| b69ab31 | | | 302 | ); |
| b69ab31 | | | 303 | dismiss(); |
| b69ab31 | | | 304 | }} |
| b69ab31 | | | 305 | disabled={bookmark.trim().length === 0}> |
| b69ab31 | | | 306 | <T>Create</T> |
| b69ab31 | | | 307 | </Button> |
| b69ab31 | | | 308 | </Row> |
| b69ab31 | | | 309 | </> |
| b69ab31 | | | 310 | ); |
| b69ab31 | | | 311 | } |