feat: login reduce

This commit is contained in:
DarkSky
2022-08-11 22:02:53 +08:00
parent b2bac25a92
commit b3f1ba6ca0
4 changed files with 116 additions and 127 deletions

View File

@@ -1,5 +1,5 @@
/* eslint-disable filename-rules/match */ /* eslint-disable filename-rules/match */
import { useCallback, useMemo, useState } from 'react'; import { useCallback, useMemo } from 'react';
import { initializeApp } from 'firebase/app'; import { initializeApp } from 'firebase/app';
import { import {
GoogleAuthProvider, GoogleAuthProvider,
@@ -8,15 +8,7 @@ import {
browserLocalPersistence, browserLocalPersistence,
} from 'firebase/auth'; } from 'firebase/auth';
import { LogoImg } from '@toeverything/components/common'; import { MuiButton } from '@toeverything/components/ui';
import {
MuiButton,
MuiBox,
MuiGrid,
MuiSnackbar,
} from '@toeverything/components/ui';
import { Error } from './../error';
const _firebaseConfig = { const _firebaseConfig = {
apiKey: 'AIzaSyD7A_VyGaKTXsPqtga9IbwrEsbWWc4rH3Y', apiKey: 'AIzaSyD7A_VyGaKTXsPqtga9IbwrEsbWWc4rH3Y',
@@ -75,7 +67,7 @@ const GoogleIcon = () => (
</svg> </svg>
); );
export const Firebase = () => { export const Firebase = (props: { onError: () => void }) => {
const [auth, provider] = useMemo(() => { const [auth, provider] = useMemo(() => {
const auth = getAuth(_app); const auth = getAuth(_app);
auth.setPersistence(browserLocalPersistence); auth.setPersistence(browserLocalPersistence);
@@ -83,8 +75,6 @@ export const Firebase = () => {
return [auth, provider]; return [auth, provider];
}, []); }, []);
const [error, setError] = useState(false);
const handleAuth = useCallback(() => { const handleAuth = useCallback(() => {
signInWithPopup(auth, provider).catch(error => { signInWithPopup(auth, provider).catch(error => {
const errorCode = error.code; const errorCode = error.code;
@@ -92,53 +82,19 @@ export const Firebase = () => {
const email = error.customData.email; const email = error.customData.email;
const credential = GoogleAuthProvider.credentialFromError(error); const credential = GoogleAuthProvider.credentialFromError(error);
console.log(errorCode, errorMessage, email, credential); console.log(errorCode, errorMessage, email, credential);
setError(true); props.onError();
setTimeout(() => setError(false), 3000);
}); });
}, [auth, provider]); }, [auth, props, provider]);
return ( return (
<MuiGrid container> <MuiButton
<MuiSnackbar variant="outlined"
anchorOrigin={{ vertical: 'top', horizontal: 'right' }} fullWidth
open={error} style={{ textTransform: 'none' }}
message="Login failed, please check if you have permission" startIcon={<GoogleIcon />}
/> onClick={handleAuth}
<MuiGrid item xs={8}> >
<Error Continue with Google
title="Welcome to AFFiNE" </MuiButton>
subTitle="blocks of knowledge to power your team"
action1Text="Login &nbsp; or &nbsp; Register"
/>
</MuiGrid>
<MuiGrid item xs={4}>
<MuiBox
onSubmit={handleAuth}
onClick={handleAuth}
style={{
textAlign: 'center',
width: '300px',
margin: '300px auto 20px auto',
}}
sx={{ mt: 1 }}
>
<LogoImg
style={{
width: '100px',
}}
/>
<MuiButton
variant="outlined"
fullWidth
style={{ textTransform: 'none' }}
startIcon={<GoogleIcon />}
>
Continue with Google
</MuiButton>
</MuiBox>
</MuiGrid>
</MuiGrid>
); );
}; };

View File

@@ -1,20 +1,22 @@
/* eslint-disable filename-rules/match */ /* eslint-disable filename-rules/match */
import { useEffect, useState } from 'react'; import { useEffect, useMemo } from 'react';
import { LogoImg } from '@toeverything/components/common'; import { LogoIcon } from '@toeverything/components/icons';
import { import { MuiButton } from '@toeverything/components/ui';
MuiButton,
MuiBox,
MuiGrid,
MuiSnackbar,
} from '@toeverything/components/ui';
import { services } from '@toeverything/datasource/db-service'; import { services } from '@toeverything/datasource/db-service';
import { useLocalTrigger } from '@toeverything/datasource/state'; import { useLocalTrigger } from '@toeverything/datasource/state';
import { Error } from './../error'; const cleanupWorkspace = (workspace: string) =>
new Promise((resolve, reject) => {
const req = indexedDB.deleteDatabase(workspace);
req.addEventListener('error', e => reject(e));
req.addEventListener('blocked', e => reject(e));
req.addEventListener('upgradeneeded', e => reject(e));
req.addEventListener('success', e => resolve(e));
});
const requestPermission = async (workspace: string) => { const requestPermission = async (workspace: string) => {
indexedDB.deleteDatabase(workspace); await cleanupWorkspace(workspace);
const dirHandler = await window.showDirectoryPicker({ const dirHandler = await window.showDirectoryPicker({
id: 'AFFiNE_' + workspace, id: 'AFFiNE_' + workspace,
mode: 'readwrite', mode: 'readwrite',
@@ -43,9 +45,16 @@ const requestPermission = async (workspace: string) => {
); );
}; };
export const FileSystem = () => { export const FileSystem = (props: { onError: () => void }) => {
const onSelected = useLocalTrigger(); const onSelected = useLocalTrigger();
const [error, setError] = useState(false);
const apiSupported = useMemo(() => {
try {
return 'showOpenFilePicker' in window;
} catch (e) {
return false;
}
}, []);
useEffect(() => { useEffect(() => {
if (process.env['NX_E2E']) { if (process.env['NX_E2E']) {
@@ -54,54 +63,25 @@ export const FileSystem = () => {
}, []); }, []);
return ( return (
<MuiGrid container> <MuiButton
<MuiSnackbar variant="outlined"
anchorOrigin={{ vertical: 'top', horizontal: 'right' }} fullWidth
open={error} style={{ textTransform: 'none' }}
message="Login failed, please check if you have permission" startIcon={<LogoIcon />}
/> onClick={async () => {
<MuiGrid item xs={8}> try {
<Error if (apiSupported) {
title="Welcome to AFFiNE" await requestPermission('AFFiNE');
subTitle="blocks of knowledge to power your team" onSelected();
action1Text="Login &nbsp; or &nbsp; Register" } else {
/> onSelected();
</MuiGrid> }
} catch (e) {
<MuiGrid item xs={4}> props.onError();
<MuiBox }
onClick={async () => { }}
try { >
await requestPermission('AFFiNE'); {apiSupported ? 'Sync to Disk' : 'Try Live Demo'}
onSelected(); </MuiButton>
} catch (e) {
setError(true);
onSelected();
setTimeout(() => setError(false), 3000);
}
}}
style={{
textAlign: 'center',
width: '300px',
margin: '300px auto 20px auto',
}}
sx={{ mt: 1 }}
>
<LogoImg
style={{
width: '100px',
}}
/>
<MuiButton
variant="outlined"
fullWidth
style={{ textTransform: 'none' }}
>
Sync to Disk
</MuiButton>
</MuiBox>
</MuiGrid>
</MuiGrid>
); );
}; };

View File

@@ -1,13 +1,64 @@
/* eslint-disable filename-rules/match */ /* eslint-disable filename-rules/match */
import { useCallback, useState } from 'react';
import { LogoImg } from '@toeverything/components/common';
import { MuiBox, MuiGrid, MuiSnackbar } from '@toeverything/components/ui';
// import { Authing } from './authing'; // import { Authing } from './authing';
import { Firebase } from './firebase'; import { Firebase } from './firebase';
import { FileSystem } from './fs'; import { FileSystem } from './fs';
import { Error } from './../error';
export function Login() { export function Login() {
const [error, setError] = useState(false);
const onError = useCallback(() => {
setError(true);
setTimeout(() => setError(false), 3000);
}, []);
return ( return (
<> <MuiGrid container>
{/* <Authing /> */} <MuiSnackbar
{process.env['NX_LOCAL'] ? <FileSystem /> : <Firebase />} anchorOrigin={{ vertical: 'top', horizontal: 'right' }}
</> open={error}
message="Login failed, please check if you have permission"
/>
<MuiGrid item xs={8}>
<Error
title="Welcome to AFFiNE"
subTitle="blocks of knowledge to power your team"
action1Text="Login &nbsp; or &nbsp; Register"
/>
</MuiGrid>
<MuiGrid item xs={4}>
{' '}
<MuiBox
style={{
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
rowGap: '1em',
width: '300px',
margin: '300px auto 20px auto',
}}
sx={{ mt: 1 }}
>
<LogoImg
style={{
width: '100px',
}}
/>
{/* {((process.env['NX_LOCAL'] || true) && ( */}
<FileSystem onError={onError} />
{/* )) ||
null}
{((!process.env['NX_LOCAL'] || true) && ( */}
<Firebase onError={onError} />
{/* )) ||
null} */}{' '}
</MuiBox>
</MuiGrid>
</MuiGrid>
); );
} }

View File

@@ -3,12 +3,14 @@ import { useParams } from 'react-router-dom';
import { Typography, styled } from '@toeverything/components/ui'; import { Typography, styled } from '@toeverything/components/ui';
import { services } from '@toeverything/datasource/db-service'; import { services } from '@toeverything/datasource/db-service';
import { useUserAndSpaces } from '@toeverything/datasource/state';
/* card.7: Demand changes, temporarily closed, see https://github.com/toeverything/AFFiNE/issues/522 */ /* card.7: Demand changes, temporarily closed, see https://github.com/toeverything/AFFiNE/issues/522 */
// import { usePageTree} from '@toeverything/components/layout'; // import { usePageTree} from '@toeverything/components/layout';
// import { pickPath } from './utils'; // import { pickPath } from './utils';
export const CurrentPageTitle = () => { export const CurrentPageTitle = () => {
const { user } = useUserAndSpaces();
const params = useParams(); const params = useParams();
const { workspace_id } = params; const { workspace_id } = params;
const [pageId, setPageId] = useState<string>(''); const [pageId, setPageId] = useState<string>('');
@@ -40,11 +42,11 @@ export const CurrentPageTitle = () => {
}, [pageId, workspace_id]); }, [pageId, workspace_id]);
useEffect(() => { useEffect(() => {
fetchPageTitle(); if (user) fetchPageTitle();
}, [fetchPageTitle]); }, [fetchPageTitle, user]);
useEffect(() => { useEffect(() => {
if (!workspace_id || !pageId || pageTitle === undefined) if (!user || !workspace_id || !pageId || pageTitle === undefined)
return () => {}; return () => {};
let unobserve: () => void; let unobserve: () => void;
@@ -63,7 +65,7 @@ export const CurrentPageTitle = () => {
return () => { return () => {
// unobserve?.(); // unobserve?.();
}; };
}, [fetchPageTitle, pageId, pageTitle, workspace_id]); }, [fetchPageTitle, pageId, pageTitle, user, workspace_id]);
useEffect(() => { useEffect(() => {
document.title = pageTitle || ''; document.title = pageTitle || '';