Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

feat/add get workspaces

MingLiang Wang153150cd3b59
diff --git a/packages/data-center/src/datacenter.ts b/packages/data-center/src/datacenter.ts
index 28de94043c..032f96b89a 100644
--- a/packages/data-center/src/datacenter.ts
+++ b/packages/data-center/src/datacenter.ts
@@ -44,13 +44,27 @@ export class DataCenter {
return Array.from(this.providerMap.values());
}
+ /**
+ * get workspaces list if focusUpdate is true, it will refresh workspaces
+ * @param {string} name workspace name
+ * @returns {Promise<WS[]>}
+ */
+ public async getWorkspaces(focusUpdate = false) {
+ if (focusUpdate) {
+ this.workspaces.refreshWorkspaces();
+ }
+ return this.workspaces.workspaces;
+ }
+
/**
* create new workspace , new workspace is a local workspace
* @param {string} name workspace name
- * @returns
+ * @returns {Promise<WS>}
*/
public async createWorkspace(name: string) {
const workspaceInfo = this.workspaces.addLocalWorkspace(name);
+ // IMP: create workspace in local provider
+ this.providerMap.get('local')?.createWorkspace({ name });
return workspaceInfo;
}
@@ -102,6 +116,7 @@ export class DataCenter {
/**
* load workspace instance by id
* @param {string} workspaceId workspace id
+ * @returns {Promise<Workspace>}
*/
public async loadWorkspace(workspaceId: string) {
const workspaceInfo = this.workspaces.getWorkspace(workspaceId);
@@ -111,13 +126,11 @@ export class DataCenter {
currentProvider.close(workspaceId);
}
const provider = this.providerMap.get(workspaceInfo.provider);
- if (provider) {
- this._logger(`Loading ${provider} workspace: `, workspaceId);
- const workspace = this._getWorkspace(workspaceId);
- this.currentWorkspace = await provider.warpWorkspace(workspace);
- return this.currentWorkspace;
- }
- return workspaceInfo;
+ assert(provider, `provide '${workspaceInfo.provider}' is not registered`);
+ this._logger(`Loading ${workspaceInfo.provider} workspace: `, workspaceId);
+ const workspace = this._getWorkspace(workspaceId);
+ this.currentWorkspace = await provider.warpWorkspace(workspace);
+ return this.currentWorkspace;
}
/**
@@ -127,7 +140,7 @@ export class DataCenter {
public async getUserInfo(providerId = 'affine') {
// XXX: maybe return all user info
const provider = this.providerMap.get(providerId);
- assert(provider, 'Provider not found');
+ assert(provider, `provide '${providerId}' is not registered`);
return provider.getUserInfo();
}
@@ -219,28 +232,31 @@ export class DataCenter {
private async _transWorkspaceProvider(
workspace: Workspace,
- provider: string
+ providerId: string
) {
- assert(workspace.room, 'No workspace');
+ assert(workspace.room, 'No workspace id');
const workspaceInfo = this.workspaces.getWorkspace(workspace.room);
assert(workspaceInfo, 'Workspace not found');
- if (workspaceInfo.provider === provider) {
+ if (workspaceInfo.provider === providerId) {
this._logger('Workspace provider is same');
return;
}
const currentProvider = this.providerMap.get(workspaceInfo.provider);
assert(currentProvider, 'Provider not found');
- const newProvider = this.providerMap.get(provider);
- assert(newProvider, `${provider} provider is not registered`);
+ const newProvider = this.providerMap.get(providerId);
+ assert(newProvider, `provide '${providerId}' is not registered`);
+ this._logger(`create ${providerId} workspace: `, workspaceInfo.name);
const newWorkspace = await newProvider.createWorkspace({
name: workspaceInfo.name,
avatar: workspaceInfo.avatar,
});
assert(newWorkspace, 'Create workspace failed');
- // load doc to another workspace
+ this._logger(
+ `update workspace data from ${workspaceInfo.provider} to ${providerId}`
+ );
applyUpdate(newWorkspace.doc, encodeStateAsUpdate(workspace.doc));
assert(newWorkspace, 'Create workspace failed');
- currentProvider.delete(workspace.room);
+ await currentProvider.delete(workspace.room);
this.workspaces.refreshWorkspaces();
}
diff --git a/packages/data-center/src/provider/base.ts b/packages/data-center/src/provider/base.ts
index 95c98fe892..e9c568fa3c 100644
--- a/packages/data-center/src/provider/base.ts
+++ b/packages/data-center/src/provider/base.ts
@@ -41,7 +41,7 @@ export class BaseProvider {
}
/**
- * warp workspace with workspace functions
+ * warp workspace with provider functions
* @param workspace
* @returns
*/
diff --git a/packages/data-center/src/workspaces/workspaces.ts b/packages/data-center/src/workspaces/workspaces.ts
index b8ee7f5df8..ec107dbe69 100644
--- a/packages/data-center/src/workspaces/workspaces.ts
+++ b/packages/data-center/src/workspaces/workspaces.ts
@@ -1,8 +1,8 @@
-import { Workspace as WS } from '../types';
+import { Workspace as WS } from 'src/types';
import { Observable } from 'lib0/observable';
import { uuidv4 } from '@blocksuite/store';
-import { DataCenter } from '../datacenter';
+import { DataCenter } from 'src/datacenter';
export class Workspaces extends Observable<string> {
private _workspaces: WS[];