swadhinbiswas Initial commit of OpenCodeHub source · 12/11/2025
1110042
361 lines · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import { useMemo, useState } from "react"

interface Commit {
  sha: string
  shortSha: string
  message: string
  author: {
    name: string
    email: string
    avatar?: string
  }
  date: string
  timestamp: number
  parents: string[]
  branch?: string
  tags?: string[]
  refs?: string[]
}

interface CommitGraphProps {
  commits: Commit[]
  branches: string[]
  onCommitClick?: (commit: Commit) => void
  onBranchClick?: (branch: string) => void
}

// Color palette for branches
const branchColors = [
  "#2ecc71", // green
  "#3498db", // blue
  "#9b59b6", // purple
  "#e74c3c", // red
  "#f1c40f", // yellow
  "#1abc9c", // teal
  "#e67e22", // orange
  "#95a5a6", // gray
]

export function CommitGraph({
  commits,
  branches,
  onCommitClick,
  onBranchClick,
}: CommitGraphProps) {
  const [selectedCommit, setSelectedCommit] = useState<string | null>(null)
  const [hoveredCommit, setHoveredCommit] = useState<string | null>(null)

  // Calculate graph layout
  const graphData = useMemo(() => {
    const columns: Map<string, number> = new Map()
    const branchHeads: Map<string, string> = new Map()
    const commitColumns: Map<string, number> = new Map()
    const connections: Array<{
      from: { row: number; col: number }
      to: { row: number; col: number }
      color: string
    }> = []

    let maxColumn = 0

    // Assign columns to branches
    branches.forEach((branch, index) => {
      columns.set(branch, index)
      maxColumn = Math.max(maxColumn, index)
    })

    // Process commits
    commits.forEach((commit, index) => {
      // Find which column this commit belongs to
      let col = 0
      if (commit.branch) {
        col = columns.get(commit.branch) || 0
      } else {
        // Find an available column
        col = maxColumn + 1
        maxColumn = col
      }
      commitColumns.set(commit.sha, col)

      // Create connections to parents
      commit.parents.forEach((parentSha) => {
        const parentIndex = commits.findIndex((c) => c.sha === parentSha)
        if (parentIndex !== -1) {
          const parentCol = commitColumns.get(parentSha) || col
          connections.push({
            from: { row: index, col },
            to: { row: parentIndex, col: parentCol },
            color: branchColors[col % branchColors.length],
          })
        }
      })
    })

    return {
      commitColumns,
      connections,
      maxColumn,
    }
  }, [commits, branches])

  const handleCommitClick = (commit: Commit) => {
    setSelectedCommit(commit.sha)
    onCommitClick?.(commit)
  }

  const rowHeight = 36
  const columnWidth = 16
  const nodeRadius = 5
  const graphWidth = (graphData.maxColumn + 2) * columnWidth

  return (
    <div className="rounded-lg border overflow-hidden bg-card">
      {/* Header */}
      <div className="flex items-center gap-4 border-b bg-muted/30 px-4 py-3">
        <h3 className="font-semibold">Commit History</h3>
        <div className="flex items-center gap-2">
          {branches.slice(0, 5).map((branch, index) => (
            <button
              key={branch}
              onClick={() => onBranchClick?.(branch)}
              className="inline-flex items-center gap-1.5 rounded-full px-2 py-0.5 text-xs font-medium"
              style={{
                backgroundColor: `${branchColors[index % branchColors.length]}20`,
                color: branchColors[index % branchColors.length],
              }}
            >
              <span
                className="h-2 w-2 rounded-full"
                style={{ backgroundColor: branchColors[index % branchColors.length] }}
              />
              {branch}
            </button>
          ))}
          {branches.length > 5 && (
            <span className="text-xs text-muted-foreground">+{branches.length - 5} more</span>
          )}
        </div>
      </div>

      {/* Graph */}
      <div className="relative overflow-x-auto">
        <div className="flex">
          {/* Graph Column */}
          <div
            className="flex-shrink-0 border-r bg-muted/10"
            style={{ width: graphWidth }}
          >
            <svg
              width={graphWidth}
              height={commits.length * rowHeight}
              className="block"
            >
              {/* Draw connections */}
              {graphData.connections.map((conn, i) => {
                const x1 = conn.from.col * columnWidth + columnWidth / 2 + 4
                const y1 = conn.from.row * rowHeight + rowHeight / 2
                const x2 = conn.to.col * columnWidth + columnWidth / 2 + 4
                const y2 = conn.to.row * rowHeight + rowHeight / 2

                // Create curved path for merge commits
                const path =
                  x1 === x2
                    ? `M ${x1} ${y1} L ${x2} ${y2}`
                    : `M ${x1} ${y1} C ${x1} ${y1 + (y2 - y1) / 2} ${x2} ${y1 + (y2 - y1) / 2} ${x2} ${y2}`

                return (
                  <path
                    key={i}
                    d={path}
                    stroke={conn.color}
                    strokeWidth={2}
                    fill="none"
                    className="commit-graph-line"
                  />
                )
              })}

              {/* Draw nodes */}
              {commits.map((commit, index) => {
                const col = graphData.commitColumns.get(commit.sha) || 0
                const x = col * columnWidth + columnWidth / 2 + 4
                const y = index * rowHeight + rowHeight / 2
                const color = branchColors[col % branchColors.length]
                const isSelected = selectedCommit === commit.sha
                const isHovered = hoveredCommit === commit.sha

                return (
                  <g key={commit.sha}>
                    <circle
                      cx={x}
                      cy={y}
                      r={isSelected || isHovered ? nodeRadius + 2 : nodeRadius}
                      fill={isSelected ? color : "var(--background)"}
                      stroke={color}
                      strokeWidth={2}
                      className="commit-graph-node cursor-pointer transition-all"
                      onMouseEnter={() => setHoveredCommit(commit.sha)}
                      onMouseLeave={() => setHoveredCommit(null)}
                      onClick={() => handleCommitClick(commit)}
                    />
                  </g>
                )
              })}
            </svg>
          </div>

          {/* Commit List */}
          <div className="flex-1 min-w-0">
            {commits.map((commit, index) => {
              const col = graphData.commitColumns.get(commit.sha) || 0
              const color = branchColors[col % branchColors.length]
              const isSelected = selectedCommit === commit.sha
              const isHovered = hoveredCommit === commit.sha

              return (
                <div
                  key={commit.sha}
                  className={`flex items-center gap-3 px-4 cursor-pointer transition-colors ${
                    isSelected
                      ? "bg-primary/10"
                      : isHovered
                      ? "bg-muted/50"
                      : "hover:bg-muted/30"
                  }`}
                  style={{ height: rowHeight }}
                  onMouseEnter={() => setHoveredCommit(commit.sha)}
                  onMouseLeave={() => setHoveredCommit(null)}
                  onClick={() => handleCommitClick(commit)}
                >
                  {/* Refs (branches, tags) */}
                  <div className="flex items-center gap-1 flex-shrink-0">
                    {commit.refs?.map((ref, i) => (
                      <span
                        key={i}
                        className="inline-flex items-center rounded px-1.5 py-0.5 text-[10px] font-medium"
                        style={{
                          backgroundColor: `${color}20`,
                          color: color,
                        }}
                      >
                        {ref}
                      </span>
                    ))}
                    {commit.tags?.map((tag, i) => (
                      <span
                        key={i}
                        className="inline-flex items-center rounded px-1.5 py-0.5 text-[10px] font-medium bg-yellow-500/20 text-yellow-600 dark:text-yellow-400"
                      >
                        🏷️ {tag}
                      </span>
                    ))}
                  </div>

                  {/* Commit SHA */}
                  <code className="text-xs font-mono text-muted-foreground flex-shrink-0 w-16">
                    {commit.shortSha}
                  </code>

                  {/* Commit Message */}
                  <span className="text-sm truncate flex-1">{commit.message}</span>

                  {/* Author */}
                  <div className="flex items-center gap-2 flex-shrink-0">
                    <div className="h-5 w-5 rounded-full bg-muted flex items-center justify-center">
                      {commit.author.avatar ? (
                        <img
                          src={commit.author.avatar}
                          alt={commit.author.name}
                          className="h-5 w-5 rounded-full"
                        />
                      ) : (
                        <span className="text-[10px] font-medium">
                          {commit.author.name.charAt(0).toUpperCase()}
                        </span>
                      )}
                    </div>
                    <span className="text-xs text-muted-foreground hidden sm:block">
                      {commit.author.name}
                    </span>
                  </div>

                  {/* Date */}
                  <span className="text-xs text-muted-foreground flex-shrink-0 w-24 text-right hidden md:block">
                    {commit.date}
                  </span>
                </div>
              )
            })}
          </div>
        </div>
      </div>
    </div>
  )
}

