mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 04:18:54 +00:00
refactor: unify synced and un-synced affine workspace (#1299)
This commit is contained in:
@@ -4,11 +4,11 @@ import { CloseIcon } from '@blocksuite/icons';
|
||||
import React from 'react';
|
||||
|
||||
import { useCurrentUser } from '../../../hooks/current/use-current-user';
|
||||
import { AffineRemoteWorkspace } from '../../../shared';
|
||||
import { AffineWorkspace } from '../../../shared';
|
||||
import { Content, ContentTitle, Header, StyleButton, StyleTips } from './style';
|
||||
|
||||
interface EnableAffineCloudModalProps {
|
||||
workspace: AffineRemoteWorkspace;
|
||||
workspace: AffineWorkspace;
|
||||
open: boolean;
|
||||
onConfirm: () => void;
|
||||
onClose: () => void;
|
||||
|
||||
@@ -19,7 +19,7 @@ import { lockMutex } from '../../../../../atoms';
|
||||
import { useMembers } from '../../../../../hooks/affine/use-members';
|
||||
import { transformWorkspace } from '../../../../../plugins';
|
||||
import {
|
||||
AffineRemoteWorkspace,
|
||||
AffineWorkspace,
|
||||
LocalWorkspace,
|
||||
RemWorkspaceFlavour,
|
||||
} from '../../../../../shared';
|
||||
@@ -45,7 +45,7 @@ import {
|
||||
|
||||
const AffineRemoteCollaborationPanel: React.FC<
|
||||
Omit<PanelProps, 'workspace'> & {
|
||||
workspace: AffineRemoteWorkspace;
|
||||
workspace: AffineWorkspace;
|
||||
}
|
||||
> = ({ workspace }) => {
|
||||
const [isInviteModalShow, setIsInviteModalShow] = useState(false);
|
||||
@@ -213,7 +213,7 @@ const LocalCollaborationPanel: React.FC<
|
||||
export const CollaborationPanel: React.FC<PanelProps> = props => {
|
||||
switch (props.workspace.flavour) {
|
||||
case RemWorkspaceFlavour.AFFINE: {
|
||||
const workspace = props.workspace as AffineRemoteWorkspace;
|
||||
const workspace = props.workspace as AffineWorkspace;
|
||||
return (
|
||||
<AffineRemoteCollaborationPanel {...props} workspace={workspace} />
|
||||
);
|
||||
|
||||
@@ -13,7 +13,7 @@ import { lockMutex } from '../../../../../atoms';
|
||||
import { useToggleWorkspacePublish } from '../../../../../hooks/affine/use-toggle-workspace-publish';
|
||||
import {
|
||||
AffineOfficialWorkspace,
|
||||
AffineRemoteWorkspace,
|
||||
AffineWorkspace,
|
||||
LocalWorkspace,
|
||||
RemWorkspaceFlavour,
|
||||
} from '../../../../../shared';
|
||||
@@ -25,7 +25,7 @@ export type PublishPanelProps = {
|
||||
};
|
||||
|
||||
export type PublishPanelAffineProps = {
|
||||
workspace: AffineRemoteWorkspace;
|
||||
workspace: AffineWorkspace;
|
||||
};
|
||||
|
||||
const PublishPanelAffine: React.FC<PublishPanelAffineProps> = ({
|
||||
|
||||
@@ -2,11 +2,11 @@ import { useCallback } from 'react';
|
||||
import { mutate } from 'swr';
|
||||
|
||||
import { QueryKey } from '../../plugins/affine/fetcher';
|
||||
import { AffineRemoteWorkspace } from '../../shared';
|
||||
import { AffineWorkspace } from '../../shared';
|
||||
import { apis } from '../../shared/apis';
|
||||
import { refreshDataCenter } from '../use-workspaces';
|
||||
|
||||
export function useToggleWorkspacePublish(workspace: AffineRemoteWorkspace) {
|
||||
export function useToggleWorkspacePublish(workspace: AffineWorkspace) {
|
||||
return useCallback(
|
||||
async (isPublish: boolean) => {
|
||||
await apis.updateWorkspace({
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
import { createAffineProviders } from '../../blocksuite';
|
||||
import { Unreachable } from '../../components/affine/affine-error-eoundary';
|
||||
import {
|
||||
AffineRemoteUnSyncedWorkspace,
|
||||
RemWorkspaceFlavour,
|
||||
} from '../../shared';
|
||||
import { AffineWorkspace, RemWorkspaceFlavour } from '../../shared';
|
||||
import { apis } from '../../shared/apis';
|
||||
import { createEmptyBlockSuiteWorkspace } from '../../utils';
|
||||
|
||||
@@ -48,7 +45,7 @@ export const fetcher = async (
|
||||
// fixme: token could be expired
|
||||
({ api: '/api/workspace', token: apis.auth.token }[k])
|
||||
);
|
||||
const remWorkspace: AffineRemoteUnSyncedWorkspace = {
|
||||
const remWorkspace: AffineWorkspace = {
|
||||
...workspace,
|
||||
flavour: RemWorkspaceFlavour.AFFINE,
|
||||
blockSuiteWorkspace,
|
||||
|
||||
@@ -9,8 +9,7 @@ import { WorkspaceSettingDetail } from '../../components/affine/workspace-settin
|
||||
import { BlockSuitePageList } from '../../components/blocksuite/block-suite-page-list';
|
||||
import { PageDetailEditor } from '../../components/page-detail-editor';
|
||||
import {
|
||||
AffineRemoteSyncedWorkspace,
|
||||
AffineRemoteUnSyncedWorkspace,
|
||||
AffineWorkspace,
|
||||
BlockSuiteWorkspace,
|
||||
LoadPriority,
|
||||
RemWorkspaceFlavour,
|
||||
@@ -20,10 +19,9 @@ import { createEmptyBlockSuiteWorkspace } from '../../utils';
|
||||
import { WorkspacePlugin } from '..';
|
||||
import { fetcher, QueryKey } from './fetcher';
|
||||
|
||||
const kAffineLocal = 'affine-local-storage';
|
||||
const kAffineLocal = 'affine-local-storage-v2';
|
||||
const schema = z.object({
|
||||
id: z.string(),
|
||||
firstBinarySynced: z.boolean(),
|
||||
type: z.number(),
|
||||
public: z.boolean(),
|
||||
permission: z.number(),
|
||||
@@ -66,9 +64,8 @@ export const AffinePlugin: WorkspacePlugin<RemWorkspaceFlavour.AFFINE> = {
|
||||
// fixme: token could be expired
|
||||
({ api: '/api/workspace', token: apis.auth.token }[k])
|
||||
);
|
||||
const affineWorkspace: AffineRemoteSyncedWorkspace = {
|
||||
const affineWorkspace: AffineWorkspace = {
|
||||
...workspace,
|
||||
firstBinarySynced: true,
|
||||
blockSuiteWorkspace,
|
||||
providers: [...createAffineProviders(blockSuiteWorkspace)],
|
||||
flavour: RemWorkspaceFlavour.AFFINE,
|
||||
@@ -93,7 +90,7 @@ export const AffinePlugin: WorkspacePlugin<RemWorkspaceFlavour.AFFINE> = {
|
||||
localStorage.removeItem(kAffineLocal);
|
||||
}
|
||||
}
|
||||
const promise: Promise<AffineRemoteUnSyncedWorkspace[]> = preload(
|
||||
const promise: Promise<AffineWorkspace[]> = preload(
|
||||
QueryKey.getWorkspaces,
|
||||
fetcher
|
||||
);
|
||||
@@ -122,13 +119,10 @@ export const AffinePlugin: WorkspacePlugin<RemWorkspaceFlavour.AFFINE> = {
|
||||
return workspaces;
|
||||
})
|
||||
.then(ws => {
|
||||
const workspaces = ws.filter(
|
||||
Boolean
|
||||
) as AffineRemoteSyncedWorkspace[];
|
||||
const workspaces = ws.filter(Boolean) as AffineWorkspace[];
|
||||
const dump = workspaces.map(workspace => {
|
||||
return {
|
||||
id: workspace.id,
|
||||
firstBinarySynced: workspace.firstBinarySynced,
|
||||
type: workspace.type,
|
||||
public: workspace.public,
|
||||
permission: workspace.permission,
|
||||
|
||||
@@ -3,10 +3,6 @@ import { Workspace as BlockSuiteWorkspace } from '@blocksuite/store';
|
||||
import { NextPage } from 'next';
|
||||
import { ReactElement, ReactNode } from 'react';
|
||||
|
||||
import { createAffineProviders } from '../blocksuite';
|
||||
import { createEmptyBlockSuiteWorkspace } from '../utils';
|
||||
import { apis } from './apis';
|
||||
|
||||
export { BlockSuiteWorkspace };
|
||||
|
||||
declare global {
|
||||
@@ -21,20 +17,11 @@ export const enum RemWorkspaceFlavour {
|
||||
}
|
||||
|
||||
export interface FlavourToWorkspace {
|
||||
[RemWorkspaceFlavour.AFFINE]:
|
||||
| AffineRemoteUnSyncedWorkspace
|
||||
| AffineRemoteSyncedWorkspace;
|
||||
[RemWorkspaceFlavour.AFFINE]: AffineWorkspace;
|
||||
[RemWorkspaceFlavour.LOCAL]: LocalWorkspace;
|
||||
}
|
||||
|
||||
export interface AffineRemoteSyncedWorkspace extends RemoteWorkspace {
|
||||
flavour: RemWorkspaceFlavour.AFFINE;
|
||||
firstBinarySynced: true;
|
||||
blockSuiteWorkspace: BlockSuiteWorkspace;
|
||||
providers: Provider[];
|
||||
}
|
||||
|
||||
export interface AffineRemoteUnSyncedWorkspace extends RemoteWorkspace {
|
||||
export interface AffineWorkspace extends RemoteWorkspace {
|
||||
flavour: RemWorkspaceFlavour.AFFINE;
|
||||
// empty
|
||||
blockSuiteWorkspace: BlockSuiteWorkspace;
|
||||
@@ -48,33 +35,6 @@ export interface LocalWorkspace {
|
||||
providers: Provider[];
|
||||
}
|
||||
|
||||
export const transformToAffineSyncedWorkspace = async (
|
||||
unSyncedWorkspace: AffineRemoteUnSyncedWorkspace,
|
||||
binary: ArrayBuffer
|
||||
): Promise<AffineRemoteSyncedWorkspace> => {
|
||||
const blockSuiteWorkspace = createEmptyBlockSuiteWorkspace(
|
||||
unSyncedWorkspace.id,
|
||||
(k: string) =>
|
||||
// fixme: token could be expired
|
||||
({ api: '/api/workspace', token: apis.auth.token }[k])
|
||||
);
|
||||
BlockSuiteWorkspace.Y.applyUpdate(
|
||||
blockSuiteWorkspace.doc,
|
||||
new Uint8Array(binary)
|
||||
);
|
||||
return new Promise(resolve => {
|
||||
// Fixme: https://github.com/toeverything/blocksuite/issues/1350
|
||||
setTimeout(() => {
|
||||
resolve({
|
||||
...unSyncedWorkspace,
|
||||
blockSuiteWorkspace,
|
||||
firstBinarySynced: true,
|
||||
providers: [...createAffineProviders(blockSuiteWorkspace)],
|
||||
});
|
||||
}, 0);
|
||||
});
|
||||
};
|
||||
|
||||
export type BaseProvider = {
|
||||
flavour: string;
|
||||
// if this is true, we will connect the provider on the background
|
||||
@@ -106,10 +66,7 @@ export type Provider =
|
||||
| AffineWebSocketProvider
|
||||
| BroadCastChannelProvider;
|
||||
|
||||
export type AffineRemoteWorkspace =
|
||||
| AffineRemoteSyncedWorkspace
|
||||
| AffineRemoteUnSyncedWorkspace;
|
||||
export type AffineOfficialWorkspace = AffineRemoteWorkspace | LocalWorkspace;
|
||||
export type AffineOfficialWorkspace = AffineWorkspace | LocalWorkspace;
|
||||
|
||||
export type RemWorkspace = AffineOfficialWorkspace;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user