@dreamcatcher-tech/commit-graph

中文文档
2.2.9发布于 2024/09/27MITTypeScriptESM + CJS近 30 天下载 1.9万

A React component to visualize a commit graph.

原始使用文档
文档目录18 节
  1. Overview
  2. Demo
  3. Features
  4. Installation
  5. Quick Start
  6. Using CommitGraph
  7. Using CommitGraph.WithInfiniteScroll
  8. Props
  9. Common Props for CommitGraph and CommitGraph.WithInfiniteSc…
  10. Additional Props for CommitGraph.WithInfiniteScroll
  11. Type Definitions
  12. Commit Type
  13. Branch Type
  14. graphStyle (object, optional)
  15. dateFormatFn (function, optional)
  16. currentBranch (string, optional)
  17. fullSha (boolean, optional)
  18. Storybook

Commit Graph

readme-header

License

Overview

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 for the implementation details.

CommitGraph is utilized by platforms like DoltHub to visualize database commit log histories.

Demo

The demo features sample commit data and real GitHub repository graphs.

Features

  • Interactive Commit Graph Visualization: Render commit history as an interactive graph, offering a clear and detailed view of repository activities.
  • Infinite Scroll Support: CommitGraph.WithInfiniteScroll enhances the user experience for large commit histories by dynamically loading new content as users scroll.
  • Customizable Styles: Extensive styling options for the commit log graph, including node colors, spacing, and more, to seamlessly match your project's design.

Installation

npm install commit-graph

Quick Start

Using CommitGraph

For a basic implementation without infinite scroll:

import React from "react";
import { CommitGraph } from "commit-graph";

const MyComponent = () => {
  const commits = [
    // Commits data according to the new Commit type
  ];
  const branchHeads = [
    // Branch heads data according to the new Branch type
  ];

  return (
    <CommitGraph
      commits={commits}
      branchHeads={branchHeads}
      graphStyle={{
        commitSpacing: 50,
        branchSpacing: 20,
        branchColors: ["#FF0000", "#00FF00", "#0000FF"],
        nodeRadius: 2,
      }}
    />
  );
};

export default MyComponent;

Using CommitGraph.WithInfiniteScroll

For implementations requiring infinite scroll to handle large commit histories:

import React from "react";
import { CommitGraph } from "commit-graph";

const MyComponent = () => {
  // Your commit and branch head data, loadMore function, and hasMore flag
  return (
    <CommitGraph.WithInfiniteScroll
      commits={/* Your commits data */}
      branchHeads={/* Your branch heads data */}
      loadMore={/* Your loadMore function */}
      hasMore={/* hasMore flag */}
    />
  );
};

export default MyComponent;

Props

Common Props for CommitGraph and CommitGraph.WithInfiniteScroll

  • commits (array): An array of Commit objects representing the commit history.
  • branchHeads (array): An array of Branch objects representing the branch heads in the commit-graph.

Additional Props for CommitGraph.WithInfiniteScroll

  • loadMore (function): Function to load more commits upon reaching the scroll end.
  • hasMore (boolean): Indicates whether more commits are available to load.

Type Definitions

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.

Commit Type

The Commit type represents individual commits in the commit history. Each Commit object should conform to the following structure:

type ParentCommit = {
  sha: string;
};

export type Commit = {
  sha: string;
  commit: {
    author: {
      name: string; // The name of the commit author
      date: string | number | Date; // The date of the commit
      email?: string; // The email of the commit author (optional)
    };
    message: string; // The commit message
  };
  parents: ParentCommit[]; // An array of parent commits
  html_url?: string; // The URL to view the commit (optional)
};

This type definition includes the commit's SHA, author information, commit message, an array of parent commits, and an optional URL to the commit.

Branch Type

The Branch type defines the structure for branches in the repository, each associated with a particular commit:

export type Branch = {
  name: string; // The name of the branch
  commit: {
    sha: string; // The SHA of the latest commit on the branch
  };
  link?: string; // A URL to the branch on GitHub (optional)
};

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.

graphStyle (object, optional)

An optional object specifying the styling options for the commit-graph. The graphStyle object should have the following properties:

  • commitSpacing (number): The vertical spacing between commits.
  • branchSpacing (number): The horizontal spacing between branches.
  • branchColors (array of strings): An array of colors to be used for different branches. Default: ['#FF0000', '#00FF00', '#0000FF'].
  • nodeRadius (number): The radius of the commit node circles.

dateFormatFn (function, optional)

An optional function to format commit dates. Takes a Date, number, or string as input and returns a string.

dateFormatFn?: (d: string | number | Date) => string;

example:

const customDateTimeFormatFn = (d: string | number | Date): string => {
  return new Date(d).toLocaleString('en-US', {
      year: 'numeric',
      month: 'short',
      day: 'numeric',
      hour: '2-digit',
      minute: '2-digit',
      second: '2-digit',
  });
};

const MyComponent = () => {
  // Your commit and branch head data, loadMore function, and hasMore flag
  return (
    <CommitGraph.WithInfiniteScroll
      commits={/* Your commits data */}
      branchHeads={/* Your branch heads data */}
      loadMore={/* Your loadMore function */}
      hasMore={/* hasMore flag */}
      dateFormatFn={customDateTimeFormatFn}
    />
  );
};

currentBranch (string, optional)

The name of the current branch.

fullSha (boolean, optional)

Instead of the default shortened SHA, display the full SHA of the commit.

Storybook

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

npm run storybook

版本与发布记录

@dreamcatcher-tech/commit-graph 共发布 6 个版本,首次发布于 2024/09/18;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.2.9latest2024/09/272 年前
2.2.82024/09/272 年前
2.2.72024/09/252 年前
2.2.62024/09/202 年前
2.2.52024/09/202 年前
2.2.42024/09/182 年前

关于 @dreamcatcher-tech/commit-graph 的常见问题

@dreamcatcher-tech/commit-graph 是什么?

A React component to visualize a commit graph.

如何安装 @dreamcatcher-tech/commit-graph?

在项目目录运行 npm install @dreamcatcher-tech/commit-graph,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=18.0.0)、react-dom(>=18.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@dreamcatcher-tech/commit-graph 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dreamcatcher-tech/commit-graph 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@dreamcatcher-tech/commit-graph 可以商用吗?

@dreamcatcher-tech/commit-graph 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dreamcatcher-tech/commit-graph 的下载量有多少?

根据 npm 官方下载统计,@dreamcatcher-tech/commit-graph 在 2026/10/13 – 2026/11/11 这 30 天内被下载 19,308 次,日均约 644 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dreamcatcher-tech/commit-graph 还在维护吗?

最新版本 2.2.9 发布于 2024/09/27(2 年前);至今共发布 6 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/09/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。