Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

truncate commit message in commit view if necessary #59913

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
35 changes: 26 additions & 9 deletions client/web/src/repo/commits/GitCommitNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { GitCommitNodeByline } from './GitCommitNodeByline'

import styles from './GitCommitNode.module.scss'

const TRUNCATED_COMMIT_MESSAGE_LENGTH = 240
export interface GitCommitNodeProps {
node: GitCommitFields

Expand Down Expand Up @@ -90,6 +91,7 @@ export const GitCommitNode: React.FunctionComponent<React.PropsWithChildren<GitC
const settings = useSettings()

const [showCommitMessageBody, setShowCommitMessageBody] = useState<boolean>(false)
const [truncateCommitMessage, setTruncateCommitMessage] = useState<boolean>(true)
const [flashCopiedToClipboardMessage, setFlashCopiedToClipboardMessage] = useState<boolean>(false)

const sourceType = node.perforceChangelist ? RepositoryType.PERFORCE_DEPOT : RepositoryType.GIT_REPOSITORY
Expand Down Expand Up @@ -166,14 +168,30 @@ export const GitCommitNode: React.FunctionComponent<React.PropsWithChildren<GitC
</div>
)

const commitMessageBody =
expandCommitMessageBody || showCommitMessageBody ? (
<div className="w-100">
<pre className={styles.messageBody}>
{node.body && <Linkified input={node.body} externalURLs={node.externalURLs} />}
</pre>
</div>
) : undefined
const commitMessage = node.body ?? ''
const truncationNeeded = commitMessage.length > TRUNCATED_COMMIT_MESSAGE_LENGTH
const truncatedCommitMessage =
truncateCommitMessage && truncationNeeded
? `${commitMessage.slice(0, TRUNCATED_COMMIT_MESSAGE_LENGTH)}...`
: commitMessage

const commitMessageBody = (
<div className="w-100">
<pre className={styles.messageBody}>
<Linkified input={truncatedCommitMessage} externalURLs={node.externalURLs} />
{truncationNeeded && (
<Button
variant="link"
size="sm"
display="inline"
onClick={() => setTruncateCommitMessage(!truncateCommitMessage)}
>
{truncateCommitMessage ? 'see more' : 'see less'}
</Button>
Comment on lines +183 to +190
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also use the commitMessageBody to render the byline below. I do not think we ever want an expand/collapse button in the byline

)}
</pre>
</div>
)

const bylineElement = (
<GitCommitNodeByline
Expand Down Expand Up @@ -362,7 +380,6 @@ export const GitCommitNode: React.FunctionComponent<React.PropsWithChildren<GitC
{!extraCompact && <Link to={canonicalURL}>{oidElement}</Link>}
{afterElement}
</div>
{commitMessageBody}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised to see this line deleted. Doesn't this just make it impossible to actually show the commit message body?

</div>
)}
</>
Expand Down
Loading