From 8e18a5e347c5f21498827c0f84a48a98d1d8159f Mon Sep 17 00:00:00 2001 From: Mohammed Emad Date: Mon, 8 Sep 2025 16:00:45 +0300 Subject: [PATCH 1/2] feat: add onSetup Prop to the Formbricks component --- .../src/components/formbricks.tsx | 67 ++++++++++--------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/packages/react-native/src/components/formbricks.tsx b/packages/react-native/src/components/formbricks.tsx index 53e35b6..b0d666e 100644 --- a/packages/react-native/src/components/formbricks.tsx +++ b/packages/react-native/src/components/formbricks.tsx @@ -5,39 +5,46 @@ import { SurveyStore } from "@/lib/survey/store"; import React, { useCallback, useEffect, useSyncExternalStore } from "react"; interface FormbricksProps { - appUrl: string; - environmentId: string; + appUrl: string; + environmentId: string; + onSetup?: () => void; } const surveyStore = SurveyStore.getInstance(); const logger = Logger.getInstance(); -export function Formbricks({ appUrl, environmentId }: FormbricksProps): React.JSX.Element | null { - // initializes sdk - useEffect(() => { - const setupFormbricks = async (): Promise => { - try { - await setup({ - environmentId, - appUrl, - }); - } catch { - logger.debug("Initialization failed"); - } - }; - - setupFormbricks().catch(() => { - logger.debug("Initialization error"); - }); - }, [environmentId, appUrl]); - - const subscribe = useCallback((callback: () => void) => { - const unsubscribe = surveyStore.subscribe(callback); - return unsubscribe; - }, []); - - const getSnapshot = useCallback(() => surveyStore.getSurvey(), []); - const survey = useSyncExternalStore(subscribe, getSnapshot); - - return survey ? : null; +export function Formbricks({ + appUrl, + environmentId, + onSetup, +}: FormbricksProps): React.JSX.Element | null { + // initializes sdk + useEffect(() => { + const setupFormbricks = async (): Promise => { + try { + await setup({ + environmentId, + appUrl, + }); + + onSetup?.(); + } catch { + logger.debug("Initialization failed"); + } + }; + + setupFormbricks().catch(() => { + logger.debug("Initialization error"); + }); + }, [environmentId, appUrl]); + + const subscribe = useCallback((callback: () => void) => { + const unsubscribe = surveyStore.subscribe(callback); + return unsubscribe; + }, []); + + const getSnapshot = useCallback(() => surveyStore.getSurvey(), []); + const survey = useSyncExternalStore(subscribe, getSnapshot); + + return survey ? : null; } From ba0159974aa16bf07b3086d736c0c6629db44c7b Mon Sep 17 00:00:00 2001 From: Mohammed Emad Date: Mon, 8 Sep 2025 16:43:16 +0300 Subject: [PATCH 2/2] fix(formbricks): correct error logging in onSetup handling - Replaced unsafe error?.message access with String(error) to satisfy TS union types - Ensured onSetup only runs when setup succeeds - Improved error logging to preserve details and removed redundant catch --- .../react-native/src/components/formbricks.tsx | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/react-native/src/components/formbricks.tsx b/packages/react-native/src/components/formbricks.tsx index b0d666e..03455b6 100644 --- a/packages/react-native/src/components/formbricks.tsx +++ b/packages/react-native/src/components/formbricks.tsx @@ -22,14 +22,22 @@ export function Formbricks({ useEffect(() => { const setupFormbricks = async (): Promise => { try { - await setup({ + const result = await setup({ environmentId, appUrl, }); - onSetup?.(); - } catch { - logger.debug("Initialization failed"); + if (result.ok) { + onSetup?.(); + } else { + logger.error(`Initialization failed: ${String(result.error)}`); + } + } catch (err) { + logger.error( + `Initialization threw: ${ + err instanceof Error ? err?.message : String(err) + }` + ); } };