diff --git a/src/hooks/use_google_auth.ts b/src/hooks/use_google_auth.ts deleted file mode 100644 index 19674de..0000000 --- a/src/hooks/use_google_auth.ts +++ /dev/null @@ -1,19 +0,0 @@ -import {useState} from 'react'; -import {googleDriveService} from '../datasource/google_drive_service'; - -/** - * Custom React hook to manage and track the state of Google Drive OAuth2 credentials. - */ -export function useGoogleAuth() { - /** Tracks whether the user has a valid cached Google Drive OAuth access token. */ - const [hasGoogleToken, setHasGoogleToken] = useState( - () => !!googleDriveService.getAccessToken(), - ); - - return { - /** Tracks whether the user has a valid cached Google Drive OAuth access token. */ - hasGoogleToken, - /** Function to update the token presence state in React. */ - setHasGoogleToken, - }; -} diff --git a/src/hooks/use_google_drive_auth_flow.ts b/src/hooks/use_google_drive_auth.ts similarity index 82% rename from src/hooks/use_google_drive_auth_flow.ts rename to src/hooks/use_google_drive_auth.ts index fdbacb4..903c4a0 100644 --- a/src/hooks/use_google_drive_auth_flow.ts +++ b/src/hooks/use_google_drive_auth.ts @@ -5,21 +5,22 @@ import { clearGoogleDriveCache, googleDriveService, } from '../datasource/google_drive_service'; -import {useGoogleAuth} from './use_google_auth'; /** - * Custom React hook that encapsulates the Google Drive OAuth authorization flow. + * Custom React hook that encapsulates the Google Drive OAuth authorization state and flow. * It manages token states, modal triggers, sign-out sessions, and navigation flows. */ -export function useGoogleDriveAuthFlow(options: { +export function useGoogleDriveAuth(options?: { /** Callback triggered to clean up state when signing out. */ - onSignOut: () => void; + onSignOut?: () => void; /** Callback triggered when authorization succeeds for the failed file. */ - onAuthSuccess: () => void; + onAuthSuccess?: () => void; }) { const [showAuthModal, setShowAuthModal] = useState(false); const [failedFileId, setFailedFileId] = useState(); - const {hasGoogleToken, setHasGoogleToken} = useGoogleAuth(); + const [hasGoogleToken, setHasGoogleToken] = useState( + () => !!googleDriveService.getAccessToken(), + ); const navigate = useNavigate(); /** @@ -30,9 +31,11 @@ export function useGoogleDriveAuthFlow(options: { await googleDriveService.signOut(); setHasGoogleToken(false); clearGoogleDriveCache(); - options.onSignOut(); + if (options?.onSignOut) { + options.onSignOut(); + } navigate({pathname: '/'}, {replace: true}); - }, [setHasGoogleToken, navigate, options]); + }, [navigate, options]); /** * Triggers the OAuth modal presentation for a failed file. @@ -50,7 +53,9 @@ export function useGoogleDriveAuthFlow(options: { setShowAuthModal(false); setHasGoogleToken(true); if (fileId === failedFileId) { - options.onAuthSuccess(); + if (options?.onAuthSuccess) { + options.onAuthSuccess(); + } } else { // If a different file was selected/authorized, navigate to that one. navigate( @@ -65,7 +70,7 @@ export function useGoogleDriveAuthFlow(options: { ); } }, - [failedFileId, navigate, setHasGoogleToken, options], + [failedFileId, navigate, options], ); /** diff --git a/src/pages/intro_page.tsx b/src/pages/intro_page.tsx index cd7ff43..8164b25 100644 --- a/src/pages/intro_page.tsx +++ b/src/pages/intro_page.tsx @@ -1,10 +1,6 @@ import {useMemo} from 'react'; -import {useLocation, useNavigate} from 'react-router'; -import { - clearGoogleDriveCache, - googleDriveService, -} from '../datasource/google_drive_service'; -import {useGoogleAuth} from '../hooks/use_google_auth'; +import {useLocation} from 'react-router'; +import {useGoogleDriveAuth} from '../hooks/use_google_drive_auth'; import {Intro} from '../intro'; import {TopBar} from '../menu/top_bar'; import {getArguments} from '../util/url_args'; @@ -14,20 +10,13 @@ import {getArguments} from '../util/url_args'; * It renders the intro text and lists examples alongside its own TopBar. */ export function IntroPage() { - const {hasGoogleToken, setHasGoogleToken} = useGoogleAuth(); - const navigate = useNavigate(); + const {hasGoogleToken, setHasGoogleToken, onGoogleSignOut} = + useGoogleDriveAuth(); const location = useLocation(); const args = useMemo(() => getArguments(location), [location]); const standalone = args.standalone; - async function onGoogleSignOut() { - await googleDriveService.signOut(); - setHasGoogleToken(false); - clearGoogleDriveCache(); - navigate({pathname: '/'}, {replace: true}); - } - return ( <>