-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathpathHeader.tsx
More file actions
311 lines (282 loc) · 12.4 KB
/
pathHeader.tsx
File metadata and controls
311 lines (282 loc) · 12.4 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
'use client';
import { cn, getCodeHostInfoForRepo } from "@/lib/utils";
import Image from "next/image";
import { getBrowsePath } from "../browse/hooks/utils";
import { ChevronRight, MoreHorizontal } from "lucide-react";
import { useCallback, useState, useMemo, useRef, useEffect } from "react";
import { useToast } from "@/components/hooks/use-toast";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { VscodeFileIcon } from "@/app/components/vscodeFileIcon";
import { CopyIconButton } from "./copyIconButton";
import Link from "next/link";
import { useDomain } from "@/hooks/useDomain";
import { CodeHostType } from "@sourcebot/db";
interface FileHeaderProps {
path: string;
pathHighlightRange?: {
from: number;
to: number;
}
pathType?: 'blob' | 'tree';
repo: {
name: string;
codeHostType: CodeHostType;
displayName?: string;
externalWebUrl?: string;
},
isBranchDisplayNameVisible?: boolean;
branchDisplayName?: string;
revisionName?: string;
branchDisplayTitle?: string;
isCodeHostIconVisible?: boolean;
isFileIconVisible?: boolean;
repoNameClassName?: string;
}
interface BreadcrumbSegment {
name: string;
fullPath: string;
isLastSegment: boolean;
highlightRange?: {
from: number;
to: number;
};
}
export const PathHeader = ({
repo,
path,
pathHighlightRange,
revisionName,
branchDisplayName = revisionName,
isBranchDisplayNameVisible = !!branchDisplayName,
branchDisplayTitle: _branchDisplayTitle,
pathType = 'blob',
isCodeHostIconVisible = true,
isFileIconVisible = true,
repoNameClassName,
}: FileHeaderProps) => {
const info = getCodeHostInfoForRepo({
name: repo.name,
codeHostType: repo.codeHostType,
displayName: repo.displayName,
externalWebUrl: repo.externalWebUrl,
});
const { toast } = useToast();
const containerRef = useRef<HTMLDivElement>(null);
const breadcrumbsRef = useRef<HTMLDivElement>(null);
const [visibleSegmentCount, setVisibleSegmentCount] = useState<number | null>(null);
const domain = useDomain();
// Create breadcrumb segments from file path
const breadcrumbSegments = useMemo(() => {
const pathParts = path.split('/').filter(Boolean);
const segments: BreadcrumbSegment[] = [];
let currentPath = '';
pathParts.forEach((part, index) => {
currentPath = currentPath ? `${currentPath}/${part}` : part;
const isLastSegment = index === pathParts.length - 1;
// Calculate highlight range for this segment if it exists
let segmentHighlight: { from: number; to: number } | undefined;
if (pathHighlightRange) {
const segmentStart = path.indexOf(part, currentPath.length - part.length);
const segmentEnd = segmentStart + part.length;
// Check if highlight overlaps with this segment
if (pathHighlightRange.from < segmentEnd && pathHighlightRange.to > segmentStart) {
segmentHighlight = {
from: Math.max(0, pathHighlightRange.from - segmentStart),
to: Math.min(part.length, pathHighlightRange.to - segmentStart)
};
}
}
segments.push({
name: part,
fullPath: currentPath,
isLastSegment,
highlightRange: segmentHighlight
});
});
return segments;
}, [path, pathHighlightRange]);
// Calculate which segments should be visible based on available space
useEffect(() => {
const measureSegments = () => {
if (!containerRef.current || !breadcrumbsRef.current) return;
const containerWidth = containerRef.current.offsetWidth;
const availableWidth = containerWidth - 175; // Reserve space for copy button and padding
// Create a temporary element to measure segment widths
const tempElement = document.createElement('div');
tempElement.style.position = 'absolute';
tempElement.style.visibility = 'hidden';
tempElement.style.whiteSpace = 'nowrap';
tempElement.className = 'font-mono text-sm';
document.body.appendChild(tempElement);
let totalWidth = 0;
let visibleCount = breadcrumbSegments.length;
// Start from the end (most important segments) and work backwards
for (let i = breadcrumbSegments.length - 1; i >= 0; i--) {
const segment = breadcrumbSegments[i];
tempElement.textContent = segment.name;
const segmentWidth = tempElement.offsetWidth;
const separatorWidth = i < breadcrumbSegments.length - 1 ? 16 : 0; // ChevronRight width
if (totalWidth + segmentWidth + separatorWidth > availableWidth && i > 0) {
// If adding this segment would overflow and it's not the last segment
visibleCount = breadcrumbSegments.length - i;
// Add width for ellipsis dropdown (approximately 24px)
if (visibleCount < breadcrumbSegments.length) {
totalWidth += 40; // Ellipsis button + separator
}
break;
}
totalWidth += segmentWidth + separatorWidth;
}
document.body.removeChild(tempElement);
setVisibleSegmentCount(visibleCount);
};
measureSegments();
const resizeObserver = new ResizeObserver(measureSegments);
if (containerRef.current) {
resizeObserver.observe(containerRef.current);
}
return () => resizeObserver.disconnect();
}, [breadcrumbSegments]);
const hiddenSegments = useMemo(() => {
if (visibleSegmentCount === null || visibleSegmentCount >= breadcrumbSegments.length) {
return [];
}
return breadcrumbSegments.slice(0, breadcrumbSegments.length - visibleSegmentCount);
}, [breadcrumbSegments, visibleSegmentCount]);
const visibleSegments = useMemo(() => {
if (visibleSegmentCount === null) {
return breadcrumbSegments;
}
return breadcrumbSegments.slice(breadcrumbSegments.length - visibleSegmentCount);
}, [breadcrumbSegments, visibleSegmentCount]);
const onCopyPath = useCallback(() => {
navigator.clipboard.writeText(path);
toast({ description: "✅ Copied to clipboard" });
return true;
}, [path, toast]);
const renderSegmentWithHighlight = (segment: BreadcrumbSegment) => {
if (!segment.highlightRange) {
return segment.name;
}
const { from, to } = segment.highlightRange;
return (
<>
{segment.name.slice(0, from)}
<span className="bg-yellow-200 dark:bg-blue-700">
{segment.name.slice(from, to)}
</span>
{segment.name.slice(to)}
</>
);
};
return (
<div className="flex flex-row gap-2 items-center w-full overflow-hidden">
{isCodeHostIconVisible && (
<>
<a href={info.externalWebUrl} target="_blank" rel="noopener noreferrer">
<Image
src={info.icon}
alt={info.codeHostName}
className={`w-4 h-4 ${info.iconClassName}`}
/>
</a>
</>
)}
<Link
className={cn("font-medium cursor-pointer hover:underline", repoNameClassName)}
href={getBrowsePath({
repoName: repo.name,
path: '/',
pathType: 'tree',
revisionName,
domain,
})}
>
{info?.displayName}
</Link>
{(isBranchDisplayNameVisible && branchDisplayName) && (
<p
className="text-xs font-semibold text-gray-500 dark:text-gray-400 mt-[3px] flex items-center gap-0.5"
style={{
marginBottom: "0.1rem",
}}
>
<span className="mr-0.5">@</span>
{`${branchDisplayName.replace(/^refs\/(heads|tags)\//, '')}`}
</p>
)}
<span>·</span>
<div ref={containerRef} className="flex-1 flex items-center overflow-hidden mt-0.5">
<div ref={breadcrumbsRef} className="flex items-center overflow-hidden">
{hiddenSegments.length > 0 && (
<>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<button
className="font-mono text-sm cursor-pointer hover:underline p-1 rounded transition-colors"
aria-label="Show hidden path segments"
>
<MoreHorizontal className="h-4 w-4" />
</button>
</DropdownMenuTrigger>
<DropdownMenuContent align="start" className="min-w-[200px]">
{hiddenSegments.map((segment) => (
<Link
href={getBrowsePath({
repoName: repo.name,
path: segment.fullPath,
pathType: segment.isLastSegment ? pathType : 'tree',
revisionName,
domain,
})}
className="font-mono text-sm hover:cursor cursor-pointer"
key={segment.fullPath}
>
<DropdownMenuItem className="hover:cursor cursor-pointer">
{renderSegmentWithHighlight(segment)}
</DropdownMenuItem>
</Link>
))}
</DropdownMenuContent>
</DropdownMenu>
<ChevronRight className="h-3 w-3 mx-0.5 text-muted-foreground flex-shrink-0" />
</>
)}
{visibleSegments.map((segment, index) => (
<div key={segment.fullPath} className="flex items-center">
{(isFileIconVisible && index === visibleSegments.length - 1) && (
<VscodeFileIcon fileName={segment.name} className="h-4 w-4 mr-1" />
)}
<Link
className={cn(
"font-mono text-sm truncate cursor-pointer hover:underline",
)}
href={getBrowsePath({
repoName: repo.name,
path: segment.fullPath,
pathType: segment.isLastSegment ? pathType : 'tree',
revisionName,
domain,
})}
>
{renderSegmentWithHighlight(segment)}
</Link>
{index < visibleSegments.length - 1 && (
<ChevronRight className="h-3 w-3 mx-0.5 text-muted-foreground flex-shrink-0" />
)}
</div>
))}
</div>
<CopyIconButton
onCopy={onCopyPath}
className="ml-2"
/>
</div>
</div>
)
}