Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

[NEW] Open uikit contextual bar #451

Merged
merged 4 commits into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/definition/accessors/IUIController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IUIKitErrorInteraction, IUIKitInteraction } from '../../definition/uikit';
import { Omit } from '../../lib/utils';
import { IUIKitModalViewParam } from '../uikit/UIKitInteractionResponder';
import { IUIKitContextualBarViewParam, IUIKitModalViewParam } from '../uikit/UIKitInteractionResponder';
import { IUser } from '../users';

export type IUIKitInteractionParam = Omit<IUIKitInteraction, 'appId' | 'type'>;
Expand All @@ -9,5 +9,7 @@ export type IUIKitErrorInteractionParam = Omit<IUIKitErrorInteraction, 'type' |
export interface IUIController {
openModalView(view: IUIKitModalViewParam, context: IUIKitInteractionParam, user: IUser): Promise<void>;
updateModalView(view: IUIKitModalViewParam, context: IUIKitInteractionParam, user: IUser): Promise<void>;
openContextualBarView(view: IUIKitContextualBarViewParam, context: IUIKitInteractionParam, user: IUser): Promise<void>;
updateContextualBarView(view: IUIKitContextualBarViewParam, context: IUIKitInteractionParam, user: IUser): Promise<void>;
setViewError(errorInteraction: IUIKitErrorInteractionParam, context: IUIKitInteractionParam, user: IUser): Promise<void>;
}
9 changes: 9 additions & 0 deletions src/definition/uikit/IUIKitInteractionType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ export enum UIKitInteractionType {
MODAL_OPEN = 'modal.open',
MODAL_CLOSE = 'modal.close',
MODAL_UPDATE = 'modal.update',
CONTEXTUAL_BAR_OPEN = 'contextual_bar.open',
CONTEXTUAL_BAR_CLOSE = 'contextual_bar.close',
CONTEXTUAL_BAR_UPDATE = 'contextual_bar.update',
ERRORS = 'errors',
}

Expand All @@ -28,5 +31,11 @@ export interface IUIKitModalInteraction extends IUIKitInteraction {
view: IUIKitSurface;
}

export interface IUIKitContextualBarInteraction extends IUIKitInteraction {
type: UIKitInteractionType.CONTEXTUAL_BAR_OPEN | UIKitInteractionType.CONTEXTUAL_BAR_UPDATE | UIKitInteractionType.CONTEXTUAL_BAR_CLOSE;
view: IUIKitSurface;
}

export interface IUIKitModalResponse extends IUIKitModalInteraction, IUIKitResponse { }
export interface IUIKitContextualBarResponse extends IUIKitContextualBarInteraction, IUIKitResponse { }
export interface IUIKitErrorResponse extends IUIKitErrorInteraction, IUIKitResponse { }
3 changes: 3 additions & 0 deletions src/definition/uikit/UIKitIncomingInteractionContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export interface IUIKitIncomingInteractionContainer {
export interface IUIKitIncomingInteractionModalContainer extends IUIKitIncomingInteractionContainer {
type: UIKitIncomingInteractionContainerType.VIEW;
}
export interface IUIKitIncomingInteractionContextualBarContainer extends IUIKitIncomingInteractionContainer {
type: UIKitIncomingInteractionContainerType.VIEW;
}
export interface IUIKitIncomingInteractionMessageContainer extends IUIKitIncomingInteractionContainer {
type: UIKitIncomingInteractionContainerType.MESSAGE;
}
3 changes: 2 additions & 1 deletion src/definition/uikit/UIKitIncomingInteractionTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { IRoom } from '../rooms';
import { IUser } from '../users';
import { IUIKitSurface } from './IUIKitSurface';
import {
IUIKitIncomingInteractionContextualBarContainer,
IUIKitIncomingInteractionMessageContainer,
IUIKitIncomingInteractionModalContainer,
} from './UIKitIncomingInteractionContainer';
Expand All @@ -22,7 +23,7 @@ export interface IUIKitBlockIncomingInteraction extends IUIKitBaseIncomingIntera
actionId: string;
blockId: string;
room: IUIKitBaseIncomingInteraction['room'];
container: IUIKitIncomingInteractionModalContainer | IUIKitIncomingInteractionMessageContainer;
container: IUIKitIncomingInteractionModalContainer | IUIKitIncomingInteractionContextualBarContainer | IUIKitIncomingInteractionMessageContainer;
}

