mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-01-16 14:06:49 +01:00
* Update environment model to get ready for request/folder environments * Folder environments in UI * Folder environments working * Tweaks and fixes * Tweak environment encryption UX * Tweak environment encryption UX * Address comments * Update fn name * Add tsc back to lint rules * Update src-web/components/EnvironmentEditor.tsx * Merge remote-tracking branch 'origin/folder-environments' into folder…
53 lines
1.9 KiB
TypeScript
53 lines
1.9 KiB
TypeScript
import type { Environment, EnvironmentVariable } from '@yaakapp-internal/models';
|
|
import { foldersAtom } from '@yaakapp-internal/models';
|
|
import { useMemo } from 'react';
|
|
import { jotaiStore } from '../lib/jotai';
|
|
import { useActiveRequest } from './useActiveRequest';
|
|
import { useEnvironmentsBreakdown } from './useEnvironmentsBreakdown';
|
|
import { useParentFolders } from './useParentFolders';
|
|
|
|
export function useEnvironmentVariables(environmentId: string | null) {
|
|
const { baseEnvironment, folderEnvironments, subEnvironments } = useEnvironmentsBreakdown();
|
|
const activeEnvironment = subEnvironments.find((e) => e.id === environmentId) ?? null;
|
|
const activeRequest = useActiveRequest();
|
|
const parentFolders = useParentFolders(activeRequest);
|
|
|
|
return useMemo(() => {
|
|
const varMap: Record<string, WrappedEnvironmentVariable> = {};
|
|
const folderVariables = parentFolders.flatMap((f) =>
|
|
wrapVariables(folderEnvironments.find((fe) => fe.parentId === f.id) ?? null),
|
|
);
|
|
|
|
const allVariables = [
|
|
...folderVariables,
|
|
...wrapVariables(activeEnvironment),
|
|
...wrapVariables(baseEnvironment),
|
|
];
|
|
|
|
for (const v of allVariables) {
|
|
if (!v.variable.enabled || !v.variable.name || v.variable.name in varMap) {
|
|
continue;
|
|
}
|
|
varMap[v.variable.name] = v;
|
|
}
|
|
|
|
return Object.values(varMap);
|
|
}, [activeEnvironment, baseEnvironment, folderEnvironments, parentFolders]);
|
|
}
|
|
|
|
export interface WrappedEnvironmentVariable {
|
|
variable: EnvironmentVariable;
|
|
environment: Environment;
|
|
source: string;
|
|
}
|
|
|
|
function wrapVariables(e: Environment | null): WrappedEnvironmentVariable[] {
|
|
if (e == null) return [];
|
|
const folders = jotaiStore.get(foldersAtom);
|
|
return e.variables.map((v) => {
|
|
const folder = e.parentModel === 'folder' ? folders.find((f) => f.id === e.parentId) : null;
|
|
const source = folder?.name ?? e.name;
|
|
return { variable: v, environment: e, source };
|
|
});
|
|
}
|