Skip to content

Commit

Permalink
set currentBlock to null when removing the current block
Browse files Browse the repository at this point in the history
  • Loading branch information
ozhanefemeral committed Aug 2, 2024
1 parent 3b6ebbb commit a1cb036
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions apps/next/app/features/code-generator/SortableBlockList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
import { CSS } from "@dnd-kit/utilities";
import { BlockViewRenderer } from "@/components/blocks";
import { Cross1Icon } from "@radix-ui/react-icons";

import { useBlockEditor } from "@/contexts/BlockEditorContext";

type SortableItemProps = {
block: CodeBlock;
Expand All @@ -37,6 +37,7 @@ const SortableItem: React.FC<SortableItemProps> = ({
}) => {
const { attributes, listeners, setNodeRef, transform, transition } =
useSortable({ id: index });
const { setCurrentBlock, currentBlock } = useBlockEditor();

const style = {
transform: CSS.Transform.toString(transform),
Expand All @@ -49,6 +50,10 @@ const SortableItem: React.FC<SortableItemProps> = ({
) => {
event.preventDefault();
onRemove(index);

if (currentBlock?.id === block.id) {
setCurrentBlock(null);
}
};

return (
Expand Down Expand Up @@ -104,7 +109,10 @@ export const SortableBlockList: React.FC = () => {
});
};

const blocksWithId = state.blocks.map((block, index) => ({ ...block, id: index + 1 }));
const blocksWithId = state.blocks.map((block, index) => ({
...block,
id: index + 1,
}));

return (
<div className="bg-gray-100 p-4 rounded-lg">
Expand Down Expand Up @@ -135,4 +143,4 @@ export const SortableBlockList: React.FC = () => {
)}
</div>
);
};
};

0 comments on commit a1cb036

Please sign in to comment.