Files
fusion/packages/dashboard/app/hooks/useAuthOnboarding.ts
gsxdsm 321ec691bd feat(FN-1203): extract App orchestration into focused hooks
- Extract modal manager, app settings, deep-link handling, favorites, and auth onboarding into dedicated hooks
- Rewire App.tsx to consume the new hooks and reduce component-level state/effect complexity
- Preserve favorite-toggle error toast behavior in the updated favorites wiring
- Add hook-level test coverage for modal manager, app settings, deep links, favorites, and auth onboarding flows
2026-04-08 05:53:24 -07:00

49 lines
1.6 KiB
TypeScript

import { useEffect } from "react";
import { fetchAuthStatus, fetchGlobalSettings } from "../api";
import type { SectionId } from "../components/SettingsModal";
export interface UseAuthOnboardingOptions {
projectId?: string;
openModelOnboarding: () => void;
openSettings: (section?: SectionId) => void;
}
/**
* Runs auth/onboarding checks and opens the appropriate setup modal.
*/
export function useAuthOnboarding({
projectId,
openModelOnboarding,
openSettings,
}: UseAuthOnboardingOptions): void {
useEffect(() => {
fetchAuthStatus()
.then(({ providers }) => {
const hasAuthenticatedProvider = providers.some((provider) => provider.authenticated);
const needsSetup = providers.length > 0 && !hasAuthenticatedProvider;
if (needsSetup || (providers.length > 0 && hasAuthenticatedProvider)) {
fetchGlobalSettings()
.then((globalSettings) => {
const hasDefaultModel = !!(globalSettings.defaultProvider && globalSettings.defaultModelId);
const setupIncomplete = !hasAuthenticatedProvider || !hasDefaultModel;
if (!globalSettings.modelOnboardingComplete && setupIncomplete) {
openModelOnboarding();
} else if (!hasAuthenticatedProvider) {
openSettings("authentication");
}
})
.catch(() => {
if (!hasAuthenticatedProvider) {
openModelOnboarding();
}
});
}
})
.catch(() => {
// Fail silently (preserves existing App behavior).
});
}, [projectId, openModelOnboarding, openSettings]);
}