Refactor environment edit dialog

This commit is contained in:
Gregory Schier
2023-11-02 20:38:33 -07:00
parent a99a36b5cc
commit b15cdec701
6 changed files with 88 additions and 47 deletions

View File

@@ -16,7 +16,10 @@ import { Icon } from './core/Icon';
import { usePrompt } from '../hooks/usePrompt'; import { usePrompt } from '../hooks/usePrompt';
import { InlineCode } from './core/InlineCode'; import { InlineCode } from './core/InlineCode';
import { useWindowSize } from 'react-use'; import { useWindowSize } from 'react-use';
import type { GenericCompletionConfig } from './core/Editor/genericCompletion'; import type {
GenericCompletionConfig,
GenericCompletionOption,
} from './core/Editor/genericCompletion';
import { useActiveWorkspace } from '../hooks/useActiveWorkspace'; import { useActiveWorkspace } from '../hooks/useActiveWorkspace';
import { useUpdateWorkspace } from '../hooks/useUpdateWorkspace'; import { useUpdateWorkspace } from '../hooks/useUpdateWorkspace';
@@ -25,8 +28,8 @@ interface Props {
} }
export const EnvironmentEditDialog = function ({ initialEnvironment }: Props) { export const EnvironmentEditDialog = function ({ initialEnvironment }: Props) {
const [selectedEnvironment, setSelectedEnvironment] = useState<Environment | null>( const [selectedEnvironmentId, setSelectedEnvironmentId] = useState<string | null>(
initialEnvironment, initialEnvironment?.id ?? null,
); );
const environments = useEnvironments(); const environments = useEnvironments();
const createEnvironment = useCreateEnvironment(); const createEnvironment = useCreateEnvironment();
@@ -35,6 +38,11 @@ export const EnvironmentEditDialog = function ({ initialEnvironment }: Props) {
const windowSize = useWindowSize(); const windowSize = useWindowSize();
const showSidebar = windowSize.width > 500; const showSidebar = windowSize.width > 500;
const selectedEnvironment = useMemo(
() => environments.find((e) => e.id === selectedEnvironmentId) ?? null,
[environments, selectedEnvironmentId],
);
return ( return (
<div <div
className={classNames( className={classNames(
@@ -45,44 +53,30 @@ export const EnvironmentEditDialog = function ({ initialEnvironment }: Props) {
{showSidebar && ( {showSidebar && (
<aside className="grid grid-rows-[minmax(0,1fr)_auto] gap-y-0.5 h-full max-w-[250px] pr-4 border-r border-gray-100"> <aside className="grid grid-rows-[minmax(0,1fr)_auto] gap-y-0.5 h-full max-w-[250px] pr-4 border-r border-gray-100">
<div className="min-w-0 h-full w-full overflow-y-scroll"> <div className="min-w-0 h-full w-full overflow-y-scroll">
<Button <SidebarButton
size="xs" active={selectedEnvironmentId == null}
color="custom" onClick={() => setSelectedEnvironmentId(null)}
justify="start"
className={classNames(
'w-full',
'text-gray-600 hocus:text-gray-800 !ring-0',
selectedEnvironment == null && 'bg-highlightSecondary !text-gray-900',
)}
onClick={() => {
setSelectedEnvironment(null);
}}
> >
Base Environment Base Environment
</Button> </SidebarButton>
{environments.map((e) => ( <div className="ml-3 pl-2 border-l border-highlight">
<Button {environments.map((e) => (
key={e.id} <SidebarButton
justify="start" key={e.id}
size="xs" active={selectedEnvironmentId === e.id}
color="custom" onClick={() => setSelectedEnvironmentId(e.id)}
className={classNames( className="pl-2"
'w-full', >
'text-gray-600 hocus:text-gray-800', {e.name}
selectedEnvironment?.id === e.id && 'bg-highlightSecondary !text-gray-900', </SidebarButton>
)} ))}
onClick={() => { </div>
setSelectedEnvironment(e);
}}
>
{e.name}
</Button>
))}
</div> </div>
<Button <Button
size="sm" size="sm"
className="w-full" className="w-full text-center"
color="gray" color="gray"
justify="center"
onClick={() => createEnvironment.mutate()} onClick={() => createEnvironment.mutate()}
> >
New Environment New Environment
@@ -123,14 +117,32 @@ const EnvironmentEditor = function ({
[updateWorkspace, updateEnvironment, environment], [updateWorkspace, updateEnvironment, environment],
); );
// Gather a list of env names from other environments, to help the user get them aligned
const nameAutocomplete = useMemo<GenericCompletionConfig>(() => { const nameAutocomplete = useMemo<GenericCompletionConfig>(() => {
const allVariableNames = environments.flatMap((e) => e.variables.map((v) => v.name)); const otherEnvironments = environments.filter((e) => e.id !== environment?.id);
// Filter out empty strings and variables that already exist in the active environment const allVariableNames =
environment == null
? [
// Nothing to autocomplete if we're in the base environment
]
: [
...workspace.variables.map((v) => v.name),
...otherEnvironments.flatMap((e) => e.variables.map((v) => v.name)),
];
// Filter out empty strings and variables that already exist
const variableNames = allVariableNames.filter( const variableNames = allVariableNames.filter(
(name) => name != '' && !variables.find((v) => v.name === name), (name) => name != '' && !variables.find((v) => v.name === name),
); );
return { options: variableNames.map((name) => ({ label: name, type: 'constant' })) }; const uniqueVariableNames = [...new Set(variableNames)];
}, [environments, variables]); const options = uniqueVariableNames.map(
(name): GenericCompletionOption => ({
label: name,
type: 'constant',
}),
);
return { options };
}, [environments, variables, workspace, environment]);
const prompt = usePrompt(); const prompt = usePrompt();
const items = useMemo<DropdownItem[] | null>( const items = useMemo<DropdownItem[] | null>(
@@ -198,3 +210,29 @@ const EnvironmentEditor = function ({
</VStack> </VStack>
); );
}; };
function SidebarButton({
children,
className,
active,
onClick,
}: {
className?: string;
children: string;
active: boolean;
onClick: () => void;
}) {
return (
<button
onClick={onClick}
className={classNames(
className,
'flex text-sm text-left w-full mb-1 h-xs',
'text-gray-600 hocus:text-gray-800 !ring-0',
active && '!text-gray-900',
)}
>
{children}
</button>
);
}

View File

@@ -46,10 +46,6 @@ export function Overlay({
variant === 'default' && 'bg-gray-600/30 dark:bg-black/30 backdrop-blur-sm', variant === 'default' && 'bg-gray-600/30 dark:bg-black/30 backdrop-blur-sm',
)} )}
/> />
{/* Add region to still be able to drag the window */}
{variant !== 'transparent' && (
<div data-tauri-drag-region className="absolute top-0 left-0 right-0 h-md" />
)}
<div className="bg-red-100">{children}</div> <div className="bg-red-100">{children}</div>
</motion.div> </motion.div>
</FocusTrap> </FocusTrap>

View File

@@ -133,8 +133,7 @@ export const WorkspaceActionsDropdown = memo(function WorkspaceActionsDropdown({
const name = await prompt({ const name = await prompt({
name: 'name', name: 'name',
label: 'Name', label: 'Name',
defaultValue: '', defaultValue: 'My Workspace',
description: 'Enter a name for the new workspace',
title: 'Create Workspace', title: 'Create Workspace',
}); });
createWorkspace.mutate({ name }); createWorkspace.mutate({ name });

View File

@@ -72,7 +72,15 @@ const _Button = forwardRef<HTMLButtonElement, ButtonProps>(function Button(
) : leftSlot ? ( ) : leftSlot ? (
<div className="mr-1">{leftSlot}</div> <div className="mr-1">{leftSlot}</div>
) : null} ) : null}
<div className={classNames('max-w-[15em] truncate w-full text-left', innerClassName)}>{children}</div> <div
className={classNames(
'max-w-[15em] truncate w-full',
justify === 'start' ? 'text-left' : 'text-center',
innerClassName,
)}
>
{children}
</div>
{rightSlot && <div className="ml-1">{rightSlot}</div>} {rightSlot && <div className="ml-1">{rightSlot}</div>}
{forDropdown && <Icon icon="chevronDown" size={size} className="ml-1 -mr-1" />} {forDropdown && <Icon icon="chevronDown" size={size} className="ml-1 -mr-1" />}
</button> </button>

View File

@@ -366,6 +366,7 @@ function MenuItem({ className, focused, onFocus, item, onSelect, ...props }: Men
'focus:bg-highlight focus:text-gray-900 rounded', 'focus:bg-highlight focus:text-gray-900 rounded',
item.variant === 'danger' && 'text-red-600', item.variant === 'danger' && 'text-red-600',
)} )}
innerClassName="!text-left"
{...props} {...props}
> >
<div <div

View File

@@ -20,7 +20,6 @@ export function useCreateEnvironment() {
const name = await prompt({ const name = await prompt({
name: 'name', name: 'name',
title: 'Create Environment', title: 'Create Environment',
description: 'Enter a name for the new environment',
label: 'Name', label: 'Name',
defaultValue: 'My Environment', defaultValue: 'My Environment',
}); });