| 1 | import { OnboardingModes } from "core/protocol/core"; |
| 2 | import { usePostHog } from "posthog-js/react"; |
| 3 | import { useContext } from "react"; |
| 4 | import { IdeMessengerContext } from "../../../context/IdeMessenger"; |
| 5 | import { getLocalStorage, setLocalStorage } from "../../../util/localStorage"; |
| 6 | import { useOnboardingCard } from "./useOnboardingCard"; |
| 7 | |
| 8 | export function useSubmitOnboarding(mode: OnboardingModes, isDialog = false) { |
| 9 | const posthog = usePostHog(); |
| 10 | const ideMessenger = useContext(IdeMessengerContext); |
| 11 | const { close: closeOnboardingCard } = useOnboardingCard(); |
| 12 | |
| 13 | function submitOnboarding(provider?: string, apiKey?: string) { |
| 14 | const onboardingStatus = getLocalStorage("onboardingStatus"); |
| 15 | |
| 16 | // Always close the onboarding card and update config.yaml |
| 17 | closeOnboardingCard(isDialog); |
| 18 | |
| 19 | ideMessenger.post("onboarding/complete", { |
| 20 | mode, |
| 21 | provider, |
| 22 | apiKey, |
| 23 | }); |
| 24 | |
| 25 | if (onboardingStatus === "Started") { |
| 26 | // Telemetry |
| 27 | posthog.capture("Onboarding Step", { status: "Completed" }); |
| 28 | posthog.capture("onboardingSelection", { |
| 29 | mode, |
| 30 | }); |
| 31 | |
| 32 | // Local state |
| 33 | setLocalStorage("onboardingStatus", "Completed"); |
| 34 | |
| 35 | // Move to next step in onboarding |
| 36 | ideMessenger.post("showTutorial", undefined); |
| 37 | } |
| 38 | |
| 39 | ideMessenger.post("config/openProfile", { profileId: undefined }); |
| 40 | } |
| 41 | |
| 42 | return { |
| 43 | submitOnboarding, |
| 44 | }; |
| 45 | } |