From e46a86b586e96a9fb70899dcad0eb0300a42fc10 Mon Sep 17 00:00:00 2001 From: yassinedorbozgithub Date: Fri, 11 Apr 2025 08:30:08 +0100 Subject: [PATCH] fix: add exception when useBlock is outside the block provider --- .../visual-editor/form/BlockFormProvider.tsx | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/frontend/src/components/visual-editor/form/BlockFormProvider.tsx b/frontend/src/components/visual-editor/form/BlockFormProvider.tsx index 043749a4..d98c9b6c 100644 --- a/frontend/src/components/visual-editor/form/BlockFormProvider.tsx +++ b/frontend/src/components/visual-editor/form/BlockFormProvider.tsx @@ -1,5 +1,5 @@ /* - * Copyright © 2024 Hexastack. All rights reserved. + * Copyright © 2025 Hexastack. All rights reserved. * * Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms: * 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission. @@ -9,13 +9,21 @@ import { createContext, ReactNode, useContext } from "react"; import { FormProvider, UseFormReturn } from "react-hook-form"; -import { IBlockAttributes, IBlock } from "@/types/block.types"; +import { IBlock, IBlockAttributes } from "@/types/block.types"; // Create a custom context for the block value const BlockContext = createContext(undefined); // Custom hook to use block context -export const useBlock = () => useContext(BlockContext); +export const useBlock = () => { + const context = useContext(BlockContext); + + if (!context) { + throw new Error("useBlock must be used within an BlockContext"); + } + + return context; +}; // This component wraps FormProvider and adds block to its context function BlockFormProvider({