import { useCreateEnvironment } from '../hooks/useCreateEnvironment'; import { useEnvironments } from '../hooks/useEnvironments'; import type { Environment } from '../lib/models'; import { Button } from './core/Button'; import classNames from 'classnames'; import { useActiveEnvironment } from '../hooks/useActiveEnvironment'; import { useAppRoutes } from '../hooks/useAppRoutes'; import { PairEditor } from './core/PairEditor'; import type { PairEditorProps } from './core/PairEditor'; import { useCallback, useMemo } from 'react'; import { useUpdateEnvironment } from '../hooks/useUpdateEnvironment'; import { HStack, VStack } from './core/Stacks'; import { IconButton } from './core/IconButton'; import { useDeleteEnvironment } from '../hooks/useDeleteEnvironment'; import type { DropdownItem } from './core/Dropdown'; import { Dropdown } from './core/Dropdown'; import { Icon } from './core/Icon'; import { usePrompt } from '../hooks/usePrompt'; import { InlineCode } from './core/InlineCode'; import { useWindowSize } from 'react-use'; import type { GenericCompletionConfig } from './core/Editor/genericCompletion'; export const EnvironmentEditDialog = function () { const routes = useAppRoutes(); const environments = useEnvironments(); const createEnvironment = useCreateEnvironment(); const activeEnvironment = useActiveEnvironment(); const windowSize = useWindowSize(); const showSidebar = windowSize.width > 500; return (
{showSidebar && ( )} {activeEnvironment != null ? ( ) : (
select an environment
)}
); }; const EnvironmentEditor = function ({ environment }: { environment: Environment }) { const environments = useEnvironments(); const updateEnvironment = useUpdateEnvironment(environment.id); const deleteEnvironment = useDeleteEnvironment(environment); const handleChange = useCallback( (variables) => { updateEnvironment.mutate({ variables }); }, [updateEnvironment], ); const nameAutocomplete = useMemo(() => { const allVariableNames = environments.flatMap((e) => e.variables.map((v) => v.name)); // Filter out empty strings and variables that already exist in the active environment const variableNames = allVariableNames.filter( (name) => name != '' && !environment.variables.find((v) => v.name === name), ); return { options: variableNames.map((name) => ({ label: name, type: 'constant' })) }; }, [environments, environment.variables]); const prompt = usePrompt(); const items = useMemo( () => [ { key: 'rename', label: 'Rename', leftSlot: , onSelect: async () => { const name = await prompt({ title: 'Rename Environment', description: ( <> Enter a new name for {environment.name} ), name: 'name', label: 'Name', defaultValue: environment.name, }); updateEnvironment.mutate({ name }); }, }, { key: 'delete', variant: 'danger', label: 'Delete', leftSlot: , onSelect: () => deleteEnvironment.mutate(), }, ], [deleteEnvironment, updateEnvironment, environment.name, prompt], ); const validateName = useCallback((name: string) => { // Empty just means the variable doesn't have a name yet, and is unusable if (name === '') return true; return name.match(/^[a-z_][a-z0-9_]*$/i) != null; }, []); return (

{environment.name}

); };