-
Notifications
You must be signed in to change notification settings - Fork 0
[design-doctor effort high] replacement clone of sourcebot#2: [design-doctor test] sourcebot-1154: Commit diff viewer #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
00784f2
059240a
3b53335
dbc4d14
7fe2972
4c33b60
29ed7b9
3e20e0d
df906c4
5c36e73
038edcc
5b7b01b
fae7b72
0a11df8
26a710d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,33 @@ | ||
| import { getRepoInfoByName } from "@/actions"; | ||
| import { PathHeader } from "@/app/(app)/components/pathHeader"; | ||
| import { Button } from "@/components/ui/button"; | ||
| import { Separator } from "@/components/ui/separator"; | ||
| import { cn, getCodeHostInfoForRepo, isServiceError } from "@/lib/utils"; | ||
| import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip"; | ||
| import { cn, getCodeHostInfoForRepo, isServiceError, truncateSha } from "@/lib/utils"; | ||
| import { X } from "lucide-react"; | ||
| import Image from "next/image"; | ||
| import Link from "next/link"; | ||
| import { getBrowsePath } from "../../../hooks/utils"; | ||
| import { PureCodePreviewPanel } from "./pureCodePreviewPanel"; | ||
| import { getFileSource } from '@/features/git'; | ||
|
|
||
| interface CodePreviewPanelProps { | ||
| path: string; | ||
| repoName: string; | ||
| revisionName?: string; | ||
| // When set, the file's content is fetched at this ref while the | ||
| // surrounding browse context (path header) stays at `revisionName`. | ||
| previewRef?: string; | ||
| } | ||
|
|
||
| export const CodePreviewPanel = async ({ path, repoName, revisionName }: CodePreviewPanelProps) => { | ||
| export const CodePreviewPanel = async ({ path, repoName, revisionName, previewRef }: CodePreviewPanelProps) => { | ||
| const contentRef = previewRef ?? revisionName; | ||
|
|
||
| const [fileSourceResponse, repoInfoResponse] = await Promise.all([ | ||
| getFileSource({ | ||
| path, | ||
| repo: repoName, | ||
| ref: revisionName, | ||
| ref: contentRef, | ||
| }, { source: 'sourcebot-web-client' }), | ||
| getRepoInfoByName(repoName), | ||
| ]); | ||
|
|
@@ -53,7 +63,7 @@ export const CodePreviewPanel = async ({ path, repoName, revisionName }: CodePre | |
| displayName: repoInfoResponse.displayName, | ||
| externalWebUrl: repoInfoResponse.externalWebUrl, | ||
| }} | ||
| revisionName={revisionName} | ||
| revisionName={contentRef} | ||
| /> | ||
|
|
||
| {fileWebUrl && ( | ||
|
|
@@ -74,12 +84,54 @@ export const CodePreviewPanel = async ({ path, repoName, revisionName }: CodePre | |
| )} | ||
| </div> | ||
| <Separator /> | ||
| {previewRef && ( | ||
| <div className="flex flex-row items-center justify-between gap-2 px-4 py-2 border-b shrink-0"> | ||
| <span className="text-sm"> | ||
| Previewing file at revision{" "} | ||
| <Link | ||
| href={getBrowsePath({ | ||
| repoName, | ||
| revisionName, | ||
| path: '', | ||
| pathType: 'commit', | ||
| commitSha: previewRef, | ||
| })} | ||
| className="font-mono text-link hover:underline" | ||
| > | ||
| {truncateSha(previewRef)} | ||
| </Link> | ||
| </span> | ||
| <Tooltip key={previewRef}> | ||
| <TooltipTrigger> | ||
| <Button | ||
| asChild | ||
| variant="ghost" | ||
| size="icon" | ||
| className="h-6 w-6 text-muted-foreground" | ||
| > | ||
| <Link | ||
| href={getBrowsePath({ | ||
| repoName, | ||
| revisionName, | ||
| path, | ||
| pathType: 'blob', | ||
| })} | ||
| aria-label="Close preview" | ||
| > | ||
| <X className="h-4 w-4" /> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Close and exit icons look slightly oversized next to nearby buttons The close and exit icons in the diff and preview headers are drawn larger than the copy and action icons sitting right beside them, so the small toolbar looks slightly uneven. Matching their size makes the header controls feel balanced. Prompt to fix with AI |
||
| </Link> | ||
| </Button> | ||
| </TooltipTrigger> | ||
| <TooltipContent>Close preview</TooltipContent> | ||
| </Tooltip> | ||
| </div> | ||
| )} | ||
| <PureCodePreviewPanel | ||
| source={fileSourceResponse.source} | ||
| language={fileSourceResponse.language} | ||
| repoName={repoName} | ||
| path={path} | ||
| revisionName={revisionName ?? 'HEAD'} | ||
| revisionName={contentRef ?? 'HEAD'} | ||
| /> | ||
| </> | ||
| ) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| 'use client'; | ||
|
|
||
| import { CopyIconButton } from "@/app/(app)/components/copyIconButton"; | ||
| import { useToast } from "@/components/hooks/use-toast"; | ||
| import Link from "next/link"; | ||
| import { Fragment, useCallback } from "react"; | ||
| import { getBrowsePath } from "../../../hooks/utils"; | ||
|
|
||
| interface CommitHashLineProps { | ||
| repoName: string; | ||
| commitHash: string; | ||
| parents: string[]; | ||
| } | ||
|
|
||
| export const CommitHashLine = ({ repoName, commitHash, parents }: CommitHashLineProps) => { | ||
| const { toast } = useToast(); | ||
|
|
||
| const onCopyHash = useCallback(() => { | ||
| navigator.clipboard.writeText(commitHash).then(() => { | ||
| toast({ description: "✅ Copied commit SHA to clipboard" }); | ||
| }); | ||
| return true; | ||
| }, [commitHash, toast]); | ||
|
|
||
| return ( | ||
| <div className="text-xs font-mono text-muted-foreground flex flex-row items-center gap-1"> | ||
| {parents.length > 0 && ( | ||
| <> | ||
| <span> | ||
| {parents.length} parent{parents.length > 1 ? 's' : ''} | ||
| </span> | ||
| {parents.map((parent, i) => ( | ||
| <Fragment key={parent}> | ||
| {i > 0 && <span>+</span>} | ||
| <Link | ||
| href={getBrowsePath({ | ||
| repoName, | ||
| path: '', | ||
| pathType: 'commit', | ||
| commitSha: parent, | ||
| })} | ||
| className="underline hover:text-foreground" | ||
| title={parent} | ||
| > | ||
| {parent.slice(0, 7)} | ||
| </Link> | ||
| </Fragment> | ||
| ))} | ||
| </> | ||
| )} | ||
| <span>commit {commitHash.slice(0, 7)}</span> | ||
| <CopyIconButton onCopy={onCopyHash} /> | ||
| </div> | ||
| ); | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| 'use client'; | ||
|
|
||
| import { CommitBody, CommitBodyToggle } from "@/app/(app)/browse/components/commitParts"; | ||
| import { useState } from "react"; | ||
|
|
||
| interface CommitMessageProps { | ||
| subject: string; | ||
| body: string; | ||
| } | ||
|
|
||
| export const CommitMessage = ({ subject, body }: CommitMessageProps) => { | ||
| const [isBodyExpanded, setIsBodyExpanded] = useState(false); | ||
| const hasBody = body.trim().length > 0; | ||
|
|
||
| return ( | ||
| <> | ||
| <div className="flex flex-row items-center gap-2"> | ||
| <h1 className="text-lg font-semibold">{subject}</h1> | ||
| {hasBody && ( | ||
| <CommitBodyToggle | ||
| pressed={isBodyExpanded} | ||
| onPressedChange={setIsBodyExpanded} | ||
| /> | ||
| )} | ||
| </div> | ||
| {hasBody && isBodyExpanded && ( | ||
| <CommitBody body={body} className="rounded max-h-[40vh] overflow-y-auto" /> | ||
| )} | ||
| </> | ||
| ); | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import { FileDiff } from "@/features/git"; | ||
|
|
||
| const TOTAL_SQUARES = 5; | ||
|
|
||
| // Count `+`/`-` lines across all hunks in a file. | ||
| export const computeChangeCounts = (file: FileDiff) => { | ||
| let additions = 0; | ||
| let deletions = 0; | ||
| for (const hunk of file.hunks) { | ||
| for (const raw of hunk.body.split('\n')) { | ||
| if (raw.startsWith('+')) { | ||
| additions++; | ||
| } else if (raw.startsWith('-')) { | ||
| deletions++; | ||
| } | ||
| } | ||
| } | ||
| return { additions, deletions }; | ||
| }; | ||
|
|
||
| // Sum line-level change counts across multiple files. | ||
| export const computeTotalChangeCounts = (files: FileDiff[]) => { | ||
| let additions = 0; | ||
| let deletions = 0; | ||
| for (const file of files) { | ||
| const counts = computeChangeCounts(file); | ||
| additions += counts.additions; | ||
| deletions += counts.deletions; | ||
| } | ||
| return { additions, deletions }; | ||
| }; | ||
|
|
||
| // Map a total change count to a number of filled squares (0–5) using a | ||
| // log-ish scale so tiny diffs still show one square and huge diffs cap out. | ||
| // Mirrors GitHub's diffstat indicator behavior. | ||
| const filledSquaresForTotal = (total: number): number => { | ||
| if (total === 0) { | ||
| return 0; | ||
| } | ||
| if (total < 5) { | ||
| return 1; | ||
| } | ||
| if (total < 10) { | ||
| return 2; | ||
| } | ||
| if (total < 30) { | ||
| return 3; | ||
| } | ||
| if (total < 100) { | ||
| return 4; | ||
| } | ||
| return 5; | ||
| }; | ||
|
|
||
| interface DiffStatProps { | ||
| additions: number; | ||
| deletions: number; | ||
| } | ||
|
|
||
| export const DiffStat = ({ additions, deletions }: DiffStatProps) => { | ||
| const total = additions + deletions; | ||
|
|
||
| // Skip rendering when there are no line-level changes (e.g. pure renames). | ||
| if (total === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| const filled = filledSquaresForTotal(total); | ||
| const greenCount = Math.round((filled * additions) / total); | ||
| const redCount = filled - greenCount; | ||
| const emptyCount = TOTAL_SQUARES - filled; | ||
|
|
||
| return ( | ||
| <div | ||
| className="flex flex-row items-center gap-2 text-xs flex-shrink-0 font-mono" | ||
| title={`${additions} additions, ${deletions} deletions`} | ||
| > | ||
| {additions > 0 && ( | ||
| <span className="text-green-700 dark:text-green-400">+{additions}</span> | ||
| )} | ||
| {deletions > 0 && ( | ||
| <span className="text-red-700 dark:text-red-400">-{deletions}</span> | ||
| )} | ||
| <div className="flex flex-row gap-px"> | ||
| {Array.from({ length: greenCount }).map((_, i) => ( | ||
| <span key={`g-${i}`} className="w-2 h-2 bg-green-500 dark:bg-green-400 rounded-[1px]" /> | ||
| ))} | ||
| {Array.from({ length: redCount }).map((_, i) => ( | ||
| <span key={`r-${i}`} className="w-2 h-2 bg-red-500 dark:bg-red-400 rounded-[1px]" /> | ||
| ))} | ||
| {Array.from({ length: emptyCount }).map((_, i) => ( | ||
| <span key={`e-${i}`} className="w-2 h-2 bg-border rounded-[1px]" /> | ||
| ))} | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Close-preview control has a doubled-up wrapper
The close control on the file preview banner is wrapped in an extra invisible control, so keyboard focus and its highlight can fall on the area around the icon rather than the button. It should focus and highlight as a single clean icon button like other dismiss controls.
Prompt to fix with AI