mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 20:38:52 +00:00
init: the first public commit for AFFiNE
This commit is contained in:
3
libs/datasource/state/src/index.ts
Normal file
3
libs/datasource/state/src/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from './ui';
|
||||
export * from './user';
|
||||
export * from './page';
|
||||
28
libs/datasource/state/src/page.ts
Normal file
28
libs/datasource/state/src/page.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { useCallback, useEffect } from 'react';
|
||||
import { useParams, useLocation } from 'react-router-dom';
|
||||
import { atom, useAtom } from 'jotai';
|
||||
// import { Virgo } from '@toeverything/components/editor-core';
|
||||
|
||||
// type EditorsMap = Record<string, Virgo>;
|
||||
type EditorsMap = Record<string, any>;
|
||||
|
||||
const _currentEditors = atom<EditorsMap>({} as EditorsMap);
|
||||
|
||||
/** hook for using editors outside page */
|
||||
export const useCurrentEditors = () => {
|
||||
const { workspace_id: workspaceId, page_id: pageId } = useParams();
|
||||
const { pathname } = useLocation();
|
||||
const [currentEditors, setCurrentEditors] = useAtom(_currentEditors);
|
||||
|
||||
useEffect(() => {
|
||||
if (!workspaceId || !pageId) return;
|
||||
if (pathname.split('/').length >= 3) {
|
||||
setCurrentEditors({});
|
||||
}
|
||||
}, [pageId, pathname, setCurrentEditors, workspaceId]);
|
||||
|
||||
return {
|
||||
currentEditors,
|
||||
setCurrentEditors,
|
||||
};
|
||||
};
|
||||
51
libs/datasource/state/src/ui.ts
Normal file
51
libs/datasource/state/src/ui.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { useCallback } from 'react';
|
||||
import { atom, useAtom } from 'jotai';
|
||||
|
||||
const _showSpaceSidebarAtom = atom<boolean>(true);
|
||||
const _fixedDisplayAtom = atom<boolean>(true);
|
||||
|
||||
/** workspace panel status including page-tree, default open */
|
||||
export const useShowSpaceSidebar = () => {
|
||||
const [showSpaceSidebar, setShowSpaceSidebar] = useAtom(
|
||||
_showSpaceSidebarAtom
|
||||
);
|
||||
|
||||
const [fixedDisplay, setAlltime] = useAtom(_fixedDisplayAtom);
|
||||
|
||||
const toggleSpaceSidebar = useCallback(() => {
|
||||
setAlltime(prev => !prev);
|
||||
setShowSpaceSidebar(false);
|
||||
}, [setAlltime, setShowSpaceSidebar]);
|
||||
|
||||
const setSpaceSidebarVisible = useCallback(
|
||||
(visible: boolean) => setShowSpaceSidebar(visible),
|
||||
[setShowSpaceSidebar]
|
||||
);
|
||||
|
||||
return {
|
||||
showSpaceSidebar,
|
||||
fixedDisplay,
|
||||
toggleSpaceSidebar,
|
||||
setSpaceSidebarVisible,
|
||||
};
|
||||
};
|
||||
|
||||
const _showSettingsSidebarAtom = atom<boolean>(false);
|
||||
|
||||
/** settings/layout/comment side panel status, default closed */
|
||||
export const useShowSettingsSidebar = () => {
|
||||
const [showSettingsSidebar, setShowSettingsSidebar] = useAtom(
|
||||
_showSettingsSidebarAtom
|
||||
);
|
||||
|
||||
const toggleSettingsSidebar = useCallback(
|
||||
() => setShowSettingsSidebar(prev => !prev),
|
||||
[setShowSettingsSidebar]
|
||||
);
|
||||
|
||||
return {
|
||||
showSettingsSidebar,
|
||||
setShowSettingsSidebar,
|
||||
toggleSettingsSidebar,
|
||||
};
|
||||
};
|
||||
83
libs/datasource/state/src/user.ts
Normal file
83
libs/datasource/state/src/user.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import {
|
||||
getAuth,
|
||||
onAuthStateChanged,
|
||||
User as FirebaseUser,
|
||||
} from 'firebase/auth';
|
||||
import { atom, useAtom } from 'jotai';
|
||||
import { useEffect, useMemo } from 'react';
|
||||
|
||||
import { useIdentifyUser } from '@toeverything/datasource/feature-flags';
|
||||
import { UserInfo } from '@toeverything/utils';
|
||||
|
||||
function _fromFirebaseUser(firebaseUser: FirebaseUser): UserInfo {
|
||||
return {
|
||||
id: firebaseUser.uid,
|
||||
nickname: firebaseUser.displayName,
|
||||
username: firebaseUser.displayName,
|
||||
email: firebaseUser.email,
|
||||
photo: firebaseUser.photoURL,
|
||||
};
|
||||
}
|
||||
|
||||
const _userAtom = atom<UserInfo | undefined>(undefined as UserInfo);
|
||||
const _loadingAtom = atom<boolean>(true);
|
||||
|
||||
const _useUserAndSpace = () => {
|
||||
const [user, setUser] = useAtom(_userAtom);
|
||||
const [loading, setLoading] = useAtom(_loadingAtom);
|
||||
const identifyUser = useIdentifyUser();
|
||||
|
||||
useEffect(() => {
|
||||
if (loading) {
|
||||
const auth = getAuth();
|
||||
const oncePromise = new Promise<void>(resolve => {
|
||||
// let resolved = false;
|
||||
onAuthStateChanged(auth, async fbuser => {
|
||||
if (fbuser) {
|
||||
const user = _fromFirebaseUser(fbuser);
|
||||
await identifyUser({
|
||||
userName: user.nickname,
|
||||
id: user.id,
|
||||
email: user.email,
|
||||
// country: user.city
|
||||
});
|
||||
setUser(user);
|
||||
setLoading(false);
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
Promise.all([oncePromise]).finally(() => {
|
||||
setLoading(false);
|
||||
});
|
||||
}
|
||||
}, []);
|
||||
|
||||
const currentSpaceId: string | undefined = useMemo(() => user?.id, [user]);
|
||||
|
||||
return {
|
||||
user,
|
||||
currentSpaceId,
|
||||
loading,
|
||||
};
|
||||
};
|
||||
|
||||
const _useUserAndSpacesForFreeLogin = () => {
|
||||
const [loading] = useAtom(_loadingAtom);
|
||||
|
||||
useEffect(() => setLoading(false), []);
|
||||
const BRAND_ID = 'AFFiNE';
|
||||
return {
|
||||
user: {
|
||||
photo: '',
|
||||
id: BRAND_ID,
|
||||
nickname: BRAND_ID,
|
||||
email: '',
|
||||
} as UserInfo,
|
||||
currentSpaceId: BRAND_ID,
|
||||
loading,
|
||||
};
|
||||
};
|
||||
export const useUserAndSpaces = process.env['NX_FREE_LOGIN']
|
||||
? _useUserAndSpacesForFreeLogin
|
||||
: _useUserAndSpace;
|
||||
Reference in New Issue
Block a user