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,
|
clearGoogleDriveCache,
|
||||||
googleDriveService,
|
googleDriveService,
|
||||||
} from '../datasource/google_drive_service';
|
} 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.
|
* 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. */
|
/** Callback triggered to clean up state when signing out. */
|
||||||
onSignOut: () => void;
|
onSignOut?: () => void;
|
||||||
/** Callback triggered when authorization succeeds for the failed file. */
|
/** Callback triggered when authorization succeeds for the failed file. */
|
||||||
onAuthSuccess: () => void;
|
onAuthSuccess?: () => void;
|
||||||
}) {
|
}) {
|
||||||
const [showAuthModal, setShowAuthModal] = useState(false);
|
const [showAuthModal, setShowAuthModal] = useState(false);
|
||||||
const [failedFileId, setFailedFileId] = useState<string>();
|
const [failedFileId, setFailedFileId] = useState<string>();
|
||||||
const {hasGoogleToken, setHasGoogleToken} = useGoogleAuth();
|
const [hasGoogleToken, setHasGoogleToken] = useState(
|
||||||
|
() => !!googleDriveService.getAccessToken(),
|
||||||
|
);
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -30,9 +31,11 @@ export function useGoogleDriveAuthFlow(options: {
|
|||||||
await googleDriveService.signOut();
|
await googleDriveService.signOut();
|
||||||
setHasGoogleToken(false);
|
setHasGoogleToken(false);
|
||||||
clearGoogleDriveCache();
|
clearGoogleDriveCache();
|
||||||
options.onSignOut();
|
if (options?.onSignOut) {
|
||||||
|
options.onSignOut();
|
||||||
|
}
|
||||||
navigate({pathname: '/'}, {replace: true});
|
navigate({pathname: '/'}, {replace: true});
|
||||||
}, [setHasGoogleToken, navigate, options]);
|
}, [navigate, options]);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Triggers the OAuth modal presentation for a failed file.
|
* Triggers the OAuth modal presentation for a failed file.
|
||||||
@@ -50,7 +53,9 @@ export function useGoogleDriveAuthFlow(options: {
|
|||||||
setShowAuthModal(false);
|
setShowAuthModal(false);
|
||||||
setHasGoogleToken(true);
|
setHasGoogleToken(true);
|
||||||
if (fileId === failedFileId) {
|
if (fileId === failedFileId) {
|
||||||
options.onAuthSuccess();
|
if (options?.onAuthSuccess) {
|
||||||
|
options.onAuthSuccess();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// If a different file was selected/authorized, navigate to that one.
|
// If a different file was selected/authorized, navigate to that one.
|
||||||
navigate(
|
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 {useMemo} from 'react';
|
||||||
import {useLocation, useNavigate} from 'react-router';
|
import {useLocation} from 'react-router';
|
||||||
import {
|
import {useGoogleDriveAuth} from '../hooks/use_google_drive_auth';
|
||||||
clearGoogleDriveCache,
|
|
||||||
googleDriveService,
|
|
||||||
} from '../datasource/google_drive_service';
|
|
||||||
import {useGoogleAuth} from '../hooks/use_google_auth';
|
|
||||||
import {Intro} from '../intro';
|
import {Intro} from '../intro';
|
||||||
import {TopBar} from '../menu/top_bar';
|
import {TopBar} from '../menu/top_bar';
|
||||||
import {getArguments} from '../util/url_args';
|
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.
|
* It renders the intro text and lists examples alongside its own TopBar.
|
||||||
*/
|
*/
|
||||||
export function IntroPage() {
|
export function IntroPage() {
|
||||||
const {hasGoogleToken, setHasGoogleToken} = useGoogleAuth();
|
const {hasGoogleToken, setHasGoogleToken, onGoogleSignOut} =
|
||||||
const navigate = useNavigate();
|
useGoogleDriveAuth();
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
|
|
||||||
const args = useMemo(() => getArguments(location), [location]);
|
const args = useMemo(() => getArguments(location), [location]);
|
||||||
const standalone = args.standalone;
|
const standalone = args.standalone;
|
||||||
|
|
||||||
async function onGoogleSignOut() {
|
|
||||||
await googleDriveService.signOut();
|
|
||||||
setHasGoogleToken(false);
|
|
||||||
clearGoogleDriveCache();
|
|
||||||
navigate({pathname: '/'}, {replace: true});
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<TopBar
|
<TopBar
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ import {ProgressPill} from '../components/progress_pill';
|
|||||||
import {DataSourceEnum} from '../datasource/data_source';
|
import {DataSourceEnum} from '../datasource/data_source';
|
||||||
import {DonatsoChart} from '../donatso-chart';
|
import {DonatsoChart} from '../donatso-chart';
|
||||||
import {useGenealogyLoader} from '../hooks/use_genealogy_loader';
|
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 {useUrlState} from '../hooks/use_url_state';
|
||||||
import {useWebMcpBridge} from '../hooks/use_webmcp_bridge';
|
import {useWebMcpBridge} from '../hooks/use_webmcp_bridge';
|
||||||
import {GoogleAuthModal} from '../menu/google_auth_modal';
|
import {GoogleAuthModal} from '../menu/google_auth_modal';
|
||||||
@@ -105,7 +105,7 @@ export function ViewPage() {
|
|||||||
triggerAuthError,
|
triggerAuthError,
|
||||||
onAuthSuccess,
|
onAuthSuccess,
|
||||||
onCancel,
|
onCancel,
|
||||||
} = useGoogleDriveAuthFlow({
|
} = useGoogleDriveAuth({
|
||||||
onSignOut: clearData,
|
onSignOut: clearData,
|
||||||
onAuthSuccess: resetLoader,
|
onAuthSuccess: resetLoader,
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user