| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import type {CommitInfo} from '../types'; |
| 9 | |
| 10 | import * as stylex from '@stylexjs/stylex'; |
| 11 | import {ErrorBoundary} from 'isl-components/ErrorNotice'; |
| 12 | import {Icon} from 'isl-components/Icon'; |
| 13 | import {Tooltip} from 'isl-components/Tooltip'; |
| 14 | import {Row} from '../ComponentUtils'; |
| 15 | import {T, t} from '../i18n'; |
| 16 | import { |
| 17 | useFetchPendingSignificantLinesOfCode, |
| 18 | useFetchSignificantLinesOfCode, |
| 19 | } from '../sloc/useFetchSignificantLinesOfCode'; |
| 20 | |
| 21 | type Props = {commit: CommitInfo}; |
| 22 | const styles = stylex.create({ |
| 23 | locInfo: { |
| 24 | alignItems: 'center', |
| 25 | fontWeight: 'bold', |
| 26 | textTransform: 'lowercase', |
| 27 | fontSize: '85%', |
| 28 | opacity: 0.9, |
| 29 | gap: 'var(--halfpad)', |
| 30 | }, |
| 31 | }); |
| 32 | export function LoadingDiffStatsView() { |
| 33 | return ( |
| 34 | <DiffStatsView> |
| 35 | <Icon icon="loading" size="XS" /> |
| 36 | <T>lines</T> |
| 37 | </DiffStatsView> |
| 38 | ); |
| 39 | } |
| 40 | export function DiffStats({commit}: Props) { |
| 41 | const {slocInfo, isLoading} = useFetchSignificantLinesOfCode(commit); |
| 42 | const significantLinesOfCode = slocInfo?.sloc; |
| 43 | |
| 44 | if (isLoading && significantLinesOfCode == null) { |
| 45 | return <LoadingDiffStatsView />; |
| 46 | } else if (!isLoading && significantLinesOfCode == null) { |
| 47 | return null; |
| 48 | } |
| 49 | return <ResolvedDiffStatsView significantLinesOfCode={significantLinesOfCode} />; |
| 50 | } |
| 51 | |
| 52 | export function PendingDiffStats() { |
| 53 | return ( |
| 54 | <ErrorBoundary> |
| 55 | <PendingDiffStatsView /> |
| 56 | </ErrorBoundary> |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | export function PendingDiffStatsView() { |
| 61 | const {slocInfo, isLoading} = useFetchPendingSignificantLinesOfCode(); |
| 62 | const significantLinesOfCode = slocInfo?.sloc; |
| 63 | |
| 64 | if (isLoading && significantLinesOfCode == null) { |
| 65 | return <LoadingDiffStatsView />; |
| 66 | } else if (!isLoading && significantLinesOfCode == null) { |
| 67 | return null; |
| 68 | } |
| 69 | return <ResolvedDiffStatsView significantLinesOfCode={significantLinesOfCode} />; |
| 70 | } |
| 71 | |
| 72 | function ResolvedDiffStatsView({ |
| 73 | significantLinesOfCode, |
| 74 | }: { |
| 75 | significantLinesOfCode: number | undefined; |
| 76 | }) { |
| 77 | if (significantLinesOfCode == null) { |
| 78 | return null; |
| 79 | } |
| 80 | |
| 81 | return ( |
| 82 | <DiffStatsView> |
| 83 | <T replace={{$num: significantLinesOfCode}}>$num lines</T> |
| 84 | </DiffStatsView> |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | function DiffStatsView({extras, children}: {extras?: React.ReactNode; children: React.ReactNode}) { |
| 89 | return ( |
| 90 | <Row xstyle={styles.locInfo}> |
| 91 | <Icon icon="code" /> |
| 92 | {children} |
| 93 | <Tooltip |
| 94 | title={t( |
| 95 | 'This number reflects significant lines of code: non-blank, non-generated additions + deletions', |
| 96 | )}> |
| 97 | <Icon icon="info" /> |
| 98 | </Tooltip> |
| 99 | {extras} |
| 100 | </Row> |
| 101 | ); |
| 102 | } |
| 103 | |