mirror of
https://github.com/PeWu/topola-viewer.git
synced 2026-07-17 17:21:48 +00:00
Merge 2 google drive hooks
This commit is contained in:
@@ -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,
|
||||
};
|
||||
}
|
||||
@@ -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<string>();
|
||||
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],
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -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 (
|
||||
<>
|
||||
<TopBar
|
||||
|
||||
@@ -15,7 +15,7 @@ import {ProgressPill} from '../components/progress_pill';
|
||||
import {DataSourceEnum} from '../datasource/data_source';
|
||||
import {DonatsoChart} from '../donatso-chart';
|
||||
import {useGenealogyLoader} from '../hooks/use_genealogy_loader';
|
||||
import {useGoogleDriveAuthFlow} from '../hooks/use_google_drive_auth_flow';
|
||||
import {useGoogleDriveAuth} from '../hooks/use_google_drive_auth';
|
||||
import {useUrlState} from '../hooks/use_url_state';
|
||||
import {useWebMcpBridge} from '../hooks/use_webmcp_bridge';
|
||||
import {GoogleAuthModal} from '../menu/google_auth_modal';
|
||||
@@ -105,7 +105,7 @@ export function ViewPage() {
|
||||
triggerAuthError,
|
||||
onAuthSuccess,
|
||||
onCancel,
|
||||
} = useGoogleDriveAuthFlow({
|
||||
} = useGoogleDriveAuth({
|
||||
onSignOut: clearData,
|
||||
onAuthSuccess: resetLoader,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user