wip: Restructure twitter integration
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import qs from 'qs';
|
import qs from 'qs';
|
||||||
import { IGlobalVariableForConnection } from '../../../helpers/global-variable/connection';
|
import { IGlobalVariableForConnection } from '@automatisch/types';
|
||||||
|
|
||||||
const verifyCredentials = async ($: IGlobalVariableForConnection) => {
|
const verifyCredentials = async ($: IGlobalVariableForConnection) => {
|
||||||
const headers = {
|
const headers = {
|
||||||
|
@@ -7,7 +7,7 @@ export default class MyTweets {
|
|||||||
this.client = client;
|
this.client = client;
|
||||||
}
|
}
|
||||||
|
|
||||||
async run(lastInternalId: string) {
|
async run() {
|
||||||
return this.getTweets(lastInternalId);
|
return this.getTweets(lastInternalId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,8 +1,12 @@
|
|||||||
import { IJSONObject, IField } from '@automatisch/types';
|
import generateRequest from '../common/generate-request';
|
||||||
import oauthClient from '../common/oauth-client';
|
import {
|
||||||
|
IJSONObject,
|
||||||
|
IField,
|
||||||
|
IGlobalVariableForConnection,
|
||||||
|
} from '@automatisch/types';
|
||||||
import { URLSearchParams } from 'url';
|
import { URLSearchParams } from 'url';
|
||||||
|
|
||||||
export default async function createAuthData($: any) {
|
export default async function createAuthData($: IGlobalVariableForConnection) {
|
||||||
try {
|
try {
|
||||||
const oauthRedirectUrlField = $.app.fields.find(
|
const oauthRedirectUrlField = $.app.fields.find(
|
||||||
(field: IField) => field.key == 'oAuthRedirectUrl'
|
(field: IField) => field.key == 'oAuthRedirectUrl'
|
||||||
@@ -10,18 +14,10 @@ export default async function createAuthData($: any) {
|
|||||||
|
|
||||||
const callbackUrl = oauthRedirectUrlField.value;
|
const callbackUrl = oauthRedirectUrlField.value;
|
||||||
|
|
||||||
const requestData = {
|
const response = await generateRequest($, {
|
||||||
url: `${$.app.baseUrl}/oauth/request_token`,
|
requestPath: '/oauth/request_token',
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
data: { oauth_callback: callbackUrl },
|
data: { oauth_callback: callbackUrl },
|
||||||
};
|
|
||||||
|
|
||||||
const authHeader = oauthClient($).toHeader(
|
|
||||||
oauthClient($).authorize(requestData)
|
|
||||||
);
|
|
||||||
|
|
||||||
const response = await $.http.post(`/oauth/request_token`, null, {
|
|
||||||
headers: { ...authHeader },
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const responseData = Object.fromEntries(new URLSearchParams(response.data));
|
const responseData = Object.fromEntries(new URLSearchParams(response.data));
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
|
import { IGlobalVariableForConnection } from '@automatisch/types';
|
||||||
import getCurrentUser from '../common/get-current-user';
|
import getCurrentUser from '../common/get-current-user';
|
||||||
|
|
||||||
const isStillVerified = async ($: any) => {
|
const isStillVerified = async ($: IGlobalVariableForConnection) => {
|
||||||
try {
|
try {
|
||||||
await getCurrentUser($);
|
await getCurrentUser($);
|
||||||
return true;
|
return true;
|
||||||
|
@@ -1,7 +1,10 @@
|
|||||||
const verifyCredentials = async ($: any) => {
|
import { IGlobalVariableForConnection } from '@automatisch/types';
|
||||||
|
import { URLSearchParams } from 'url';
|
||||||
|
|
||||||
|
const verifyCredentials = async ($: IGlobalVariableForConnection) => {
|
||||||
try {
|
try {
|
||||||
const response = await $.http.post(
|
const response = await $.http.post(
|
||||||
`/oauth/access_token?oauth_verifier=${$.auth.oauthVerifier}&oauth_token=${$.auth.accessToken}`,
|
`/oauth/access_token?oauth_verifier=${$.auth.data.oauthVerifier}&oauth_token=${$.auth.data.accessToken}`,
|
||||||
null
|
null
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@@ -0,0 +1,39 @@
|
|||||||
|
import { IGlobalVariableForConnection, IJSONObject } from '@automatisch/types';
|
||||||
|
import oauthClient from './oauth-client';
|
||||||
|
import { Token } from 'oauth-1.0a';
|
||||||
|
|
||||||
|
type IGenereateRequestOptons = {
|
||||||
|
requestPath: string;
|
||||||
|
method: string;
|
||||||
|
data?: IJSONObject;
|
||||||
|
};
|
||||||
|
|
||||||
|
const generateRequest = async (
|
||||||
|
$: IGlobalVariableForConnection,
|
||||||
|
options: IGenereateRequestOptons
|
||||||
|
) => {
|
||||||
|
const { requestPath, method, data } = options;
|
||||||
|
|
||||||
|
const token: Token = {
|
||||||
|
key: $.auth.data.accessToken as string,
|
||||||
|
secret: $.auth.data.accessSecret as string,
|
||||||
|
};
|
||||||
|
|
||||||
|
const requestData = {
|
||||||
|
url: `${$.app.baseUrl}${requestPath}`,
|
||||||
|
method,
|
||||||
|
data,
|
||||||
|
};
|
||||||
|
|
||||||
|
const authHeader = oauthClient($).toHeader(
|
||||||
|
oauthClient($).authorize(requestData, token)
|
||||||
|
);
|
||||||
|
|
||||||
|
const response = await $.http.post(`/oauth/request_token`, null, {
|
||||||
|
headers: { ...authHeader },
|
||||||
|
});
|
||||||
|
|
||||||
|
return response;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default generateRequest;
|
@@ -0,0 +1,14 @@
|
|||||||
|
import { IGlobalVariableForConnection } from '@automatisch/types';
|
||||||
|
import generateRequest from './generate-request';
|
||||||
|
|
||||||
|
const getCurrentUser = async ($: IGlobalVariableForConnection) => {
|
||||||
|
const response = await generateRequest($, {
|
||||||
|
requestPath: '/2/users/me',
|
||||||
|
method: 'GET',
|
||||||
|
});
|
||||||
|
|
||||||
|
const currentUser = response.data.data;
|
||||||
|
return currentUser;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default getCurrentUser;
|
@@ -0,0 +1,25 @@
|
|||||||
|
import { IGlobalVariableForConnection, IJSONObject } from '@automatisch/types';
|
||||||
|
import generateRequest from './generate-request';
|
||||||
|
|
||||||
|
const getUserByUsername = async (
|
||||||
|
$: IGlobalVariableForConnection,
|
||||||
|
username: string
|
||||||
|
) => {
|
||||||
|
const response = await generateRequest($, {
|
||||||
|
requestPath: `/2/users/by/username/${username}`,
|
||||||
|
method: 'GET',
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.data.errors) {
|
||||||
|
const errorMessages = response.data.errors
|
||||||
|
.map((error: IJSONObject) => error.detail)
|
||||||
|
.join(' ');
|
||||||
|
|
||||||
|
throw new Error(`Error occured while fetching user data: ${errorMessages}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const user = response.data.data;
|
||||||
|
return user;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default getUserByUsername;
|
54
packages/backend/src/apps/twitter2/common/get-user-tweets.ts
Normal file
54
packages/backend/src/apps/twitter2/common/get-user-tweets.ts
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
import { IGlobalVariableForConnection, IJSONObject } from '@automatisch/types';
|
||||||
|
import { URLSearchParams } from 'url';
|
||||||
|
import omitBy from 'lodash/omitBy';
|
||||||
|
import isEmpty from 'lodash/isEmpty';
|
||||||
|
import generateRequest from './generate-request';
|
||||||
|
|
||||||
|
const getUserTweets = async (
|
||||||
|
$: IGlobalVariableForConnection,
|
||||||
|
userId: string,
|
||||||
|
lastInternalId?: string
|
||||||
|
) => {
|
||||||
|
let response;
|
||||||
|
const tweets: IJSONObject[] = [];
|
||||||
|
|
||||||
|
do {
|
||||||
|
const params: IJSONObject = {
|
||||||
|
since_id: lastInternalId,
|
||||||
|
pagination_token: response?.data?.meta?.next_token,
|
||||||
|
};
|
||||||
|
|
||||||
|
const queryParams = new URLSearchParams(omitBy(params, isEmpty));
|
||||||
|
|
||||||
|
const requestPath = `/2/users/${userId}/tweets${
|
||||||
|
queryParams.toString() ? `?${queryParams.toString()}` : ''
|
||||||
|
}`;
|
||||||
|
|
||||||
|
response = await generateRequest($, {
|
||||||
|
requestPath,
|
||||||
|
method: 'GET',
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.data.meta.result_count > 0) {
|
||||||
|
response.data.data.forEach((tweet: IJSONObject) => {
|
||||||
|
if (!lastInternalId || Number(tweet.id) > Number(lastInternalId)) {
|
||||||
|
tweets.push(tweet);
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} while (response.data.meta.next_token && lastInternalId);
|
||||||
|
|
||||||
|
if (response.data.errors) {
|
||||||
|
const errorMessages = response.data.errors
|
||||||
|
.map((error: IJSONObject) => error.detail)
|
||||||
|
.join(' ');
|
||||||
|
|
||||||
|
throw new Error(`Error occured while fetching user data: ${errorMessages}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return tweets;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default getUserTweets;
|
@@ -1,10 +1,11 @@
|
|||||||
|
import { IGlobalVariableForConnection } from '@automatisch/types';
|
||||||
import crypto from 'crypto';
|
import crypto from 'crypto';
|
||||||
import OAuth from 'oauth-1.0a';
|
import OAuth from 'oauth-1.0a';
|
||||||
|
|
||||||
const oauthClient = ($: any) => {
|
const oauthClient = ($: IGlobalVariableForConnection) => {
|
||||||
const consumerData = {
|
const consumerData = {
|
||||||
key: $.auth.consumerKey as string,
|
key: $.auth.data.consumerKey as string,
|
||||||
secret: $.auth.consumerSecret as string,
|
secret: $.auth.data.consumerSecret as string,
|
||||||
};
|
};
|
||||||
|
|
||||||
return new OAuth({
|
return new OAuth({
|
@@ -0,0 +1,37 @@
|
|||||||
|
import { IGlobalVariableForConnection } from '@automatisch/types';
|
||||||
|
import getCurrentUser from '../../common/get-current-user';
|
||||||
|
import getUserByUsername from '../../common/get-user-by-username';
|
||||||
|
import getUserTweets from '../../common/get-user-tweets';
|
||||||
|
|
||||||
|
export default {
|
||||||
|
name: 'My Tweets',
|
||||||
|
key: 'myTweets',
|
||||||
|
pollInterval: 15,
|
||||||
|
description: 'Will be triggered when you tweet something new.',
|
||||||
|
substeps: [
|
||||||
|
{
|
||||||
|
key: 'chooseConnection',
|
||||||
|
name: 'Choose connection',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'testStep',
|
||||||
|
name: 'Test trigger',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
|
||||||
|
async run($: IGlobalVariableForConnection) {
|
||||||
|
return this.getTweets($, await $.db.flow.lastInternalId());
|
||||||
|
},
|
||||||
|
|
||||||
|
async testRun($: IGlobalVariableForConnection) {
|
||||||
|
return this.getTweets($);
|
||||||
|
},
|
||||||
|
|
||||||
|
async getTweets($: IGlobalVariableForConnection, lastInternalId?: string) {
|
||||||
|
const { username } = await getCurrentUser($);
|
||||||
|
const user = await getUserByUsername($, username);
|
||||||
|
|
||||||
|
const tweets = await getUserTweets($, user.id, lastInternalId);
|
||||||
|
return tweets;
|
||||||
|
},
|
||||||
|
};
|
@@ -1,5 +1,6 @@
|
|||||||
import createHttpClient from '../http-client';
|
import createHttpClient from '../http-client';
|
||||||
import Connection from '../../models/connection';
|
import Connection from '../../models/connection';
|
||||||
|
import Flow from '../../models/flow';
|
||||||
import {
|
import {
|
||||||
IJSONObject,
|
IJSONObject,
|
||||||
IApp,
|
IApp,
|
||||||
@@ -8,7 +9,8 @@ import {
|
|||||||
|
|
||||||
const prepareGlobalVariableForConnection = (
|
const prepareGlobalVariableForConnection = (
|
||||||
connection: Connection,
|
connection: Connection,
|
||||||
appData: IApp
|
appData: IApp,
|
||||||
|
flow?: Flow
|
||||||
): IGlobalVariableForConnection => {
|
): IGlobalVariableForConnection => {
|
||||||
return {
|
return {
|
||||||
auth: {
|
auth: {
|
||||||
@@ -24,6 +26,9 @@ const prepareGlobalVariableForConnection = (
|
|||||||
},
|
},
|
||||||
app: appData,
|
app: appData,
|
||||||
http: createHttpClient({ baseURL: appData.baseUrl }),
|
http: createHttpClient({ baseURL: appData.baseUrl }),
|
||||||
|
db: {
|
||||||
|
flow: flow,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -3,14 +3,15 @@ import Base from './base';
|
|||||||
import Execution from './execution';
|
import Execution from './execution';
|
||||||
import Step from './step';
|
import Step from './step';
|
||||||
import Telemetry from '../helpers/telemetry';
|
import Telemetry from '../helpers/telemetry';
|
||||||
|
import { IJSONObject } from '@automatisch/types';
|
||||||
|
|
||||||
class ExecutionStep extends Base {
|
class ExecutionStep extends Base {
|
||||||
id!: string;
|
id!: string;
|
||||||
executionId!: string;
|
executionId!: string;
|
||||||
stepId!: string;
|
stepId!: string;
|
||||||
dataIn!: Record<string, unknown>;
|
dataIn!: IJSONObject;
|
||||||
dataOut!: Record<string, unknown>;
|
dataOut!: IJSONObject;
|
||||||
errorDetails: Record<string, unknown>;
|
errorDetails: IJSONObject;
|
||||||
status = 'failure';
|
status = 'failure';
|
||||||
step: Step;
|
step: Step;
|
||||||
|
|
||||||
@@ -23,7 +24,7 @@ class ExecutionStep extends Base {
|
|||||||
id: { type: 'string', format: 'uuid' },
|
id: { type: 'string', format: 'uuid' },
|
||||||
executionId: { type: 'string', format: 'uuid' },
|
executionId: { type: 'string', format: 'uuid' },
|
||||||
stepId: { type: 'string' },
|
stepId: { type: 'string' },
|
||||||
dataIn: { type: 'object' },
|
dataIn: { type: ['object', 'null'] },
|
||||||
dataOut: { type: ['object', 'null'] },
|
dataOut: { type: ['object', 'null'] },
|
||||||
status: { type: 'string', enum: ['success', 'failure'] },
|
status: { type: 'string', enum: ['success', 'failure'] },
|
||||||
errorDetails: { type: ['object', 'null'] },
|
errorDetails: { type: ['object', 'null'] },
|
||||||
|
@@ -10,7 +10,7 @@ class Flow extends Base {
|
|||||||
name!: string;
|
name!: string;
|
||||||
userId!: string;
|
userId!: string;
|
||||||
active: boolean;
|
active: boolean;
|
||||||
steps?: [Step];
|
steps: Step[];
|
||||||
published_at: string;
|
published_at: string;
|
||||||
|
|
||||||
static tableName = 'flows';
|
static tableName = 'flows';
|
||||||
|
@@ -20,7 +20,7 @@ class Step extends Base {
|
|||||||
parameters: Record<string, unknown>;
|
parameters: Record<string, unknown>;
|
||||||
connection?: Connection;
|
connection?: Connection;
|
||||||
flow: Flow;
|
flow: Flow;
|
||||||
executionSteps?: [ExecutionStep];
|
executionSteps: ExecutionStep[];
|
||||||
|
|
||||||
static tableName = 'steps';
|
static tableName = 'steps';
|
||||||
|
|
||||||
|
@@ -10,9 +10,9 @@ class User extends Base {
|
|||||||
id!: string;
|
id!: string;
|
||||||
email!: string;
|
email!: string;
|
||||||
password!: string;
|
password!: string;
|
||||||
connections?: [Connection];
|
connections?: Connection[];
|
||||||
flows?: [Flow];
|
flows?: Flow[];
|
||||||
steps?: [Step];
|
steps?: Step[];
|
||||||
|
|
||||||
static tableName = 'users';
|
static tableName = 'users';
|
||||||
|
|
||||||
|
@@ -112,8 +112,8 @@ class Processor {
|
|||||||
await execution.$relatedQuery('executionSteps').insertAndFetch({
|
await execution.$relatedQuery('executionSteps').insertAndFetch({
|
||||||
stepId: id,
|
stepId: id,
|
||||||
status: 'failure',
|
status: 'failure',
|
||||||
dataIn: computedParameters,
|
dataIn: null,
|
||||||
dataOut: null,
|
dataOut: computedParameters,
|
||||||
errorDetails: fetchedActionData.error,
|
errorDetails: fetchedActionData.error,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
15
packages/types/index.d.ts
vendored
15
packages/types/index.d.ts
vendored
@@ -47,21 +47,21 @@ export interface IExecution {
|
|||||||
|
|
||||||
export interface IStep {
|
export interface IStep {
|
||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name?: string;
|
||||||
flowId: string;
|
flowId: string;
|
||||||
key: string;
|
key?: string;
|
||||||
appKey: string;
|
appKey?: string;
|
||||||
iconUrl: string;
|
iconUrl: string;
|
||||||
type: 'action' | 'trigger';
|
type: 'action' | 'trigger';
|
||||||
connectionId: string;
|
connectionId?: string;
|
||||||
status: string;
|
status: string;
|
||||||
position: number;
|
position: number;
|
||||||
parameters: Record<string, unknown>;
|
parameters: Record<string, unknown>;
|
||||||
connection: Partial<IConnection>;
|
connection?: Partial<IConnection>;
|
||||||
flow: IFlow;
|
flow: IFlow;
|
||||||
executionSteps: IExecutionStep[];
|
executionSteps: IExecutionStep[];
|
||||||
// FIXME: remove this property once execution steps are properly exposed via queries
|
// FIXME: remove this property once execution steps are properly exposed via queries
|
||||||
output: IJSONObject;
|
output?: IJSONObject;
|
||||||
appData?: IApp;
|
appData?: IApp;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -202,6 +202,9 @@ export type IGlobalVariableForConnection = {
|
|||||||
};
|
};
|
||||||
app: IApp;
|
app: IApp;
|
||||||
http: IHttpClient;
|
http: IHttpClient;
|
||||||
|
db: {
|
||||||
|
flow: IFlow;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
declare module 'axios' {
|
declare module 'axios' {
|
||||||
|
@@ -15,7 +15,7 @@ import FlowSubstepTitle from 'components/FlowSubstepTitle';
|
|||||||
import type { IApp, IStep, ISubstep } from '@automatisch/types';
|
import type { IApp, IStep, ISubstep } from '@automatisch/types';
|
||||||
|
|
||||||
type ChooseAppAndEventSubstepProps = {
|
type ChooseAppAndEventSubstepProps = {
|
||||||
substep: ISubstep,
|
substep: ISubstep;
|
||||||
expanded?: boolean;
|
expanded?: boolean;
|
||||||
onExpand: () => void;
|
onExpand: () => void;
|
||||||
onCollapse: () => void;
|
onCollapse: () => void;
|
||||||
@@ -24,14 +24,17 @@ type ChooseAppAndEventSubstepProps = {
|
|||||||
step: IStep;
|
step: IStep;
|
||||||
};
|
};
|
||||||
|
|
||||||
const optionGenerator = (app: IApp): { label: string; value: string; } => ({
|
const optionGenerator = (app: IApp): { label: string; value: string } => ({
|
||||||
label: app.name as string,
|
label: app.name as string,
|
||||||
value: app.key as string,
|
value: app.key as string,
|
||||||
});
|
});
|
||||||
|
|
||||||
const getOption = (options: Record<string, unknown>[], appKey: IApp["key"]) => options.find(option => option.value === appKey as string) || null;
|
const getOption = (options: Record<string, unknown>[], appKey?: IApp['key']) =>
|
||||||
|
options.find((option) => option.value === (appKey as string)) || null;
|
||||||
|
|
||||||
function ChooseAppAndEventSubstep(props: ChooseAppAndEventSubstepProps): React.ReactElement {
|
function ChooseAppAndEventSubstep(
|
||||||
|
props: ChooseAppAndEventSubstepProps
|
||||||
|
): React.ReactElement {
|
||||||
const {
|
const {
|
||||||
substep,
|
substep,
|
||||||
expanded = false,
|
expanded = false,
|
||||||
@@ -47,59 +50,74 @@ function ChooseAppAndEventSubstep(props: ChooseAppAndEventSubstepProps): React.R
|
|||||||
|
|
||||||
const isTrigger = step.type === 'trigger';
|
const isTrigger = step.type === 'trigger';
|
||||||
|
|
||||||
const { data } = useQuery(GET_APPS, { variables: { onlyWithTriggers: isTrigger }});
|
const { data } = useQuery(GET_APPS, {
|
||||||
|
variables: { onlyWithTriggers: isTrigger },
|
||||||
|
});
|
||||||
const apps: IApp[] = data?.getApps;
|
const apps: IApp[] = data?.getApps;
|
||||||
const app = apps?.find((currentApp: IApp) => currentApp.key === step.appKey);
|
const app = apps?.find((currentApp: IApp) => currentApp.key === step.appKey);
|
||||||
|
|
||||||
const appOptions = React.useMemo(() => apps?.map((app) => optionGenerator(app)), [apps]);
|
const appOptions = React.useMemo(
|
||||||
|
() => apps?.map((app) => optionGenerator(app)),
|
||||||
|
[apps]
|
||||||
|
);
|
||||||
const actionsOrTriggers = isTrigger ? app?.triggers : app?.actions;
|
const actionsOrTriggers = isTrigger ? app?.triggers : app?.actions;
|
||||||
const actionOptions = React.useMemo(() => actionsOrTriggers?.map((trigger) => optionGenerator(trigger)) ?? [], [app?.key]);
|
const actionOptions = React.useMemo(
|
||||||
const selectedActionOrTrigger = actionsOrTriggers?.find((actionOrTrigger) => actionOrTrigger.key === step?.key) || null;
|
() => actionsOrTriggers?.map((trigger) => optionGenerator(trigger)) ?? [],
|
||||||
|
[app?.key]
|
||||||
|
);
|
||||||
|
const selectedActionOrTrigger =
|
||||||
|
actionsOrTriggers?.find(
|
||||||
|
(actionOrTrigger) => actionOrTrigger.key === step?.key
|
||||||
|
) || null;
|
||||||
|
|
||||||
const {
|
const { name } = substep;
|
||||||
name,
|
|
||||||
} = substep;
|
|
||||||
|
|
||||||
const valid: boolean = !!step.key && !!step.appKey;
|
const valid: boolean = !!step.key && !!step.appKey;
|
||||||
|
|
||||||
// placeholders
|
// placeholders
|
||||||
const onEventChange = React.useCallback((event: React.SyntheticEvent, selectedOption: unknown) => {
|
const onEventChange = React.useCallback(
|
||||||
if (typeof selectedOption === 'object') {
|
(event: React.SyntheticEvent, selectedOption: unknown) => {
|
||||||
// TODO: try to simplify type casting below.
|
if (typeof selectedOption === 'object') {
|
||||||
const typedSelectedOption = selectedOption as { value: string };
|
// TODO: try to simplify type casting below.
|
||||||
const option: { value: string } = typedSelectedOption;
|
const typedSelectedOption = selectedOption as { value: string };
|
||||||
const eventKey = option?.value as string;
|
const option: { value: string } = typedSelectedOption;
|
||||||
|
const eventKey = option?.value as string;
|
||||||
|
|
||||||
if (step.key !== eventKey) {
|
if (step.key !== eventKey) {
|
||||||
onChange({
|
onChange({
|
||||||
step: {
|
step: {
|
||||||
...step,
|
...step,
|
||||||
key: eventKey,
|
key: eventKey,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}, [step, onChange]);
|
[step, onChange]
|
||||||
|
);
|
||||||
|
|
||||||
const onAppChange = React.useCallback((event: React.SyntheticEvent, selectedOption: unknown) => {
|
const onAppChange = React.useCallback(
|
||||||
if (typeof selectedOption === 'object') {
|
(event: React.SyntheticEvent, selectedOption: unknown) => {
|
||||||
// TODO: try to simplify type casting below.
|
if (typeof selectedOption === 'object') {
|
||||||
const typedSelectedOption = selectedOption as { value: string };
|
// TODO: try to simplify type casting below.
|
||||||
const option: { value: string } = typedSelectedOption;
|
const typedSelectedOption = selectedOption as { value: string };
|
||||||
const appKey = option?.value as string;
|
const option: { value: string } = typedSelectedOption;
|
||||||
|
const appKey = option?.value as string;
|
||||||
|
|
||||||
if (step.appKey !== appKey) {
|
if (step.appKey !== appKey) {
|
||||||
onChange({
|
onChange({
|
||||||
step: {
|
step: {
|
||||||
...step,
|
...step,
|
||||||
key: '',
|
key: '',
|
||||||
appKey,
|
appKey,
|
||||||
parameters: {},
|
parameters: {},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}, [step, onChange]);
|
[step, onChange]
|
||||||
|
);
|
||||||
|
|
||||||
const onToggle = expanded ? onCollapse : onExpand;
|
const onToggle = expanded ? onCollapse : onExpand;
|
||||||
|
|
||||||
@@ -112,14 +130,26 @@ function ChooseAppAndEventSubstep(props: ChooseAppAndEventSubstepProps): React.R
|
|||||||
valid={valid}
|
valid={valid}
|
||||||
/>
|
/>
|
||||||
<Collapse in={expanded} timeout="auto" unmountOnExit>
|
<Collapse in={expanded} timeout="auto" unmountOnExit>
|
||||||
<ListItem sx={{ pt: 2, pb: 3, flexDirection: 'column', alignItems: 'flex-start' }}>
|
<ListItem
|
||||||
|
sx={{
|
||||||
|
pt: 2,
|
||||||
|
pb: 3,
|
||||||
|
flexDirection: 'column',
|
||||||
|
alignItems: 'flex-start',
|
||||||
|
}}
|
||||||
|
>
|
||||||
<Autocomplete
|
<Autocomplete
|
||||||
fullWidth
|
fullWidth
|
||||||
disablePortal
|
disablePortal
|
||||||
disableClearable
|
disableClearable
|
||||||
disabled={editorContext.readOnly}
|
disabled={editorContext.readOnly}
|
||||||
options={appOptions}
|
options={appOptions}
|
||||||
renderInput={(params) => <TextField {...params} label={formatMessage('flowEditor.chooseApp')} />}
|
renderInput={(params) => (
|
||||||
|
<TextField
|
||||||
|
{...params}
|
||||||
|
label={formatMessage('flowEditor.chooseApp')}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
value={getOption(appOptions, step.appKey)}
|
value={getOption(appOptions, step.appKey)}
|
||||||
onChange={onAppChange}
|
onChange={onAppChange}
|
||||||
/>
|
/>
|
||||||
@@ -137,7 +167,12 @@ function ChooseAppAndEventSubstep(props: ChooseAppAndEventSubstepProps): React.R
|
|||||||
disableClearable
|
disableClearable
|
||||||
disabled={editorContext.readOnly}
|
disabled={editorContext.readOnly}
|
||||||
options={actionOptions}
|
options={actionOptions}
|
||||||
renderInput={(params) => <TextField {...params} label={formatMessage('flowEditor.chooseEvent')} />}
|
renderInput={(params) => (
|
||||||
|
<TextField
|
||||||
|
{...params}
|
||||||
|
label={formatMessage('flowEditor.chooseEvent')}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
value={getOption(actionOptions, step.key)}
|
value={getOption(actionOptions, step.key)}
|
||||||
onChange={onEventChange}
|
onChange={onEventChange}
|
||||||
/>
|
/>
|
||||||
@@ -147,7 +182,9 @@ function ChooseAppAndEventSubstep(props: ChooseAppAndEventSubstepProps): React.R
|
|||||||
{isTrigger && selectedActionOrTrigger?.pollInterval && (
|
{isTrigger && selectedActionOrTrigger?.pollInterval && (
|
||||||
<TextField
|
<TextField
|
||||||
label={formatMessage('flowEditor.pollIntervalLabel')}
|
label={formatMessage('flowEditor.pollIntervalLabel')}
|
||||||
value={formatMessage('flowEditor.pollIntervalValue', { minutes: selectedActionOrTrigger.pollInterval })}
|
value={formatMessage('flowEditor.pollIntervalValue', {
|
||||||
|
minutes: selectedActionOrTrigger.pollInterval,
|
||||||
|
})}
|
||||||
sx={{ mt: 2 }}
|
sx={{ mt: 2 }}
|
||||||
fullWidth
|
fullWidth
|
||||||
disabled
|
disabled
|
||||||
|
@@ -24,13 +24,10 @@ const LIST_HEIGHT = 256;
|
|||||||
|
|
||||||
const getPartialArray = (array: any[], length = array.length) => {
|
const getPartialArray = (array: any[], length = array.length) => {
|
||||||
return array.slice(0, length);
|
return array.slice(0, length);
|
||||||
}
|
};
|
||||||
|
|
||||||
const Suggestions = (props: SuggestionsProps) => {
|
const Suggestions = (props: SuggestionsProps) => {
|
||||||
const {
|
const { data, onSuggestionClick = () => null } = props;
|
||||||
data,
|
|
||||||
onSuggestionClick = () => null,
|
|
||||||
} = props;
|
|
||||||
const [current, setCurrent] = React.useState<number | null>(0);
|
const [current, setCurrent] = React.useState<number | null>(0);
|
||||||
const [listLength, setListLength] = React.useState<number>(SHORT_LIST_LENGTH);
|
const [listLength, setListLength] = React.useState<number>(SHORT_LIST_LENGTH);
|
||||||
|
|
||||||
@@ -40,41 +37,43 @@ const Suggestions = (props: SuggestionsProps) => {
|
|||||||
|
|
||||||
const collapseList = () => {
|
const collapseList = () => {
|
||||||
setListLength(SHORT_LIST_LENGTH);
|
setListLength(SHORT_LIST_LENGTH);
|
||||||
}
|
};
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
setListLength(SHORT_LIST_LENGTH);
|
setListLength(SHORT_LIST_LENGTH);
|
||||||
}, [current])
|
}, [current]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Paper
|
<Paper elevation={5} sx={{ width: '100%' }}>
|
||||||
elevation={5}
|
<Typography variant="subtitle2" sx={{ p: 2 }}>
|
||||||
sx={{ width: '100%' }}
|
Variables
|
||||||
>
|
</Typography>
|
||||||
<Typography variant="subtitle2" sx={{ p: 2, }}>Variables</Typography>
|
<List disablePadding>
|
||||||
<List
|
|
||||||
disablePadding
|
|
||||||
>
|
|
||||||
{data.map((option: IStep, index: number) => (
|
{data.map((option: IStep, index: number) => (
|
||||||
<>
|
<>
|
||||||
<ListItemButton
|
<ListItemButton
|
||||||
divider
|
divider
|
||||||
onClick={() => setCurrent((currentIndex) => currentIndex === index ? null : index)}
|
onClick={() =>
|
||||||
sx={{ py: 0.5, }}
|
setCurrent((currentIndex) =>
|
||||||
|
currentIndex === index ? null : index
|
||||||
|
)
|
||||||
|
}
|
||||||
|
sx={{ py: 0.5 }}
|
||||||
>
|
>
|
||||||
<ListItemText
|
<ListItemText primary={option.name} />
|
||||||
primary={option.name}
|
|
||||||
/>
|
|
||||||
|
|
||||||
{!!option.output?.length && (
|
{!!option.output?.length &&
|
||||||
current === index ? <ExpandLess /> : <ExpandMore />
|
(current === index ? <ExpandLess /> : <ExpandMore />)}
|
||||||
)}
|
|
||||||
</ListItemButton>
|
</ListItemButton>
|
||||||
|
|
||||||
<Collapse in={current === index} timeout="auto" unmountOnExit>
|
<Collapse in={current === index} timeout="auto" unmountOnExit>
|
||||||
<List component="div" disablePadding sx={{ maxHeight: LIST_HEIGHT, overflowY: 'auto' }}>
|
<List
|
||||||
{getPartialArray(option.output as any || [], listLength)
|
component="div"
|
||||||
.map((suboption: any, index: number) => (
|
disablePadding
|
||||||
|
sx={{ maxHeight: LIST_HEIGHT, overflowY: 'auto' }}
|
||||||
|
>
|
||||||
|
{getPartialArray((option.output as any) || [], listLength).map(
|
||||||
|
(suboption: any, index: number) => (
|
||||||
<ListItemButton
|
<ListItemButton
|
||||||
sx={{ pl: 4 }}
|
sx={{ pl: 4 }}
|
||||||
divider
|
divider
|
||||||
@@ -85,7 +84,7 @@ const Suggestions = (props: SuggestionsProps) => {
|
|||||||
primaryTypographyProps={{
|
primaryTypographyProps={{
|
||||||
variant: 'subtitle1',
|
variant: 'subtitle1',
|
||||||
title: 'Property name',
|
title: 'Property name',
|
||||||
sx: { fontWeight: 700 }
|
sx: { fontWeight: 700 },
|
||||||
}}
|
}}
|
||||||
secondary={suboption.value || ''}
|
secondary={suboption.value || ''}
|
||||||
secondaryTypographyProps={{
|
secondaryTypographyProps={{
|
||||||
@@ -95,24 +94,18 @@ const Suggestions = (props: SuggestionsProps) => {
|
|||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</ListItemButton>
|
</ListItemButton>
|
||||||
))
|
)
|
||||||
}
|
)}
|
||||||
</List>
|
</List>
|
||||||
|
|
||||||
{option.output?.length > listLength && (
|
{(option.output?.length || 0) > listLength && (
|
||||||
<Button
|
<Button fullWidth onClick={expandList}>
|
||||||
fullWidth
|
|
||||||
onClick={expandList}
|
|
||||||
>
|
|
||||||
Show all
|
Show all
|
||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{listLength === Infinity && (
|
{listLength === Infinity && (
|
||||||
<Button
|
<Button fullWidth onClick={collapseList}>
|
||||||
fullWidth
|
|
||||||
onClick={collapseList}
|
|
||||||
>
|
|
||||||
Show less
|
Show less
|
||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
@@ -122,6 +115,6 @@ const Suggestions = (props: SuggestionsProps) => {
|
|||||||
</List>
|
</List>
|
||||||
</Paper>
|
</Paper>
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
export default Suggestions;
|
export default Suggestions;
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
import type { IStep } from '@automatisch/types';
|
import type { IStep } from '@automatisch/types';
|
||||||
|
|
||||||
const joinBy = (delimiter = '.', ...args: string[]) => args.filter(Boolean).join(delimiter);
|
const joinBy = (delimiter = '.', ...args: string[]) =>
|
||||||
|
args.filter(Boolean).join(delimiter);
|
||||||
|
|
||||||
const process = (data: any, parentKey?: any, index?: number): any[] => {
|
const process = (data: any, parentKey?: any, index?: number): any[] => {
|
||||||
if (typeof data !== 'object') {
|
if (typeof data !== 'object') {
|
||||||
@@ -8,14 +9,19 @@ const process = (data: any, parentKey?: any, index?: number): any[] => {
|
|||||||
{
|
{
|
||||||
name: `${parentKey}.${index}`,
|
name: `${parentKey}.${index}`,
|
||||||
value: data,
|
value: data,
|
||||||
}
|
},
|
||||||
]
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
const entries = Object.entries(data);
|
const entries = Object.entries(data);
|
||||||
|
|
||||||
return entries.flatMap(([name, value]) => {
|
return entries.flatMap(([name, value]) => {
|
||||||
const fullName = joinBy('.', parentKey, (index as number)?.toString(), name);
|
const fullName = joinBy(
|
||||||
|
'.',
|
||||||
|
parentKey,
|
||||||
|
(index as number)?.toString(),
|
||||||
|
name
|
||||||
|
);
|
||||||
|
|
||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value)) {
|
||||||
return value.flatMap((item, index) => process(item, fullName, index));
|
return value.flatMap((item, index) => process(item, fullName, index));
|
||||||
@@ -25,10 +31,12 @@ const process = (data: any, parentKey?: any, index?: number): any[] => {
|
|||||||
return process(value, fullName);
|
return process(value, fullName);
|
||||||
}
|
}
|
||||||
|
|
||||||
return [{
|
return [
|
||||||
name: fullName,
|
{
|
||||||
value,
|
name: fullName,
|
||||||
}];
|
value,
|
||||||
|
},
|
||||||
|
];
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -39,12 +47,17 @@ export const processStepWithExecutions = (steps: IStep[]): any[] => {
|
|||||||
.filter((step: IStep) => {
|
.filter((step: IStep) => {
|
||||||
const hasExecutionSteps = !!step.executionSteps?.length;
|
const hasExecutionSteps = !!step.executionSteps?.length;
|
||||||
|
|
||||||
return hasExecutionSteps
|
return hasExecutionSteps;
|
||||||
})
|
})
|
||||||
.map((step: IStep, index: number) => ({
|
.map((step: IStep, index: number) => ({
|
||||||
id: step.id,
|
id: step.id,
|
||||||
// TODO: replace with step.name once introduced
|
// TODO: replace with step.name once introduced
|
||||||
name: `${index + 1}. ${step.appKey?.charAt(0)?.toUpperCase() + step.appKey?.slice(1)}`,
|
name: `${index + 1}. ${
|
||||||
output: process(step.executionSteps?.[0]?.dataOut || {}, `step.${step.id}`),
|
(step.appKey || '').charAt(0)?.toUpperCase() + step.appKey?.slice(1)
|
||||||
|
}`,
|
||||||
|
output: process(
|
||||||
|
step.executionSteps?.[0]?.dataOut || {},
|
||||||
|
`step.${step.id}`
|
||||||
|
),
|
||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user