-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathblameViewToggle.tsx
More file actions
65 lines (59 loc) · 2.17 KB
/
blameViewToggle.tsx
File metadata and controls
65 lines (59 loc) · 2.17 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
'use client';
import { useRouter } from "next/navigation";
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group";
import { getBrowsePath } from "@/app/(app)/browse/hooks/utils";
interface BlameViewToggleProps {
repoName: string;
revisionName?: string;
path: string;
blame: boolean;
}
export const BlameViewToggle = ({ repoName, revisionName, path, blame }: BlameViewToggleProps) => {
const router = useRouter();
const handleValueChange = (value: string) => {
// Radix calls onValueChange with an empty string when the user clicks
// the already-selected item (would deselect). Ignore that — we want
// exactly one of Code / Blame to always be selected.
if (!value) {
return;
}
router.push(getBrowsePath({
repoName,
revisionName,
path,
pathType: 'blob',
blame: value === 'blame',
}));
};
// The Toggle "default" size is icon-sized (h-7 w-7 p-0) since it's the
// codebase's only declared size. `w-auto min-w-0 px-3` lets the items size
// to their text. The remaining classes turn the two items into a connected
// segmented control: gap-0 on the group removes the flex gap, rounded-*-none
// squares off the inner corners, and -ml-px pulls the second item over so
// its left border overlaps the first item's right border (no double seam).
const baseItemClass = "w-auto min-w-0 px-3";
return (
<ToggleGroup
type="single"
value={blame ? 'blame' : 'code'}
onValueChange={handleValueChange}
variant="outline"
className="gap-0"
>
<ToggleGroupItem
value="code"
aria-label="View source code"
className={`${baseItemClass} rounded-r-none`}
>
Code
</ToggleGroupItem>
<ToggleGroupItem
value="blame"
aria-label="View blame"
className={`${baseItemClass} rounded-l-none -ml-px`}
>
Blame
</ToggleGroupItem>
</ToggleGroup>
);
};