| 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 {EnsureAssignedTogether} from 'shared/EnsureAssignedTogether'; |
| b69ab31 | | | 9 | import type {DiffType} from 'shared/patch/types'; |
| b69ab31 | | | 10 | import type {RepoPath} from 'shared/types/common'; |
| b69ab31 | | | 11 | |
| b69ab31 | | | 12 | import {Button} from 'isl-components/Button'; |
| b69ab31 | | | 13 | import {Icon} from 'isl-components/Icon'; |
| b69ab31 | | | 14 | import {Tooltip} from 'isl-components/Tooltip'; |
| b69ab31 | | | 15 | import React from 'react'; |
| b69ab31 | | | 16 | import {t} from '../../i18n'; |
| b69ab31 | | | 17 | import platform from '../../platform'; |
| b69ab31 | | | 18 | |
| b69ab31 | | | 19 | import './SplitDiffHunk.css'; |
| b69ab31 | | | 20 | |
| b69ab31 | | | 21 | /** |
| b69ab31 | | | 22 | * Decides the icon of the file header. |
| b69ab31 | | | 23 | * Subset of `DiffType` - no Copied for Removed. |
| b69ab31 | | | 24 | */ |
| b69ab31 | | | 25 | export enum IconType { |
| b69ab31 | | | 26 | Modified = 'Modified', |
| b69ab31 | | | 27 | Added = 'Added', |
| b69ab31 | | | 28 | Removed = 'Removed', |
| b69ab31 | | | 29 | } |
| b69ab31 | | | 30 | |
| b69ab31 | | | 31 | export function diffTypeToIconType(diffType?: DiffType): IconType { |
| b69ab31 | | | 32 | if (diffType != null) { |
| b69ab31 | | | 33 | const diffTypeStr = diffType as string; |
| b69ab31 | | | 34 | if (diffTypeStr === 'Added' || diffTypeStr === 'Removed' || diffTypeStr === 'Modified') { |
| b69ab31 | | | 35 | return diffTypeStr as IconType; |
| b69ab31 | | | 36 | } |
| b69ab31 | | | 37 | } |
| b69ab31 | | | 38 | // "Copied" and "Renamed" should only apply to new files. |
| b69ab31 | | | 39 | return IconType.Added; |
| b69ab31 | | | 40 | } |
| b69ab31 | | | 41 | |
| b69ab31 | | | 42 | export function FileHeader({ |
| b69ab31 | | | 43 | path, |
| b69ab31 | | | 44 | copyFrom, |
| b69ab31 | | | 45 | iconType, |
| b69ab31 | | | 46 | open, |
| b69ab31 | | | 47 | onChangeOpen, |
| b69ab31 | | | 48 | fileActions, |
| b69ab31 | | | 49 | }: { |
| b69ab31 | | | 50 | path: RepoPath; |
| b69ab31 | | | 51 | copyFrom?: RepoPath; |
| b69ab31 | | | 52 | iconType?: IconType; |
| b69ab31 | | | 53 | fileActions?: JSX.Element; |
| b69ab31 | | | 54 | } & EnsureAssignedTogether<{ |
| b69ab31 | | | 55 | open: boolean; |
| b69ab31 | | | 56 | onChangeOpen: (open: boolean) => void; |
| b69ab31 | | | 57 | }>) { |
| b69ab31 | | | 58 | // Even though the enclosing <SplitDiffView> will have border-radius set, we |
| b69ab31 | | | 59 | // have to define it again here or things don't look right. |
| b69ab31 | | | 60 | const color = |
| b69ab31 | | | 61 | iconType === undefined ? 'var(--scm-modified-foreground)' : iconTypeToColor[iconType]; |
| b69ab31 | | | 62 | |
| b69ab31 | | | 63 | const pathSeparator = '/'; |
| b69ab31 | | | 64 | const pathParts = path.split(pathSeparator); |
| b69ab31 | | | 65 | |
| b69ab31 | | | 66 | // show: dir1 / dir2 / ... / dir9 / file |
| b69ab31 | | | 67 | // ^^^^ ^^^^ ^^^^ |
| b69ab31 | | | 68 | // copy: full path "dir9/file" "file" |
| b69ab31 | | | 69 | |
| b69ab31 | | | 70 | // with copyFrom: "dir1/dir2/dir3/foo" renamed to "dir1/dir2/dir4/bar" |
| b69ab31 | | | 71 | // show: dir1 / dir2 / { dir3 / foo -> dir4 / bar } |
| b69ab31 | | | 72 | // commonPrefixLen = 2 # (dir1 / dir2) |
| b69ab31 | | | 73 | // copyFromRest = "dir3/foo" |
| b69ab31 | | | 74 | let commonPrefixLen = -1; |
| b69ab31 | | | 75 | let copyFromRest = ''; |
| b69ab31 | | | 76 | if (copyFrom != null && copyFrom !== path) { |
| b69ab31 | | | 77 | const copyFromParts = copyFrom.split(pathSeparator); |
| b69ab31 | | | 78 | commonPrefixLen = commonPrefixLength(pathParts, copyFromParts); |
| b69ab31 | | | 79 | copyFromRest = copyFromParts.slice(commonPrefixLen).join(pathSeparator); |
| b69ab31 | | | 80 | } |
| b69ab31 | | | 81 | |
| b69ab31 | | | 82 | const copySpan = (s: string) => <span className="file-header-copyfrom-path">{s}</span>; |
| b69ab31 | | | 83 | const filePathParts = pathParts.map((part, idx) => { |
| b69ab31 | | | 84 | const pathSoFar = pathParts.slice(idx).join(pathSeparator); |
| b69ab31 | | | 85 | let copyFromLeft = null; |
| b69ab31 | | | 86 | let copyFromRight = null; |
| b69ab31 | | | 87 | if (idx === commonPrefixLen && copyFromRest.length > 0) { |
| b69ab31 | | | 88 | // Insert "{" (when commonPrefix is not empty), " copyFromRest ->". |
| b69ab31 | | | 89 | const prefix = commonPrefixLen > 0 ? '{ ' : ''; |
| b69ab31 | | | 90 | copyFromLeft = ( |
| b69ab31 | | | 91 | <Tooltip |
| b69ab31 | | | 92 | title={t('Renamed or copied from $path', {replace: {$path: copyFrom ?? ''}})} |
| b69ab31 | | | 93 | delayMs={100} |
| b69ab31 | | | 94 | placement="bottom"> |
| b69ab31 | | | 95 | {copySpan(`${prefix}${copyFromRest} →`)} |
| b69ab31 | | | 96 | </Tooltip> |
| b69ab31 | | | 97 | ); |
| b69ab31 | | | 98 | } |
| b69ab31 | | | 99 | if (idx + 1 === pathParts.length && commonPrefixLen > 0 && copyFromRest.length > 0) { |
| b69ab31 | | | 100 | // Append "}" (when commonPrefix is not empty) |
| b69ab31 | | | 101 | copyFromRight = copySpan('}'); |
| b69ab31 | | | 102 | } |
| b69ab31 | | | 103 | return ( |
| b69ab31 | | | 104 | <React.Fragment key={idx}> |
| b69ab31 | | | 105 | {copyFromLeft} |
| b69ab31 | | | 106 | <span className={'file-header-copyable-path'}> |
| b69ab31 | | | 107 | <Tooltip |
| b69ab31 | | | 108 | component={() => ( |
| b69ab31 | | | 109 | <span className="file-header-copyable-path-hover"> |
| b69ab31 | | | 110 | {t('Copy $path', {replace: {$path: pathSoFar}})} |
| b69ab31 | | | 111 | </span> |
| b69ab31 | | | 112 | )} |
| b69ab31 | | | 113 | delayMs={100} |
| b69ab31 | | | 114 | placement="bottom"> |
| b69ab31 | | | 115 | <span onClick={() => platform.clipboardCopy(pathSoFar)}> |
| b69ab31 | | | 116 | {part} |
| b69ab31 | | | 117 | {idx < pathParts.length - 1 ? pathSeparator : ''} |
| b69ab31 | | | 118 | </span> |
| b69ab31 | | | 119 | </Tooltip> |
| b69ab31 | | | 120 | </span> |
| b69ab31 | | | 121 | {copyFromRight} |
| b69ab31 | | | 122 | </React.Fragment> |
| b69ab31 | | | 123 | ); |
| b69ab31 | | | 124 | }); |
| b69ab31 | | | 125 | |
| b69ab31 | | | 126 | return ( |
| b69ab31 | | | 127 | <div |
| b69ab31 | | | 128 | className={`split-diff-view-file-header file-header-${open ? 'open' : 'collapsed'}`} |
| b69ab31 | | | 129 | style={{color}}> |
| b69ab31 | | | 130 | {onChangeOpen && ( |
| b69ab31 | | | 131 | <Button |
| b69ab31 | | | 132 | icon |
| b69ab31 | | | 133 | className="split-diff-view-file-header-open-button" |
| b69ab31 | | | 134 | data-testid={`split-diff-view-file-header-${open ? 'collapse' : 'expand'}-button`} |
| b69ab31 | | | 135 | onClick={() => onChangeOpen(!open)}> |
| b69ab31 | | | 136 | <Icon icon={open ? 'chevron-down' : 'chevron-right'} /> |
| b69ab31 | | | 137 | </Button> |
| b69ab31 | | | 138 | )} |
| b69ab31 | | | 139 | {iconType !== undefined && ( |
| b69ab31 | | | 140 | <Tooltip title={iconTypeToTooltip[iconType]}> |
| b69ab31 | | | 141 | <Icon icon={iconTypeToIcon[iconType]} /> |
| b69ab31 | | | 142 | </Tooltip> |
| b69ab31 | | | 143 | )} |
| b69ab31 | | | 144 | <div className="split-diff-view-file-path-parts">{filePathParts}</div> |
| b69ab31 | | | 145 | {fileActions} |
| b69ab31 | | | 146 | </div> |
| b69ab31 | | | 147 | ); |
| b69ab31 | | | 148 | } |
| b69ab31 | | | 149 | |
| b69ab31 | | | 150 | const iconTypeToColor: Record<keyof typeof IconType, string> = { |
| b69ab31 | | | 151 | Modified: 'var(--scm-modified-foreground)', |
| b69ab31 | | | 152 | Added: 'var(--scm-added-foreground)', |
| b69ab31 | | | 153 | Removed: 'var(--scm-removed-foreground)', |
| b69ab31 | | | 154 | }; |
| b69ab31 | | | 155 | |
| b69ab31 | | | 156 | const iconTypeToIcon: Record<keyof typeof IconType, string> = { |
| b69ab31 | | | 157 | Modified: 'diff-modified', |
| b69ab31 | | | 158 | Added: 'diff-added', |
| b69ab31 | | | 159 | Removed: 'diff-removed', |
| b69ab31 | | | 160 | }; |
| b69ab31 | | | 161 | |
| b69ab31 | | | 162 | const iconTypeToTooltip: Record<keyof typeof IconType, string> = { |
| b69ab31 | | | 163 | Modified: t('This file was modified.'), |
| b69ab31 | | | 164 | Added: t('This file was added.'), |
| b69ab31 | | | 165 | Removed: t('This file was removed.'), |
| b69ab31 | | | 166 | }; |
| b69ab31 | | | 167 | |
| b69ab31 | | | 168 | function commonPrefixLength<T>(a: Array<T>, b: Array<T>): number { |
| b69ab31 | | | 169 | let i = 0; |
| b69ab31 | | | 170 | while (i < a.length && i < b.length && a[i] === b[i]) { |
| b69ab31 | | | 171 | i++; |
| b69ab31 | | | 172 | } |
| b69ab31 | | | 173 | return i; |
| b69ab31 | | | 174 | } |