Skip to content

Commit bdc4ab4

Browse files
committed
Merge branch 'main' of github.com:PROCEED-Labs/proceed into ms/user-task-data-access
2 parents 17d065b + 6b14e7d commit bdc4ab4

8 files changed

Lines changed: 379 additions & 65 deletions

File tree

src/management-system-v2/app/(dashboard)/[environmentId]/processes/[mode]/[processId]/flow-condition-modal.tsx

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import { Checkbox, Form, Input, Modal } from 'antd';
55
import useModelerStateStore from './use-modeler-state-store';
66
import { Editor, Monaco } from '@monaco-editor/react';
77
import * as monaco from 'monaco-editor';
8-
import languageExtension from './monaco-typescript-language-extension.js';
8+
import { getProceedLanguageExtension } from './monaco-typescript-language-extension';
99
import styles from './flow-condition-modal.module.scss';
1010
import cn from 'classnames';
11+
import useProcessVariables from './use-process-variables';
12+
import { typeTypescriptMap } from '@/lib/process-variable-schema';
1113

1214
export function isConditionalFlow(element?: Element) {
1315
return (
@@ -47,10 +49,13 @@ const FlowConditionModal: React.FC<FlowConditionModalProps> = ({
4749
}) => {
4850
const [description, setDescription] = useState('');
4951
const [isDefault, setIsDefault] = useState(false);
52+
const [initialized, setInitialized] = useState(false);
5053

5154
const monacoEditorRef = useRef<null | monaco.editor.IStandaloneCodeEditor>(null);
5255
const monacoRef = useRef<null | Monaco>(null);
5356

57+
const { variables } = useProcessVariables();
58+
5459
const modeler = useModelerStateStore((state) => state.modeler);
5560

5661
useEffect(() => {
@@ -66,24 +71,45 @@ const FlowConditionModal: React.FC<FlowConditionModalProps> = ({
6671
monacoRef.current = monaco;
6772

6873
const defaultOptions =
69-
monacoRef.current.languages.typescript.javascriptDefaults.getCompilerOptions();
74+
monacoRef.current.languages.typescript.typescriptDefaults.getCompilerOptions();
7075

71-
monacoRef.current.languages.typescript.javascriptDefaults.setCompilerOptions({
76+
monacoRef.current.languages.typescript.typescriptDefaults.setCompilerOptions({
7277
...defaultOptions,
7378
target: monacoRef.current.languages.typescript.ScriptTarget.ES2017,
79+
strictNullChecks: true,
7480
lib: ['es2017'],
7581
});
7682

7783
monacoEditorRef.current.setValue(getConditionString(element));
7884

79-
monacoRef.current.languages.typescript.javascriptDefaults.addExtraLib(languageExtension);
80-
monacoRef.current.editor.createModel(languageExtension, 'typescript');
85+
setInitialized(true);
8186

8287
editor.onKeyDown((e) => {
8388
if (e.keyCode == monaco.KeyCode.Enter) e.preventDefault();
8489
});
8590
};
8691

92+
useEffect(() => {
93+
if (initialized) {
94+
const languageExtension = getProceedLanguageExtension(
95+
variables.map((variable) => ({
96+
name: variable.name,
97+
type: typeTypescriptMap[variable.dataType],
98+
})),
99+
true,
100+
);
101+
102+
const lib =
103+
monacoRef.current!.languages.typescript.typescriptDefaults.addExtraLib(languageExtension);
104+
const model = monacoRef.current!.editor.createModel(languageExtension, 'typescript');
105+
106+
return () => {
107+
lib.dispose();
108+
model.dispose();
109+
};
110+
}
111+
}, [initialized, variables]);
112+
87113
const handleSubmit = () => {
88114
if (!modeler || !element) return;
89115

src/management-system-v2/app/(dashboard)/[environmentId]/processes/[mode]/[processId]/monaco-typescript-language-extension.js renamed to src/management-system-v2/app/(dashboard)/[environmentId]/processes/[mode]/[processId]/monaco-typescript-language-extension.ts

Lines changed: 70 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,89 @@
1-
export default `
2-
class Variable {
1+
export function getProceedLanguageExtension(
2+
variables: { name: string; type: string }[],
3+
writeVarsIntoGlobalScope = false,
4+
) {
5+
const hasVariables = variables.length;
6+
7+
const varNames = hasVariables
8+
? variables.map((variable) => `'${variable.name}'`).join(' | ')
9+
: 'never';
10+
11+
const withoutLogs = `{
12+
${variables.map((variable) => `'${variable.name}'?: ${variable.type},`).join('\n ')}
13+
}`;
14+
15+
const withLogs = `{
16+
${variables.map((variable) => `'${variable.name}'?: { value: ${variable.type}, log: { changedTime: number; changedBy?: string; oldValue?: ${variable.type}; }[] },`).join('\n ')}
17+
}`;
18+
19+
return `
20+
type __varNameTypeMap = {
21+
${variables.map((variable) => `'${variable.name}': ${variable.type},`).join('\n ')}
22+
};
23+
24+
type Variable = {
325
/**
4-
* Allows setting values for instance variables
26+
* ${hasVariables ? 'Allows setting values for instance variables' : 'Define a variable to be able to use this function'}
527
**/
6-
set(varName: string, value: any): void;
28+
set: ${hasVariables ? `<VariableName extends ${varNames}>(varName: VariableName, value: __varNameTypeMap[VariableName]) => void;` : 'never;'}
29+
// in theory it is possible to call set on a variable that is not predefined
30+
// but we prefer users to predefine all variables so we disable this type definition
31+
// set(varName: string, value: any): void;
732
833
/**
9-
* Returns value of an instance variable
34+
* ${hasVariables ? 'Returns value of an instance variable' : 'Define a variable to be able to use this function'}
1035
**/
11-
get(varName: string): any;
36+
get: ${hasVariables ? `<VariableName extends ${varNames}>(varName: VariableName) => __varNameTypeMap[VariableName] | undefined;` : 'never;'}
37+
// in theory it is possible to call get on a variable that is not predefined
38+
// but we prefer users to predefine all variables so we disable this type definition
39+
// get(varName: string): any;
1240
1341
/**
1442
* Returns all instance variables and their values
1543
**/
16-
getAll(): object;
44+
getAll: () => ${withoutLogs};
1745
18-
// TODO: jsdoc for these
19-
getWithLogs(): any;
46+
/**
47+
* Returns all instance variables with their values and information about change events for each variable
48+
**/
49+
getWithLogs: () => ${withLogs};
2050
21-
getProcess(name: string): any;
51+
/**
52+
* ${hasVariables ? 'Returns the value of an instance variable ignoring changes made earlier in this script' : 'Define a variable to be able to use this function'}
53+
**/
54+
getProcess: ${hasVariables ? `<VariableName extends ${varNames}>(name: VariableName) => __varNameTypeMap[VariableName] | undefined;` : 'never;'}
2255
23-
getAllProcess(): any;
56+
/**
57+
* Returns all instance variables and their values ignoring changes made earlier in this script
58+
**/
59+
getAllProcess: () => ${withoutLogs};
2460
25-
getWithLogsProcess(): any;
61+
/**
62+
* Returns all instance variables with their values and information about change events for each variable ignoring changes made earlier in this script
63+
**/
64+
getWithLogsProcess: () => ${withLogs};
2665
27-
setProcess(name: string, value: any);
66+
/**
67+
* ${hasVariables ? 'Allows setting values for instance variables directly in the instance state' : 'Define a variable to be able to use this function'}
68+
**/
69+
setProcess: ${hasVariables ? `<VariableName extends ${varNames}>(name: VariableName, value: __varNameTypeMap[VariableName]) => void;` : 'never;'}
2870
29-
setGlobal(varPath: string, value: any): void;
71+
setGlobal: (varPath: string, value: any) => void;
3072
31-
getGlobal(varPath: string);
73+
getGlobal: (varPath: string) => any;
3274
33-
getGlobalFull(varPath: string);
75+
getGlobalFull: (varPath: string) => any;
3476
35-
setGlobalOrg(varPath: string, value: any): void;
77+
setGlobalOrg: (varPath: string, value: any) => void;
3678
37-
getGlobalOrg(varPath: string);
79+
getGlobalOrg: (varPath: string) => any;
3880
39-
getGlobalOrgFull(varPath: string);
81+
getGlobalOrgFull: (varPath: string) => any;
4082
}
4183
declare var variable: Variable;
4284
85+
${writeVarsIntoGlobalScope ? variables.map((variable) => `declare var ${variable.name}: undefined | ${variable.type};`).join('\n') : ''}
86+
4387
declare class Log {
4488
trace(message?: any): void;
4589
debug(message?: any): void;
@@ -75,7 +119,7 @@ declare function setInterval(callback: () => any, timeout: number): number;
75119
/**
76120
* Cancel an interval
77121
**/
78-
declare function clearInterval(intervalId): boolean;
122+
declare function clearInterval(intervalId: number): boolean;
79123
80124
// declare class CapabilityService {
81125
// /**
@@ -233,7 +277,7 @@ type _Request = {
233277
// files?: { name: string; data: any };
234278
};
235279
236-
class _Response {
280+
type _Response = {
237281
status(code: number): _Response;
238282
send(body: any): void;
239283
}
@@ -264,16 +308,16 @@ declare class NetworkServer {
264308
getAsync(path: string): Promise<{ req: _Request; res: _Response }>;
265309
266310
/** Open a POST route listener */
267-
post(path: string, callback: ({ req: _Request, res: _Response }) => void): void;
311+
post(path: string, callback: ({ req, res }: { req: _Request, res: _Response }) => void): void;
268312
269313
/** Open a PUT route listener */
270-
put(path: string, callback: ({ req: _Request, res: _Response }) => void): void;
314+
put(path: string, callback: ({ req, res }: { req: _Request, res: _Response }) => void): void;
271315
272316
/** Open a DELETE route listener */
273-
delete(path: string, callback: ({ req: _Request, res: _Response }) => void): void;
317+
delete(path: string, callback: ({ req, res }: { req: _Request, res: _Response }) => void): void;
274318
275319
/** Open a GET route listener */
276-
get(path: string, callback: ({ req: _Request, res: _Response }) => void): void;
320+
get(path: string, callback: ({ req, res }: { req: _Request, res: _Response }) => void): void;
277321
278322
/**
279323
* Close the network server. This is necessary for the task script to stop, after a route handler
@@ -303,3 +347,4 @@ declare function wait(ms: number): void;
303347
/** Returns a promise that is resolved after the given amount of milliseconds */
304348
declare function waitAsync(ms: number): Promise<void>;
305349
`;
350+
}

0 commit comments

Comments
 (0)