Extract reusable board entity badge

This commit is contained in:
Mike Cao committed 2026-03-06 23:54:48 -08:00
1 parent 25e60758b5
commit b429e7b24d
7 files changed
+60 -18

No files matched your search

@@ -0,0 +1,14 @@
import { Icon, Row, Text } from '@umami/react-zen';
import { Globe, Grid2x2, Link } from '@/components/icons';
import type { BoardEntityType } from '@/lib/boards';
export function BoardEntityBadge({ type, name }: { type: BoardEntityType; name: string }) {
return (
<Row padding borderRadius="full" backgroundColor="surface-base" border gap="2">
<Icon>
{type === 'pixel' ? <Grid2x2 /> : type === 'link' ? <Link /> : <Globe />}
</Icon>
<Text size="sm">{name}</Text>
</Row>
);
}
@@ -1,15 +1,19 @@
import { Box, Column } from '@umami/react-zen';
import { Panel } from '@/components/common/Panel';
import { useBoard } from '@/components/hooks';
import { getResolvedComponentEntity } from '@/lib/boards';
import { getBoardType, getResolvedComponentEntity, isOpenBoardType } from '@/lib/boards';
import type { BoardComponentConfig } from '@/lib/types';
import { BoardEntityBadge } from '../BoardEntityBadge';
import { getComponentDefinition } from '../boardComponentRegistry';
import { useBoardEntityBadgeProps } from '../useBoardEntityBadgeProps';
import { BoardComponentRenderer } from './BoardComponentRenderer';
export function BoardViewColumn({ component }: { component?: BoardComponentConfig }) {
const { board } = useBoard();
const boardType = getBoardType(board);
const definition = component ? getComponentDefinition(component.type) : undefined;
const { entityId } = getResolvedComponentEntity(board, component);
const { entityType, entityId } = getResolvedComponentEntity(board, component);
const entityBadge = useBoardEntityBadgeProps(entityType, entityId);
if (!component || (!entityId && definition?.requiresWebsite !== false)) {
return null;
@@ -19,7 +23,12 @@ export function BoardViewColumn({ component }: { component?: BoardComponentConfi
const description = component.description;
return (
<Panel title={title} description={description} height="100%">
<Panel title={title} description={description} height="100%" position="relative">
{isOpenBoardType(boardType) && entityBadge && (
<Box position="absolute" top="12px" right="12px" zIndex={100}>
<BoardEntityBadge {...entityBadge} />
</Box>
)}
<Column width="100%" height="100%" style={{ minHeight: 0 }}>
<Box width="100%" flexGrow={1} style={{ minHeight: 0 }}>
<BoardComponentRenderer config={component} websiteId={entityId} />
@@ -1,27 +1,24 @@
import { Icon, Row, Text } from '@umami/react-zen';
import { Row } from '@umami/react-zen';
import { IconLabel } from '@/components/common/IconLabel';
import { LinkButton } from '@/components/common/LinkButton';
import { PageHeader } from '@/components/common/PageHeader';
import { useBoard, useMessages, useNavigation, useWebsiteQuery } from '@/components/hooks';
import { Edit, Globe } from '@/components/icons';
import { useBoard, useMessages, useNavigation } from '@/components/hooks';
import { getBoardEntity } from '@/lib/boards';
import { Edit } from '@/components/icons';
import { BoardEntityBadge } from '../BoardEntityBadge';
import { useBoardEntityBadgeProps } from '../useBoardEntityBadgeProps';
export function BoardViewHeader() {
const { board } = useBoard();
const { renderUrl } = useNavigation();
const { t, labels } = useMessages();
const { data: website } = useWebsiteQuery(board?.parameters?.websiteId);
const { entityType, entityId } = getBoardEntity(board);
const entityBadge = useBoardEntityBadgeProps(entityType, entityId);
return (
<PageHeader title={board?.name} description={board?.description}>
<Row alignItems="center" gap>
{website?.name && (
<Row padding borderRadius="full" backgroundColor="surface-base" border gap="2">
<Icon>
<Globe />
</Icon>
<Text size="sm">{website.name}</Text>
</Row>
)}
{entityBadge && <BoardEntityBadge {...entityBadge} />}
<LinkButton href={renderUrl(`/boards/${board?.id}/edit`, false)}>
<IconLabel icon={<Edit />}>{t(labels.edit)}</IconLabel>
</LinkButton>
@@ -0,0 +1,22 @@
import { useLinkQuery, usePixelQuery, useWebsiteQuery } from '@/components/hooks';
import type { BoardEntityType } from '@/lib/boards';
export function useBoardEntityBadgeProps(entityType?: BoardEntityType, entityId?: string) {
const { data: website } = useWebsiteQuery(entityType === 'website' ? entityId : undefined);
const { data: pixel } = usePixelQuery(entityType === 'pixel' ? entityId : undefined);
const { data: link } = useLinkQuery(entityType === 'link' ? entityId : undefined);
if (entityType === 'website' && website?.name) {
return { type: entityType, name: website.name };
}
if (entityType === 'pixel' && pixel?.name) {
return { type: entityType, name: pixel.name };
}
if (entityType === 'link' && link?.name) {
return { type: entityType, name: link.name };
}
return null;
}
+1 -1
View File
@@ -1,7 +1,7 @@
import { useApi } from '../useApi';
import { useModified } from '../useModified';
export function useLinkQuery(linkId: string) {
export function useLinkQuery(linkId?: string) {
const { get, useQuery } = useApi();
const { modified } = useModified(`link:${linkId}`);
@@ -1,7 +1,7 @@
import { useApi } from '../useApi';
import { useModified } from '../useModified';
export function usePixelQuery(pixelId: string) {
export function usePixelQuery(pixelId?: string) {
const { get, useQuery } = useApi();
const { modified } = useModified(`pixel:${pixelId}`);
@@ -3,7 +3,7 @@ import type { ReactQueryOptions } from '@/lib/types';
import { useApi } from '../useApi';
import { useModified } from '../useModified';
export function useWebsiteQuery(websiteId: string, options?: ReactQueryOptions) {
export function useWebsiteQuery(websiteId?: string, options?: ReactQueryOptions) {
const { get, useQuery } = useApi();
const { modified } = useModified(`website:${websiteId}`);