Skip to content

Commit

Permalink
refactor: keep local DropZone preview in sync with Zustand state
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvxd committed Jan 20, 2025
1 parent b33cae4 commit f1dec0e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
7 changes: 4 additions & 3 deletions packages/core/components/DropZone/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,13 @@ const DropZoneEdit = forwardRef<HTMLDivElement, DropZoneProps>(
inNextDeepestArea,
draggedComponentType,
userIsDragging,
preview,
} = useContextStore(ZoneStoreContext, (s) => {
return {
isDeepestZone: s.zoneDepthIndex[zoneCompound] ?? false,
inNextDeepestArea: s.nextAreaDepthIndex[areaId || ""],
draggedItemId: s.draggedItem?.id,
draggedComponentType: s.draggedItem?.data.componentType,
userIsDragging: !!s.draggedItem,
preview: s.previewIndex[zoneCompound],
};
});

Expand Down Expand Up @@ -189,7 +187,10 @@ const DropZoneEdit = forwardRef<HTMLDivElement, DropZoneProps>(
isEnabled = acceptsTarget(draggedComponentType);
}

const contentWithPreview = useContentWithPreview(content, zoneCompound);
const [contentWithPreview, preview] = useContentWithPreview(
content,
zoneCompound
);

const isDropEnabled =
isEnabled &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useAppContext } from "../../Puck/context";
export const useContentWithPreview = (
content: Content,
zoneCompound: string
) => {
): [Content, Preview | undefined] => {
const { draggedItemId, preview, previewExists } = useContextStore(
ZoneStoreContext,
(s) => {
Expand All @@ -30,6 +30,9 @@ export const useContentWithPreview = (
} = useAppContext();

const [contentWithPreview, setContentWithPreview] = useState(content);
const [localPreview, setLocalPreview] = useState<Preview | undefined>(
preview
);

const updateContent = useRenderedCallback(
(content: Content, preview: Preview | undefined, isDragging: boolean) => {
Expand Down Expand Up @@ -71,6 +74,8 @@ export const useContentWithPreview = (
: content
);
}

setLocalPreview(preview);
},
[draggedItemId, previewExists]
);
Expand All @@ -79,5 +84,5 @@ export const useContentWithPreview = (
updateContent(content, preview, isDragging);
}, [content, preview, isDragging]);

return contentWithPreview;
return [contentWithPreview, localPreview];
};

0 comments on commit f1dec0e

Please sign in to comment.