Skip to content

Commit 9c28eff

Browse files
fix(dashboard): Page block fixes (#3955)
1 parent e62fce2 commit 9c28eff

File tree

3 files changed

+34
-45
lines changed

3 files changed

+34
-45
lines changed

docs/docs/guides/extending-the-dashboard/customizing-pages/page-blocks.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ defineDashboardExtension({
2626
location: {
2727
// This is the pageId of the page where this block will be
2828
pageId: 'product-detail',
29-
// can be "main" or "side"
29+
// can be "main", "side" or "full"
3030
column: 'side',
3131
position: {
3232
// Blocks are positioned relative to existing blocks on
@@ -90,10 +90,11 @@ position: {
9090

9191
## Block Columns
9292

93-
Blocks can be placed in two columns:
93+
Blocks can be placed in three columns:
9494

9595
- **`main`**: The main content area (wider column on the left)
9696
- **`side`**: The sidebar area (narrower column on the right)
97+
- **`full`**: Takes up the full horizontal width. This is mostly useful for adding blocks to list pages
9798

9899
## Context Data
99100

packages/dashboard/src/lib/framework/extension-api/types/layout.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export type PageBlockPosition = { blockId: string; order: 'before' | 'after' | '
7979
export type PageBlockLocation = {
8080
pageId: string;
8181
position: PageBlockPosition;
82-
column: 'main' | 'side';
82+
column: 'main' | 'side' | 'full';
8383
};
8484

8585
/**

packages/dashboard/src/lib/framework/layout-engine/page-layout.tsx

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -242,89 +242,77 @@ export function PageLayout({ children, className }: Readonly<PageLayoutProps>) {
242242

243243
// sort the blocks to make sure we have the correct order
244244
const arrangedExtensionBlocks = matchingExtensionBlocks.sort((a, b) => {
245-
const orderPriority = { 'before': 1, 'replace': 2, 'after': 3 };
245+
const orderPriority = { before: 1, replace: 2, after: 3 };
246246
return orderPriority[a.location.position.order] - orderPriority[b.location.position.order];
247-
})
248-
249-
// get the length of blocks with the "before" position to know when to insert the child block
250-
const beforeExtensionBlocksLength = arrangedExtensionBlocks.filter(
251-
block => block.location.position.order === 'before',
252-
).length;
247+
});
253248

254249
const replacementBlockExists = arrangedExtensionBlocks.some(
255250
block => block.location.position.order === 'replace',
256-
)
251+
);
257252

258253
let childBlockInserted = false;
259254
if (matchingExtensionBlocks.length > 0) {
260255
for (const extensionBlock of arrangedExtensionBlocks) {
261-
262256
let extensionBlockShouldRender = true;
263257
if (typeof extensionBlock?.shouldRender === 'function') {
264258
extensionBlockShouldRender = extensionBlock.shouldRender(page);
265259
}
266260

267261
// Insert child block before the first non-"before" block
268-
if (!childBlockInserted && !replacementBlockExists && extensionBlock.location.position.order !== 'before') {
269-
finalChildArray.push(childBlock)
270-
childBlockInserted = true
271-
}
262+
if (
263+
!childBlockInserted &&
264+
!replacementBlockExists &&
265+
extensionBlock.location.position.order !== 'before'
266+
) {
267+
finalChildArray.push(childBlock);
268+
childBlockInserted = true;
269+
}
270+
271+
const isFullWidth = extensionBlock.location.column === 'full';
272+
const BlockComponent = isFullWidth ? FullWidthPageBlock : PageBlock;
272273

273274
const ExtensionBlock =
274275
extensionBlock.component && extensionBlockShouldRender ? (
275-
<PageBlock
276+
<BlockComponent
276277
key={extensionBlock.id}
277278
column={extensionBlock.location.column}
278279
blockId={extensionBlock.id}
279280
title={extensionBlock.title}
280281
>
281282
{<extensionBlock.component context={page} />}
282-
</PageBlock>
283+
</BlockComponent>
283284
) : undefined;
284285

285-
if(extensionBlockShouldRender && ExtensionBlock) {
286-
finalChildArray.push(ExtensionBlock)
286+
if (extensionBlockShouldRender && ExtensionBlock) {
287+
finalChildArray.push(ExtensionBlock);
287288
}
288289
}
289290

290291
// If all blocks were "before", insert child block at the end
291292
if (!childBlockInserted && !replacementBlockExists) {
292-
finalChildArray.push(childBlock)
293+
finalChildArray.push(childBlock);
293294
}
294-
295295
} else {
296296
finalChildArray.push(childBlock);
297297
}
298298
}
299299
}
300300

301+
const fullWidthBlocks = finalChildArray.filter(
302+
child => isPageBlock(child) && isOfType(child, FullWidthPageBlock),
303+
);
304+
const mainBlocks = finalChildArray.filter(child => isPageBlock(child) && child.props.column === 'main');
305+
const sideBlocks = finalChildArray.filter(child => isPageBlock(child) && child.props.column === 'side');
306+
301307
return (
302308
<div className={cn('w-full space-y-4', className, '@container/layout')}>
303309
{isDesktop ? (
304310
<div className="grid grid-cols-1 gap-4 @3xl/layout:grid-cols-4">
305-
{finalChildArray.map((child, index) => {
306-
const key = child.props?.blockId ?? `block-${index}`;
307-
if(isPageBlock(child )) {
308-
if (isOfType(child, FullWidthPageBlock)) {
309-
return (
310-
<div key={key} className="@md/layout:col-span-5 space-y-4">{child}</div>
311-
);
312-
}
313-
314-
if (child.props.column === 'main') {
315-
return (
316-
<div key={key} className="@3xl/layout:col-span-3 space-y-4">{child}</div>
317-
)
318-
}
319-
320-
if (child.props.column === 'side') {
321-
return (
322-
<div key={key} className="@3xl/layout:col-span-1 space-y-4">{child}</div>
323-
)
324-
}
325-
}
326-
return null
327-
})}
311+
{fullWidthBlocks.length > 0 && (
312+
<div className="@md/layout:col-span-5 space-y-4">{fullWidthBlocks}</div>
313+
)}
314+
<div className="@3xl/layout:col-span-3 space-y-4">{mainBlocks}</div>
315+
<div className="@3xl/layout:col-span-1 space-y-4">{sideBlocks}</div>
328316
</div>
329317
) : (
330318
<div className="space-y-4">{finalChildArray}</div>

0 commit comments

Comments
 (0)