diff --git a/packages/react-native/src/components/formbricks.tsx b/packages/react-native/src/components/formbricks.tsx index 53e35b6..03455b6 100644 --- a/packages/react-native/src/components/formbricks.tsx +++ b/packages/react-native/src/components/formbricks.tsx @@ -5,39 +5,54 @@ 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 { + const result = await setup({ + environmentId, + appUrl, + }); + + 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) + }` + ); + } + }; + + 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; }