Skip to content

Commit 60b3e67

Browse files
committed
fix: update code to comments
1 parent 13599cd commit 60b3e67

3 files changed

Lines changed: 30 additions & 7 deletions

File tree

src/project-settings/assets/classpath/features/components/Libraries.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,21 @@ import { removeReferencedLibrary, addLibraries } from "../classpathConfiguration
1111
import { ClasspathRequest } from "../../../vscode/utils";
1212

1313
import { ClasspathEntry, ClasspathEntryKind } from "../../../../types";
14-
import { ProjectType } from "../../../../../utils/webview";
14+
import { isProjectType } from "../../../../../utils/webview";
1515

1616
const Libraries = (): JSX.Element => {
1717

1818
const [hoveredRow, setHoveredRow] = useState<string | null>(null);
19+
const addLibraryBtnRef = useRef<HTMLElement>(null);
1920
const activeProjectIndex: number = useSelector((state: any) => state.commonConfig.ui.activeProjectIndex);
2021
const activeProjectIndexRef = useRef(activeProjectIndex);
2122
useEffect(() => {
2223
activeProjectIndexRef.current = activeProjectIndex;
2324
}, [activeProjectIndex]);
2425

2526
const libraries: ClasspathEntry[] = useSelector((state: any) => state.classpathConfig.data.libraries[activeProjectIndex]);
26-
const projectType: ProjectType = useSelector((state: any) => state.commonConfig.data.projectType[activeProjectIndex]);
27+
const projectType: unknown = useSelector((state: any) => state.commonConfig.data.projectType[activeProjectIndex]);
28+
const canAddLibrary = isProjectType(projectType);
2729
const dispatch: Dispatch<any> = useDispatch();
2830

2931
const handleRemove = (index: number) => {
@@ -34,9 +36,22 @@ const Libraries = (): JSX.Element => {
3436
};
3537

3638
const handleAdd = () => {
39+
if (!canAddLibrary) {
40+
return;
41+
}
3742
ClasspathRequest.onWillSelectLibraries(projectType);
3843
};
3944

45+
useEffect(() => {
46+
const el = addLibraryBtnRef.current;
47+
if (!el) return;
48+
if (canAddLibrary) {
49+
el.removeAttribute("disabled");
50+
} else {
51+
el.setAttribute("disabled", "");
52+
}
53+
}, [canAddLibrary]);
54+
4055
const onDidAddLibraries = (event: OnDidAddLibrariesEvent) => {
4156
const {data} = event;
4257
if (data.command === "classpath.onDidAddLibraries") {
@@ -112,7 +127,7 @@ const Libraries = (): JSX.Element => {
112127
<div className="setting-section">
113128
<div>
114129
<div id="list-actions" className="flex-center setting-list-actions">
115-
<vscode-button class="ghost-button" onClick={() => handleAdd()}>
130+
<vscode-button ref={addLibraryBtnRef} class="ghost-button" onClick={() => handleAdd()}>
116131
<span className="codicon codicon-add mr-1"></span>
117132
Add Library...
118133
</vscode-button>

src/project-settings/handlers/ClasspathRequestHandler.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { ClasspathComponent, VmInstall, ClasspathEntry, ClasspathEntryKind } fro
88
import _ from "lodash";
99
import { instrumentOperation, sendError, sendInfo, setUserError } from "vscode-extension-telemetry-wrapper";
1010
import { getProjectType } from "../../utils/jdt";
11-
import { ProjectType } from "../../utils/webview";
11+
import { ProjectType, isProjectType } from "../../utils/webview";
1212

1313
export class ClasspathRequestHandler implements vscode.Disposable {
1414
private webview: vscode.Webview;
@@ -362,7 +362,7 @@ export class ClasspathRequestHandler implements vscode.Disposable {
362362
}
363363
});
364364

365-
private selectLibraries = instrumentOperation("projectSettings.classpath.selectLibraries", async (_operationId: string, currentProjectRoot: vscode.Uri, projectType: ProjectType) => {
365+
private selectLibraries = instrumentOperation("projectSettings.classpath.selectLibraries", async (_operationId: string, currentProjectRoot: vscode.Uri, projectType: unknown) => {
366366
const jarFiles: vscode.Uri[] | undefined = await vscode.window.showOpenDialog({
367367
defaultUri: vscode.workspace.workspaceFolders?.[0].uri,
368368
openLabel: "Select Jar File",
@@ -386,8 +386,8 @@ export class ClasspathRequestHandler implements vscode.Disposable {
386386
}
387387
});
388388

389-
private toLibraryPathForProject(uri: vscode.Uri, currentProjectRoot: vscode.Uri, projectType: ProjectType): string {
390-
if (projectType !== ProjectType.UnmanagedFolder) {
389+
private toLibraryPathForProject(uri: vscode.Uri, currentProjectRoot: vscode.Uri, projectType: unknown): string {
390+
if (isProjectType(projectType) && projectType !== ProjectType.UnmanagedFolder) {
391391
return uri.fsPath;
392392
}
393393

src/utils/webview.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ export enum ProjectType {
5454
Others = "Others",
5555
}
5656

57+
export function isProjectType(projectType: unknown): projectType is ProjectType {
58+
return projectType === ProjectType.Default ||
59+
projectType === ProjectType.UnmanagedFolder ||
60+
projectType === ProjectType.Maven ||
61+
projectType === ProjectType.Gradle ||
62+
projectType === ProjectType.Others;
63+
}
64+
5765
export enum NatureId {
5866
Maven = "org.eclipse.m2e.core.maven2Nature",
5967
Gradle = "org.eclipse.buildship.core.gradleprojectnature",

0 commit comments

Comments
 (0)