Skip to content

Commit

Permalink
move env editor to folder tab (#7534)
Browse files Browse the repository at this point in the history
* move env editor to folder tab

* remove old unused test
  • Loading branch information
jackkav committed Jun 12, 2024
1 parent 9db2d42 commit 8648f49
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 175 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,80 +144,5 @@ test.describe('Debug-Sidebar', async () => {
await page.getByRole('menuitemradio', { name: 'Http Request' }).click();
await page.getByLabel('Request Collection').getByRole('row', { name: 'New Request' }).click();
});

test('Add new string variable via environment overrides', async ({ page }) => {
// Create new Folder
await page.getByLabel('Create in collection').click();
await page.getByLabel('New Folder').click();
await page.locator('#prompt-input').fill('New Folder');
await page.getByText('New Folder').press('Enter');

// Open 'New folder' folder
const folderLocator = page.getByTestId('Dropdown-New-Folder');
const environmentLocator = page.getByRole('menuitemradio', { name: 'Environment' });
await folderLocator.click();
await environmentLocator.click();

// Add a new string environment variable
const expected = '{ "foo":"bar" }';
const editorTextLocator = await page.getByTestId('CodeEditor').getByRole('textbox');
const selectAllShortcut = process.platform === 'darwin' ? 'Meta+A' : 'Control+A';
await editorTextLocator.press(selectAllShortcut);
await editorTextLocator.fill(expected);

// Close and re-open modal
await page.getByText('Close').click();
await folderLocator.click();
await environmentLocator.click();

// Validate expected values persisted
const actualRows = await page.getByTestId('CodeEditor').locator('.CodeMirror-line').allInnerTexts();
expect(actualRows.length).toBeGreaterThan(0);

const actualJSON = JSON.parse(actualRows.join(' '));
expect(actualJSON).toEqual(JSON.parse(expected));
});

test('Add new string variable to an existing environment overrides folder', async ({ page }) => {
// Open 'Test Folder' folder
const folderLocator = page.getByTestId('Dropdown-test-folder');
const environmentLocator = page.getByRole('menuitemradio', { name: 'Environment' });
await folderLocator.click();
await environmentLocator.click();

// Add a new string environment variable to existing overrides

// 1. Retrieve current editor rows
const editorLocator = page.getByTestId('CodeEditor').locator('.CodeMirror-line');
const rows = await editorLocator.allInnerTexts();

// 2. Merge rows and convert to JSON
const editorJSON = JSON.parse(rows.join(' '));

// 3. Modify JSON with new string environment variable
editorJSON.REQUEST = 'HTTP';
const expected = editorJSON;

// 4. Apply new JSON to editor
const editorTextLocator = await page.getByTestId('CodeEditor').getByRole('textbox');
const selectAllShortcut = process.platform === 'darwin' ? 'Meta+A' : 'Control+A';
await editorTextLocator.press(selectAllShortcut);
await editorTextLocator.fill(JSON.stringify(expected));

// Close and re-open Modal
await page.getByText('Close').click();
await folderLocator.click();
await environmentLocator.click();

// Validate expected values persisted
const actualRows = await editorLocator.allInnerTexts();
expect(actualRows.length).toBeGreaterThan(0);

const actualJSON = JSON.parse(actualRows.join(' '));
expect(actualJSON).toEqual(expected);

});

// TODO: more scenarios will be added in follow-up iterations of increasing test coverage
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export interface CodeEditorProps {
noLint?: boolean;
noMatchBrackets?: boolean;
noStyleActiveLine?: boolean;
// used only for saving env editor state
// used only for saving env editor state, focusEvent doesn't work well
onBlur?: (e: FocusEvent) => void;
onChange?: (value: string) => void;
onPaste?: (value: string) => string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { type DropdownHandle, type DropdownProps } from '../base/dropdown';
import { Icon } from '../icon';
import { showError, showModal, showPrompt } from '../modals';
import { AskModal } from '../modals/ask-modal';
import { EnvironmentEditModal } from '../modals/environment-edit-modal';
import { PasteCurlModal } from '../modals/paste-curl-modal';
import { RequestGroupSettingsModal } from '../modals/request-group-settings-modal';
interface Props extends Partial<DropdownProps> {
Expand Down Expand Up @@ -226,12 +225,6 @@ export const RequestGroupActionsDropdown = ({
hint: hotKeyRegistry.request_createHTTP,
action: () => handleRequestGroupDuplicate(),
},
{
id: 'Environment',
name: 'Environment',
icon: 'code',
action: () => showModal(EnvironmentEditModal, { requestGroup }),
},
{
id: 'Rename',
name: 'Rename',
Expand Down

This file was deleted.

42 changes: 41 additions & 1 deletion packages/insomnia/src/ui/components/panes/request-group-pane.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC, useState } from 'react';
import React, { FC, useRef, useState } from 'react';
import { Tab, TabList, TabPanel, Tabs } from 'react-aria-components';
import { useRouteLoaderData } from 'react-router-dom';

Expand All @@ -8,6 +8,7 @@ import { useActiveRequestSyncVCSVersion, useGitVCSVersion } from '../../hooks/us
import { RequestGroupLoaderData } from '../../routes/request-group';
import { WorkspaceLoaderData } from '../../routes/workspace';
import { AuthWrapper } from '../editors/auth/auth-wrapper';
import { EnvironmentEditor, EnvironmentEditorHandle } from '../editors/environment-editor';
import { RequestHeadersEditor } from '../editors/request-headers-editor';
import { RequestScriptEditor } from '../editors/request-script-editor';
import { ErrorBoundary } from '../error-boundary';
Expand All @@ -25,7 +26,24 @@ export const RequestGroupPane: FC<{ settings: Settings }> = ({ settings }) => {
const uniqueKey = `${activeEnvironment?.modified}::${activeRequestGroup._id}::${gitVersion}::${activeRequestSyncVersion}`;
const folderHeaders = activeRequestGroup?.headers || [];
const headersCount = folderHeaders.filter(h => !h.disabled)?.length || 0;
const environmentEditorRef = useRef<EnvironmentEditorHandle>(null);
const patchGroup = useRequestGroupPatcher();

const saveChanges = () => {
if (environmentEditorRef.current?.isValid()) {
try {
const data = environmentEditorRef.current?.getValue();
if (activeRequestGroup && data) {
patchGroup(activeRequestGroup._id, {
environment: data.object,
environmentPropertyOrder: data.propertyOrder,
});
}
} catch (err) {
console.warn('Failed to update environment', err);
}
}
};
return (
<>
<Tabs aria-label='Request group tabs' className="flex-1 w-full h-full flex flex-col">
Expand Down Expand Up @@ -58,6 +76,12 @@ export const RequestGroupPane: FC<{ settings: Settings }> = ({ settings }) => {
</span>
)}
</Tab>
<Tab
className='flex-shrink-0 h-full flex items-center justify-between cursor-pointer gap-2 outline-none select-none px-3 py-1 text-[--hl] aria-selected:text-[--color-font] hover:bg-[--hl-sm] hover:text-[--color-font] aria-selected:bg-[--hl-xs] aria-selected:focus:bg-[--hl-sm] aria-selected:hover:bg-[--hl-sm] focus:bg-[--hl-sm] transition-colors duration-300'
id='environment'
>
Environment
</Tab>
<Tab
className='flex-shrink-0 h-full flex items-center justify-between cursor-pointer gap-2 outline-none select-none px-3 py-1 text-[--hl] aria-selected:text-[--color-font] hover:bg-[--hl-sm] hover:text-[--color-font] aria-selected:bg-[--hl-xs] aria-selected:focus:bg-[--hl-sm] aria-selected:hover:bg-[--hl-sm] focus:bg-[--hl-sm] transition-colors duration-300'
id='docs'
Expand Down Expand Up @@ -145,6 +169,22 @@ export const RequestGroupPane: FC<{ settings: Settings }> = ({ settings }) => {
</TabPanel>
</Tabs>
</TabPanel>
<TabPanel className='w-full flex-1 overflow-y-auto ' id='environment'>
<ErrorBoundary
key={uniqueKey}
errorClassName="font-error pad text-center"
>
<EnvironmentEditor
ref={environmentEditorRef}
key={activeRequestGroup ? activeRequestGroup._id : 'n/a'}
environmentInfo={{
object: activeRequestGroup ? activeRequestGroup.environment : {},
propertyOrder: activeRequestGroup && activeRequestGroup.environmentPropertyOrder,
}}
onBlur={saveChanges}
/>
</ErrorBoundary>
</TabPanel>
<TabPanel className='w-full flex-1 overflow-y-auto ' id='docs'>
{activeRequestGroup.description ? (
<div>
Expand Down
5 changes: 0 additions & 5 deletions packages/insomnia/src/ui/routes/modals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { AddKeyCombinationModal } from '../components/modals/add-key-combination
import { AlertModal } from '../components/modals/alert-modal';
import { AskModal } from '../components/modals/ask-modal';
import { CodePromptModal } from '../components/modals/code-prompt-modal';
import { EnvironmentEditModal } from '../components/modals/environment-edit-modal';
import { ErrorModal } from '../components/modals/error-modal';
import { FilterHelpModal } from '../components/modals/filter-help-modal';
import { GenerateCodeModal } from '../components/modals/generate-code-modal';
Expand Down Expand Up @@ -72,10 +71,6 @@ const Modals: FC = () => {
ref={instance => registerModal(instance, 'ResponseDebugModal')}
/>

<EnvironmentEditModal
ref={instance => registerModal(instance, 'EnvironmentEditModal')}
/>

<AddKeyCombinationModal
ref={instance => registerModal(instance, 'AddKeyCombinationModal')}
/>
Expand Down

0 comments on commit 8648f49

Please sign in to comment.