refactor(chat): make external browser launch user initiated
This commit is contained in:
@@ -1,75 +0,0 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useRef } from "react";
|
||||
|
||||
import {
|
||||
openChatInExternalBrowser,
|
||||
recordExternalBrowserPromptShown,
|
||||
resolveExternalBrowserPromptEligibility,
|
||||
} from "@/lib/chat/chat_external_browser";
|
||||
|
||||
import {
|
||||
type ExternalBrowserPromptState,
|
||||
shouldStartExternalBrowserPrompt,
|
||||
} from "../chat-screen.helpers";
|
||||
|
||||
const EXTERNAL_BROWSER_PROMPT_DELAY_MS = 3000;
|
||||
|
||||
export function useExternalBrowserPrompt({
|
||||
hasInitialized,
|
||||
isLoading,
|
||||
loginStatus,
|
||||
}: ExternalBrowserPromptState): void {
|
||||
const promptShownRef = useRef(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (
|
||||
!shouldStartExternalBrowserPrompt({
|
||||
hasInitialized,
|
||||
isLoading,
|
||||
loginStatus,
|
||||
})
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
let mounted = true;
|
||||
let timer: number | undefined;
|
||||
|
||||
const init = async () => {
|
||||
if (promptShownRef.current) return;
|
||||
|
||||
const eligibility = await resolveExternalBrowserPromptEligibility();
|
||||
if (!mounted || !eligibility.canShow) return;
|
||||
|
||||
promptShownRef.current = true;
|
||||
timer = window.setTimeout(() => {
|
||||
if (!mounted) return;
|
||||
void (async () => {
|
||||
try {
|
||||
await recordExternalBrowserPromptShown(eligibility.today);
|
||||
} catch (error) {
|
||||
console.warn(
|
||||
"[chat] Failed to record external browser auto launch",
|
||||
error,
|
||||
);
|
||||
}
|
||||
if (!mounted) return;
|
||||
try {
|
||||
await openChatInExternalBrowser();
|
||||
} catch (error) {
|
||||
console.warn("[chat] Failed to open external browser", error);
|
||||
}
|
||||
})();
|
||||
}, EXTERNAL_BROWSER_PROMPT_DELAY_MS);
|
||||
};
|
||||
|
||||
void init();
|
||||
|
||||
return () => {
|
||||
mounted = false;
|
||||
if (timer !== undefined) window.clearTimeout(timer);
|
||||
};
|
||||
}, [hasInitialized, isLoading, loginStatus]);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user