Skip to content

Commit

Permalink
Add interaface for block-builder classes for better typing
Browse files Browse the repository at this point in the history
  • Loading branch information
joonashak committed Aug 9, 2023
1 parent b6da9ad commit 86d0d25
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
16 changes: 16 additions & 0 deletions app-nest/src/gui/block-builder.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {
Appendable,
BlockBuilder as BlockBuilderType,
} from "slack-block-builder/dist/internal";

/**
* Interface for classes that build views with `slack-block-builder`.
*
* Type parameter should be imported from `slack-block-builder` with respect
* to the intended consumer of the class implementing this interface. This way
* the library's type system will take care of checking that only allowed
* blocks are used throughout the block tree.
*/
export interface BlockBuilder<T extends BlockBuilderType> {
build(...args: unknown[]): Appendable<T> | Promise<Appendable<T>>;
}
12 changes: 10 additions & 2 deletions app-nest/src/gui/dev/dev-tools.builder.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { Injectable } from "@nestjs/common";
import { Actions, Button, Context, Divider, Header } from "slack-block-builder";
import {
Actions,
Button,
Context,
Divider,
Header,
ViewBlockBuilder,
} from "slack-block-builder";
import BoltActions from "../../bolt/enums/bolt-actions.enum";
import { BlockBuilder } from "../block-builder.interface";

@Injectable()
export class DevToolsBuilder {
export class DevToolsBuilder implements BlockBuilder<ViewBlockBuilder> {
build() {
return [
Header({ text: ":wrench: Developer Tools" }),
Expand Down
4 changes: 3 additions & 1 deletion app-nest/src/gui/tabs/home/day-list-item.builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ import {
Option,
OverflowMenu,
StaticSelect,
ViewBlockBuilder,
} from "slack-block-builder";
import BoltActions from "../../../bolt/enums/bolt-actions.enum";
import { BlockBuilder } from "../../block-builder.interface";

type DayListItemProps = {
date: Dayjs;
};

@Injectable()
export class DayListItemBuilder {
export class DayListItemBuilder implements BlockBuilder<ViewBlockBuilder> {
build({ date }: DayListItemProps) {
const dateString = date.toISOString();

Expand Down
4 changes: 3 additions & 1 deletion app-nest/src/gui/tabs/home/day-list.builder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Injectable } from "@nestjs/common";
import dayjs, { Dayjs } from "dayjs";
import { flatten } from "lodash";
import { ViewBlockBuilder } from "slack-block-builder";
import { BlockBuilder } from "../../block-builder.interface";
import { DayListItemBuilder } from "./day-list-item.builder";

/**
Expand All @@ -26,7 +28,7 @@ const dayRange = (len: number, days: Dayjs[] = [], i = 0): Dayjs[] => {
};

@Injectable()
export class DayListBuilder {
export class DayListBuilder implements BlockBuilder<ViewBlockBuilder> {
constructor(private dayListItemBuilder: DayListItemBuilder) {}

async build() {
Expand Down
6 changes: 3 additions & 3 deletions app-nest/src/gui/tabs/home/home-tab.builder.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Injectable } from "@nestjs/common";
import { Header } from "slack-block-builder";
import { Header, ViewBlockBuilder } from "slack-block-builder";
import { BlockBuilder } from "../../block-builder.interface";
import { DevToolsBuilder } from "../../dev/dev-tools.builder";
import { DayListBuilder } from "./day-list.builder";

// TODO: Create interface for these kind of builder classes.
@Injectable()
export class HomeTabBuilder {
export class HomeTabBuilder implements BlockBuilder<ViewBlockBuilder> {
constructor(
private dayListBlocks: DayListBuilder,
private devToolsBuilder: DevToolsBuilder,
Expand Down

0 comments on commit 86d0d25

Please sign in to comment.