mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-15 05:37:32 +00:00
feat: auth & workspace load
This commit is contained in:
@@ -17,8 +17,8 @@ export const AffineIcon = () => {
|
||||
fill="#FFF"
|
||||
/>
|
||||
<path
|
||||
fill-rule="evenodd"
|
||||
clip-rule="evenodd"
|
||||
fillRule="evenodd"
|
||||
clipRule="evenodd"
|
||||
d="M18.6303 8.79688L11.2559 29.8393H15.5752L20.2661 15.2858L24.959 29.8393H29.2637L21.8881 8.79688H18.6303Z"
|
||||
fill="#6880FF"
|
||||
/>
|
||||
|
||||
@@ -26,6 +26,7 @@ export const useEnsureWorkspace = () => {
|
||||
meta => meta.id.toString() === router.query.workspaceId
|
||||
) === -1
|
||||
) {
|
||||
debugger;
|
||||
router.push('/404');
|
||||
return;
|
||||
}
|
||||
@@ -35,6 +36,7 @@ export const useEnsureWorkspace = () => {
|
||||
router.query.workspaceId &&
|
||||
router.query.workspaceId !== defaultOutLineWorkspaceId
|
||||
) {
|
||||
debugger;
|
||||
router.push('/404');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ import { useEffect } from 'react';
|
||||
import type { Page } from '@blocksuite/store';
|
||||
import '@blocksuite/blocks';
|
||||
import { EditorContainer } from '@blocksuite/editor';
|
||||
import { BlockSchema } from '@blocksuite/blocks/models';
|
||||
import type { LoadWorkspaceHandler, CreateEditorHandler } from './context';
|
||||
import { getDataCenter } from '@affine/datacenter';
|
||||
|
||||
@@ -19,54 +18,15 @@ const DynamicBlocksuite = ({
|
||||
const openWorkspace: LoadWorkspaceHandler = (workspaceId: string, user) =>
|
||||
// eslint-disable-next-line no-async-promise-executor
|
||||
new Promise(async resolve => {
|
||||
const workspace = await getDataCenter()
|
||||
.then(dc => dc.initWorkspace(workspaceId))
|
||||
.then(w => w.register(BlockSchema));
|
||||
if (workspaceId) {
|
||||
const workspace = await getDataCenter().then(dc =>
|
||||
dc.initWorkspace(workspaceId, 'affine')
|
||||
);
|
||||
|
||||
// console.log('websocket', websocket);
|
||||
console.log('user', user);
|
||||
|
||||
// if (websocket && token.refresh) {
|
||||
// // FIXME: if add websocket provider, the first page will be blank
|
||||
// const ws = new WebsocketProvider(
|
||||
// `ws${window.location.protocol === 'https:' ? 's' : ''}://${
|
||||
// window.location.host
|
||||
// }/api/sync/`,
|
||||
// workspaceId,
|
||||
// workspace.doc,
|
||||
// {
|
||||
// params: {
|
||||
// token: token.refresh,
|
||||
// },
|
||||
// awareness: workspace.meta.awareness.awareness,
|
||||
// }
|
||||
// );
|
||||
//
|
||||
// ws.shouldConnect = false;
|
||||
//
|
||||
// // FIXME: there needs some method to destroy websocket.
|
||||
// // Or we need a manager to manage websocket.
|
||||
// // eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// // @ts-expect-error
|
||||
// workspace.__ws__ = ws;
|
||||
// }
|
||||
|
||||
// const indexDBProvider = workspace.providers.find(
|
||||
// p => p instanceof IndexedDBDocProvider
|
||||
// );
|
||||
|
||||
if (user) {
|
||||
// if after update, the space:meta is empty, then we need to get map with doc
|
||||
workspace.doc.getMap('space:meta');
|
||||
resolve(workspace);
|
||||
} else {
|
||||
resolve(null);
|
||||
}
|
||||
|
||||
// if (indexDBProvider) {
|
||||
// (indexDBProvider as IndexedDBDocProvider).whenSynced.then(() => {
|
||||
// resolve(workspace);
|
||||
// });
|
||||
// } else {
|
||||
resolve(workspace);
|
||||
// }
|
||||
});
|
||||
|
||||
setLoadWorkspaceHandler(openWorkspace);
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
import { useEffect } from 'react';
|
||||
import { AccessTokenMessage, getWorkspaces, token } from '@affine/datacenter';
|
||||
import { LoadWorkspaceHandler } from '../context';
|
||||
|
||||
export const useSyncData = ({
|
||||
loadWorkspaceHandler,
|
||||
}: {
|
||||
loadWorkspaceHandler: LoadWorkspaceHandler;
|
||||
}) => {
|
||||
useEffect(() => {
|
||||
if (!loadWorkspaceHandler) {
|
||||
return;
|
||||
}
|
||||
const start = async () => {
|
||||
const isLogin = await token.refreshToken().catch(() => false);
|
||||
return isLogin;
|
||||
};
|
||||
start();
|
||||
|
||||
const callback = async (user: AccessTokenMessage | null) => {
|
||||
const workspacesMeta = user
|
||||
? await getWorkspaces().catch(() => {
|
||||
return [];
|
||||
})
|
||||
: [];
|
||||
// setState(state => ({
|
||||
// ...state,
|
||||
// user: user,
|
||||
// workspacesMeta,
|
||||
// synced: true,
|
||||
// }));
|
||||
return workspacesMeta;
|
||||
};
|
||||
|
||||
token.onChange(callback);
|
||||
token.refreshToken().catch(err => {
|
||||
// FIXME: should resolve invalid refresh token
|
||||
console.log(err);
|
||||
});
|
||||
return () => {
|
||||
token.offChange(callback);
|
||||
};
|
||||
}, [loadWorkspaceHandler]);
|
||||
};
|
||||
Reference in New Issue
Block a user