| 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 { |
| b69ab31 | | | 9 | AbsolutePath, |
| b69ab31 | | | 10 | CwdInfo, |
| b69ab31 | | | 11 | CwdRelativePath, |
| b69ab31 | | | 12 | RepoRelativePath, |
| b69ab31 | | | 13 | Submodule, |
| b69ab31 | | | 14 | SubmodulesByRoot, |
| b69ab31 | | | 15 | } from './types'; |
| b69ab31 | | | 16 | |
| b69ab31 | | | 17 | import * as stylex from '@stylexjs/stylex'; |
| b69ab31 | | | 18 | import {Badge} from 'isl-components/Badge'; |
| b69ab31 | | | 19 | import {Button, buttonStyles} from 'isl-components/Button'; |
| b69ab31 | | | 20 | import {ButtonDropdown} from 'isl-components/ButtonDropdown'; |
| b69ab31 | | | 21 | import {Divider} from 'isl-components/Divider'; |
| b69ab31 | | | 22 | import {Icon} from 'isl-components/Icon'; |
| b69ab31 | | | 23 | import {Kbd} from 'isl-components/Kbd'; |
| b69ab31 | | | 24 | import {KeyCode, Modifier} from 'isl-components/KeyboardShortcuts'; |
| b69ab31 | | | 25 | import {RadioGroup} from 'isl-components/Radio'; |
| b69ab31 | | | 26 | import {Subtle} from 'isl-components/Subtle'; |
| b69ab31 | | | 27 | import {TextField} from 'isl-components/TextField'; |
| b69ab31 | | | 28 | import {Tooltip} from 'isl-components/Tooltip'; |
| b69ab31 | | | 29 | import {atom, useAtomValue} from 'jotai'; |
| b69ab31 | | | 30 | import {Suspense, useState} from 'react'; |
| b69ab31 | | | 31 | import {basename} from 'shared/utils'; |
| b69ab31 | | | 32 | import {colors, spacing} from '../../components/theme/tokens.stylex'; |
| b69ab31 | | | 33 | import serverAPI from './ClientToServerAPI'; |
| b69ab31 | | | 34 | import {Column, Row, ScrollY} from './ComponentUtils'; |
| b69ab31 | | | 35 | import {DropdownField, DropdownFields} from './DropdownFields'; |
| b69ab31 | | | 36 | import {useCommandEvent} from './ISLShortcuts'; |
| b69ab31 | | | 37 | import {codeReviewProvider} from './codeReview/CodeReviewInfo'; |
| b69ab31 | | | 38 | import {T, t} from './i18n'; |
| b69ab31 | | | 39 | import {writeAtom} from './jotaiUtils'; |
| b69ab31 | | | 40 | import platform from './platform'; |
| b69ab31 | | | 41 | import {serverCwd} from './repositoryData'; |
| b69ab31 | | | 42 | import {repositoryInfo, submodulesByRoot} from './serverAPIState'; |
| b69ab31 | | | 43 | import {registerCleanup, registerDisposable} from './utils'; |
| b69ab31 | | | 44 | |
| b69ab31 | | | 45 | /** |
| b69ab31 | | | 46 | * Give the relative path to `path` from `root` |
| b69ab31 | | | 47 | * For example, relativePath('/home/user', '/home') -> 'user' |
| b69ab31 | | | 48 | */ |
| b69ab31 | | | 49 | export function relativePath(root: AbsolutePath, path: AbsolutePath) { |
| b69ab31 | | | 50 | if (root == null || path === '') { |
| b69ab31 | | | 51 | return ''; |
| b69ab31 | | | 52 | } |
| b69ab31 | | | 53 | const sep = guessPathSep(path); |
| b69ab31 | | | 54 | return maybeTrimPrefix(path.replace(root, ''), sep); |
| b69ab31 | | | 55 | } |
| b69ab31 | | | 56 | |
| b69ab31 | | | 57 | /** |
| b69ab31 | | | 58 | * Simple version of path.join() |
| b69ab31 | | | 59 | * Expect an absolute root path and a relative path |
| b69ab31 | | | 60 | * e.g. |
| b69ab31 | | | 61 | * joinPaths('/home', 'user') -> '/home/user' |
| b69ab31 | | | 62 | * joinPaths('/home/', 'user/.config') -> '/home/user/.config' |
| b69ab31 | | | 63 | */ |
| b69ab31 | | | 64 | export function joinPaths(root: AbsolutePath, path: CwdRelativePath, sep = '/'): AbsolutePath { |
| b69ab31 | | | 65 | return root.endsWith(sep) ? root + path : root + sep + path; |
| b69ab31 | | | 66 | } |
| b69ab31 | | | 67 | |
| b69ab31 | | | 68 | /** |
| b69ab31 | | | 69 | * Trim a suffix if it exists |
| b69ab31 | | | 70 | * maybeTrimSuffix('abc/', '/') -> 'abc' |
| b69ab31 | | | 71 | * maybeTrimSuffix('abc', '/') -> 'abc' |
| b69ab31 | | | 72 | */ |
| b69ab31 | | | 73 | function maybeTrimSuffix(s: string, c: string): string { |
| b69ab31 | | | 74 | return s.endsWith(c) ? s.slice(0, -c.length) : s; |
| b69ab31 | | | 75 | } |
| b69ab31 | | | 76 | |
| b69ab31 | | | 77 | function maybeTrimPrefix(s: string, c: string): string { |
| b69ab31 | | | 78 | return s.startsWith(c) ? s.slice(c.length) : s; |
| b69ab31 | | | 79 | } |
| b69ab31 | | | 80 | |
| b69ab31 | | | 81 | function getMainSelectorLabel( |
| b69ab31 | | | 82 | directRepoRoot: AbsolutePath, |
| b69ab31 | | | 83 | nestedRepoRoots: AbsolutePath[] | undefined, |
| b69ab31 | | | 84 | cwd: string, |
| b69ab31 | | | 85 | ) { |
| b69ab31 | | | 86 | const sep = guessPathSep(cwd); |
| b69ab31 | | | 87 | |
| b69ab31 | | | 88 | // If there are multiple nested repo roots, |
| b69ab31 | | | 89 | // show the first one as there will be following selectors for the rest |
| b69ab31 | | | 90 | if (nestedRepoRoots && nestedRepoRoots.length > 1) { |
| b69ab31 | | | 91 | return maybeTrimSuffix(basename(nestedRepoRoots[0], sep), sep); |
| b69ab31 | | | 92 | } |
| b69ab31 | | | 93 | |
| b69ab31 | | | 94 | // Otherwise, build the label with the direct and only repo root |
| b69ab31 | | | 95 | const repoBasename = maybeTrimSuffix(basename(directRepoRoot, sep), sep); |
| b69ab31 | | | 96 | const repoRelativeCwd = relativePath(directRepoRoot, cwd); |
| b69ab31 | | | 97 | if (repoRelativeCwd === '') { |
| b69ab31 | | | 98 | return repoBasename; |
| b69ab31 | | | 99 | } |
| b69ab31 | | | 100 | return joinPaths(repoBasename, repoRelativeCwd, sep); |
| b69ab31 | | | 101 | } |
| b69ab31 | | | 102 | |
| b69ab31 | | | 103 | export const availableCwds = atom<Array<CwdInfo>>([]); |
| b69ab31 | | | 104 | registerCleanup( |
| b69ab31 | | | 105 | availableCwds, |
| b69ab31 | | | 106 | serverAPI.onConnectOrReconnect(() => { |
| b69ab31 | | | 107 | serverAPI.postMessage({ |
| b69ab31 | | | 108 | type: 'platform/subscribeToAvailableCwds', |
| b69ab31 | | | 109 | }); |
| b69ab31 | | | 110 | }), |
| b69ab31 | | | 111 | import.meta.hot, |
| b69ab31 | | | 112 | ); |
| b69ab31 | | | 113 | |
| b69ab31 | | | 114 | registerDisposable( |
| b69ab31 | | | 115 | availableCwds, |
| b69ab31 | | | 116 | serverAPI.onMessageOfType('platform/availableCwds', event => |
| b69ab31 | | | 117 | writeAtom(availableCwds, event.options), |
| b69ab31 | | | 118 | ), |
| b69ab31 | | | 119 | import.meta.hot, |
| b69ab31 | | | 120 | ); |
| b69ab31 | | | 121 | |
| b69ab31 | | | 122 | const styles = stylex.create({ |
| b69ab31 | | | 123 | container: { |
| b69ab31 | | | 124 | display: 'flex', |
| b69ab31 | | | 125 | gap: 0, |
| b69ab31 | | | 126 | }, |
| b69ab31 | | | 127 | hideRightBorder: { |
| b69ab31 | | | 128 | borderRight: 0, |
| b69ab31 | | | 129 | marginRight: 0, |
| b69ab31 | | | 130 | borderTopRightRadius: 0, |
| b69ab31 | | | 131 | borderBottomRightRadius: 0, |
| b69ab31 | | | 132 | }, |
| b69ab31 | | | 133 | hideLeftBorder: { |
| b69ab31 | | | 134 | borderLeft: 0, |
| b69ab31 | | | 135 | marginLeft: 0, |
| b69ab31 | | | 136 | borderTopLeftRadius: 0, |
| b69ab31 | | | 137 | borderBottomLeftRadius: 0, |
| b69ab31 | | | 138 | }, |
| b69ab31 | | | 139 | submoduleSelect: { |
| b69ab31 | | | 140 | width: 'auto', |
| b69ab31 | | | 141 | maxWidth: '96px', |
| b69ab31 | | | 142 | textOverflow: 'ellipsis', |
| b69ab31 | | | 143 | boxShadow: 'none', |
| b69ab31 | | | 144 | outline: 'none', |
| b69ab31 | | | 145 | }, |
| b69ab31 | | | 146 | submoduleSeparator: { |
| b69ab31 | | | 147 | // Override background to disable hover effect |
| b69ab31 | | | 148 | background: { |
| b69ab31 | | | 149 | default: colors.subtleHoverDarken, |
| b69ab31 | | | 150 | }, |
| b69ab31 | | | 151 | }, |
| b69ab31 | | | 152 | submoduleDropdownContainer: { |
| b69ab31 | | | 153 | minWidth: '200px', |
| b69ab31 | | | 154 | alignItems: 'flex-start', |
| b69ab31 | | | 155 | gap: spacing.pad, |
| b69ab31 | | | 156 | }, |
| b69ab31 | | | 157 | submoduleList: { |
| b69ab31 | | | 158 | width: '100%', |
| b69ab31 | | | 159 | overflow: 'hidden', |
| b69ab31 | | | 160 | }, |
| b69ab31 | | | 161 | submoduleOption: { |
| b69ab31 | | | 162 | padding: 'var(--halfpad)', |
| b69ab31 | | | 163 | borderRadius: 'var(--halfpad)', |
| b69ab31 | | | 164 | cursor: 'pointer', |
| b69ab31 | | | 165 | overflow: 'hidden', |
| b69ab31 | | | 166 | textOverflow: 'ellipsis', |
| b69ab31 | | | 167 | boxSizing: 'border-box', |
| b69ab31 | | | 168 | backgroundColor: { |
| b69ab31 | | | 169 | ':hover': 'var(--hover-darken)', |
| b69ab31 | | | 170 | ':focus': 'var(--hover-darken)', |
| b69ab31 | | | 171 | }, |
| b69ab31 | | | 172 | width: '100%', |
| b69ab31 | | | 173 | }, |
| b69ab31 | | | 174 | }); |
| b69ab31 | | | 175 | |
| b69ab31 | | | 176 | export function CwdSelector() { |
| b69ab31 | | | 177 | const info = useAtomValue(repositoryInfo); |
| b69ab31 | | | 178 | const currentCwd = useAtomValue(serverCwd); |
| b69ab31 | | | 179 | const submodulesMap = useAtomValue(submodulesByRoot); |
| b69ab31 | | | 180 | |
| b69ab31 | | | 181 | if (info == null) { |
| b69ab31 | | | 182 | return null; |
| b69ab31 | | | 183 | } |
| b69ab31 | | | 184 | // The most direct root of the cwd |
| b69ab31 | | | 185 | const repoRoot = info.repoRoot; |
| b69ab31 | | | 186 | // The list of repo roots down to the cwd, in order from furthest to closest |
| b69ab31 | | | 187 | const repoRoots = info.repoRoots; |
| b69ab31 | | | 188 | |
| b69ab31 | | | 189 | const mainLabel = getMainSelectorLabel(repoRoot, repoRoots, currentCwd); |
| b69ab31 | | | 190 | |
| b69ab31 | | | 191 | return ( |
| b69ab31 | | | 192 | <div {...stylex.props(styles.container)}> |
| b69ab31 | | | 193 | <MainCwdSelector |
| b69ab31 | | | 194 | currentCwd={currentCwd} |
| b69ab31 | | | 195 | label={mainLabel} |
| b69ab31 | | | 196 | hideRightBorder={ |
| b69ab31 | | | 197 | (repoRoots && repoRoots.length > 1) || |
| b69ab31 | | | 198 | (submodulesMap?.get(repoRoot)?.value?.filter(m => m.active).length ?? 0) > 0 |
| b69ab31 | | | 199 | } |
| b69ab31 | | | 200 | /> |
| b69ab31 | | | 201 | <SubmoduleSelectorGroup repoRoots={repoRoots} submoduleOptions={submodulesMap} /> |
| b69ab31 | | | 202 | </div> |
| b69ab31 | | | 203 | ); |
| b69ab31 | | | 204 | } |
| b69ab31 | | | 205 | |
| b69ab31 | | | 206 | /** |
| b69ab31 | | | 207 | * The leftmost tooltip that can show cwd and repo details. |
| b69ab31 | | | 208 | */ |
| b69ab31 | | | 209 | function MainCwdSelector({ |
| b69ab31 | | | 210 | currentCwd, |
| b69ab31 | | | 211 | label, |
| b69ab31 | | | 212 | hideRightBorder, |
| b69ab31 | | | 213 | }: { |
| b69ab31 | | | 214 | currentCwd: AbsolutePath; |
| b69ab31 | | | 215 | label: string; |
| b69ab31 | | | 216 | hideRightBorder: boolean; |
| b69ab31 | | | 217 | }) { |
| b69ab31 | | | 218 | const allCwdOptions = useCwdOptions(); |
| b69ab31 | | | 219 | const cwdOptions = allCwdOptions.filter(opt => opt.valid); |
| b69ab31 | | | 220 | const additionalToggles = useCommandEvent('ToggleCwdDropdown'); |
| b69ab31 | | | 221 | |
| b69ab31 | | | 222 | return ( |
| b69ab31 | | | 223 | <Tooltip |
| b69ab31 | | | 224 | trigger="click" |
| b69ab31 | | | 225 | component={dismiss => <CwdDetails dismiss={dismiss} />} |
| b69ab31 | | | 226 | additionalToggles={additionalToggles.asEventTarget()} |
| b69ab31 | | | 227 | group="topbar" |
| b69ab31 | | | 228 | placement="bottom" |
| b69ab31 | | | 229 | title={ |
| b69ab31 | | | 230 | <T replace={{$shortcut: <Kbd keycode={KeyCode.C} modifiers={[Modifier.ALT]} />}}> |
| b69ab31 | | | 231 | Repository info & cwd ($shortcut) |
| b69ab31 | | | 232 | </T> |
| b69ab31 | | | 233 | }> |
| b69ab31 | | | 234 | {hideRightBorder || cwdOptions.length < 2 ? ( |
| b69ab31 | | | 235 | <Button |
| b69ab31 | | | 236 | icon |
| b69ab31 | | | 237 | data-testid="cwd-dropdown-button" |
| b69ab31 | | | 238 | {...stylex.props(hideRightBorder && styles.hideRightBorder)}> |
| b69ab31 | | | 239 | <Icon icon="folder" /> |
| b69ab31 | | | 240 | {label} |
| b69ab31 | | | 241 | </Button> |
| b69ab31 | | | 242 | ) : ( |
| b69ab31 | | | 243 | // use a ButtonDropdown as a shortcut to quickly change cwd |
| b69ab31 | | | 244 | <ButtonDropdown |
| b69ab31 | | | 245 | data-testid="cwd-dropdown-button" |
| b69ab31 | | | 246 | kind="icon" |
| b69ab31 | | | 247 | options={cwdOptions} |
| b69ab31 | | | 248 | selected={ |
| b69ab31 | | | 249 | cwdOptions.find(opt => opt.id === currentCwd) ?? { |
| b69ab31 | | | 250 | id: currentCwd, |
| b69ab31 | | | 251 | label, |
| b69ab31 | | | 252 | valid: true, |
| b69ab31 | | | 253 | } |
| b69ab31 | | | 254 | } |
| b69ab31 | | | 255 | icon={<Icon icon="folder" />} |
| b69ab31 | | | 256 | onClick={ |
| b69ab31 | | | 257 | () => null // fall through to the Tooltip |
| b69ab31 | | | 258 | } |
| b69ab31 | | | 259 | onChangeSelected={value => { |
| b69ab31 | | | 260 | if (value.id !== currentCwd) { |
| b69ab31 | | | 261 | changeCwd(value.id); |
| b69ab31 | | | 262 | } |
| b69ab31 | | | 263 | }}></ButtonDropdown> |
| b69ab31 | | | 264 | )} |
| b69ab31 | | | 265 | </Tooltip> |
| b69ab31 | | | 266 | ); |
| b69ab31 | | | 267 | } |
| b69ab31 | | | 268 | |
| b69ab31 | | | 269 | function SubmoduleSelectorGroup({ |
| b69ab31 | | | 270 | repoRoots, |
| b69ab31 | | | 271 | submoduleOptions, |
| b69ab31 | | | 272 | }: { |
| b69ab31 | | | 273 | repoRoots: AbsolutePath[] | undefined; |
| b69ab31 | | | 274 | submoduleOptions: SubmodulesByRoot; |
| b69ab31 | | | 275 | }) { |
| b69ab31 | | | 276 | const currentCwd = useAtomValue(serverCwd); |
| b69ab31 | | | 277 | if (repoRoots == null) { |
| b69ab31 | | | 278 | return null; |
| b69ab31 | | | 279 | } |
| b69ab31 | | | 280 | const numRoots = repoRoots.length; |
| b69ab31 | | | 281 | const directRepoRoot = repoRoots[numRoots - 1]; |
| b69ab31 | | | 282 | if (currentCwd !== directRepoRoot) { |
| b69ab31 | | | 283 | // If the actual cwd is deeper than the supeproject root, |
| b69ab31 | | | 284 | // submodule selectors don't make sense |
| b69ab31 | | | 285 | return null; |
| b69ab31 | | | 286 | } |
| b69ab31 | | | 287 | const submodulesToBeSelected = submoduleOptions.get(directRepoRoot)?.value?.filter(m => m.active); |
| b69ab31 | | | 288 | |
| b69ab31 | | | 289 | const out = []; |
| b69ab31 | | | 290 | |
| b69ab31 | | | 291 | for (let i = 1; i < numRoots; i++) { |
| b69ab31 | | | 292 | const currRoot = repoRoots[i]; |
| b69ab31 | | | 293 | const prevRoot = repoRoots[i - 1]; |
| b69ab31 | | | 294 | const submodules = submoduleOptions.get(prevRoot)?.value?.filter(m => m.active); |
| b69ab31 | | | 295 | if (submodules != null && submodules.length > 0) { |
| b69ab31 | | | 296 | out.push( |
| b69ab31 | | | 297 | <SubmoduleSelector |
| b69ab31 | | | 298 | submodules={submodules} |
| b69ab31 | | | 299 | selected={submodules?.find(opt => opt.path === relativePath(prevRoot, currRoot))} |
| b69ab31 | | | 300 | onChangeSelected={value => { |
| b69ab31 | | | 301 | if (value.path !== relativePath(prevRoot, currRoot)) { |
| b69ab31 | | | 302 | changeCwd(joinPaths(prevRoot, value.path)); |
| b69ab31 | | | 303 | } |
| b69ab31 | | | 304 | }} |
| b69ab31 | | | 305 | hideRightBorder={i < numRoots - 1 || submodulesToBeSelected != undefined} |
| b69ab31 | | | 306 | root={prevRoot} |
| b69ab31 | | | 307 | key={prevRoot} |
| b69ab31 | | | 308 | />, |
| b69ab31 | | | 309 | ); |
| b69ab31 | | | 310 | } |
| b69ab31 | | | 311 | } |
| b69ab31 | | | 312 | |
| b69ab31 | | | 313 | if (submodulesToBeSelected != null && submodulesToBeSelected.length > 0) { |
| b69ab31 | | | 314 | out.push( |
| b69ab31 | | | 315 | <SubmoduleSelector |
| b69ab31 | | | 316 | submodules={submodulesToBeSelected} |
| b69ab31 | | | 317 | onChangeSelected={value => { |
| b69ab31 | | | 318 | if (value.path !== relativePath(directRepoRoot, currentCwd)) { |
| b69ab31 | | | 319 | changeCwd(joinPaths(directRepoRoot, value.path)); |
| b69ab31 | | | 320 | } |
| b69ab31 | | | 321 | }} |
| b69ab31 | | | 322 | hideRightBorder={false} |
| b69ab31 | | | 323 | root={directRepoRoot} |
| b69ab31 | | | 324 | key={directRepoRoot} |
| b69ab31 | | | 325 | />, |
| b69ab31 | | | 326 | ); |
| b69ab31 | | | 327 | } |
| b69ab31 | | | 328 | |
| b69ab31 | | | 329 | return out; |
| b69ab31 | | | 330 | } |
| b69ab31 | | | 331 | |
| b69ab31 | | | 332 | function CwdDetails({dismiss}: {dismiss: () => unknown}) { |
| b69ab31 | | | 333 | const info = useAtomValue(repositoryInfo); |
| b69ab31 | | | 334 | const repoRoot = info?.repoRoot ?? null; |
| b69ab31 | | | 335 | const provider = useAtomValue(codeReviewProvider); |
| b69ab31 | | | 336 | const cwd = useAtomValue(serverCwd); |
| b69ab31 | | | 337 | const AddMoreCwdsHint = platform.AddMoreCwdsHint; |
| b69ab31 | | | 338 | return ( |
| b69ab31 | | | 339 | <DropdownFields title={<T>Repository info</T>} icon="folder" data-testid="cwd-details-dropdown"> |
| b69ab31 | | | 340 | <CwdSelections dismiss={dismiss} divider /> |
| b69ab31 | | | 341 | {AddMoreCwdsHint && ( |
| b69ab31 | | | 342 | <Suspense> |
| b69ab31 | | | 343 | <AddMoreCwdsHint /> |
| b69ab31 | | | 344 | </Suspense> |
| b69ab31 | | | 345 | )} |
| b69ab31 | | | 346 | <DropdownField title={<T>Active working directory</T>}> |
| b69ab31 | | | 347 | <code>{cwd}</code> |
| b69ab31 | | | 348 | </DropdownField> |
| b69ab31 | | | 349 | <DropdownField title={<T>Repository Root</T>}> |
| b69ab31 | | | 350 | <code>{repoRoot}</code> |
| b69ab31 | | | 351 | </DropdownField> |
| b69ab31 | | | 352 | {provider != null ? ( |
| b69ab31 | | | 353 | <DropdownField title={<T>Code Review Provider</T>}> |
| b69ab31 | | | 354 | <span> |
| b69ab31 | | | 355 | <Badge>{provider?.name}</Badge> <provider.RepoInfo /> |
| b69ab31 | | | 356 | </span> |
| b69ab31 | | | 357 | </DropdownField> |
| b69ab31 | | | 358 | ) : null} |
| b69ab31 | | | 359 | </DropdownFields> |
| b69ab31 | | | 360 | ); |
| b69ab31 | | | 361 | } |
| b69ab31 | | | 362 | |
| b69ab31 | | | 363 | function changeCwd(newCwd: string) { |
| b69ab31 | | | 364 | serverAPI.postMessage({ |
| b69ab31 | | | 365 | type: 'changeCwd', |
| b69ab31 | | | 366 | cwd: newCwd, |
| b69ab31 | | | 367 | }); |
| b69ab31 | | | 368 | serverAPI.cwdChanged(); |
| b69ab31 | | | 369 | } |
| b69ab31 | | | 370 | |
| b69ab31 | | | 371 | function useCwdOptions() { |
| b69ab31 | | | 372 | const cwdOptions = useAtomValue(availableCwds); |
| b69ab31 | | | 373 | |
| b69ab31 | | | 374 | return cwdOptions.map((cwd, index) => ({ |
| b69ab31 | | | 375 | id: cwdOptions[index].cwd, |
| b69ab31 | | | 376 | label: cwd.repoRelativeCwdLabel ?? cwd.cwd, |
| b69ab31 | | | 377 | valid: cwd.repoRoot != null, |
| b69ab31 | | | 378 | })); |
| b69ab31 | | | 379 | } |
| b69ab31 | | | 380 | |
| b69ab31 | | | 381 | function guessPathSep(path: string): '/' | '\\' { |
| b69ab31 | | | 382 | if (path.includes('\\')) { |
| b69ab31 | | | 383 | return '\\'; |
| b69ab31 | | | 384 | } else { |
| b69ab31 | | | 385 | return '/'; |
| b69ab31 | | | 386 | } |
| b69ab31 | | | 387 | } |
| b69ab31 | | | 388 | |
| b69ab31 | | | 389 | export function CwdSelections({dismiss, divider}: {dismiss: () => unknown; divider?: boolean}) { |
| b69ab31 | | | 390 | const currentCwd = useAtomValue(serverCwd); |
| b69ab31 | | | 391 | const options = useCwdOptions(); |
| b69ab31 | | | 392 | if (options.length < 2) { |
| b69ab31 | | | 393 | return null; |
| b69ab31 | | | 394 | } |
| b69ab31 | | | 395 | |
| b69ab31 | | | 396 | return ( |
| b69ab31 | | | 397 | <DropdownField title={<T>Change active working directory</T>}> |
| b69ab31 | | | 398 | <RadioGroup |
| b69ab31 | | | 399 | choices={options.map(({id, label, valid}) => ({ |
| b69ab31 | | | 400 | title: valid ? ( |
| b69ab31 | | | 401 | label |
| b69ab31 | | | 402 | ) : ( |
| b69ab31 | | | 403 | <Row key={id}> |
| b69ab31 | | | 404 | {label}{' '} |
| b69ab31 | | | 405 | <Subtle> |
| b69ab31 | | | 406 | <T>Not a valid repository</T> |
| b69ab31 | | | 407 | </Subtle> |
| b69ab31 | | | 408 | </Row> |
| b69ab31 | | | 409 | ), |
| b69ab31 | | | 410 | value: id, |
| b69ab31 | | | 411 | tooltip: valid |
| b69ab31 | | | 412 | ? id |
| b69ab31 | | | 413 | : t('Path $path does not appear to be a valid Sapling repository', { |
| b69ab31 | | | 414 | replace: {$path: id}, |
| b69ab31 | | | 415 | }), |
| b69ab31 | | | 416 | disabled: !valid, |
| b69ab31 | | | 417 | }))} |
| b69ab31 | | | 418 | current={currentCwd} |
| b69ab31 | | | 419 | onChange={newCwd => { |
| b69ab31 | | | 420 | if (newCwd === currentCwd) { |
| b69ab31 | | | 421 | // nothing to change |
| b69ab31 | | | 422 | return; |
| b69ab31 | | | 423 | } |
| b69ab31 | | | 424 | changeCwd(newCwd); |
| b69ab31 | | | 425 | dismiss(); |
| b69ab31 | | | 426 | }} |
| b69ab31 | | | 427 | /> |
| b69ab31 | | | 428 | {divider && <Divider />} |
| b69ab31 | | | 429 | </DropdownField> |
| b69ab31 | | | 430 | ); |
| b69ab31 | | | 431 | } |
| b69ab31 | | | 432 | |
| b69ab31 | | | 433 | /** |
| b69ab31 | | | 434 | * Dropdown selector for submodules in a breadcrumb style. |
| b69ab31 | | | 435 | */ |
| b69ab31 | | | 436 | function SubmoduleSelector({ |
| b69ab31 | | | 437 | submodules, |
| b69ab31 | | | 438 | selected, |
| b69ab31 | | | 439 | onChangeSelected, |
| b69ab31 | | | 440 | root, |
| b69ab31 | | | 441 | hideRightBorder = true, |
| b69ab31 | | | 442 | }: { |
| b69ab31 | | | 443 | submodules: ReadonlyArray<Submodule>; |
| b69ab31 | | | 444 | selected?: Submodule; |
| b69ab31 | | | 445 | onChangeSelected: (newSelected: Submodule) => unknown; |
| b69ab31 | | | 446 | root: AbsolutePath; |
| b69ab31 | | | 447 | hideRightBorder?: boolean; |
| b69ab31 | | | 448 | }) { |
| b69ab31 | | | 449 | const selectedValue = submodules.find(m => m.path === selected?.path)?.path; |
| b69ab31 | | | 450 | const [query, setQuery] = useState(''); |
| b69ab31 | | | 451 | const toDisplay = submodules |
| b69ab31 | | | 452 | .filter(m => m.name.toLowerCase().includes(query.toLowerCase())) |
| b69ab31 | | | 453 | .sort((a, b) => a.name.localeCompare(b.name)); |
| b69ab31 | | | 454 | |
| b69ab31 | | | 455 | return ( |
| b69ab31 | | | 456 | <> |
| b69ab31 | | | 457 | <Icon |
| b69ab31 | | | 458 | icon="chevron-right" |
| b69ab31 | | | 459 | {...stylex.props( |
| b69ab31 | | | 460 | buttonStyles.icon, |
| b69ab31 | | | 461 | styles.submoduleSeparator, |
| b69ab31 | | | 462 | styles.hideLeftBorder, |
| b69ab31 | | | 463 | styles.hideRightBorder, |
| b69ab31 | | | 464 | )} |
| b69ab31 | | | 465 | /> |
| b69ab31 | | | 466 | <Tooltip |
| b69ab31 | | | 467 | trigger="click" |
| b69ab31 | | | 468 | placement="bottom" |
| b69ab31 | | | 469 | title={<SubmoduleHint path={selectedValue} root={root} />} |
| b69ab31 | | | 470 | component={dismiss => ( |
| b69ab31 | | | 471 | <Column xstyle={styles.submoduleDropdownContainer}> |
| b69ab31 | | | 472 | <TextField |
| b69ab31 | | | 473 | autoFocus |
| b69ab31 | | | 474 | width="100%" |
| b69ab31 | | | 475 | placeholder={t('search submodule name')} |
| b69ab31 | | | 476 | value={query} |
| b69ab31 | | | 477 | onInput={e => setQuery(e.currentTarget?.value ?? '')} |
| b69ab31 | | | 478 | /> |
| b69ab31 | | | 479 | <div {...stylex.props(styles.submoduleList)}> |
| b69ab31 | | | 480 | <ScrollY maxSize={360}> |
| b69ab31 | | | 481 | {toDisplay.map(m => ( |
| b69ab31 | | | 482 | <div |
| b69ab31 | | | 483 | key={m.path} |
| b69ab31 | | | 484 | {...stylex.props(styles.submoduleOption)} |
| b69ab31 | | | 485 | onClick={() => { |
| b69ab31 | | | 486 | onChangeSelected(m); |
| b69ab31 | | | 487 | setQuery(''); |
| b69ab31 | | | 488 | dismiss(); |
| b69ab31 | | | 489 | }} |
| b69ab31 | | | 490 | title={m.path}> |
| b69ab31 | | | 491 | {m.name} |
| b69ab31 | | | 492 | </div> |
| b69ab31 | | | 493 | ))} |
| b69ab31 | | | 494 | </ScrollY> |
| b69ab31 | | | 495 | </div> |
| b69ab31 | | | 496 | </Column> |
| b69ab31 | | | 497 | )}> |
| b69ab31 | | | 498 | <Button |
| b69ab31 | | | 499 | kind="icon" |
| b69ab31 | | | 500 | {...stylex.props( |
| b69ab31 | | | 501 | styles.submoduleSelect, |
| b69ab31 | | | 502 | styles.hideLeftBorder, |
| b69ab31 | | | 503 | hideRightBorder && styles.hideRightBorder, |
| b69ab31 | | | 504 | )}> |
| b69ab31 | | | 505 | {selected ? selected.name : `${t('submodules')}...`} |
| b69ab31 | | | 506 | </Button> |
| b69ab31 | | | 507 | </Tooltip> |
| b69ab31 | | | 508 | </> |
| b69ab31 | | | 509 | ); |
| b69ab31 | | | 510 | } |
| b69ab31 | | | 511 | |
| b69ab31 | | | 512 | function SubmoduleHint({path, root}: {path: RepoRelativePath | undefined; root: AbsolutePath}) { |
| b69ab31 | | | 513 | return ( |
| b69ab31 | | | 514 | <T>{path ? `${t('Submodule at')}: ${path}` : `${t('Select a submodule under')}: ${root}`}</T> |
| b69ab31 | | | 515 | ); |
| b69ab31 | | | 516 | } |