refactor: parse variables

This commit is contained in:
katherinehhh 2025-02-05 17:24:38 +08:00
parent a046e2a955
commit 431b16cdad
4 changed files with 47 additions and 11 deletions

View File

@ -100,6 +100,7 @@ export class Application {
public dataSourceManager: DataSourceManager;
public name: string;
public globalVars: Record<string, any> = {};
public globalVarsCtx: Record<string, any> = {};
loading = true;
maintained = false;
@ -484,6 +485,9 @@ export class Application {
addGlobalVar(key: string, value: any) {
set(this.globalVars, key, value);
}
setGlobalVarCtx(key: string, value: any) {
set(this.globalVarsCtx, key, value);
}
getGlobalVar(key) {
return get(this.globalVars, key);
@ -491,4 +495,7 @@ export class Application {
getGlobalVars() {
return this.globalVars;
}
getGlobalVarsCtx() {
return this.globalVarsCtx;
}
}

View File

@ -10,6 +10,7 @@
import { isFunction } from 'lodash';
import { useMemo } from 'react';
import { useApp } from './';
import { VariableOption } from '../../variables/types';
export const useGlobalVariable = (key: string) => {
const app = useApp();
@ -55,3 +56,19 @@ export const useGlobalVariables = () => {
return result as any;
};
//获取全局变量的值
export const useGlobalVariablesCtx = () => {
const app = useApp();
const variablesCtx = app.getGlobalVarsCtx();
const uniqueValues = new Set();
Object.entries(variablesCtx).forEach(([key, value]) => {
if (!value) return;
uniqueValues.add({
name: key,
ctx: value,
});
});
return [...uniqueValues] as VariableOption[];
};

View File

@ -223,15 +223,18 @@ const VariablesProvider = ({ children, filterVariables }: any) => {
[setCtx],
);
const getVariable = useCallback((variableName: string): VariableOption => {
if (!ctxRef.current[variableName]) {
return null;
}
const getVariable = useCallback(
(variableName: string): VariableOption => {
if (!ctxRef.current[variableName] && !builtinVariables.find((v) => v.name === variableName)) {
return null;
}
return {
...variablesStore[variableName],
};
}, []);
return {
...variablesStore[variableName],
};
},
[builtinVariables],
);
const removeVariable = useCallback(
(variableName: string) => {
@ -338,7 +341,6 @@ const VariablesProvider = ({ children, filterVariables }: any) => {
}) as VariablesContextType,
[getCollectionField, getVariable, parseVariable, registerVariable, removeVariable, setCtx],
);
return <VariablesContext.Provider value={value}>{children}</VariablesContext.Provider>;
};

View File

@ -15,6 +15,7 @@ import { useAPITokenVariable } from '../../schema-settings/VariableInput/hooks/u
import { useCurrentRoleVariable } from '../../schema-settings/VariableInput/hooks/useRoleVariable';
import { useURLSearchParamsVariable } from '../../schema-settings/VariableInput/hooks/useURLSearchParamsVariable';
import { VariableOption } from '../types';
import { useGlobalVariablesCtx } from '../../application/hooks/useGlobalVariable';
/**
*
@ -26,6 +27,7 @@ const useBuiltInVariables = () => {
const { apiTokenCtx } = useAPITokenVariable();
const { datetimeCtx } = useDatetimeVariable();
const { urlSearchParamsCtx, name: urlSearchParamsName, defaultValue } = useURLSearchParamsVariable();
const globalVariablesCtx = useGlobalVariablesCtx();
const builtinVariables: VariableOption[] = useMemo(() => {
return [
{
@ -88,9 +90,17 @@ const useBuiltInVariables = () => {
ctx: urlSearchParamsCtx,
defaultValue,
},
...globalVariablesCtx,
];
}, [currentRoleCtx, currentUserCtx, datetimeCtx, defaultValue, urlSearchParamsCtx, urlSearchParamsName]);
}, [
currentRoleCtx,
currentUserCtx,
datetimeCtx,
defaultValue,
urlSearchParamsCtx,
urlSearchParamsName,
globalVariablesCtx,
]);
return { builtinVariables };
};