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