-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathelementMenu.tsx
More file actions
171 lines (163 loc) · 7.85 KB
/
elementMenu.tsx
File metadata and controls
171 lines (163 loc) · 7.85 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
"use client"
import { Dispatch, RefObject, SetStateAction, useEffect, useState } from "react";
import { Link, Node } from "./model";
import { ChevronsLeftRight, Copy, EyeOff, Globe, Maximize2, Minimize2, Waypoints } from "lucide-react";
import DataPanel from "./dataPanel";
import { Path } from "@/lib/utils";
import { Position } from "./graphView";
interface Props {
obj: Node | Link | undefined;
objects: Node[];
setPath: Dispatch<SetStateAction<Path | undefined>>;
handleRemove: (ids: number[], type: "nodes" | "links") => void;
position: Position | undefined;
url: string;
handleExpand: (nodes: Node[], expand: boolean) => void;
parentRef: RefObject<HTMLDivElement>;
}
export default function ElementMenu({ obj, objects, setPath, handleRemove, position, url, handleExpand, parentRef }: Props) {
const [currentObj, setCurrentObj] = useState<Node | Link>();
const [containerWidth, setContainerWidth] = useState(0);
useEffect(() => {
setCurrentObj(undefined)
}, [obj])
if (!obj || !position) return null
const objURL = obj.category === "File"
? `${url}/tree/master/${obj.path}/${obj.name}`
: `${url}/tree/master/${obj.path}#L${obj.src_start}-L${obj.src_end + 1}`
return (
<>
<div
ref={(ref) => {
if (!ref) return
setContainerWidth(ref.clientWidth)
}}
className="absolute z-10 bg-black rounded-lg shadow-lg flex divide-x divide-[#434343]"
id="elementMenu"
style={{
left: Math.max(8, Math.min(position.x - containerWidth / 2, (parentRef?.current?.clientWidth || 0) - containerWidth - 8)),
top: Math.max(8, Math.min(position.y - 153, (parentRef?.current?.clientHeight || 0) - containerWidth - 8)),
}}
>
{
objects.some(o => o.id === obj.id) && objects.length > 1 ?
<>
{
objects.length === 2 &&
<button
className="p-2"
title="Create a path"
onClick={() => setPath({ start: { id: Number(objects[0].id), name: objects[0].name }, end: { id: Number(objects[1].id), name: objects[1].name } })}
>
<Waypoints color="white" />
</button>
}
<button
className="p-2"
title="Remove"
onClick={() => handleRemove(objects.map(o => o.id), "nodes")}
>
<EyeOff color="white" />
</button>
<button
className="p-2"
onClick={() => handleExpand(objects, true)}
>
<Maximize2 color="white" />
</button>
<button
className="p-2"
onClick={() => handleExpand(objects, false)}
>
<Minimize2 color="white" />
</button>
</>
: <>
{
"category" in obj &&
<>
<button
className="p-2"
title="Copy src to clipboard"
onClick={async () => {
try {
await navigator.clipboard.writeText(obj.src || "");
} catch (err) {
// Fallback for older browsers
const textArea = document.createElement('textarea');
textArea.value = obj.src || "";
textArea.style.position = 'fixed';
textArea.style.left = '-999999px';
document.body.appendChild(textArea);
textArea.select();
try {
document.execCommand('copy');
} catch (e) {
console.error('Failed to copy text: ', e);
}
document.body.removeChild(textArea);
}
}}
>
<Copy color="white" />
</button>
</>
}
<button
className="p-2"
title="Remove"
onClick={() => handleRemove([obj.id], "category" in obj ? "nodes" : "links")}
>
<EyeOff color="white" />
</button>
{
"category" in obj &&
<>
<a
className="p-2"
href={objURL}
target="_blank"
title="Go to repo"
onClick={() => {
window.open(objURL, '_blank');
}}
>
<Globe color="white" />
</a>
</>
}
<button
className="flex p-2"
title="View Node"
onClick={() => setCurrentObj(obj)}
>
<ChevronsLeftRight color="white" />
</button>
{
"category" in obj &&
<>
<button
className="p-2"
onClick={() => handleExpand([obj as Node], true)}
>
<Maximize2 color="white" />
</button>
<button
className="p-2"
onClick={() => handleExpand([obj as Node], false)}
>
<Minimize2 color="white" />
</button>
</>
}
</>
}
</div>
<DataPanel
obj={currentObj}
setObj={setCurrentObj}
url={objURL}
/>
</>
)
}