Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(editor): Render dates correctly in parameter hint #9089

Merged
merged 2 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import InputTriple from '@/components/InputTriple/InputTriple.vue';
import ParameterInputFull from '@/components/ParameterInputFull.vue';
import ParameterInputHint from '@/components/ParameterInputHint.vue';
import ParameterIssues from '@/components/ParameterIssues.vue';
import { resolveParameter } from '@/composables/useWorkflowHelpers';
import { isExpression } from '@/utils/expressions';
import { isObject } from '@jsplumb/util';
import type { AssignmentValue, INodeProperties } from 'n8n-workflow';
import { useWorkflowHelpers } from '@/composables/useWorkflowHelpers';
import { isExpression, stringifyExpressionResult } from '@/utils/expressions';
import type { AssignmentValue, INodeProperties, Result } from 'n8n-workflow';
import { computed, ref } from 'vue';
import TypeSelect from './TypeSelect.vue';
import { useNDVStore } from '@/stores/ndv.store';
import { useI18n } from '@/composables/useI18n';
import { useRouter } from 'vue-router';

interface Props {
path: string;
Expand All @@ -32,7 +31,8 @@ const emit = defineEmits<{
}>();

const ndvStore = useNDVStore();
const i18n = useI18n();
const router = useRouter();
const { resolveExpression } = useWorkflowHelpers({ router });

const assignmentTypeToNodeProperty = (
type: string,
Expand All @@ -56,6 +56,21 @@ const assignmentTypeToNodeProperty = (
}
};

function getExpressionResult(value: string): Result<unknown, unknown> {
elsmr marked this conversation as resolved.
Show resolved Hide resolved
try {
const resolvedValue = resolveExpression(value, {
targetItem: ndvStore.hoveringItem ?? undefined,
inputNodeName: ndvStore.ndvInputNodeName,
inputRunIndex: ndvStore.ndvInputRunIndex,
inputBranchIndex: ndvStore.ndvInputBranchIndex,
}) as unknown;

return { ok: true, result: resolvedValue };
} catch (error) {
return { ok: false, error };
}
}

const nameParameter = computed<INodeProperties>(() => ({
name: 'name',
displayName: 'Name',
Expand All @@ -80,30 +95,7 @@ const hint = computed(() => {
if (typeof value !== 'string' || !value.startsWith('=')) {
return '';
}

try {
const resolvedValue = resolveParameter(value, {
targetItem: ndvStore.hoveringItem ?? undefined,
inputNodeName: ndvStore.ndvInputNodeName,
inputRunIndex: ndvStore.ndvInputRunIndex,
inputBranchIndex: ndvStore.ndvInputBranchIndex,
}) as unknown;

if (isObject(resolvedValue)) {
return JSON.stringify(resolvedValue);
}
if (typeof resolvedValue === 'boolean' || typeof resolvedValue === 'number') {
return resolvedValue.toString();
}

if (resolvedValue === '') {
return i18n.baseText('parameterInput.emptyString');
}

return resolvedValue as string;
} catch (error) {
return '';
}
return stringifyExpressionResult(getExpressionResult(value));
});

const highlightHint = computed(() =>
Expand Down
25 changes: 2 additions & 23 deletions packages/editor-ui/src/components/ParameterInputWrapper.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ import type { EventBus } from 'n8n-design-system/utils';
import { createEventBus } from 'n8n-design-system/utils';
import { useRouter } from 'vue-router';
import { useWorkflowHelpers } from '@/composables/useWorkflowHelpers';
import { getExpressionErrorMessage, getResolvableState } from '@/utils/expressions';
import { stringifyExpressionResult } from '@/utils/expressions';

export default defineComponent({
name: 'ParameterInputWrapper',
Expand Down Expand Up @@ -227,28 +227,7 @@ export default defineComponent({
return evaluated.ok ? evaluated.result : null;
},
evaluatedExpressionString(): string | null {
const evaluated = this.evaluatedExpression;

if (!evaluated.ok) {
if (getResolvableState(evaluated.error) !== 'invalid') {
return null;
}

return `[${this.$locale.baseText('parameterInput.error')}: ${getExpressionErrorMessage(
evaluated.error as Error,
)}]`;
}

if (evaluated.result === null) {
return null;
}

if (typeof evaluated.result === 'string' && evaluated.result.length === 0) {
return this.$locale.baseText('parameterInput.emptyString');
}
return typeof evaluated.result === 'string'
? evaluated.result
: JSON.stringify(evaluated.result);
return stringifyExpressionResult(this.evaluatedExpression);
},
expressionOutput(): string | null {
if (this.isValueExpression && this.evaluatedExpressionString) {
Expand Down
34 changes: 34 additions & 0 deletions packages/editor-ui/src/utils/__tests__/expressions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { ExpressionError } from 'n8n-workflow';
import { stringifyExpressionResult } from '../expressions';

describe('stringifyExpressionResult()', () => {
it('should return empty string for non-critical errors', () => {
expect(
stringifyExpressionResult({
ok: false,
error: new ExpressionError('error message', { type: 'no_execution_data' }),
}),
).toEqual('');
});

it('should return an error message for critical errors', () => {
expect(
stringifyExpressionResult({
ok: false,
error: new ExpressionError('error message', { type: 'no_input_connection' }),
}),
).toEqual('[ERROR: No input connected]');
});

it('should return empty string when result is null', () => {
expect(stringifyExpressionResult({ ok: true, result: null })).toEqual('');
});

it('should return [empty] message when result is empty string', () => {
expect(stringifyExpressionResult({ ok: true, result: '' })).toEqual('[empty]');
});

it('should return the result when it is a string', () => {
expect(stringifyExpressionResult({ ok: true, result: 'foo' })).toEqual('foo');
});
});
24 changes: 23 additions & 1 deletion packages/editor-ui/src/utils/expressions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ResolvableState } from '@/types/expressions';
import { ExpressionError, ExpressionParser } from 'n8n-workflow';
import { ExpressionError, ExpressionParser, type Result } from 'n8n-workflow';
import { i18n } from '@/plugins/i18n';
import { useWorkflowsStore } from '@/stores/workflows.store';

Expand Down Expand Up @@ -110,3 +110,25 @@ export const getExpressionErrorMessage = (error: Error): string => {

return error.message;
};

export const stringifyExpressionResult = (result: Result<unknown, unknown>): string => {
if (!result.ok) {
if (getResolvableState(result.error) !== 'invalid') {
return '';
}

return `[${i18n.baseText('parameterInput.error')}: ${getExpressionErrorMessage(
result.error as Error,
)}]`;
}

if (result.result === null) {
return '';
}

if (typeof result.result === 'string' && result.result.length === 0) {
return i18n.baseText('parameterInput.emptyString');
}

return typeof result.result === 'string' ? result.result : JSON.stringify(result.result);
};