mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-10 19:16:55 +02:00
Fixes for last commit
This commit is contained in:
@@ -1,16 +1,44 @@
|
||||
import type { Environment, EnvironmentVariable } from '@yaakapp-internal/models';
|
||||
import { updateModel } from '@yaakapp-internal/models';
|
||||
import { openFolderSettings } from '../commands/openFolderSettings';
|
||||
import type { PairEditorHandle } from '../components/core/PairEditor';
|
||||
import { ensurePairId } from '../components/core/PairEditor.util';
|
||||
import { EnvironmentEditDialog } from '../components/EnvironmentEditDialog';
|
||||
import { environmentsBreakdownAtom } from '../hooks/useEnvironmentsBreakdown';
|
||||
import { toggleDialog } from './dialog';
|
||||
import { jotaiStore } from './jotai';
|
||||
|
||||
interface Options {
|
||||
addOrFocusVariable?: EnvironmentVariable;
|
||||
}
|
||||
|
||||
export function editEnvironment(environment: Environment | null, options: Options = {}) {
|
||||
if (environment?.parentModel === 'folder' && environment.parentId != null) {
|
||||
openFolderSettings(environment.parentId, 'variables');
|
||||
export async function editEnvironment(
|
||||
initialEnvironment: Environment | null,
|
||||
options: Options = {},
|
||||
) {
|
||||
if (initialEnvironment?.parentModel === 'folder' && initialEnvironment.parentId != null) {
|
||||
openFolderSettings(initialEnvironment.parentId, 'variables');
|
||||
} else {
|
||||
const { addOrFocusVariable } = options;
|
||||
const { baseEnvironment } = jotaiStore.get(environmentsBreakdownAtom);
|
||||
let environment = initialEnvironment ?? baseEnvironment;
|
||||
let focusId: string | null = null;
|
||||
|
||||
if (addOrFocusVariable && environment != null) {
|
||||
const existing = environment.variables.find(
|
||||
(v) => v.id === addOrFocusVariable.id || v.name === addOrFocusVariable.name,
|
||||
);
|
||||
if (existing) {
|
||||
focusId = existing.id ?? null;
|
||||
} else {
|
||||
const newVar = ensurePairId(addOrFocusVariable);
|
||||
environment = { ...environment, variables: [...environment.variables, newVar] };
|
||||
await updateModel(environment);
|
||||
environment.variables.push(newVar);
|
||||
focusId = newVar.id;
|
||||
}
|
||||
}
|
||||
|
||||
toggleDialog({
|
||||
id: 'environment-editor',
|
||||
noPadding: true,
|
||||
@@ -19,7 +47,11 @@ export function editEnvironment(environment: Environment | null, options: Option
|
||||
render: () => (
|
||||
<EnvironmentEditDialog
|
||||
initialEnvironmentId={environment?.id ?? null}
|
||||
addOrFocusVariable={options.addOrFocusVariable}
|
||||
setRef={(pairEditor: PairEditorHandle | null) => {
|
||||
if (focusId) {
|
||||
pairEditor?.focusValue(focusId);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user