@@ -5,39 +5,46 @@ import { SurveyStore } from "@/lib/survey/store";
55import React , { useCallback , useEffect , useSyncExternalStore } from "react" ;
66
77interface FormbricksProps {
8- appUrl : string ;
9- environmentId : string ;
8+ appUrl : string ;
9+ environmentId : string ;
10+ onSetup ?: ( ) => void ;
1011}
1112
1213const surveyStore = SurveyStore . getInstance ( ) ;
1314const logger = Logger . getInstance ( ) ;
1415
15- export function Formbricks ( { appUrl, environmentId } : FormbricksProps ) : React . JSX . Element | null {
16- // initializes sdk
17- useEffect ( ( ) => {
18- const setupFormbricks = async ( ) : Promise < void > => {
19- try {
20- await setup ( {
21- environmentId,
22- appUrl,
23- } ) ;
24- } catch {
25- logger . debug ( "Initialization failed" ) ;
26- }
27- } ;
28-
29- setupFormbricks ( ) . catch ( ( ) => {
30- logger . debug ( "Initialization error" ) ;
31- } ) ;
32- } , [ environmentId , appUrl ] ) ;
33-
34- const subscribe = useCallback ( ( callback : ( ) => void ) => {
35- const unsubscribe = surveyStore . subscribe ( callback ) ;
36- return unsubscribe ;
37- } , [ ] ) ;
38-
39- const getSnapshot = useCallback ( ( ) => surveyStore . getSurvey ( ) , [ ] ) ;
40- const survey = useSyncExternalStore ( subscribe , getSnapshot ) ;
41-
42- return survey ? < SurveyWebView survey = { survey } /> : null ;
16+ export function Formbricks ( {
17+ appUrl,
18+ environmentId,
19+ onSetup,
20+ } : FormbricksProps ) : React . JSX . Element | null {
21+ // initializes sdk
22+ useEffect ( ( ) => {
23+ const setupFormbricks = async ( ) : Promise < void > => {
24+ try {
25+ await setup ( {
26+ environmentId,
27+ appUrl,
28+ } ) ;
29+
30+ onSetup ?.( ) ;
31+ } catch {
32+ logger . debug ( "Initialization failed" ) ;
33+ }
34+ } ;
35+
36+ setupFormbricks ( ) . catch ( ( ) => {
37+ logger . debug ( "Initialization error" ) ;
38+ } ) ;
39+ } , [ environmentId , appUrl ] ) ;
40+
41+ const subscribe = useCallback ( ( callback : ( ) => void ) => {
42+ const unsubscribe = surveyStore . subscribe ( callback ) ;
43+ return unsubscribe ;
44+ } , [ ] ) ;
45+
46+ const getSnapshot = useCallback ( ( ) => surveyStore . getSurvey ( ) , [ ] ) ;
47+ const survey = useSyncExternalStore ( subscribe , getSnapshot ) ;
48+
49+ return survey ? < SurveyWebView survey = { survey } /> : null ;
4350}
0 commit comments