mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-14 05:14:54 +00:00
refactor!: next generation AFFiNE code structure (#1176)
This commit is contained in:
36
apps/web/src/atoms/index.ts
Normal file
36
apps/web/src/atoms/index.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { atom } from 'jotai';
|
||||
import { createStore } from 'jotai/index';
|
||||
import { atomWithStorage } from 'jotai/utils';
|
||||
import { unstable_batchedUpdates } from 'react-dom';
|
||||
|
||||
// workspace necessary atoms
|
||||
export const currentWorkspaceIdAtom = atomWithStorage<string | null>(
|
||||
'affine-current-workspace-id',
|
||||
null
|
||||
);
|
||||
export const currentPageIdAtom = atomWithStorage<string | null>(
|
||||
'affine-current-page-id',
|
||||
null
|
||||
);
|
||||
// If the workspace is locked, it means that the user maybe updating the workspace
|
||||
// from local to remote or vice versa
|
||||
export const workspaceLockAtom = atom(false);
|
||||
export async function lockMutex(fn: () => Promise<unknown>) {
|
||||
if (jotaiStore.get(workspaceLockAtom)) {
|
||||
throw new Error('Workspace is locked');
|
||||
}
|
||||
unstable_batchedUpdates(() => {
|
||||
jotaiStore.set(workspaceLockAtom, true);
|
||||
});
|
||||
await fn();
|
||||
unstable_batchedUpdates(() => {
|
||||
jotaiStore.set(workspaceLockAtom, false);
|
||||
});
|
||||
}
|
||||
|
||||
// modal atoms
|
||||
export const openWorkspacesModalAtom = atom(false);
|
||||
export const openCreateWorkspaceModalAtom = atom(false);
|
||||
export const openQuickSearchModalAtom = atom(false);
|
||||
|
||||
export const jotaiStore = createStore();
|
||||
48
apps/web/src/atoms/public-workspace/index.ts
Normal file
48
apps/web/src/atoms/public-workspace/index.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { atom } from 'jotai/index';
|
||||
|
||||
import {
|
||||
BlockSuiteWorkspace,
|
||||
LocalWorkspace,
|
||||
RemWorkspaceFlavour,
|
||||
} from '../../shared';
|
||||
import { apis } from '../../shared/apis';
|
||||
import { createEmptyBlockSuiteWorkspace } from '../../utils';
|
||||
|
||||
export const publicWorkspaceIdAtom = atom<string | null>(null);
|
||||
export const publicBlockSuiteAtom = atom<Promise<BlockSuiteWorkspace>>(
|
||||
async get => {
|
||||
const workspaceId = get(publicWorkspaceIdAtom);
|
||||
if (!workspaceId) {
|
||||
throw new Error('No workspace id');
|
||||
}
|
||||
const binary = await apis.downloadWorkspace(workspaceId, true);
|
||||
const blockSuiteWorkspace = createEmptyBlockSuiteWorkspace(workspaceId);
|
||||
BlockSuiteWorkspace.Y.applyUpdate(
|
||||
blockSuiteWorkspace.doc,
|
||||
new Uint8Array(binary)
|
||||
);
|
||||
blockSuiteWorkspace.awarenessStore.setFlag('enable_block_hub', false);
|
||||
blockSuiteWorkspace.awarenessStore.setFlag('enable_set_remote_flag', false);
|
||||
blockSuiteWorkspace.awarenessStore.setFlag('enable_database', false);
|
||||
blockSuiteWorkspace.awarenessStore.setFlag(
|
||||
'enable_edgeless_toolbar',
|
||||
false
|
||||
);
|
||||
blockSuiteWorkspace.awarenessStore.setFlag('enable_slash_menu', false);
|
||||
blockSuiteWorkspace.awarenessStore.setFlag('enable_drag_handle', false);
|
||||
return new Promise(resolve => {
|
||||
setTimeout(() => {
|
||||
const workspace: LocalWorkspace = {
|
||||
id: workspaceId,
|
||||
blockSuiteWorkspace,
|
||||
flavour: RemWorkspaceFlavour.LOCAL,
|
||||
syncBinary: () => Promise.resolve(workspace),
|
||||
providers: [],
|
||||
};
|
||||
dataCenter.workspaces.push(workspace);
|
||||
dataCenter.callbacks.forEach(cb => cb());
|
||||
resolve(blockSuiteWorkspace);
|
||||
}, 0);
|
||||
});
|
||||
}
|
||||
);
|
||||
Reference in New Issue
Block a user