init: the first public commit for AFFiNE

This commit is contained in:
DarkSky
2022-07-22 15:49:21 +08:00
commit e3e3741393
1451 changed files with 108124 additions and 0 deletions
+12
View File
@@ -0,0 +1,12 @@
{
"presets": [
[
"@nrwl/react/babel",
{
"runtime": "automatic",
"useBuiltIns": "usage"
}
]
],
"plugins": []
}
+18
View File
@@ -0,0 +1,18 @@
{
"extends": ["plugin:@nrwl/nx/react", "../../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
+3
View File
@@ -0,0 +1,3 @@
# datasource-state
> global state management for ligo-virgo
+9
View File
@@ -0,0 +1,9 @@
module.exports = {
displayName: 'datasource-state',
preset: '../../../jest.preset.js',
transform: {
'^.+\\.[tj]sx?$': 'babel-jest',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../../coverage/libs/datasource/state',
};
+12
View File
@@ -0,0 +1,12 @@
{
"name": "@toeverything/datasource/state",
"version": "0.0.1",
"license": "MIT",
"dependencies": {
"jotai": "^1.7.4"
},
"devDependencies": {
"authing-js-sdk": "^4.23.33",
"firebase": "^9.8.4"
}
}
+44
View File
@@ -0,0 +1,44 @@
{
"sourceRoot": "libs/datasource/state/src",
"projectType": "library",
"tags": ["datasource:state"],
"targets": {
"build": {
"executor": "@nrwl/web:rollup",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/libs/datasource/state",
"tsConfig": "libs/datasource/state/tsconfig.lib.json",
"project": "libs/datasource/state/package.json",
"entryFile": "libs/datasource/state/src/index.ts",
"external": ["react/jsx-runtime"],
"rollupConfig": "libs/rollup-ts-checker.config.cjs",
"compiler": "babel",
"assets": [
{
"glob": "libs/datasource/state/README.md",
"input": ".",
"output": "."
}
]
}
},
"lint": {
"executor": "@nrwl/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": [
"libs/datasource/state/**/*.{ts,tsx,js,jsx}"
]
}
},
"test": {
"executor": "@nrwl/jest:jest",
"outputs": ["coverage/libs/datasource/state"],
"options": {
"jestConfig": "libs/datasource/state/jest.config.js",
"passWithNoTests": true
}
}
}
}
+3
View File
@@ -0,0 +1,3 @@
export * from './ui';
export * from './user';
export * from './page';
+28
View 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
View 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
View 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;
+26
View File
@@ -0,0 +1,26 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"jsx": "react-jsx",
"allowJs": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": false
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
+22
View File
@@ -0,0 +1,22 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc",
"types": ["node"]
},
"files": [
"../../../node_modules/@nrwl/react/typings/cssmodule.d.ts",
"../../../node_modules/@nrwl/react/typings/image.d.ts"
],
"exclude": [
"**/*.spec.ts",
"**/*.test.ts",
"**/*.spec.tsx",
"**/*.test.tsx",
"**/*.spec.js",
"**/*.test.js",
"**/*.spec.jsx",
"**/*.test.jsx"
],
"include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"]
}
+19
View File
@@ -0,0 +1,19 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": [
"**/*.test.ts",
"**/*.spec.ts",
"**/*.test.tsx",
"**/*.spec.tsx",
"**/*.test.js",
"**/*.spec.js",
"**/*.test.jsx",
"**/*.spec.jsx",
"**/*.d.ts"
]
}