Skip to content

Commit

Permalink
fix(ui-kit): Fix SurfaceRendererPayload (#556)
Browse files Browse the repository at this point in the history
  • Loading branch information
tassoevan authored and ggazzo committed Oct 20, 2021
1 parent b806337 commit 0508689
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 47 deletions.
30 changes: 10 additions & 20 deletions packages/ui-kit/src/rendering/SurfaceRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { SectionBlock } from '../blocks/layout/SectionBlock';
import { Markdown } from '../blocks/text/Markdown';
import { PlainText } from '../blocks/text/PlainText';
import { isNotNull } from '../isNotNull';
import { Permutation } from '../utils/Permutation';
import { BlockContext } from './BlockContext';
import { BlockRenderers } from './BlockRenderers';
import { Conditions } from './Conditions';
Expand All @@ -30,12 +29,14 @@ export abstract class SurfaceRenderer<
B extends RenderableLayoutBlock = RenderableLayoutBlock
> implements BlockRenderers<T>
{
public constructor(
protected readonly allowedLayoutBlockTypes: Permutation<B['type']>
) {}
protected readonly allowedLayoutBlockTypes: Set<B['type']>;

public constructor(allowedLayoutBlockTypes: B['type'][]) {
this.allowedLayoutBlockTypes = new Set(allowedLayoutBlockTypes);
}

private isAllowedLayoutBlock = (block: Block): block is B =>
(this.allowedLayoutBlockTypes as string[])?.includes(block.type) ?? true;
this.allowedLayoutBlockTypes.has(block.type as B['type']);

public render(blocks: readonly Block[], conditions?: Conditions): T[] {
if (!Array.isArray(blocks)) {
Expand All @@ -62,9 +63,7 @@ export abstract class SurfaceRenderer<
index: number
): T | null {
if (
(this.allowedLayoutBlockTypes as string[])?.includes(
LayoutBlockType.ACTIONS
) === false &&
this.allowedLayoutBlockTypes.has(LayoutBlockType.ACTIONS) === false &&
!isActionsBlockElement(block)
) {
return null;
Expand All @@ -88,9 +87,7 @@ export abstract class SurfaceRenderer<
index: number
): T | null {
if (
(this.allowedLayoutBlockTypes as string[])?.includes(
LayoutBlockType.CONTEXT
) === false &&
this.allowedLayoutBlockTypes.has(LayoutBlockType.CONTEXT) === false &&
!isContextBlockElement(block)
) {
return null;
Expand All @@ -115,9 +112,7 @@ export abstract class SurfaceRenderer<

public renderInputBlockElement(block: BlockElement, index: number): T | null {
if (
(this.allowedLayoutBlockTypes as string[])?.includes(
LayoutBlockType.INPUT
) === false &&
this.allowedLayoutBlockTypes.has(LayoutBlockType.INPUT) === false &&
!isInputBlockElement(block)
) {
return null;
Expand All @@ -141,9 +136,7 @@ export abstract class SurfaceRenderer<
index: number
): T | null {
if (
(this.allowedLayoutBlockTypes as string[])?.includes(
LayoutBlockType.SECTION
) === false &&
this.allowedLayoutBlockTypes.has(LayoutBlockType.SECTION) === false &&
!isSectionBlockAccessoryElement(block)
) {
return null;
Expand Down Expand Up @@ -201,6 +194,3 @@ export abstract class SurfaceRenderer<
index: number
): T | null;
}

export type SurfaceRendererPayload<S extends BlockRenderers<any>> =
S extends SurfaceRenderer<any, infer T> ? T[] : never;
15 changes: 10 additions & 5 deletions packages/ui-kit/src/rendering/surfaces/UiKitParserAttachment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@ import { ContextBlock } from '../../blocks/layout/ContextBlock';
import { DividerBlock } from '../../blocks/layout/DividerBlock';
import { ImageBlock } from '../../blocks/layout/ImageBlock';
import { SectionBlock } from '../../blocks/layout/SectionBlock';
import { SurfaceRenderer, SurfaceRendererPayload } from '../SurfaceRenderer';
import { SurfaceRenderer } from '../SurfaceRenderer';

type AttachmentSurfaceLayoutBlock =
| ActionsBlock
| ContextBlock
| DividerBlock
| ImageBlock
| SectionBlock;

export abstract class UiKitParserAttachment<T> extends SurfaceRenderer<
T,
ActionsBlock | ContextBlock | DividerBlock | ImageBlock | SectionBlock
AttachmentSurfaceLayoutBlock
> {
public constructor() {
super(['actions', 'context', 'divider', 'image', 'section']);
}
}

export type AttachmentSurfaceLayout = SurfaceRendererPayload<
UiKitParserAttachment<any>
>;
export type AttachmentSurfaceLayout = AttachmentSurfaceLayoutBlock[];
15 changes: 8 additions & 7 deletions packages/ui-kit/src/rendering/surfaces/UiKitParserBanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@ import { DividerBlock } from '../../blocks/layout/DividerBlock';
import { ImageBlock } from '../../blocks/layout/ImageBlock';
import { InputBlock } from '../../blocks/layout/InputBlock';
import { SectionBlock } from '../../blocks/layout/SectionBlock';
import { SurfaceRenderer, SurfaceRendererPayload } from '../SurfaceRenderer';
import { SurfaceRenderer } from '../SurfaceRenderer';

export abstract class UiKitParserBanner<T> extends SurfaceRenderer<
T,
type BannerSurfaceLayoutBlock =
| ActionsBlock
| ContextBlock
| DividerBlock
| ImageBlock
| InputBlock
| SectionBlock
| SectionBlock;

export abstract class UiKitParserBanner<T> extends SurfaceRenderer<
T,
BannerSurfaceLayoutBlock
> {
public constructor() {
super(['actions', 'context', 'divider', 'image', 'input', 'section']);
}
}

export type BannerSurfaceLayout = SurfaceRendererPayload<
UiKitParserBanner<any>
>;
export type BannerSurfaceLayout = BannerSurfaceLayoutBlock[];
15 changes: 10 additions & 5 deletions packages/ui-kit/src/rendering/surfaces/UiKitParserMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@ import { ContextBlock } from '../../blocks/layout/ContextBlock';
import { DividerBlock } from '../../blocks/layout/DividerBlock';
import { ImageBlock } from '../../blocks/layout/ImageBlock';
import { SectionBlock } from '../../blocks/layout/SectionBlock';
import { SurfaceRenderer, SurfaceRendererPayload } from '../SurfaceRenderer';
import { SurfaceRenderer } from '../SurfaceRenderer';

type MessageSurfaceLayoutBlock =
| ActionsBlock
| ContextBlock
| DividerBlock
| ImageBlock
| SectionBlock;

export abstract class UiKitParserMessage<OutputElement> extends SurfaceRenderer<
OutputElement,
ActionsBlock | ContextBlock | DividerBlock | ImageBlock | SectionBlock
MessageSurfaceLayoutBlock
> {
public constructor() {
super(['actions', 'context', 'divider', 'image', 'section']);
}
}

export type MessageSurfaceLayout = SurfaceRendererPayload<
UiKitParserMessage<any>
>;
export type MessageSurfaceLayout = MessageSurfaceLayoutBlock[];
13 changes: 8 additions & 5 deletions packages/ui-kit/src/rendering/surfaces/UiKitParserModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@ import { DividerBlock } from '../../blocks/layout/DividerBlock';
import { ImageBlock } from '../../blocks/layout/ImageBlock';
import { InputBlock } from '../../blocks/layout/InputBlock';
import { SectionBlock } from '../../blocks/layout/SectionBlock';
import { SurfaceRenderer, SurfaceRendererPayload } from '../SurfaceRenderer';
import { SurfaceRenderer } from '../SurfaceRenderer';

export abstract class UiKitParserModal<OutputElement> extends SurfaceRenderer<
OutputElement,
type ModalSurfaceLayoutBlock =
| ActionsBlock
| ContextBlock
| DividerBlock
| ImageBlock
| InputBlock
| SectionBlock
| SectionBlock;

export abstract class UiKitParserModal<OutputElement> extends SurfaceRenderer<
OutputElement,
ModalSurfaceLayoutBlock
> {
public constructor() {
super(['actions', 'context', 'divider', 'image', 'input', 'section']);
}
}

export type ModalSurfaceLayout = SurfaceRendererPayload<UiKitParserModal<any>>;
export type ModalSurfaceLayout = ModalSurfaceLayoutBlock[];
5 changes: 0 additions & 5 deletions packages/ui-kit/src/utils/Permutation.ts

This file was deleted.

0 comments on commit 0508689

Please sign in to comment.