Skip to content

Commit

Permalink
fix: fix crash if component in data is missing from config
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvxd committed Jun 6, 2024
1 parent fb9aa4e commit 0daf478
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
13 changes: 8 additions & 5 deletions packages/core/components/DropZone/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { DropZoneProvider, dropZoneContext } from "./context";
import { getZoneId } from "../../lib/get-zone-id";
import { useAppContext } from "../Puck/context";
import { DropZoneProps } from "./types";
import { PuckContext } from "../../types/Config";
import { ComponentConfig, PuckContext } from "../../types/Config";

const getClassName = getClassNameFactory("DropZone", styles);

Expand Down Expand Up @@ -231,6 +231,12 @@ function DropZoneEdit({ zone, allow, disallow, style }: DropZoneProps) {
</div>
);

const componentConfig: ComponentConfig | undefined =
config.components[item.type];

const label =
componentConfig?.["label"] ?? item.type.toString();

return (
<div
key={item.props.id}
Expand All @@ -244,10 +250,7 @@ function DropZoneEdit({ zone, allow, disallow, style }: DropZoneProps) {
}}
>
<DraggableComponent
label={
config.components[item.type]["label"] ??
item.type.toString()
}
label={label}
id={`draggable-${componentId}`}
index={i}
isSelected={isSelected}
Expand Down
10 changes: 6 additions & 4 deletions packages/core/components/LayerTree/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import styles from "./styles.module.css";
import getClassNameFactory from "../../lib/get-class-name-factory";
import { Config, Data } from "../../types/Config";
import { ComponentConfig, Config, Data } from "../../types/Config";
import { ItemSelector, getItem } from "../../lib/get-item";
import { scrollIntoView } from "../../lib/scroll-into-view";
import { ChevronDown, LayoutGrid, Layers, Type } from "lucide-react";
Expand Down Expand Up @@ -71,6 +71,10 @@ export const LayerTree = ({

const childIsSelected = isChildOfZone(item, selectedItem, ctx);

const componentConfig: ComponentConfig | undefined =
config.components[item.type];
const label = componentConfig?.["label"] ?? item.type.toString();

return (
<li
className={getClassNameLayer({
Expand Down Expand Up @@ -132,9 +136,7 @@ export const LayerTree = ({
<LayoutGrid size="16" />
)}
</div>
<div className={getClassNameLayer("name")}>
{config.components[item.type]["label"] ?? item.type}
</div>
<div className={getClassNameLayer("name")}>{label}</div>
</div>
</button>
</div>
Expand Down
13 changes: 7 additions & 6 deletions packages/core/components/Puck/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,12 @@ export function Puck<UserConfig extends Config = Config>({
setMounted(true);
}, []);

const selectedComponentConfig =
selectedItem && config.components[selectedItem.type];
const selectedComponentLabel = selectedItem
? selectedComponentConfig?.["label"] ?? selectedItem.type.toString()
: "";

return (
<div className={`Puck ${getClassName()}`}>
<AppProvider
Expand Down Expand Up @@ -592,12 +598,7 @@ export function Puck<UserConfig extends Config = Config>({
noPadding
noBorderTop
showBreadcrumbs
title={
selectedItem
? config.components[selectedItem.type]["label"] ??
selectedItem.type
: "Page"
}
title={selectedItem ? selectedComponentLabel : "Page"}
>
<Fields />
</SidebarSection>
Expand Down

0 comments on commit 0daf478

Please sign in to comment.