-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathLibraries.tsx
More file actions
136 lines (117 loc) · 4.92 KB
/
Copy pathLibraries.tsx
File metadata and controls
136 lines (117 loc) · 4.92 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import "@vscode-elements/elements/dist/vscode-button/index.js";
import "@vscode-elements/elements/dist/vscode-divider/index.js";
import { Dispatch } from "@reduxjs/toolkit";
import { useEffect, useRef, useState } from "react";
import { useSelector, useDispatch } from "react-redux";
import { removeReferencedLibrary, addLibraries } from "../classpathConfigurationViewSlice";
import { ClasspathRequest } from "../../../vscode/utils";
import { ClasspathEntry, ClasspathEntryKind } from "../../../../types";
import { ProjectType } from "../../../../../utils/webview";
const Libraries = (): JSX.Element => {
const [hoveredRow, setHoveredRow] = useState<string | null>(null);
const activeProjectIndex: number = useSelector((state: any) => state.commonConfig.ui.activeProjectIndex);
const activeProjectIndexRef = useRef(activeProjectIndex);
useEffect(() => {
activeProjectIndexRef.current = activeProjectIndex;
}, [activeProjectIndex]);
const libraries: ClasspathEntry[] = useSelector((state: any) => state.classpathConfig.data.libraries[activeProjectIndex]);
const projectType: ProjectType = useSelector((state: any) => state.commonConfig.data.projectType[activeProjectIndex]);
const dispatch: Dispatch<any> = useDispatch();
const handleRemove = (index: number) => {
dispatch(removeReferencedLibrary({
activeProjectIndex,
removedIndex: index
}));
};
const handleAdd = () => {
ClasspathRequest.onWillSelectLibraries(projectType);
};
const onDidAddLibraries = (event: OnDidAddLibrariesEvent) => {
const {data} = event;
if (data.command === "classpath.onDidAddLibraries") {
dispatch(addLibraries({
activeProjectIndex: activeProjectIndexRef.current,
libraries:data.jars
}));
}
};
useEffect(() => {
window.addEventListener("message", onDidAddLibraries);
return () => {
window.removeEventListener("message", onDidAddLibraries);
}
}, []);
const resolveLibPath = (entry: ClasspathEntry): string => {
if (entry.kind === ClasspathEntryKind.Project) {
return resolveProjectPath(entry);
} else if (entry.attributes?.hasOwnProperty("maven.pomderived")) {
return resolveMavenLibPath(entry);
} else if (entry.attributes?.hasOwnProperty("gradle_used_by_scope")|| entry.attributes?.hasOwnProperty("gradle.buildServer")) {
return resolveGradleLibPath(entry);
}
return entry.path;
}
const resolveProjectPath = (entry: ClasspathEntry): string => {
return `Project: ${getLastPathComponent(entry.path)}`;
}
const resolveMavenLibPath = (entry: ClasspathEntry): string => {
if (entry.attributes?.["maven.groupId"] && entry.attributes?.["maven.artifactId"] && entry.attributes?.["maven.version"]) {
return `Maven: ${entry.attributes["maven.groupId"]}:${entry.attributes["maven.artifactId"]}:${entry.attributes["maven.version"]}`;
}
return entry.path;
}
const resolveGradleLibPath = (entry: ClasspathEntry): string => {
return `Gradle: ${getLastPathComponent(entry.path)}`;
}
const getLastPathComponent = (path: string): string => {
const pathComponents = path.split(/[\\/]/);
return pathComponents[pathComponents.length - 1];
}
let librariesSections: JSX.Element | JSX.Element[];
if (libraries.length === 0) {
librariesSections = (
<div className="source-row">
<span><em>No libraries are configured.</em></span>
</div>
);
} else {
librariesSections = libraries.map((library, index) => (
<div className="source-row" id={`library-${index}`} onMouseEnter={() => setHoveredRow(`library-${index}`)} onMouseLeave={() => setHoveredRow(null)} key={library.path}>
<div title={library.path} className="setting-section-grid-cell" style={{flex: 1}}>
<span className={`codicon ${library.kind === 2 ? "codicon-project" : "codicon-library"} mr-1`}></span>
<span>{resolveLibPath(library)}</span>
</div>
<div className={`source-row-actions ${hoveredRow === `library-${index}` ? "" : "hidden"}`}>
<vscode-button class="ghost-button" icon-only onClick={() => handleRemove(index)} title="Remove">
<span className="codicon codicon-close"></span>
</vscode-button>
</div>
</div>
));
}
return (
<div className="setting-section">
<div>
<div id="list-actions" className="flex-center setting-list-actions">
<vscode-button class="ghost-button" onClick={() => handleAdd()}>
<span className="codicon codicon-add mr-1"></span>
Add Library...
</vscode-button>
</div>
<vscode-divider className="mb-0"/>
<div className="setting-overflow-area">
{librariesSections}
</div>
</div>
</div>
);
};
interface OnDidAddLibrariesEvent {
data: {
command: string;
jars: ClasspathEntry[];
};
}
export default Libraries;