|
1 | 1 | import * as React from "react";
|
2 | 2 | import {
|
3 | 3 | GitgraphCore,
|
4 |
| - Commit, |
| 4 | + Commit as CommitCore, |
5 | 5 | Mode,
|
6 | 6 | Coordinate,
|
7 | 7 | } from "@gitgraph/core";
|
8 |
| -import { CommitElement, ReactSvgElement } from "./types"; |
| 8 | +import { ReactSvgElement } from "./types"; |
9 | 9 | import { Dot } from "./Dot";
|
10 | 10 | import { Tooltip } from "./Tooltip";
|
11 | 11 | import { Arrow } from "./Arrow";
|
12 | 12 | import { Message } from "./Message";
|
13 |
| -import { Tag } from "./Tag"; |
| 13 | +import { Tag, TAG_PADDING_X } from "./Tag"; |
14 | 14 | import { BranchLabel } from "./BranchLabel";
|
| 15 | +import { MutableRefObject } from "react"; |
15 | 16 |
|
16 | 17 | interface CommitsProps {
|
17 |
| - commits: Array<Commit<ReactSvgElement>>; |
18 |
| - commit: Commit<ReactSvgElement>; |
19 |
| - currentCommitOver: Commit<ReactSvgElement> | null; |
| 18 | + commits: Array<CommitCore<ReactSvgElement>>; |
| 19 | + commit: CommitCore<ReactSvgElement>; |
| 20 | + currentCommitOver: CommitCore<ReactSvgElement> | null; |
20 | 21 | gitgraph: GitgraphCore<ReactSvgElement>;
|
21 |
| - initCommitElements: (commit: Commit<ReactSvgElement>) => void; |
22 |
| - commitsElements: { |
23 |
| - [commitHash: string]: CommitElement; |
24 |
| - }; |
25 | 22 | getWithCommitOffset: (props: any) => Coordinate;
|
26 | 23 | setTooltip: (val: React.ReactElement<SVGGElement> | null) => void;
|
27 |
| - setCurrentCommitOver: (val: Commit<ReactSvgElement> | null) => void; |
| 24 | + setCurrentCommitOver: (val: CommitCore<ReactSvgElement> | null) => void; |
| 25 | + commitMessagesX: number; |
28 | 26 | }
|
29 | 27 |
|
30 |
| -export class CommitComp extends React.Component<CommitsProps, {}> { |
31 |
| - public render() { |
32 |
| - const commit = this.props.commit; |
33 |
| - const { x, y } = this.props.getWithCommitOffset(commit); |
34 |
| - |
35 |
| - const shouldRenderTooltip = |
36 |
| - this.props.currentCommitOver === commit && |
37 |
| - (this.props.gitgraph.isHorizontal || |
38 |
| - (this.props.gitgraph.mode === Mode.Compact && |
39 |
| - commit.style.hasTooltipInCompactMode)); |
40 |
| - |
41 |
| - if (shouldRenderTooltip) { |
42 |
| - this.props.setTooltip( |
43 |
| - <g transform={`translate(${x}, ${y})`}> |
44 |
| - <Tooltip commit={commit}> |
45 |
| - {commit.hashAbbrev} - {commit.subject} |
46 |
| - </Tooltip> |
47 |
| - </g>, |
48 |
| - ); |
49 |
| - } |
| 28 | +export const Commit = (props: CommitsProps) => { |
| 29 | + const {commit, commits, gitgraph, commitMessagesX} = props; |
50 | 30 |
|
51 |
| - return ( |
52 |
| - <g transform={`translate(${x}, ${y})`}> |
53 |
| - <Dot |
54 |
| - commit={commit} |
55 |
| - onMouseOver={() => { |
56 |
| - this.props.setCurrentCommitOver(commit); |
57 |
| - commit.onMouseOver(); |
58 |
| - }} |
59 |
| - onMouseOut={() => { |
60 |
| - this.props.setCurrentCommitOver(null); |
61 |
| - this.props.setTooltip(null); |
62 |
| - commit.onMouseOut(); |
63 |
| - }} |
64 |
| - /> |
65 |
| - {this.props.gitgraph.template.arrow.size && this.renderArrows(commit)} |
66 |
| - |
67 |
| - {/* These elements are positionned after component update. */} |
68 |
| - <g transform={`translate(${-x}, 0)`}> |
69 |
| - { |
70 |
| - commit.style.message.display && |
71 |
| - <Message |
72 |
| - commit={commit} |
73 |
| - commitsElements={this.props.commitsElements} |
74 |
| - initCommitElements={this.props.initCommitElements} |
75 |
| - /> |
76 |
| - } |
77 |
| - {this.renderBranchLabels(commit)} |
78 |
| - {this.renderTags(commit)} |
79 |
| - </g> |
80 |
| - </g> |
81 |
| - ); |
82 |
| - } |
| 31 | + /** |
| 32 | + * This _should_ likely be an array, but is not in order to intentionally keep |
| 33 | + * a potential bug in the codebase that existed prior to Hook-ifying this component |
| 34 | + * @see https://github.com/nicoespeon/gitgraph.js/blob/be9cdf45c7f00970e68e1a4ba579ca7f5c672da4/packages/gitgraph-react/src/Gitgraph.tsx#L197 |
| 35 | + * (notice that it's a single `null` value instead of an array |
| 36 | + * |
| 37 | + * The potential bug in question is "what happens when there are more than one |
| 38 | + * branch label rendered? Do they overlap or cause the message X position to be |
| 39 | + * in the wrong position?" |
| 40 | + * |
| 41 | + * TODO: Investigate potential bug outlined above |
| 42 | + */ |
| 43 | + const branchLabelRef = React.useRef<SVGGElement>(); |
| 44 | + const tagRefs: MutableRefObject<SVGGElement[]> = React.useRef([]); |
| 45 | + // "as unknown as any" needed to avoid `ref` mistypings later. :( |
| 46 | + const messageRef: MutableRefObject<SVGGElement> = React.useRef<SVGGElement>() as unknown as any; |
| 47 | + |
| 48 | + const [branchLabelX, setBranchLabelX] = React.useState(0); |
| 49 | + const [tagXs, setTagXs] = React.useState<number[]>([]); |
| 50 | + const [messageX, setMessageX] = React.useState(0); |
83 | 51 |
|
84 |
| - private renderArrows(commit: Commit<ReactSvgElement>) { |
| 52 | + const arrows = React.useMemo(() => { |
| 53 | + if (!gitgraph.template.arrow.size) return null; |
85 | 54 | const commitRadius = commit.style.dot.size;
|
86 | 55 |
|
87 | 56 | return commit.parents.map((parentHash: string) => {
|
88 | 57 | return (
|
89 | 58 | <Arrow
|
90 | 59 | key={parentHash}
|
91 |
| - commits={this.props.commits} |
| 60 | + commits={commits} |
92 | 61 | commit={commit}
|
93 |
| - gitgraph={this.props.gitgraph} |
| 62 | + gitgraph={gitgraph} |
94 | 63 | parentHash={parentHash}
|
95 | 64 | commitRadius={commitRadius}
|
96 | 65 | />
|
97 | 66 | );
|
98 | 67 | });
|
99 |
| - } |
| 68 | + }, [commits, commit, gitgraph]); |
100 | 69 |
|
| 70 | + const branchLabels = React.useMemo(() => { |
| 71 | + // @gitgraph/core could compute branch labels into commits directly. |
| 72 | + // That will make it easier to retrieve them, just like tags. |
| 73 | + const branches = Array.from(gitgraph.branches.values()); |
| 74 | + return branches.map((branch) => { |
| 75 | + return ( |
| 76 | + <BranchLabel |
| 77 | + key={branch.name} |
| 78 | + gitgraph={gitgraph} |
| 79 | + branch={branch} |
| 80 | + commit={commit} |
| 81 | + ref={branchLabelRef} |
| 82 | + branchLabelX={branchLabelX} |
| 83 | + /> |
| 84 | + ); |
| 85 | + }); |
| 86 | + }, [gitgraph, commit, branchLabelX]) |
101 | 87 |
|
102 |
| - private renderTags(commit: Commit<ReactSvgElement>) { |
| 88 | + const tags = React.useMemo(() => { |
| 89 | + tagRefs.current = []; |
103 | 90 | if (!commit.tags) return null;
|
104 |
| - if (this.props.gitgraph.isHorizontal) return null; |
| 91 | + if (gitgraph.isHorizontal) return null; |
105 | 92 |
|
106 |
| - return commit.tags.map((tag) => |
| 93 | + return commit.tags.map((tag, i) => |
107 | 94 | <Tag
|
108 | 95 | key={`${commit.hashAbbrev}-${tag.name}`}
|
109 | 96 | commit={commit}
|
110 |
| - initCommitElements={this.props.initCommitElements} |
111 |
| - commitsElements={this.props.commitsElements} |
112 | 97 | tag={tag}
|
| 98 | + ref={r => tagRefs.current[i] = r!} |
| 99 | + tagX={tagXs[i] || 0} |
113 | 100 | />,
|
114 | 101 | );
|
115 |
| - } |
| 102 | + }, [commit, gitgraph, tagXs]) |
116 | 103 |
|
| 104 | + const { x, y } = props.getWithCommitOffset(commit); |
117 | 105 |
|
118 |
| - private renderBranchLabels(commit: Commit<ReactSvgElement>) { |
119 |
| - // @gitgraph/core could compute branch labels into commits directly. |
120 |
| - // That will make it easier to retrieve them, just like tags. |
121 |
| - const branches = Array.from(this.props.gitgraph.branches.values()); |
122 |
| - return branches.map((branch) => { |
123 |
| - return ( |
124 |
| - <BranchLabel |
125 |
| - key={branch.name} |
126 |
| - gitgraph={this.props.gitgraph} |
127 |
| - initCommitElements={this.props.initCommitElements} |
128 |
| - commitsElements={this.props.commitsElements} |
129 |
| - branch={branch} |
130 |
| - commit={commit} |
131 |
| - /> |
132 |
| - ); |
| 106 | + // positionCommitsElements |
| 107 | + React.useLayoutEffect(() => { |
| 108 | + if (gitgraph.isHorizontal) { |
| 109 | + // Elements don't appear on horizontal mode, yet. |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + const padding = 10; |
| 114 | + |
| 115 | + let translateX = commitMessagesX; |
| 116 | + |
| 117 | + if (branchLabelRef.current) { |
| 118 | + setBranchLabelX(translateX); |
| 119 | + |
| 120 | + // For some reason, one paddingX is missing in BBox width. |
| 121 | + const branchLabelWidth = |
| 122 | + branchLabelRef.current.getBBox().width + BranchLabel.paddingX; |
| 123 | + translateX += branchLabelWidth + padding; |
| 124 | + } |
| 125 | + |
| 126 | + const allTagXs = tagRefs.current.map((tag) => { |
| 127 | + if (!tag) return 0; |
| 128 | + |
| 129 | + const tagX = translateX; |
| 130 | + |
| 131 | + // For some reason, one paddingX is missing in BBox width. |
| 132 | + const tagWidth = tag.getBBox().width + TAG_PADDING_X; |
| 133 | + translateX += tagWidth + padding; |
| 134 | + |
| 135 | + return tagX; |
133 | 136 | });
|
| 137 | + |
| 138 | + setTagXs(allTagXs); |
| 139 | + |
| 140 | + if (messageRef.current) { |
| 141 | + setMessageX(translateX); |
| 142 | + } |
| 143 | + }, [tagRefs, gitgraph, commitMessagesX]) |
| 144 | + |
| 145 | + const shouldRenderTooltip = |
| 146 | + props.currentCommitOver === commit && |
| 147 | + (props.gitgraph.isHorizontal || |
| 148 | + (props.gitgraph.mode === Mode.Compact && |
| 149 | + commit.style.hasTooltipInCompactMode)); |
| 150 | + |
| 151 | + if (shouldRenderTooltip) { |
| 152 | + props.setTooltip( |
| 153 | + <g transform={`translate(${x}, ${y})`}> |
| 154 | + <Tooltip commit={commit}> |
| 155 | + {commit.hashAbbrev} - {commit.subject} |
| 156 | + </Tooltip> |
| 157 | + </g>, |
| 158 | + ); |
134 | 159 | }
|
| 160 | + |
| 161 | + return ( |
| 162 | + <g transform={`translate(${x}, ${y})`}> |
| 163 | + <Dot |
| 164 | + commit={commit} |
| 165 | + onMouseOver={() => { |
| 166 | + props.setCurrentCommitOver(commit); |
| 167 | + commit.onMouseOver(); |
| 168 | + }} |
| 169 | + onMouseOut={() => { |
| 170 | + props.setCurrentCommitOver(null); |
| 171 | + props.setTooltip(null); |
| 172 | + commit.onMouseOut(); |
| 173 | + }} |
| 174 | + /> |
| 175 | + {arrows} |
| 176 | + <g transform={`translate(${-x}, 0)`}> |
| 177 | + { |
| 178 | + commit.style.message.display && |
| 179 | + <Message |
| 180 | + commit={commit} |
| 181 | + ref={messageRef} |
| 182 | + messageX={messageX} |
| 183 | + /> |
| 184 | + } |
| 185 | + {branchLabels} |
| 186 | + {tags} |
| 187 | + </g> |
| 188 | + </g> |
| 189 | + ); |
135 | 190 | }
|
0 commit comments