Skip to content

Commit

Permalink
Create IterableActionRunner.ts
Browse files Browse the repository at this point in the history
hani-iterable authored Dec 25, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent d583ba4 commit f5b7df3
Showing 1 changed file with 121 additions and 0 deletions.
121 changes: 121 additions & 0 deletions src/embedded/IterableActionRunner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
export enum IterableActionSource {
PUSH = 'PUSH',
APP_LINK = 'APP_LINK',
IN_APP = 'IN_APP',
EMBEDDED = 'EMBEDDED'
}

export interface IterableAction {
type: string;
data: string;
}

export interface IterableActionContext {
action: IterableAction;
source: IterableActionSource;
}

interface IterableConfig {
urlHandler?: {
handleIterableURL(
uri: string,
actionContext: IterableActionContext
): boolean;
};
customActionHandler?: {
handleIterableCustomAction(
action: IterableAction,
actionContext: IterableActionContext
): boolean;
};
}

class IterableActionRunnerConfig {
static config: IterableConfig = {};
}

class IterableActionRunnerImpl {
private static TAG = 'IterableActionRunner';

executeAction(
context: any,
action: IterableAction | null,
source: IterableActionSource
): boolean {
if (action === null) {
return false;
}

const actionContext: IterableActionContext = { action, source };

if (action.type === 'openUrl') {
return this.openUri(action.data, actionContext);
} else {
return this.callCustomActionIfSpecified(action, actionContext);
}
}

private openUri(uri: string, actionContext: IterableActionContext): boolean {
if (IterableActionRunnerConfig.config.urlHandler) {
if (
IterableActionRunnerConfig.config.urlHandler.handleIterableURL(
uri,
actionContext
)
) {
return true;
}
}

window.open(uri, '_blank');

return true;
}

private callCustomActionIfSpecified(
action: IterableAction,
actionContext: IterableActionContext
): boolean {
if (action.type && action.type !== '') {
if (IterableActionRunnerConfig.config.customActionHandler) {
return IterableActionRunnerConfig.config.customActionHandler.handleIterableCustomAction(
action,
actionContext
);
}
}
return false;
}
}

IterableActionRunnerConfig.config = {
urlHandler: {
handleIterableURL(
uri: string,
actionContext: IterableActionContext
): boolean {
global.open(uri, '_blank', 'noopener,noreferrer');
return true;
}
},
customActionHandler: {
handleIterableCustomAction(
action: IterableAction,
actionContext: IterableActionContext
): boolean {
let uri = '';
if (action.type.startsWith('action://')) {
uri = action.type.replace('action://', '');
} else if (action.type.startsWith('iterable://')) {
uri = action.type.replace('iterable://', '');
}

global.open(uri, '_blank', 'noopener,noreferrer');
return true;
}
}
};

export class IterableActionRunner extends IterableActionRunnerImpl {
static config = IterableActionRunnerConfig.config;
}

0 comments on commit f5b7df3

Please sign in to comment.