// Simplified linear commit list
interface CommitListProps {
  commits: Commit[]
  onCommitClick?: (commit: Commit) => void
}

export function CommitList({ commits, onCommitClick }: CommitListProps) {
  return (
    <div className="rounded-lg border divide-y overflow-hidden">
      {commits.map((commit) => (
        <div
          key={commit.sha}
          className="flex items-start gap-4 p-4 hover:bg-muted/30 cursor-pointer transition-colors"
          onClick={() => onCommitClick?.(commit)}
        >
          <div className="h-10 w-10 rounded-full bg-muted flex items-center justify-center flex-shrink-0">
            {commit.author.avatar ? (
              <img
                src={commit.author.avatar}
                alt={commit.author.name}
                className="h-10 w-10 rounded-full"
              />
            ) : (
              <span className="text-sm font-medium">
                {commit.author.name.charAt(0).toUpperCase()}
              </span>
            )}
          </div>
          <div className="flex-1 min-w-0">
            <p className="text-sm font-medium truncate">{commit.message}</p>
            <div className="flex items-center gap-2 mt-1 text-xs text-muted-foreground">
              <span className="font-medium">{commit.author.name}</span>
              <span>committed</span>
              <code className="font-mono">{commit.shortSha}</code>
              <span>·</span>
              <span>{commit.date}</span>
            </div>
            {(commit.refs?.length || commit.tags?.length) && (
              <div className="flex items-center gap-1 mt-2">
                {commit.refs?.map((ref, i) => (
                  <span
                    key={i}
                    className="inline-flex items-center rounded-full px-2 py-0.5 text-[10px] font-medium bg-blue-500/20 text-blue-600 dark:text-blue-400"
                  >
                    {ref}
                  </span>
                ))}
                {commit.tags?.map((tag, i) => (
                  <span
                    key={i}
                    className="inline-flex items-center rounded-full px-2 py-0.5 text-[10px] font-medium bg-yellow-500/20 text-yellow-600 dark:text-yellow-400"
                  >
                    🏷️ {tag}
                  </span>
                ))}
              </div>
            )}
          </div>
        </div>
      ))}
    </div>
  )
}

export type { Commit, CommitGraphProps, CommitListProps }