import { useSettings } from "@settings/contexts/SettingsContext"; import { saveApiKey } from "@app/services/api"; export type ConnectApiKeyResult = | { ok: true } | { ok: false; message: string }; /** * Handles the first-time API key connection flow used by the onboarding * splash screen: the server validates the key against the Biogr.id API, * persists the flat settings, and upserts each linked Instagram account's * config record (see WP-4403). */ export const useConnectApiKey = () => { const { initialSettings, setInitialSettings } = useSettings(); const connect = async (apiKey: string): Promise => { const resp = await saveApiKey(apiKey); if (resp?.error) { return { ok: false, message: resp?.message || sfsyncI18n.data.splash_connect_error_generic, }; } setInitialSettings({ ...initialSettings, api_key: apiKey, api_key_verified: resp.api_key_verified ?? true, selected_config: resp.selected_config ?? initialSettings.selected_config, config_list: { ...(initialSettings.config_list || {}), ...(resp.config_list || {}), }, }); return { ok: true }; }; return { connect }; };