| 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 type {ParsedDiff} from 'diff'; |
| b69ab31 | | | 10 | import {Icon} from 'isl-components/Icon'; |
| b69ab31 | | | 11 | import type {ReactNode} from 'react'; |
| b69ab31 | | | 12 | import {forwardRef, lazy, Suspense} from 'react'; |
| b69ab31 | | | 13 | import {ComparisonType} from 'shared/Comparison'; |
| b69ab31 | | | 14 | import type {DiffViewMode} from '../ComparisonView/SplitDiffView/types'; |
| b69ab31 | | | 15 | import {Column, Row} from '../ComponentUtils'; |
| b69ab31 | | | 16 | import type {ThemeColor} from '../theme'; |
| b69ab31 | | | 17 | import type {CodeChange, DiffComment, SuggestedChange} from '../types'; |
| b69ab31 | | | 18 | import {CodePatchSuggestionStatus, SuggestedChangeType} from '../types'; |
| b69ab31 | | | 19 | import InlineCommentContent from './InlineCommentContent'; |
| b69ab31 | | | 20 | import InlineCommentSuggestionActionBottomBar from './InlineCommentSuggestionActionBottomBar'; |
| b69ab31 | | | 21 | |
| b69ab31 | | | 22 | const InlineCommentComparisonView = lazy(() => import('./InlineCommentComparisonView')); |
| b69ab31 | | | 23 | |
| b69ab31 | | | 24 | const styles = stylex.create({ |
| b69ab31 | | | 25 | alignTop: {alignItems: 'flex-start'}, |
| b69ab31 | | | 26 | subheadingsAlignBaseline: {alignItems: 'baseline', gap: '4px'}, |
| b69ab31 | | | 27 | tooltipBar: {width: '100%', justifyContent: 'space-between'}, |
| b69ab31 | | | 28 | boldText: {fontWeight: '700', fontSize: '100%'}, |
| b69ab31 | | | 29 | subtle: {fontSize: '90%', opacity: 0.8}, |
| b69ab31 | | | 30 | headerRow: { |
| b69ab31 | | | 31 | justifyContent: 'space-between', |
| b69ab31 | | | 32 | alignItems: 'start', |
| b69ab31 | | | 33 | wordBreak: 'break-word', |
| b69ab31 | | | 34 | }, |
| b69ab31 | | | 35 | headerContent: { |
| b69ab31 | | | 36 | maxWidth: '320px', |
| b69ab31 | | | 37 | whiteSpace: 'nowrap', |
| b69ab31 | | | 38 | overflow: 'hidden', |
| b69ab31 | | | 39 | textOverflow: 'ellipsis', |
| b69ab31 | | | 40 | }, |
| b69ab31 | | | 41 | headerControl: { |
| b69ab31 | | | 42 | gap: '4px', |
| b69ab31 | | | 43 | }, |
| b69ab31 | | | 44 | shortCutTipLink: { |
| b69ab31 | | | 45 | overflow: 'hidden', |
| b69ab31 | | | 46 | textOverflow: 'ellipsis', |
| b69ab31 | | | 47 | fontSize: '12px', |
| b69ab31 | | | 48 | color: 'var(--vscode-descriptionForeground)', |
| b69ab31 | | | 49 | cursor: 'pointer', |
| b69ab31 | | | 50 | }, |
| b69ab31 | | | 51 | underlineOnHoverText: { |
| b69ab31 | | | 52 | textDecoration: { |
| b69ab31 | | | 53 | ':hover': 'underline', |
| b69ab31 | | | 54 | }, |
| b69ab31 | | | 55 | }, |
| b69ab31 | | | 56 | }); |
| b69ab31 | | | 57 | |
| b69ab31 | | | 58 | export const InlineComment = forwardRef( |
| b69ab31 | | | 59 | ( |
| b69ab31 | | | 60 | { |
| b69ab31 | | | 61 | comment, |
| b69ab31 | | | 62 | versionInfo, |
| b69ab31 | | | 63 | collapsed, |
| b69ab31 | | | 64 | diffViewMode, |
| b69ab31 | | | 65 | onAccept, |
| b69ab31 | | | 66 | onReject, |
| b69ab31 | | | 67 | onResolve, |
| b69ab31 | | | 68 | onUnresolve, |
| b69ab31 | | | 69 | onClickHeader, |
| b69ab31 | | | 70 | headerControls, |
| b69ab31 | | | 71 | bottomControls, |
| b69ab31 | | | 72 | useThemeHook, |
| b69ab31 | | | 73 | }: { |
| b69ab31 | | | 74 | comment: DiffComment; |
| b69ab31 | | | 75 | versionInfo?: { |
| b69ab31 | | | 76 | isLatestVersion?: boolean; |
| b69ab31 | | | 77 | versionAbbr?: string; |
| b69ab31 | | | 78 | }; |
| b69ab31 | | | 79 | collapsed: boolean; |
| b69ab31 | | | 80 | diffViewMode: DiffViewMode; |
| b69ab31 | | | 81 | |
| b69ab31 | | | 82 | onAccept: (codeSuggestion?: SuggestedChange) => unknown; |
| b69ab31 | | | 83 | onReject: (codeSuggestion?: SuggestedChange) => unknown; |
| b69ab31 | | | 84 | onResolve: () => unknown; |
| b69ab31 | | | 85 | onUnresolve: () => unknown; |
| b69ab31 | | | 86 | onClickHeader: () => unknown; |
| b69ab31 | | | 87 | |
| b69ab31 | | | 88 | headerControls: ReactNode; |
| b69ab31 | | | 89 | bottomControls: ReactNode; |
| b69ab31 | | | 90 | |
| b69ab31 | | | 91 | useThemeHook: () => ThemeColor; |
| b69ab31 | | | 92 | }, |
| b69ab31 | | | 93 | ref: React.ForwardedRef<HTMLDivElement>, |
| b69ab31 | | | 94 | ) => { |
| b69ab31 | | | 95 | const path = comment?.filename ?? ''; |
| b69ab31 | | | 96 | const codeSuggestion = comment?.suggestedChange ?? null; |
| b69ab31 | | | 97 | const authorName = comment.authorName; |
| b69ab31 | | | 98 | const codeChange = codeSuggestion?.codeChange; |
| b69ab31 | | | 99 | |
| b69ab31 | | | 100 | const renderDiffView = (codeChange: CodeChange[]) => { |
| b69ab31 | | | 101 | const changes = codeChange?.filter( |
| b69ab31 | | | 102 | (change): change is CodeChange & {patch: ParsedDiff} => change.patch != null, |
| b69ab31 | | | 103 | ); |
| b69ab31 | | | 104 | |
| b69ab31 | | | 105 | if (changes == null || changes.length === 0) { |
| b69ab31 | | | 106 | return null; |
| b69ab31 | | | 107 | } |
| b69ab31 | | | 108 | |
| b69ab31 | | | 109 | return changes.map((change, i) => { |
| b69ab31 | | | 110 | return ( |
| b69ab31 | | | 111 | <div key={i}> |
| b69ab31 | | | 112 | {changes.length === 1 ? null : ( |
| b69ab31 | | | 113 | <div {...stylex.props(styles.boldText)}>Change {i + 1}</div> |
| b69ab31 | | | 114 | )} |
| b69ab31 | | | 115 | <InlineCommentComparisonView |
| b69ab31 | | | 116 | path={path} |
| b69ab31 | | | 117 | suggestion={change.patch} |
| b69ab31 | | | 118 | ctx={{ |
| b69ab31 | | | 119 | collapsed: false, |
| b69ab31 | | | 120 | displayLineNumbers: changes.length > 1, // TODO: currently this line number is not aligned value |
| b69ab31 | | | 121 | id: { |
| b69ab31 | | | 122 | comparison: {type: ComparisonType.HeadChanges}, |
| b69ab31 | | | 123 | path, |
| b69ab31 | | | 124 | }, |
| b69ab31 | | | 125 | setCollapsed: () => null, |
| b69ab31 | | | 126 | display: diffViewMode, |
| b69ab31 | | | 127 | useThemeHook, |
| b69ab31 | | | 128 | }} |
| b69ab31 | | | 129 | /> |
| b69ab31 | | | 130 | </div> |
| b69ab31 | | | 131 | ); |
| b69ab31 | | | 132 | }); |
| b69ab31 | | | 133 | }; |
| b69ab31 | | | 134 | |
| b69ab31 | | | 135 | return ( |
| b69ab31 | | | 136 | <div |
| b69ab31 | | | 137 | ref={ref} |
| b69ab31 | | | 138 | className="container" |
| b69ab31 | | | 139 | style={{width: collapsed || diffViewMode === 'unified' ? 600 : 1000}}> |
| b69ab31 | | | 140 | {collapsed ? ( |
| b69ab31 | | | 141 | <div className="headerRow" onClick={onClickHeader}> |
| b69ab31 | | | 142 | <div className="headerLeftContent"> |
| b69ab31 | | | 143 | <Icon icon="comment" /> |
| b69ab31 | | | 144 | <div className="headerTitle"> |
| b69ab31 | | | 145 | <span {...stylex.props(styles.boldText)}>{authorName}</span> |
| b69ab31 | | | 146 | {comment.content != null && comment.content !== '' && ( |
| b69ab31 | | | 147 | <div {...stylex.props(styles.subtle, styles.headerContent)}> |
| b69ab31 | | | 148 | {comment.content} |
| b69ab31 | | | 149 | </div> |
| b69ab31 | | | 150 | )} |
| b69ab31 | | | 151 | </div> |
| b69ab31 | | | 152 | </div> |
| b69ab31 | | | 153 | <Row xstyle={styles.headerControl}>{headerControls}</Row> |
| b69ab31 | | | 154 | </div> |
| b69ab31 | | | 155 | ) : ( |
| b69ab31 | | | 156 | <> |
| b69ab31 | | | 157 | <Row xstyle={styles.headerRow}> |
| b69ab31 | | | 158 | <Column alignStart style={{marginBlock: '8px'}}> |
| b69ab31 | | | 159 | <InlineCommentContent |
| b69ab31 | | | 160 | comment={comment} |
| b69ab31 | | | 161 | isHeadComment={true} |
| b69ab31 | | | 162 | isLatestVersion={versionInfo?.isLatestVersion} |
| b69ab31 | | | 163 | versionAbbr={versionInfo?.versionAbbr} |
| b69ab31 | | | 164 | /> |
| b69ab31 | | | 165 | {comment.replies.map((reply, i) => ( |
| b69ab31 | | | 166 | <InlineCommentContent comment={reply} key={i} /> |
| b69ab31 | | | 167 | ))} |
| b69ab31 | | | 168 | </Column> |
| b69ab31 | | | 169 | <Row xstyle={styles.headerControl}>{headerControls}</Row> |
| b69ab31 | | | 170 | </Row> |
| b69ab31 | | | 171 | <Column alignStart style={{marginBlock: '8px'}}> |
| b69ab31 | | | 172 | {path && codeSuggestion != null && codeChange != null && ( |
| b69ab31 | | | 173 | <> |
| b69ab31 | | | 174 | {codeSuggestion.type !== SuggestedChangeType.HUMAN_SUGGESTION && ( |
| b69ab31 | | | 175 | <Row xstyle={styles.subheadingsAlignBaseline}> |
| b69ab31 | | | 176 | <div {...stylex.props(styles.boldText)}> |
| b69ab31 | | | 177 | {codeSuggestion.type === SuggestedChangeType.METAMATE_SUGGESTION |
| b69ab31 | | | 178 | ? 'Metamate' |
| b69ab31 | | | 179 | : 'Signal'} |
| b69ab31 | | | 180 | </div> |
| b69ab31 | | | 181 | <div {...stylex.props(styles.subtle)}>suggested changes</div> |
| b69ab31 | | | 182 | </Row> |
| b69ab31 | | | 183 | )} |
| b69ab31 | | | 184 | <Suspense>{renderDiffView(codeChange)}</Suspense> |
| b69ab31 | | | 185 | </> |
| b69ab31 | | | 186 | )} |
| b69ab31 | | | 187 | <Row xstyle={styles.tooltipBar}> |
| b69ab31 | | | 188 | {codeSuggestion?.status != null ? ( |
| b69ab31 | | | 189 | <InlineCommentSuggestionActionBottomBar |
| b69ab31 | | | 190 | resolved={codeSuggestion.status === CodePatchSuggestionStatus.Accepted} |
| b69ab31 | | | 191 | onAccept={() => onAccept(codeSuggestion)} |
| b69ab31 | | | 192 | onReject={() => onReject(codeSuggestion)} |
| b69ab31 | | | 193 | /> |
| b69ab31 | | | 194 | ) : ( |
| b69ab31 | | | 195 | <InlineCommentSuggestionActionBottomBar |
| b69ab31 | | | 196 | resolved={comment.isResolved ?? false} |
| b69ab31 | | | 197 | onAccept={onResolve} |
| b69ab31 | | | 198 | onReject={onUnresolve} |
| b69ab31 | | | 199 | acceptLabel="Resolve" |
| b69ab31 | | | 200 | rejectLabel="Unresolve" |
| b69ab31 | | | 201 | isToggle={true} |
| b69ab31 | | | 202 | /> |
| b69ab31 | | | 203 | )} |
| b69ab31 | | | 204 | <Row>{bottomControls}</Row> |
| b69ab31 | | | 205 | </Row> |
| b69ab31 | | | 206 | </Column> |
| b69ab31 | | | 207 | </> |
| b69ab31 | | | 208 | )} |
| b69ab31 | | | 209 | </div> |
| b69ab31 | | | 210 | ); |
| b69ab31 | | | 211 | }, |
| b69ab31 | | | 212 | ); |