Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@
user-select: none;
}

.approot-select-label-collapsible {
cursor: pointer;
display: flex;
align-items: center;
gap: 6px;
}

.approot-select-item {
height: 32px;
line-height: 1.2;
Expand Down
90 changes: 65 additions & 25 deletions packages/vscode-extension/src/webview/components/AppRootSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as Select from "@radix-ui/react-select";
import "./AppRootSelect.css";
import "./shared/Dropdown.css";
import _ from "lodash";
import React, { PropsWithChildren, useEffect, useMemo } from "react";
import React, { PropsWithChildren, useEffect, useMemo, useState } from "react";
import { use$ } from "@legendapp/state/react";
import { useProject } from "../providers/ProjectProvider";
import { LaunchConfiguration, LaunchConfigurationKind } from "../../common/LaunchConfig";
Expand Down Expand Up @@ -46,6 +46,8 @@ function renderLaunchConfigurations(
prefix: string,
customLaunchConfigurations: LaunchConfiguration[],
selectedValue: string | undefined,
isExpanded: boolean,
onToggleExpand: () => void,
Comment on lines +49 to +50
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this function should be made into a full-blown component, and these should be the internal state of that component, rather than arguments that are passed through three separate functions.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure Ill cook it in a minute

onEditConfig?: (config: LaunchConfiguration, isSelected: boolean) => void
) {
if (customLaunchConfigurations.length === 0) {
Expand All @@ -54,33 +56,41 @@ function renderLaunchConfigurations(

return (
<Select.Group>
<Select.Label className="approot-select-label">{groupLabel}</Select.Label>
{customLaunchConfigurations.map((config, idx) => (
<RichSelectItem
value={`${prefix}:${idx}`}
key={idx}
data-testid={`approot-select-item-${config.name || config.appRoot}`}
icon={<span className="codicon codicon-folder" />}
title={displayNameForConfig(config) ?? config.appRoot ?? "./"}
subtitle={displayNameForConfig(config) ? config.appRoot : undefined}
isSelected={selectedValue === `${prefix}:${idx}`}>
{onEditConfig && (
<ConfigureButton
dataTest={`edit-launch-config-button-${config.name || idx}`}
onClick={() =>
onEditConfig(config as LaunchConfiguration, selectedValue === `${prefix}:${idx}`)
}
/>
)}
</RichSelectItem>
))}
<div className="approot-select-label-collapsible" onClick={onToggleExpand}>
<span
className={`codicon ${isExpanded ? "codicon-chevron-down" : "codicon-chevron-right"}`}
/>
<Select.Label className="approot-select-label">{groupLabel}</Select.Label>
</div>
{isExpanded &&
customLaunchConfigurations.map((config, idx) => (
<RichSelectItem
value={`${prefix}:${idx}`}
key={idx}
data-testid={`approot-select-item-${config.name || config.appRoot}`}
icon={<span className="codicon codicon-folder" />}
title={displayNameForConfig(config) ?? config.appRoot ?? "./"}
subtitle={displayNameForConfig(config) ? config.appRoot : undefined}
isSelected={selectedValue === `${prefix}:${idx}`}>
{onEditConfig && (
<ConfigureButton
dataTest={`edit-launch-config-button-${config.name || idx}`}
onClick={() =>
onEditConfig(config as LaunchConfiguration, selectedValue === `${prefix}:${idx}`)
}
/>
)}
</RichSelectItem>
))}
</Select.Group>
);
}

function renderDetectedLaunchConfigurations(
detectedConfigurations: LaunchConfiguration[],
selectedValue: string | undefined
selectedValue: string | undefined,
isExpanded: boolean,
onToggleExpand: () => void
) {
if (detectedConfigurations.length === 0) {
return null;
Expand All @@ -90,13 +100,17 @@ function renderDetectedLaunchConfigurations(
"Detected applications",
"detected",
detectedConfigurations,
selectedValue
selectedValue,
isExpanded,
onToggleExpand
);
}

function renderCustomLaunchConfigurations(
customLaunchConfigurations: LaunchConfiguration[],
selectedValue: string | undefined,
isExpanded: boolean,
onToggleExpand: () => void,
onEditConfig: (config: LaunchConfiguration, isSelected: boolean) => void
) {
if (customLaunchConfigurations.length === 0) {
Expand All @@ -108,6 +122,8 @@ function renderCustomLaunchConfigurations(
"custom",
customLaunchConfigurations,
selectedValue,
isExpanded,
onToggleExpand,
onEditConfig
);
}
Expand Down Expand Up @@ -149,6 +165,11 @@ function AppRootSelect() {
const selectedAppRoot = applicationRoots.find((root) => root.path === selectedAppRootPath);
const { openModal } = useModal();

const [expandedSections, setExpandedSections] = useState<{ detected: boolean; custom: boolean }>({
detected: false,
custom: false,
});

function onEditConfig(config: LaunchConfiguration, isSelected: boolean) {
openModal(<LaunchConfigurationView launchConfig={config} isCurrentConfig={isSelected} />, {
title: "Launch Configuration",
Expand Down Expand Up @@ -199,6 +220,14 @@ function AppRootSelect() {
}
})();

useEffect(() => {
if (!projectInitialized) return;
setExpandedSections({
detected: selectedConfiguration.kind === LaunchConfigurationKind.Detected,
custom: selectedConfiguration.kind === LaunchConfigurationKind.Custom,
});
}, [projectInitialized, selectedConfiguration.kind]);

useUnknownConfigurationAlert(projectInitialized && selectedValue === "unknown");

const configurationsCount = detectedConfigurations.length + customConfigurations.length;
Expand Down Expand Up @@ -232,8 +261,19 @@ function AppRootSelect() {
<span className="codicon codicon-chevron-up" />
</Select.ScrollUpButton>
<Select.Viewport className="approot-select-viewport">
{renderDetectedLaunchConfigurations(detectedConfigurations, selectedValue)}
{renderCustomLaunchConfigurations(customConfigurations, selectedValue, onEditConfig)}
{renderDetectedLaunchConfigurations(
detectedConfigurations,
selectedValue,
expandedSections.detected,
() => setExpandedSections((prev) => ({ ...prev, detected: !prev.detected }))
)}
{renderCustomLaunchConfigurations(
customConfigurations,
selectedValue,
expandedSections.custom,
() => setExpandedSections((prev) => ({ ...prev, custom: !prev.custom })),
onEditConfig
)}
{configurationsCount > 0 && <Select.Separator className="approot-select-separator" />}
<SelectItem value="manage" data-testid="add-launch-config-button">
<span className="codicon codicon-add" />
Expand Down
Loading