Add board type binding support

This commit is contained in:
Mike Cao committed 2026-03-06 23:54:46 -08:00
1 parent 07ff25bf7e
commit f76b22b303
16 files changed
+496 -99

No files matched your search

+171
View File
@@ -0,0 +1,171 @@
import type { Board, BoardComponentConfig, BoardParameters } from './types';
export const BOARD_TYPES = {
dashboard: 'dashboard',
open: 'open',
website: 'website',
pixel: 'pixel',
link: 'link',
} as const;
export const BOARD_ENTITY_TYPES = {
website: 'website',
pixel: 'pixel',
link: 'link',
} as const;
export type BoardType = (typeof BOARD_TYPES)[keyof typeof BOARD_TYPES];
export type BoardEntityType = (typeof BOARD_ENTITY_TYPES)[keyof typeof BOARD_ENTITY_TYPES];
const boardTypes = new Set<string>(Object.values(BOARD_TYPES));
export function getLegacyBoardType(parameters?: BoardParameters): BoardType {
if (parameters?.pixelId) {
return BOARD_TYPES.pixel;
}
if (parameters?.linkId) {
return BOARD_TYPES.link;
}
if (parameters?.websiteId) {
return BOARD_TYPES.website;
}
return BOARD_TYPES.open;
}
export function getBoardType(
board?: Pick<Board, 'type' | 'parameters'> | Partial<Board>,
{ coerceDashboard = false }: { coerceDashboard?: boolean } = {},
): BoardType {
const type = board?.type;
if (type === BOARD_TYPES.dashboard && coerceDashboard) {
return getLegacyBoardType(board?.parameters);
}
if (type && boardTypes.has(type)) {
return type as BoardType;
}
return getLegacyBoardType(board?.parameters);
}
export function isOpenBoardType(type?: string) {
return type === BOARD_TYPES.open || type === BOARD_TYPES.dashboard;
}
export function requiresBoardEntity(type?: string) {
return (
type === BOARD_TYPES.website || type === BOARD_TYPES.pixel || type === BOARD_TYPES.link
);
}
export function getBoardEntity(board?: Pick<Board, 'type' | 'parameters'> | Partial<Board>): {
entityType?: BoardEntityType;
entityId?: string;
} {
const type = getBoardType(board);
if (type === BOARD_TYPES.website) {
return {
entityType: BOARD_ENTITY_TYPES.website,
entityId: board?.parameters?.websiteId,
};
}
if (type === BOARD_TYPES.pixel) {
return {
entityType: BOARD_ENTITY_TYPES.pixel,
entityId: board?.parameters?.pixelId,
};
}
if (type === BOARD_TYPES.link) {
return {
entityType: BOARD_ENTITY_TYPES.link,
entityId: board?.parameters?.linkId,
};
}
return {};
}
export function getComponentEntity(config?: BoardComponentConfig): {
entityType?: BoardEntityType;
entityId?: string;
} {
if (config?.entityType && config?.entityId) {
return {
entityType: config.entityType,
entityId: config.entityId,
};
}
if (config?.websiteId) {
return {
entityType: BOARD_ENTITY_TYPES.website,
entityId: config.websiteId,
};
}
return {};
}
export function getResolvedComponentEntity(
board?: Pick<Board, 'type' | 'parameters'> | Partial<Board>,
config?: BoardComponentConfig,
) {
const boardEntity = getBoardEntity(board);
if (boardEntity.entityId) {
return boardEntity;
}
return getComponentEntity(config);
}
export function getFirstBoardComponentEntity(
board?: Pick<Board, 'type' | 'parameters'> | Partial<Board>,
) {
for (const row of board?.parameters?.rows ?? []) {
for (const column of row.columns ?? []) {
const entity = getComponentEntity(column.component);
if (entity.entityId) {
return entity;
}
}
}
return {};
}
export function clearBoardEntity(parameters: BoardParameters = {}): BoardParameters {
const { websiteId, pixelId, linkId, ...rest } = parameters;
return rest;
}
export function setBoardEntity(
parameters: BoardParameters = {},
type: BoardType,
entityId?: string,
): BoardParameters {
const next = clearBoardEntity(parameters);
if (type === BOARD_TYPES.website && entityId) {
next.websiteId = entityId;
}
if (type === BOARD_TYPES.pixel && entityId) {
next.pixelId = entityId;
}
if (type === BOARD_TYPES.link && entityId) {
next.linkId = entityId;
}
return next;
}
+4
View File
@@ -150,6 +150,8 @@ export interface ApiError extends Error {
export interface BoardComponentConfig {
type: string;
entityType?: 'website' | 'pixel' | 'link';
entityId?: string;
websiteId?: string;
title?: string;
description?: string;
@@ -170,6 +172,8 @@ export interface BoardRow {
export interface BoardParameters {
websiteId?: string;
pixelId?: string;
linkId?: string;
rows?: BoardRow[];
}