| 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 { useLump } from "../../mainInput/Lump/LumpContext"; |
| 7 | import { useOnboardingCard } from "./useOnboardingCard"; |
| 8 | |
| 9 | export function useSubmitOnboarding(mode: OnboardingModes, isDialog = false) { |
| 10 | const posthog = usePostHog(); |
| 11 | const ideMessenger = useContext(IdeMessengerContext); |
| 12 | const { close: closeOnboardingCard } = useOnboardingCard(); |
| 13 | const { setSelectedSection } = useLump(); |
| 14 | |
| 15 | function submitOnboarding(provider?: string, apiKey?: string) { |
| 16 | const onboardingStatus = getLocalStorage("onboardingStatus"); |
| 17 | |
| 18 | // Always close the onboarding card and update config.yaml |
| 19 | closeOnboardingCard(isDialog); |
| 20 | |
| 21 | ideMessenger.post("onboarding/complete", { |
| 22 | mode, |
| 23 | provider, |
| 24 | apiKey, |
| 25 | }); |
| 26 | |
| 27 | if (onboardingStatus === "Started") { |
| 28 | // Telemetry |
| 29 | posthog.capture("Onboarding Step", { status: "Completed" }); |
| 30 | posthog.capture("onboardingSelection", { |
| 31 | mode, |
| 32 | }); |
| 33 | |
| 34 | // Local state |
| 35 | setLocalStorage("onboardingStatus", "Completed"); |
| 36 | |
| 37 | // Move to next step in onboarding |
| 38 | ideMessenger.post("showTutorial", undefined); |
| 39 | } |
| 40 | |
| 41 | ideMessenger.post("config/openProfile", { profileId: undefined }); |
| 42 | setSelectedSection("models"); |
| 43 | } |
| 44 | |
| 45 | return { |
| 46 | submitOnboarding, |
| 47 | }; |
| 48 | } |