Skip to content

Commit 0508689

Browse files
tassoevanggazzo
authored andcommitted
fix(ui-kit): Fix SurfaceRendererPayload (#556)
1 parent b806337 commit 0508689

File tree

6 files changed

+46
-47
lines changed

6 files changed

+46
-47
lines changed

packages/ui-kit/src/rendering/SurfaceRenderer.ts

+10-20
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import { SectionBlock } from '../blocks/layout/SectionBlock';
1616
import { Markdown } from '../blocks/text/Markdown';
1717
import { PlainText } from '../blocks/text/PlainText';
1818
import { isNotNull } from '../isNotNull';
19-
import { Permutation } from '../utils/Permutation';
2019
import { BlockContext } from './BlockContext';
2120
import { BlockRenderers } from './BlockRenderers';
2221
import { Conditions } from './Conditions';
@@ -30,12 +29,14 @@ export abstract class SurfaceRenderer<
3029
B extends RenderableLayoutBlock = RenderableLayoutBlock
3130
> implements BlockRenderers<T>
3231
{
33-
public constructor(
34-
protected readonly allowedLayoutBlockTypes: Permutation<B['type']>
35-
) {}
32+
protected readonly allowedLayoutBlockTypes: Set<B['type']>;
33+
34+
public constructor(allowedLayoutBlockTypes: B['type'][]) {
35+
this.allowedLayoutBlockTypes = new Set(allowedLayoutBlockTypes);
36+
}
3637

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

4041
public render(blocks: readonly Block[], conditions?: Conditions): T[] {
4142
if (!Array.isArray(blocks)) {
@@ -62,9 +63,7 @@ export abstract class SurfaceRenderer<
6263
index: number
6364
): T | null {
6465
if (
65-
(this.allowedLayoutBlockTypes as string[])?.includes(
66-
LayoutBlockType.ACTIONS
67-
) === false &&
66+
this.allowedLayoutBlockTypes.has(LayoutBlockType.ACTIONS) === false &&
6867
!isActionsBlockElement(block)
6968
) {
7069
return null;
@@ -88,9 +87,7 @@ export abstract class SurfaceRenderer<
8887
index: number
8988
): T | null {
9089
if (
91-
(this.allowedLayoutBlockTypes as string[])?.includes(
92-
LayoutBlockType.CONTEXT
93-
) === false &&
90+
this.allowedLayoutBlockTypes.has(LayoutBlockType.CONTEXT) === false &&
9491
!isContextBlockElement(block)
9592
) {
9693
return null;
@@ -115,9 +112,7 @@ export abstract class SurfaceRenderer<
115112

116113
public renderInputBlockElement(block: BlockElement, index: number): T | null {
117114
if (
118-
(this.allowedLayoutBlockTypes as string[])?.includes(
119-
LayoutBlockType.INPUT
120-
) === false &&
115+
this.allowedLayoutBlockTypes.has(LayoutBlockType.INPUT) === false &&
121116
!isInputBlockElement(block)
122117
) {
123118
return null;
@@ -141,9 +136,7 @@ export abstract class SurfaceRenderer<
141136
index: number
142137
): T | null {
143138
if (
144-
(this.allowedLayoutBlockTypes as string[])?.includes(
145-
LayoutBlockType.SECTION
146-
) === false &&
139+
this.allowedLayoutBlockTypes.has(LayoutBlockType.SECTION) === false &&
147140
!isSectionBlockAccessoryElement(block)
148141
) {
149142
return null;
@@ -201,6 +194,3 @@ export abstract class SurfaceRenderer<
201194
index: number
202195
): T | null;
203196
}
204-
205-
export type SurfaceRendererPayload<S extends BlockRenderers<any>> =
206-
S extends SurfaceRenderer<any, infer T> ? T[] : never;

packages/ui-kit/src/rendering/surfaces/UiKitParserAttachment.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,22 @@ import { ContextBlock } from '../../blocks/layout/ContextBlock';
33
import { DividerBlock } from '../../blocks/layout/DividerBlock';
44
import { ImageBlock } from '../../blocks/layout/ImageBlock';
55
import { SectionBlock } from '../../blocks/layout/SectionBlock';
6-
import { SurfaceRenderer, SurfaceRendererPayload } from '../SurfaceRenderer';
6+
import { SurfaceRenderer } from '../SurfaceRenderer';
7+
8+
type AttachmentSurfaceLayoutBlock =
9+
| ActionsBlock
10+
| ContextBlock
11+
| DividerBlock
12+
| ImageBlock
13+
| SectionBlock;
714

815
export abstract class UiKitParserAttachment<T> extends SurfaceRenderer<
916
T,
10-
ActionsBlock | ContextBlock | DividerBlock | ImageBlock | SectionBlock
17+
AttachmentSurfaceLayoutBlock
1118
> {
1219
public constructor() {
1320
super(['actions', 'context', 'divider', 'image', 'section']);
1421
}
1522
}
1623

17-
export type AttachmentSurfaceLayout = SurfaceRendererPayload<
18-
UiKitParserAttachment<any>
19-
>;
24+
export type AttachmentSurfaceLayout = AttachmentSurfaceLayoutBlock[];

packages/ui-kit/src/rendering/surfaces/UiKitParserBanner.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,23 @@ import { DividerBlock } from '../../blocks/layout/DividerBlock';
44
import { ImageBlock } from '../../blocks/layout/ImageBlock';
55
import { InputBlock } from '../../blocks/layout/InputBlock';
66
import { SectionBlock } from '../../blocks/layout/SectionBlock';
7-
import { SurfaceRenderer, SurfaceRendererPayload } from '../SurfaceRenderer';
7+
import { SurfaceRenderer } from '../SurfaceRenderer';
88

9-
export abstract class UiKitParserBanner<T> extends SurfaceRenderer<
10-
T,
9+
type BannerSurfaceLayoutBlock =
1110
| ActionsBlock
1211
| ContextBlock
1312
| DividerBlock
1413
| ImageBlock
1514
| InputBlock
16-
| SectionBlock
15+
| SectionBlock;
16+
17+
export abstract class UiKitParserBanner<T> extends SurfaceRenderer<
18+
T,
19+
BannerSurfaceLayoutBlock
1720
> {
1821
public constructor() {
1922
super(['actions', 'context', 'divider', 'image', 'input', 'section']);
2023
}
2124
}
2225

23-
export type BannerSurfaceLayout = SurfaceRendererPayload<
24-
UiKitParserBanner<any>
25-
>;
26+
export type BannerSurfaceLayout = BannerSurfaceLayoutBlock[];

packages/ui-kit/src/rendering/surfaces/UiKitParserMessage.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,22 @@ import { ContextBlock } from '../../blocks/layout/ContextBlock';
33
import { DividerBlock } from '../../blocks/layout/DividerBlock';
44
import { ImageBlock } from '../../blocks/layout/ImageBlock';
55
import { SectionBlock } from '../../blocks/layout/SectionBlock';
6-
import { SurfaceRenderer, SurfaceRendererPayload } from '../SurfaceRenderer';
6+
import { SurfaceRenderer } from '../SurfaceRenderer';
7+
8+
type MessageSurfaceLayoutBlock =
9+
| ActionsBlock
10+
| ContextBlock
11+
| DividerBlock
12+
| ImageBlock
13+
| SectionBlock;
714

815
export abstract class UiKitParserMessage<OutputElement> extends SurfaceRenderer<
916
OutputElement,
10-
ActionsBlock | ContextBlock | DividerBlock | ImageBlock | SectionBlock
17+
MessageSurfaceLayoutBlock
1118
> {
1219
public constructor() {
1320
super(['actions', 'context', 'divider', 'image', 'section']);
1421
}
1522
}
1623

17-
export type MessageSurfaceLayout = SurfaceRendererPayload<
18-
UiKitParserMessage<any>
19-
>;
24+
export type MessageSurfaceLayout = MessageSurfaceLayoutBlock[];

packages/ui-kit/src/rendering/surfaces/UiKitParserModal.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@ import { DividerBlock } from '../../blocks/layout/DividerBlock';
44
import { ImageBlock } from '../../blocks/layout/ImageBlock';
55
import { InputBlock } from '../../blocks/layout/InputBlock';
66
import { SectionBlock } from '../../blocks/layout/SectionBlock';
7-
import { SurfaceRenderer, SurfaceRendererPayload } from '../SurfaceRenderer';
7+
import { SurfaceRenderer } from '../SurfaceRenderer';
88

9-
export abstract class UiKitParserModal<OutputElement> extends SurfaceRenderer<
10-
OutputElement,
9+
type ModalSurfaceLayoutBlock =
1110
| ActionsBlock
1211
| ContextBlock
1312
| DividerBlock
1413
| ImageBlock
1514
| InputBlock
16-
| SectionBlock
15+
| SectionBlock;
16+
17+
export abstract class UiKitParserModal<OutputElement> extends SurfaceRenderer<
18+
OutputElement,
19+
ModalSurfaceLayoutBlock
1720
> {
1821
public constructor() {
1922
super(['actions', 'context', 'divider', 'image', 'input', 'section']);
2023
}
2124
}
2225

23-
export type ModalSurfaceLayout = SurfaceRendererPayload<UiKitParserModal<any>>;
26+
export type ModalSurfaceLayout = ModalSurfaceLayoutBlock[];

packages/ui-kit/src/utils/Permutation.ts

-5
This file was deleted.

0 commit comments

Comments
 (0)