-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add onSetup Prop to the Formbricks component #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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<void> => { | ||||||||||||||||||||||||||
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 ? <SurveyWebView survey={survey} /> : null; | ||||||||||||||||||||||||||
export function Formbricks({ | ||||||||||||||||||||||||||
appUrl, | ||||||||||||||||||||||||||
environmentId, | ||||||||||||||||||||||||||
onSetup, | ||||||||||||||||||||||||||
}: FormbricksProps): React.JSX.Element | null { | ||||||||||||||||||||||||||
// initializes sdk | ||||||||||||||||||||||||||
useEffect(() => { | ||||||||||||||||||||||||||
const setupFormbricks = async (): Promise<void> => { | ||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||
const result = await setup({ | ||||||||||||||||||||||||||
environmentId, | ||||||||||||||||||||||||||
appUrl, | ||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if (result.ok) { | ||||||||||||||||||||||||||
onSetup?.(); | ||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||
logger.error(`Initialization failed: ${String(result.error)}`); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
Comment on lines
+30
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick (assertive) Prefer robust error message extraction to avoid "object Object". Log the message when present; fall back to stringifying the error. - } else {
- logger.error(`Initialization failed: ${String(result.error)}`);
- }
+ } else {
+ const msg =
+ (result as any)?.error?.message ?? JSON.stringify(result.error);
+ logger.error(`Initialization failed: ${msg}`);
+ } 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||
} 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 ? <SurveyWebView survey={survey} /> : null; | ||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧹 Nitpick (assertive)
Keep the latest onSetup without retriggering setup; also prevent duplicate calls per env/app pair.
React 18 StrictMode can double-invoke effects in dev; setup() may also return ok when already initialized. Use refs to (a) always call the latest onSetup and (b) ensure it fires once per (environmentId, appUrl).
Also applies to: 30-31
🤖 Prompt for AI Agents