mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-22 01:19:13 +01:00
Tackled remaining perf wins
This commit is contained in:
@@ -1,24 +1,26 @@
|
||||
import type { EnvironmentVariable } from '@yaakapp-internal/models';
|
||||
import { useMemo } from 'react';
|
||||
import { useActiveEnvironment } from './useActiveEnvironment';
|
||||
import { useEnvironments } from './useEnvironments';
|
||||
import { atom, useAtomValue } from 'jotai';
|
||||
import { activeEnvironmentAtom } from './useActiveEnvironment';
|
||||
import { environmentsBreakdownAtom } from './useEnvironments';
|
||||
|
||||
const activeEnvironmentVariablesAtom = atom((get) => {
|
||||
const { baseEnvironment } = get(environmentsBreakdownAtom);
|
||||
const activeEnvironment = get(activeEnvironmentAtom);
|
||||
|
||||
const varMap: Record<string, EnvironmentVariable> = {};
|
||||
const allVariables = [
|
||||
...(baseEnvironment?.variables ?? []),
|
||||
...(activeEnvironment?.variables ?? []),
|
||||
];
|
||||
|
||||
for (const v of allVariables) {
|
||||
if (!v.enabled || !v.name) continue;
|
||||
varMap[v.name] = v;
|
||||
}
|
||||
|
||||
return Object.values(varMap);
|
||||
});
|
||||
|
||||
export function useActiveEnvironmentVariables() {
|
||||
const { baseEnvironment } = useEnvironments();
|
||||
const [environment] = useActiveEnvironment();
|
||||
|
||||
const variables = useMemo(() => {
|
||||
const varMap: Record<string, EnvironmentVariable> = {};
|
||||
|
||||
const allVariables = [...(baseEnvironment?.variables ?? []), ...(environment?.variables ?? [])];
|
||||
|
||||
for (const v of allVariables) {
|
||||
if (!v.enabled || !v.name) continue;
|
||||
varMap[v.name] = v;
|
||||
}
|
||||
|
||||
return Object.values(varMap);
|
||||
}, [baseEnvironment?.variables, environment?.variables]);
|
||||
|
||||
return variables;
|
||||
return useAtomValue(activeEnvironmentVariablesAtom);
|
||||
}
|
||||
|
||||
@@ -4,11 +4,18 @@ import { atom } from 'jotai/index';
|
||||
|
||||
export const environmentsAtom = atom<Environment[]>([]);
|
||||
|
||||
export function useEnvironments() {
|
||||
const allEnvironments = useAtomValue(environmentsAtom);
|
||||
export const environmentsBreakdownAtom = atom<{
|
||||
baseEnvironment: Environment | null;
|
||||
allEnvironments: Environment[];
|
||||
subEnvironments: Environment[];
|
||||
}>((get) => {
|
||||
const allEnvironments = get(environmentsAtom);
|
||||
const baseEnvironment = allEnvironments.find((e) => e.environmentId == null) ?? null;
|
||||
const subEnvironments =
|
||||
allEnvironments.filter((e) => e.environmentId === (baseEnvironment?.id ?? 'n/a')) ?? [];
|
||||
|
||||
return { baseEnvironment, subEnvironments, allEnvironments } as const;
|
||||
});
|
||||
|
||||
export function useEnvironments() {
|
||||
return useAtomValue(environmentsBreakdownAtom);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user