Skip to content

Commit cfafade

Browse files
authored
Merge pull request #219 from liuliu-dev/add-diff-view
Add diff view
2 parents 3cd666a + a895ef5 commit cfafade

18 files changed

+597
-135
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@
1919
.npmignore
2020

2121
storybook-static/
22+
23+
.env

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,7 @@ If you are upgrading from a version prior to 2.0.0, please note the changes to p
180180
## [2.2.7]
181181

182182
-- Add optional `onCommitClick` function, click color block.
183+
184+
## [2.3.0]
185+
186+
-- Added diff stats on commit click via the `getDiff` function.

README.md

Lines changed: 98 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,18 @@
88

99
The Commit Graph package is a React component suite designed to visualize commit graphs in an interactive and informative way. It showcases commit history within a repository with support for infinite scroll loading. See [this post](https://www.dolthub.com/blog/2024-08-07-drawing-a-commit-graph/) for the implementation details.
1010

11-
`CommitGraph` is utilized by platforms like [DoltHub](https://www.dolthub.com) to visualize database commit log histories.
11+
It is used by platforms like [DoltHub](https://www.dolthub.com) to visualize database commit log histories.
1212

1313
## Demo
1414

15-
The [demo](https://liuliu-dev.github.io/CommitGraph/) features sample commit data and real GitHub repository graphs.
15+
Explore the [demo](https://liuliu-dev.github.io/CommitGraph/) for sample commit data and real GitHub repository graphs.
1616

1717
## Features
1818

19-
- **Interactive Commit Graph Visualization:** Render commit history as an interactive graph, offering a clear and detailed view of repository activities.
20-
- **Infinite Scroll Support:** `CommitGraph.WithInfiniteScroll` enhances the user experience for large commit histories by dynamically loading new content as users scroll.
21-
- **Customizable Styles:** Extensive styling options for the commit log graph, including node colors, spacing, and more, to seamlessly match your project's design.
19+
- **Interactive Commit Graph Visualization:** Provides a dynamic visual of commit history with detailed repository activity.
20+
- **Infinite Scroll Support:** `CommitGraph.WithInfiniteScroll` dynamically loads new content as users scroll through large commit histories.
21+
- **Customizable Styles:** Easily style the commit log graph, including node colors, spacing, and more.
22+
- **Diff Stats on Commit Click:** If a `getDiff` function is provided, clicking a commit will display additional file change stats, including additions, deletions, and file modifications.
2223

2324
## Installation
2425

@@ -28,7 +29,7 @@ npm install commit-graph
2829

2930
## Quick Start
3031

31-
### Using CommitGraph
32+
### UBasic Usage (without infinite scroll):
3233

3334
For a basic implementation without infinite scroll:
3435

@@ -54,16 +55,29 @@ const MyComponent = () => {
5455
branchColors: ["#FF0000", "#00FF00", "#0000FF"],
5556
nodeRadius: 2,
5657
}}
58+
getDiff={async (base, head) => {
59+
// Your implementation to fetch diff stats
60+
return {
61+
files: [
62+
{
63+
filename: "example.txt",
64+
status: "modified",
65+
additions: 10,
66+
deletions: 2,
67+
},
68+
],
69+
};
70+
}}
5771
/>
5872
);
5973
};
6074

6175
export default MyComponent;
6276
```
6377

64-
### Using CommitGraph.WithInfiniteScroll
78+
### Using `CommitGraph.WithInfiniteScroll`
6579

66-
For implementations requiring infinite scroll to handle large commit histories:
80+
For large commit histories requiring infinite scroll:
6781

6882
```jsx
6983
import React from "react";
@@ -89,20 +103,74 @@ export default MyComponent;
89103
### Common Props for CommitGraph and CommitGraph.WithInfiniteScroll
90104

91105
- commits (array): An array of `Commit` objects representing the commit history.
92-
- branchHeads (array): An array of `Branch` objects representing the branch heads in the commit-graph.
106+
107+
- branchHeads (array): An array of `Branch` objects representing the branch heads.
108+
109+
- getDiff (function, optional): A function that returns a `Promise` resolving to a [`Diff`](#diff-type) object. Fetches file changes for clicked commits.
110+
111+
- currentBranch (string, optional): The name of the current branch.
112+
113+
- fullSha (boolean, optional): Displays the full SHA of a commit instead of the shortened SHA.
114+
115+
- onCommitClick (function, optional): Function to be called when a commit is clicked, receiving the clicked commit as an argument.
116+
117+
- dateFormatFn (function, optional): Formats the commit dates. Accepts a string, number, or Date and returns a formatted string.
118+
119+
```typescript
120+
dateFormatFn?: (d: string | number | Date) => string;
121+
```
122+
123+
Example:
124+
125+
```typescript
126+
const customDateTimeFormatFn = (d: string | number | Date): string => {
127+
return new Date(d).toLocaleString('en-US', {
128+
year: 'numeric',
129+
month: 'short',
130+
day: 'numeric',
131+
hour: '2-digit',
132+
minute: '2-digit',
133+
second: '2-digit',
134+
});
135+
};
136+
137+
const MyComponent = () => {
138+
// Your commit and branch head data, loadMore function, and hasMore flag
139+
return (
140+
<CommitGraph.WithInfiniteScroll
141+
commits={/* Your commits data */}
142+
branchHeads={/* Your branch heads data */}
143+
loadMore={/* Your loadMore function */}
144+
hasMore={/* hasMore flag */}
145+
dateFormatFn={customDateTimeFormatFn}
146+
/>
147+
);
148+
};
149+
150+
```
151+
152+
- graphStyle (object, optional): Customize the graph styling. Example:
153+
154+
```typescript
155+
const graphStyle = {
156+
commitSpacing: 60,
157+
branchSpacing: 20,
158+
nodeRadius: 2,
159+
branchColors: ["#FF0000", "#00FF00", "#0000FF"],
160+
};
161+
```
93162

94163
### Additional Props for CommitGraph.WithInfiniteScroll
95164

96-
- loadMore (function): Function to load more commits upon reaching the scroll end.
97-
- hasMore (boolean): Indicates whether more commits are available to load.
165+
- loadMore (function): Function to load more commits as the user scrolls.
98166

99-
## Type Definitions
167+
- hasMore (boolean): Boolean flag indicating whether more commits are available to load.
100168

101-
These type definitions should be used to structure the data passed to the commits and branchHeads props of both CommitGraph and CommitGraph.WithInfiniteScroll components, ensuring proper visualization of commit history and branch information.
169+
## Type Definitions
102170

103171
### `Commit` Type
104172

105-
The `Commit` type represents individual commits in the commit history. Each `Commit` object should conform to the following structure:
173+
Represents individual commits:
106174

107175
```typescript
108176
type ParentCommit = {
@@ -124,11 +192,9 @@ export type Commit = {
124192
};
125193
```
126194

127-
This type definition includes the commit's SHA, author information, commit message, an array of parent commits, and an optional URL to the commit.
128-
129195
### `Branch` Type
130196

131-
The `Branch` type defines the structure for branches in the repository, each associated with a particular commit:
197+
Defines repository branches, each associated with a commit:
132198

133199
```typescript
134200
export type Branch = {
@@ -140,66 +206,31 @@ export type Branch = {
140206
};
141207
```
142208

143-
Each Branch object should include the branch's name, the SHA of the latest commit on the branch, and an optional link to the branch.
144-
145-
### `graphStyle` (object, optional)
209+
### `Diff` Type
146210

147-
An optional object specifying the styling options for the commit-graph. The `graphStyle` object should have the following properties:
148-
149-
- `commitSpacing` (number): The vertical spacing between commits.
150-
- `branchSpacing` (number): The horizontal spacing between branches.
151-
- `branchColors` (array of strings): An array of colors to be used for different branches. Default: `['#FF0000', '#00FF00', '#0000FF']`.
152-
- `nodeRadius` (number): The radius of the commit node circles.
153-
154-
### `dateFormatFn` (function, optional)
155-
156-
An optional function to format commit dates. Takes a Date, number, or string as input and returns a string.
211+
Represents changes between two commits:
157212

158213
```typescript
159-
dateFormatFn?: (d: string | number | Date) => string;
214+
export type Diff = {
215+
files: ChangedFile[];
216+
};
160217
```
161218

162-
example:
219+
### `ChangedFile` Type
163220

164-
```typescript
165-
const customDateTimeFormatFn = (d: string | number | Date): string => {
166-
return new Date(d).toLocaleString('en-US', {
167-
year: 'numeric',
168-
month: 'short',
169-
day: 'numeric',
170-
hour: '2-digit',
171-
minute: '2-digit',
172-
second: '2-digit',
173-
});
174-
};
221+
Details of files changed between commits:
175222

176-
const MyComponent = () => {
177-
// Your commit and branch head data, loadMore function, and hasMore flag
178-
return (
179-
<CommitGraph.WithInfiniteScroll
180-
commits={/* Your commits data */}
181-
branchHeads={/* Your branch heads data */}
182-
loadMore={/* Your loadMore function */}
183-
hasMore={/* hasMore flag */}
184-
dateFormatFn={customDateTimeFormatFn}
185-
/>
186-
);
223+
```typescript
224+
export type ChangedFile = {
225+
filename: string;
226+
status: string; // "added", "modified", or "deleted"
227+
additions: number;
228+
deletions: number;
229+
patch?: string; // Optional patch data for the changes
230+
blob_url?: string; // Optional URL to the file blob
187231
};
188-
189232
```
190233

191-
### `currentBranch` (string, optional)
192-
193-
The name of the current branch.
194-
195-
### `fullSha` (boolean, optional)
196-
197-
Instead of the default shortened SHA, display the full SHA of the commit.
198-
199-
### `onCommitClick` (function, optional)
200-
201-
An optional function to be called when a commit is clicked. The function receives the clicked commit as an argument.
202-
203234
## Storybook
204235

205236
Explore the Commit Graph component and its features by running storybook:

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "commit-graph",
3-
"version": "2.2.7",
3+
"version": "2.3.0",
44
"homepage": "https://liuliu-dev.github.io/CommitGraph/",
55
"author": "Liu Liu <[email protected]>",
66
"description": "A React component to visualize a commit graph.",
@@ -50,7 +50,9 @@
5050
"@dolthub/web-utils": "^0.1.5",
5151
"classnames": "^2.3.2",
5252
"react": ">=18.0.0",
53+
"react-copy-to-clipboard": "^5.1.0",
5354
"react-dom": ">=18.0.0",
55+
"react-icons": "^5.3.0",
5456
"react-infinite-scroller": "^1.2.6",
5557
"react-tooltip": "^5.26.3",
5658
"reactjs-popup": "^2.0.6"

src/components/CommitDetails/index.module.css

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
margin-top: auto;
66
margin-bottom: auto;
77
max-width: 1800px;
8+
position: relative;
89
}
910

1011
.labelAndLink {
@@ -36,3 +37,26 @@
3637
max-width: 90%;
3738
visibility: visible;
3839
}
40+
41+
.diffSection {
42+
font-family: "Source Sans Pro";
43+
font-size: small;
44+
border: 1px solid #e1e4e8;
45+
background-color: white;
46+
position: absolute;
47+
width: 320px;
48+
right: 1rem;
49+
top: 1rem;
50+
z-index: 10;
51+
color: #010a40;
52+
border-radius: 0.5rem;
53+
}
54+
55+
.comments {
56+
border: 1px solid #e1e4e8;
57+
background-color: white;
58+
width: 320px;
59+
position: fixed;
60+
z-index: 10;
61+
right: 0;
62+
}

0 commit comments

Comments
 (0)