export interface IUIKitViewSubmitIncomingInteraction extends IUIKitBaseIncomingInteraction {
Expand Down
37 changes: 31 additions & 6 deletions src/definition/uikit/UIKitInteractionPayloadFormatter.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { IUIKitErrorInteractionParam } from '../accessors/IUIController';
import { IUIKitErrorInteraction, IUIKitInteraction, IUIKitModalInteraction, UIKitInteractionType } from './IUIKitInteractionType';
import { IUIKitContextualBarInteraction, IUIKitErrorInteraction, IUIKitInteraction, IUIKitModalInteraction, UIKitInteractionType } from './IUIKitInteractionType';
import { IUIKitSurface, UIKitSurfaceType } from './IUIKitSurface';
import { IUIKitModalViewParam } from './UIKitInteractionResponder';
import { IUIKitContextualBarViewParam, IUIKitModalViewParam } from './UIKitInteractionResponder';

import uuid = require('uuid/v1');

function isModalInteraction(type: IUIKitInteraction['type']): type is IUIKitModalInteraction['type'] {
return [UIKitInteractionType.MODAL_OPEN, UIKitInteractionType.MODAL_UPDATE, UIKitInteractionType.MODAL_CLOSE].includes(type);
}

export function formatModalInteraction(view: IUIKitModalViewParam, context: IUIKitInteraction): IUIKitModalInteraction {
if (![UIKitInteractionType.MODAL_OPEN, UIKitInteractionType.MODAL_UPDATE, UIKitInteractionType.MODAL_CLOSE].includes(context.type)) {
if (!isModalInteraction(context.type)) {
throw new Error(`Invalid type "${ context.type }" for modal interaction`);
}

const type = context.type as UIKitInteractionType.MODAL_OPEN | UIKitInteractionType.MODAL_UPDATE | UIKitInteractionType.MODAL_CLOSE;

return {
type,
type: context.type,
triggerId: context.triggerId,
appId: context.appId,
view: {
Expand All @@ -26,6 +28,29 @@ export function formatModalInteraction(view: IUIKitModalViewParam, context: IUIK
};
}

function isContextualBarInteraction(type: IUIKitInteraction['type']): type is IUIKitContextualBarInteraction['type'] {
return [UIKitInteractionType.CONTEXTUAL_BAR_OPEN, UIKitInteractionType.CONTEXTUAL_BAR_UPDATE, UIKitInteractionType.CONTEXTUAL_BAR_CLOSE].includes(type);
}

export function formatContextualBarInteraction(view: IUIKitContextualBarViewParam, context: IUIKitInteraction): IUIKitContextualBarInteraction {
if (!isContextualBarInteraction(context.type)) {
throw new Error(`Invalid type "${ context.type }" for contextual bar interaction`);
}

return {
type: context.type,
triggerId: context.triggerId,
appId: context.appId,
view: {
appId: context.appId,
type: UIKitSurfaceType.CONTEXTUAL_BAR,
id: view.id ? view.id : uuid(),
...view,
showIcon: true,
} as IUIKitSurface,
};
}

export function formatErrorInteraction(errorInteraction: IUIKitErrorInteractionParam, context: IUIKitInteraction): IUIKitErrorInteraction {
if (UIKitInteractionType.ERRORS !== context.type) {
throw new Error(`Invalid type "${ context.type }" for error interaction`);
Expand Down
24 changes: 22 additions & 2 deletions src/definition/uikit/UIKitInteractionResponder.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Omit } from '../../lib/utils';
import { IUIKitErrorInteractionParam } from '../accessors/IUIController';
import { IUIKitErrorResponse, IUIKitModalResponse, IUIKitResponse, UIKitInteractionType } from './IUIKitInteractionType';
import { IUIKitContextualBarResponse, IUIKitErrorResponse, IUIKitModalResponse, IUIKitResponse, UIKitInteractionType } from './IUIKitInteractionType';
import { IUIKitSurface } from './IUIKitSurface';
import { IUIKitBaseIncomingInteraction } from './UIKitIncomingInteractionTypes';
import { formatModalInteraction } from './UIKitInteractionPayloadFormatter';
import { formatContextualBarInteraction, formatModalInteraction } from './UIKitInteractionPayloadFormatter';

export type IUIKitModalViewParam = Omit<IUIKitSurface, 'appId' | 'id' | 'type'> & Partial<Pick<IUIKitSurface, 'id'>>;
export type IUIKitContextualBarViewParam = Omit<IUIKitSurface, 'appId' | 'id' | 'type'> & Partial<Pick<IUIKitSurface, 'id'>>;

export class UIKitInteractionResponder {
constructor(private readonly baseContext: IUIKitBaseIncomingInteraction) { }
Expand All @@ -21,6 +22,7 @@ export class UIKitInteractionResponder {
success: false,
};
}

public openModalViewResponse(viewData: IUIKitModalViewParam): IUIKitModalResponse {
const { appId, triggerId } = this.baseContext;

Expand All @@ -39,6 +41,24 @@ export class UIKitInteractionResponder {
};
}

public openContextualBarViewResponse(viewData: IUIKitContextualBarViewParam): IUIKitContextualBarResponse {
const { appId, triggerId } = this.baseContext;

return {
success: true,
...formatContextualBarInteraction(viewData, { appId, triggerId, type: UIKitInteractionType.CONTEXTUAL_BAR_OPEN }),
};
}

public updateContextualBarViewResponse(viewData: IUIKitContextualBarViewParam): IUIKitContextualBarResponse {
const { appId, triggerId } = this.baseContext;

return {
success: true,
...formatContextualBarInteraction(viewData, { appId, triggerId, type: UIKitInteractionType.CONTEXTUAL_BAR_UPDATE }),
};
}

public viewErrorResponse(errorInteraction: IUIKitErrorInteractionParam): IUIKitErrorResponse {
const { appId, triggerId } = this.baseContext;

Expand Down
24 changes: 22 additions & 2 deletions src/server/accessors/UIController.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { IUIController } from '../../definition/accessors';
import { IUIKitErrorInteractionParam, IUIKitInteractionParam } from '../../definition/accessors/IUIController';
import { UIKitInteractionType } from '../../definition/uikit';
import { formatErrorInteraction, formatModalInteraction } from '../../definition/uikit/UIKitInteractionPayloadFormatter';
import { IUIKitModalViewParam } from '../../definition/uikit/UIKitInteractionResponder';
import { formatContextualBarInteraction, formatErrorInteraction, formatModalInteraction } from '../../definition/uikit/UIKitInteractionPayloadFormatter';
import { IUIKitContextualBarViewParam, IUIKitModalViewParam } from '../../definition/uikit/UIKitInteractionResponder';
import { IUser } from '../../definition/users';
import { AppBridges, UiInteractionBridge } from '../bridges';

Expand Down Expand Up @@ -35,6 +35,26 @@ export class UIController implements IUIController {
return this.uiInteractionBridge.doNotifyUser(user, formatModalInteraction(view, interactionContext), this.appId);
}

public openContextualBarView(view: IUIKitContextualBarViewParam, context: IUIKitInteractionParam, user: IUser) {
const interactionContext = {
...context,
type: UIKitInteractionType.CONTEXTUAL_BAR_OPEN,
appId: this.appId,
};

return this.uiInteractionBridge.doNotifyUser(user, formatContextualBarInteraction(view, interactionContext), this.appId);
}

public updateContextualBarView(view: IUIKitContextualBarViewParam, context: IUIKitInteractionParam, user: IUser) {
const interactionContext = {
...context,
type: UIKitInteractionType.CONTEXTUAL_BAR_UPDATE,
appId: this.appId,
};

return this.uiInteractionBridge.doNotifyUser(user, formatContextualBarInteraction(view, interactionContext), this.appId);
}

public setViewError(errorInteraction: IUIKitErrorInteractionParam, context: IUIKitInteractionParam, user: IUser) {
const interactionContext = {
...context,
Expand Down