mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 12:28:42 +00:00
refactor: remove package @affine/datacenter (#1705)
This commit is contained in:
@@ -68,7 +68,6 @@ const nextConfig = {
|
||||
reactStrictMode: true,
|
||||
transpilePackages: [
|
||||
'@affine/component',
|
||||
'@affine/datacenter',
|
||||
'@affine/i18n',
|
||||
'@affine/debug',
|
||||
'@affine/env',
|
||||
|
||||
@@ -10,7 +10,6 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@affine/component": "workspace:*",
|
||||
"@affine/datacenter": "workspace:*",
|
||||
"@affine/debug": "workspace:*",
|
||||
"@affine/env": "workspace:*",
|
||||
"@affine/i18n": "workspace:*",
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { getLoginStorage } from '@affine/workspace/affine/login';
|
||||
import { createEmptyBlockSuiteWorkspace } from '@affine/workspace/utils';
|
||||
import { atom } from 'jotai/index';
|
||||
|
||||
import { BlockSuiteWorkspace } from '../../shared';
|
||||
import { apis } from '../../shared/apis';
|
||||
import { affineApis } from '../../shared/apis';
|
||||
|
||||
export const publicWorkspaceIdAtom = atom<string | null>(null);
|
||||
export const publicBlockSuiteAtom = atom<Promise<BlockSuiteWorkspace>>(
|
||||
@@ -11,7 +12,7 @@ export const publicBlockSuiteAtom = atom<Promise<BlockSuiteWorkspace>>(
|
||||
if (!workspaceId) {
|
||||
throw new Error('No workspace id');
|
||||
}
|
||||
const binary = await apis.downloadWorkspace(workspaceId, true);
|
||||
const binary = await affineApis.downloadWorkspace(workspaceId, true);
|
||||
// fixme: this is a hack
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
const prefixUrl = params.get('prefixUrl')
|
||||
@@ -21,7 +22,9 @@ export const publicBlockSuiteAtom = atom<Promise<BlockSuiteWorkspace>>(
|
||||
workspaceId,
|
||||
(k: string) =>
|
||||
// fixme: token could be expired
|
||||
({ api: `${prefixUrl}api/workspace`, token: apis.auth.token }[k])
|
||||
({ api: `${prefixUrl}api/workspace`, token: getLoginStorage()?.token }[
|
||||
k
|
||||
])
|
||||
);
|
||||
BlockSuiteWorkspace.Y.applyUpdate(
|
||||
blockSuiteWorkspace.doc,
|
||||
|
||||
@@ -2,7 +2,7 @@ import { assertExists } from '@blocksuite/store';
|
||||
|
||||
import type { AffineDownloadProvider } from '../../../shared';
|
||||
import { BlockSuiteWorkspace } from '../../../shared';
|
||||
import { apis } from '../../../shared/apis';
|
||||
import { affineApis } from '../../../shared/apis';
|
||||
import { providerLogger } from '../../logger';
|
||||
|
||||
const hashMap = new Map<string, ArrayBuffer>();
|
||||
@@ -25,7 +25,7 @@ export const createAffineDownloadProvider = (
|
||||
);
|
||||
return;
|
||||
}
|
||||
apis.downloadWorkspace(id, false).then(binary => {
|
||||
affineApis.downloadWorkspace(id, false).then(binary => {
|
||||
hashMap.set(id, binary);
|
||||
providerLogger.debug('applyUpdate');
|
||||
BlockSuiteWorkspace.Y.applyUpdate(
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { WebsocketProvider } from '@affine/datacenter';
|
||||
import { getLoginStorage } from '@affine/workspace/affine/login';
|
||||
import { WebsocketProvider } from '@affine/workspace/affine/sync';
|
||||
import { assertExists } from '@blocksuite/store';
|
||||
import { IndexeddbPersistence } from 'y-indexeddb';
|
||||
|
||||
@@ -7,7 +8,6 @@ import type {
|
||||
BlockSuiteWorkspace,
|
||||
LocalIndexedDBProvider,
|
||||
} from '../../shared';
|
||||
import { apis } from '../../shared/apis';
|
||||
import { providerLogger } from '../logger';
|
||||
import { createBroadCastChannelProvider } from './broad-cast-channel';
|
||||
|
||||
@@ -32,7 +32,7 @@ const createAffineWebSocketProvider = (
|
||||
blockSuiteWorkspace.id,
|
||||
blockSuiteWorkspace.doc,
|
||||
{
|
||||
params: { token: apis.auth.refresh },
|
||||
params: { token: getLoginStorage()?.token ?? '' },
|
||||
// @ts-expect-error ignore the type
|
||||
awareness: blockSuiteWorkspace.awarenessStore.awareness,
|
||||
// we maintain broadcast channel by ourselves
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { RequestError } from '@affine/datacenter';
|
||||
import { RequestError } from '@affine/workspace/affine/api';
|
||||
import type { NextRouter } from 'next/router';
|
||||
import type { ErrorInfo } from 'react';
|
||||
import type React from 'react';
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Button, IconButton, Menu, MenuItem, Wrapper } from '@affine/component';
|
||||
import { PermissionType } from '@affine/datacenter';
|
||||
import { useTranslation } from '@affine/i18n';
|
||||
import { PermissionType } from '@affine/workspace/affine/api';
|
||||
import { WorkspaceFlavour } from '@affine/workspace/type';
|
||||
import {
|
||||
DeleteTemporarilyIcon,
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
import { displayFlex, IconButton, styled, Tooltip } from '@affine/component';
|
||||
import { useTranslation } from '@affine/i18n';
|
||||
import {
|
||||
getLoginStorage,
|
||||
setLoginStorage,
|
||||
SignMethod,
|
||||
} from '@affine/workspace/affine/login';
|
||||
import { WorkspaceFlavour } from '@affine/workspace/type';
|
||||
import {
|
||||
CloudWorkspaceIcon,
|
||||
@@ -10,13 +15,13 @@ import { assertEquals, assertExists } from '@blocksuite/store';
|
||||
import { useRouter } from 'next/router';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
|
||||
import { affineAuth } from '../../../../hooks/affine/use-affine-log-in';
|
||||
import { useCurrentWorkspace } from '../../../../hooks/current/use-current-workspace';
|
||||
import { useTransformWorkspace } from '../../../../hooks/use-transform-workspace';
|
||||
import type {
|
||||
AffineOfficialWorkspace,
|
||||
LocalWorkspace,
|
||||
} from '../../../../shared';
|
||||
import { apis } from '../../../../shared/apis';
|
||||
import { TransformWorkspaceToAffineModal } from '../../../affine/transform-workspace-to-affine-modal';
|
||||
|
||||
const IconWrapper = styled('div')(({ theme }) => {
|
||||
@@ -112,8 +117,13 @@ export const SyncUser = () => {
|
||||
setOpen(false);
|
||||
}}
|
||||
onConform={async () => {
|
||||
if (!apis.auth.isLogin) {
|
||||
await apis.signInWithGoogle();
|
||||
if (!getLoginStorage()) {
|
||||
const response = await affineAuth.generateToken(
|
||||
SignMethod.Google
|
||||
);
|
||||
if (response) {
|
||||
setLoginStorage(response);
|
||||
}
|
||||
router.reload();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { FlexWrapper } from '@affine/component';
|
||||
import { IconButton } from '@affine/component';
|
||||
import { Tooltip } from '@affine/component';
|
||||
import type { AccessTokenMessage } from '@affine/datacenter';
|
||||
import { useTranslation } from '@affine/i18n';
|
||||
import type { AccessTokenMessage } from '@affine/workspace/affine/login';
|
||||
import { CloudWorkspaceIcon, SignOutIcon } from '@blocksuite/icons';
|
||||
import type { CSSProperties } from 'react';
|
||||
import type React from 'react';
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { MessageCode } from '@affine/datacenter';
|
||||
import { messages } from '@affine/datacenter';
|
||||
import { MessageCode, Messages } from '@affine/env/constant';
|
||||
import { setLoginStorage, SignMethod } from '@affine/workspace/affine/login';
|
||||
import type React from 'react';
|
||||
import { memo, useEffect, useState } from 'react';
|
||||
|
||||
import { affineAuth } from '../../../hooks/affine/use-affine-log-in';
|
||||
import { useAffineLogOut } from '../../../hooks/affine/use-affine-log-out';
|
||||
import { apis } from '../../../shared/apis';
|
||||
import { toast } from '../../../utils';
|
||||
|
||||
declare global {
|
||||
@@ -33,9 +33,12 @@ export const MessageCenter: React.FC = memo(function MessageCenter() {
|
||||
event.detail.code === MessageCode.loginError)
|
||||
) {
|
||||
setPopup(true);
|
||||
apis
|
||||
.signInWithGoogle()
|
||||
.then(() => {
|
||||
affineAuth
|
||||
.generateToken(SignMethod.Google)
|
||||
.then(response => {
|
||||
if (response) {
|
||||
setLoginStorage(response);
|
||||
}
|
||||
setPopup(false);
|
||||
})
|
||||
.catch(() => {
|
||||
@@ -43,7 +46,7 @@ export const MessageCenter: React.FC = memo(function MessageCenter() {
|
||||
onLogout();
|
||||
});
|
||||
} else {
|
||||
toast(messages[event.detail.code].message);
|
||||
toast(Messages[event.detail.code].message);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { PermissionType } from '@affine/datacenter';
|
||||
import { useTranslation } from '@affine/i18n';
|
||||
import { PermissionType } from '@affine/workspace/affine/api';
|
||||
import { WorkspaceFlavour } from '@affine/workspace/type';
|
||||
import { SettingsIcon } from '@blocksuite/icons';
|
||||
import type React from 'react';
|
||||
|
||||
@@ -4,8 +4,8 @@ import {
|
||||
ModalWrapper,
|
||||
Tooltip,
|
||||
} from '@affine/component';
|
||||
import type { AccessTokenMessage } from '@affine/datacenter';
|
||||
import { useTranslation } from '@affine/i18n';
|
||||
import type { AccessTokenMessage } from '@affine/workspace/affine/login';
|
||||
import { HelpIcon, PlusIcon } from '@blocksuite/icons';
|
||||
|
||||
import type { RemWorkspace } from '../../../shared';
|
||||
|
||||
@@ -1,26 +1,30 @@
|
||||
import { currentAffineUserAtom } from '@affine/workspace/affine/atom';
|
||||
import {
|
||||
createAffineAuth,
|
||||
parseIdToken,
|
||||
setLoginStorage,
|
||||
SignMethod,
|
||||
} from '@affine/workspace/affine/login';
|
||||
import { useSetAtom } from 'jotai';
|
||||
import { useRouter } from 'next/router';
|
||||
import { useCallback } from 'react';
|
||||
|
||||
import { apis } from '../../shared/apis';
|
||||
import { toast } from '../../utils';
|
||||
|
||||
export const affineAuth = createAffineAuth();
|
||||
|
||||
export function useAffineLogIn() {
|
||||
const router = useRouter();
|
||||
const setUser = useSetAtom(currentAffineUserAtom);
|
||||
return useCallback(async () => {
|
||||
const response = await affineAuth.generateToken(SignMethod.Google);
|
||||
if (response) {
|
||||
setLoginStorage(response);
|
||||
apis.auth.setLogin(response);
|
||||
const user = parseIdToken(response.token);
|
||||
setUser(user);
|
||||
router.reload();
|
||||
} else {
|
||||
toast('Login failed');
|
||||
}
|
||||
}, [router]);
|
||||
}, [router, setUser]);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { currentAffineUserAtom } from '@affine/workspace/affine/atom';
|
||||
import { clearLoginStorage } from '@affine/workspace/affine/login';
|
||||
import { WorkspaceFlavour } from '@affine/workspace/type';
|
||||
import { useSetAtom } from 'jotai';
|
||||
@@ -6,13 +7,12 @@ import { useCallback } from 'react';
|
||||
|
||||
import { jotaiWorkspacesAtom } from '../../atoms';
|
||||
import { WorkspacePlugins } from '../../plugins';
|
||||
import { apis } from '../../shared/apis';
|
||||
|
||||
export function useAffineLogOut() {
|
||||
const set = useSetAtom(jotaiWorkspacesAtom);
|
||||
const router = useRouter();
|
||||
const setCurrentUser = useSetAtom(currentAffineUserAtom);
|
||||
return useCallback(() => {
|
||||
apis.auth.clear();
|
||||
set(workspaces =>
|
||||
workspaces.filter(
|
||||
workspace => workspace.flavour !== WorkspaceFlavour.AFFINE
|
||||
@@ -20,6 +20,7 @@ export function useAffineLogOut() {
|
||||
);
|
||||
WorkspacePlugins[WorkspaceFlavour.AFFINE].cleanup?.();
|
||||
clearLoginStorage();
|
||||
setCurrentUser(null);
|
||||
router.reload();
|
||||
}, [router, set]);
|
||||
}, [router, set, setCurrentUser]);
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ import {
|
||||
} from '@affine/workspace/affine/login';
|
||||
import useSWR from 'swr';
|
||||
|
||||
import { apis } from '../../shared/apis';
|
||||
import { affineAuth } from './use-affine-log-in';
|
||||
|
||||
const logger = new DebugLogger('auth-token');
|
||||
@@ -22,7 +21,6 @@ const revalidate = async () => {
|
||||
const response = await affineAuth.refreshToken(storage);
|
||||
if (response) {
|
||||
setLoginStorage(response);
|
||||
apis.auth.setLogin(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { PermissionType } from '@affine/datacenter';
|
||||
import { PermissionType } from '@affine/workspace/affine/api';
|
||||
|
||||
import type { AffineOfficialWorkspace } from '../../shared';
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import type { Member } from '@affine/datacenter';
|
||||
import type { Member } from '@affine/workspace/affine/api';
|
||||
import { useCallback } from 'react';
|
||||
import useSWR from 'swr';
|
||||
|
||||
import { QueryKey } from '../../plugins/affine/fetcher';
|
||||
import { apis } from '../../shared/apis';
|
||||
import { affineApis } from '../../shared/apis';
|
||||
|
||||
export function useMembers(workspaceId: string) {
|
||||
const { data, mutate } = useSWR<Member[]>(
|
||||
@@ -15,7 +15,7 @@ export function useMembers(workspaceId: string) {
|
||||
|
||||
const inviteMember = useCallback(
|
||||
async (email: string) => {
|
||||
await apis.inviteMember({
|
||||
await affineApis.inviteMember({
|
||||
id: workspaceId,
|
||||
email,
|
||||
});
|
||||
@@ -26,8 +26,7 @@ export function useMembers(workspaceId: string) {
|
||||
|
||||
const removeMember = useCallback(
|
||||
async (permissionId: number) => {
|
||||
// fixme: what about the workspaceId?
|
||||
await apis.removeMember({
|
||||
await affineApis.removeMember({
|
||||
permissionId,
|
||||
});
|
||||
return mutate();
|
||||
|
||||
@@ -4,13 +4,13 @@ import useSWR from 'swr';
|
||||
import { jotaiStore, jotaiWorkspacesAtom } from '../../atoms';
|
||||
import { QueryKey } from '../../plugins/affine/fetcher';
|
||||
import type { AffineWorkspace } from '../../shared';
|
||||
import { apis } from '../../shared/apis';
|
||||
import { affineApis } from '../../shared/apis';
|
||||
|
||||
export function useToggleWorkspacePublish(workspace: AffineWorkspace) {
|
||||
const { mutate } = useSWR(QueryKey.getWorkspaces);
|
||||
return useCallback(
|
||||
async (isPublish: boolean) => {
|
||||
await apis.updateWorkspace({
|
||||
await affineApis.updateWorkspace({
|
||||
id: workspace.id,
|
||||
public: isPublish,
|
||||
});
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
import type { AccessTokenMessage } from '@affine/datacenter';
|
||||
import useSWR from 'swr';
|
||||
|
||||
import { QueryKey } from '../../plugins/affine/fetcher';
|
||||
import { currentAffineUserAtom } from '@affine/workspace/affine/atom';
|
||||
import type { AccessTokenMessage } from '@affine/workspace/affine/login';
|
||||
import { useAtomValue } from 'jotai';
|
||||
|
||||
export function useCurrentUser(): AccessTokenMessage | null {
|
||||
const { data } = useSWR<AccessTokenMessage | null>(QueryKey.getUser, {
|
||||
fallbackData: null,
|
||||
});
|
||||
return data ?? null;
|
||||
return useAtomValue(currentAffineUserAtom);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { displayFlex, styled } from '@affine/component';
|
||||
import { Button } from '@affine/component';
|
||||
import type { Permission } from '@affine/datacenter';
|
||||
import type { Permission } from '@affine/workspace/affine/api';
|
||||
import {
|
||||
SucessfulDuotoneIcon,
|
||||
UnsucessfulDuotoneIcon,
|
||||
|
||||
@@ -1,4 +1,11 @@
|
||||
import { useTranslation } from '@affine/i18n';
|
||||
import { currentAffineUserAtom } from '@affine/workspace/affine/atom';
|
||||
import {
|
||||
getLoginStorage,
|
||||
parseIdToken,
|
||||
setLoginStorage,
|
||||
SignMethod,
|
||||
} from '@affine/workspace/affine/login';
|
||||
import type { SettingPanel, WorkspaceRegistry } from '@affine/workspace/type';
|
||||
import {
|
||||
settingPanel,
|
||||
@@ -7,7 +14,7 @@ import {
|
||||
} from '@affine/workspace/type';
|
||||
import { SettingsIcon } from '@blocksuite/icons';
|
||||
import { assertExists } from '@blocksuite/store';
|
||||
import { useAtom } from 'jotai';
|
||||
import { useAtom, useSetAtom } from 'jotai';
|
||||
import { atomWithStorage } from 'jotai/utils';
|
||||
import Head from 'next/head';
|
||||
import { useRouter } from 'next/router';
|
||||
@@ -16,6 +23,7 @@ import React, { useCallback, useEffect } from 'react';
|
||||
import { Unreachable } from '../../../components/affine/affine-error-eoundary';
|
||||
import { PageLoading } from '../../../components/pure/loading';
|
||||
import { WorkspaceTitle } from '../../../components/pure/workspace-title';
|
||||
import { affineAuth } from '../../../hooks/affine/use-affine-log-in';
|
||||
import { useCurrentWorkspace } from '../../../hooks/current/use-current-workspace';
|
||||
import { useSyncRouterWithCurrentWorkspace } from '../../../hooks/use-sync-router-with-current-workspace';
|
||||
import { useTransformWorkspace } from '../../../hooks/use-transform-workspace';
|
||||
@@ -23,7 +31,6 @@ import { useWorkspacesHelper } from '../../../hooks/use-workspaces';
|
||||
import { WorkspaceLayout } from '../../../layouts';
|
||||
import { WorkspacePlugins } from '../../../plugins';
|
||||
import type { NextPageWithLayout } from '../../../shared';
|
||||
import { apis } from '../../../shared/apis';
|
||||
|
||||
const settingPanelAtom = atomWithStorage<SettingPanel>(
|
||||
'workspaceId',
|
||||
@@ -101,15 +108,20 @@ const SettingPage: NextPageWithLayout = () => {
|
||||
return helper.deleteWorkspace(workspaceId);
|
||||
}, [currentWorkspace, helper]);
|
||||
const transformWorkspace = useTransformWorkspace();
|
||||
const setUser = useSetAtom(currentAffineUserAtom);
|
||||
const onTransformWorkspace = useCallback(
|
||||
async <From extends WorkspaceFlavour, To extends WorkspaceFlavour>(
|
||||
from: From,
|
||||
to: To,
|
||||
workspace: WorkspaceRegistry[From]
|
||||
): Promise<void> => {
|
||||
const needRefresh = to === WorkspaceFlavour.AFFINE && !apis.auth.isLogin;
|
||||
const needRefresh = to === WorkspaceFlavour.AFFINE && !getLoginStorage();
|
||||
if (needRefresh) {
|
||||
await apis.signInWithGoogle();
|
||||
const response = await affineAuth.generateToken(SignMethod.Google);
|
||||
if (response) {
|
||||
setLoginStorage(response);
|
||||
setUser(parseIdToken(response.token));
|
||||
}
|
||||
}
|
||||
const workspaceId = await transformWorkspace(from, to, workspace);
|
||||
await router.replace({
|
||||
@@ -119,11 +131,8 @@ const SettingPage: NextPageWithLayout = () => {
|
||||
workspaceId,
|
||||
},
|
||||
});
|
||||
if (needRefresh) {
|
||||
router.reload();
|
||||
}
|
||||
},
|
||||
[router, transformWorkspace]
|
||||
[router, setUser, transformWorkspace]
|
||||
);
|
||||
if (!router.isReady) {
|
||||
return <PageLoading />;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { getLoginStorage } from '@affine/workspace/affine/login';
|
||||
import { WorkspaceFlavour } from '@affine/workspace/type';
|
||||
import { createEmptyBlockSuiteWorkspace } from '@affine/workspace/utils';
|
||||
import { assertExists } from '@blocksuite/store';
|
||||
@@ -6,7 +7,7 @@ import { jotaiStore, workspacesAtom } from '../../atoms';
|
||||
import { createAffineProviders } from '../../blocksuite';
|
||||
import { Unreachable } from '../../components/affine/affine-error-eoundary';
|
||||
import type { AffineWorkspace } from '../../shared';
|
||||
import { apis } from '../../shared/apis';
|
||||
import { affineApis } from '../../shared/apis';
|
||||
|
||||
type Query = (typeof QueryKey)[keyof typeof QueryKey];
|
||||
|
||||
@@ -17,24 +18,21 @@ export const fetcher = async (
|
||||
| [Query, string]
|
||||
| [Query, string, string]
|
||||
) => {
|
||||
if (query === QueryKey.getUser) {
|
||||
return apis.auth.user ?? null;
|
||||
}
|
||||
if (Array.isArray(query)) {
|
||||
if (query[0] === QueryKey.downloadWorkspace) {
|
||||
if (typeof query[2] !== 'boolean') {
|
||||
throw new Unreachable();
|
||||
}
|
||||
return apis.downloadWorkspace(query[1], query[2]);
|
||||
return affineApis.downloadWorkspace(query[1], query[2]);
|
||||
} else if (query[0] === QueryKey.getMembers) {
|
||||
return apis.getWorkspaceMembers({
|
||||
return affineApis.getWorkspaceMembers({
|
||||
id: query[1],
|
||||
});
|
||||
} else if (query[0] === QueryKey.getUserByEmail) {
|
||||
if (typeof query[2] !== 'string') {
|
||||
throw new Unreachable();
|
||||
}
|
||||
return apis.getUserByEmail({
|
||||
return affineApis.getUserByEmail({
|
||||
workspace_id: query[1],
|
||||
email: query[2],
|
||||
});
|
||||
@@ -57,19 +55,19 @@ export const fetcher = async (
|
||||
if (typeof invitingCode !== 'string') {
|
||||
throw new TypeError('invitingCode must be a string');
|
||||
}
|
||||
return apis.acceptInviting({
|
||||
return affineApis.acceptInviting({
|
||||
invitingCode,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
if (query === QueryKey.getWorkspaces) {
|
||||
return apis.getWorkspaces().then(workspaces => {
|
||||
return affineApis.getWorkspaces().then(workspaces => {
|
||||
return workspaces.map(workspace => {
|
||||
const blockSuiteWorkspace = createEmptyBlockSuiteWorkspace(
|
||||
workspace.id,
|
||||
(k: string) =>
|
||||
// fixme: token could be expired
|
||||
({ api: '/api/workspace', token: apis.auth.token }[k])
|
||||
({ api: '/api/workspace', token: getLoginStorage()?.token }[k])
|
||||
);
|
||||
const remWorkspace: AffineWorkspace = {
|
||||
...workspace,
|
||||
@@ -81,14 +79,13 @@ export const fetcher = async (
|
||||
});
|
||||
});
|
||||
}
|
||||
return (apis as any)[query]();
|
||||
return (affineApis as any)[query]();
|
||||
}
|
||||
};
|
||||
|
||||
export const QueryKey = {
|
||||
acceptInvite: 'acceptInvite',
|
||||
getImage: 'getImage',
|
||||
getUser: 'getUser',
|
||||
getWorkspaces: 'getWorkspaces',
|
||||
downloadWorkspace: 'downloadWorkspace',
|
||||
getMembers: 'getMembers',
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { getLoginStorage } from '@affine/workspace/affine/login';
|
||||
import { LoadPriority, WorkspaceFlavour } from '@affine/workspace/type';
|
||||
import { createEmptyBlockSuiteWorkspace } from '@affine/workspace/utils';
|
||||
import { createJSONStorage } from 'jotai/utils';
|
||||
@@ -12,8 +13,8 @@ import { BlockSuitePageList } from '../../components/blocksuite/block-suite-page
|
||||
import { PageDetailEditor } from '../../components/page-detail-editor';
|
||||
import type { AffineWorkspace } from '../../shared';
|
||||
import { BlockSuiteWorkspace } from '../../shared';
|
||||
import { apis, clientAuth } from '../../shared/apis';
|
||||
import { initPage } from '../../utils/blocksuite';
|
||||
import { affineApis } from '../../shared/apis';
|
||||
import { initPage } from '../../utils';
|
||||
import type { WorkspacePlugin } from '..';
|
||||
import { QueryKey } from './fetcher';
|
||||
|
||||
@@ -39,7 +40,7 @@ const getPersistenceAllWorkspace = () => {
|
||||
item.id,
|
||||
(k: string) =>
|
||||
// fixme: token could be expired
|
||||
({ api: '/api/workspace', token: apis.auth.token }[k])
|
||||
({ api: '/api/workspace', token: getLoginStorage()?.token }[k])
|
||||
);
|
||||
const affineWorkspace: AffineWorkspace = {
|
||||
...item,
|
||||
@@ -65,7 +66,9 @@ export const AffinePlugin: WorkspacePlugin<WorkspaceFlavour.AFFINE> = {
|
||||
const binary = BlockSuiteWorkspace.Y.encodeStateAsUpdate(
|
||||
blockSuiteWorkspace.doc
|
||||
);
|
||||
const { id } = await apis.createWorkspace(new Blob([binary.buffer]));
|
||||
const { id } = await affineApis.createWorkspace(
|
||||
new Blob([binary.buffer])
|
||||
);
|
||||
// fixme: syncing images
|
||||
const newWorkspaceId = id;
|
||||
|
||||
@@ -77,12 +80,7 @@ export const AffinePlugin: WorkspacePlugin<WorkspaceFlavour.AFFINE> = {
|
||||
const url = await blobs.get(id);
|
||||
if (url) {
|
||||
const blob = await fetch(url).then(res => res.blob());
|
||||
await clientAuth.put(`api/workspace/${newWorkspaceId}/blob`, {
|
||||
body: blob,
|
||||
headers: {
|
||||
'Content-Type': blob.type,
|
||||
},
|
||||
});
|
||||
await affineApis.uploadBlob(newWorkspaceId, blob);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -103,14 +101,14 @@ export const AffinePlugin: WorkspacePlugin<WorkspaceFlavour.AFFINE> = {
|
||||
items.filter(item => item.id !== workspace.id)
|
||||
);
|
||||
}
|
||||
await apis.deleteWorkspace({
|
||||
await affineApis.deleteWorkspace({
|
||||
id: workspace.id,
|
||||
});
|
||||
await mutate(matcher => matcher === QueryKey.getWorkspaces);
|
||||
},
|
||||
get: async workspaceId => {
|
||||
try {
|
||||
if (!apis.auth.isLogin) {
|
||||
if (!getLoginStorage()) {
|
||||
const workspaces = getPersistenceAllWorkspace();
|
||||
return (
|
||||
workspaces.find(workspace => workspace.id === workspaceId) ?? null
|
||||
@@ -130,69 +128,65 @@ export const AffinePlugin: WorkspacePlugin<WorkspaceFlavour.AFFINE> = {
|
||||
list: async () => {
|
||||
const allWorkspaces = getPersistenceAllWorkspace();
|
||||
try {
|
||||
if (apis.auth.isLogin) {
|
||||
const workspaces = await apis.getWorkspaces().then(workspaces => {
|
||||
return workspaces.map(workspace => {
|
||||
const blockSuiteWorkspace = createEmptyBlockSuiteWorkspace(
|
||||
workspace.id,
|
||||
(k: string) =>
|
||||
// fixme: token could be expired
|
||||
({ api: '/api/workspace', token: apis.auth.token }[k])
|
||||
);
|
||||
const dump = workspaces.map(workspace => {
|
||||
return {
|
||||
id: workspace.id,
|
||||
type: workspace.type,
|
||||
public: workspace.public,
|
||||
permission: workspace.permission,
|
||||
} satisfies z.infer<typeof schema>;
|
||||
});
|
||||
const old = storage.getItem(kAffineLocal);
|
||||
if (
|
||||
Array.isArray(old) &&
|
||||
old.every(item => schema.safeParse(item).success)
|
||||
) {
|
||||
const data = [...dump];
|
||||
old.forEach((item: z.infer<typeof schema>) => {
|
||||
const has = dump.find(dump => dump.id === item.id);
|
||||
if (!has) {
|
||||
data.push(item);
|
||||
}
|
||||
});
|
||||
storage.setItem(kAffineLocal, [...data]);
|
||||
}
|
||||
|
||||
const affineWorkspace: AffineWorkspace = {
|
||||
...workspace,
|
||||
flavour: WorkspaceFlavour.AFFINE,
|
||||
blockSuiteWorkspace,
|
||||
providers: [...createAffineProviders(blockSuiteWorkspace)],
|
||||
};
|
||||
return affineWorkspace;
|
||||
});
|
||||
});
|
||||
workspaces.forEach(workspace => {
|
||||
const idx = allWorkspaces.findIndex(
|
||||
({ id }) => id === workspace.id
|
||||
const workspaces = await affineApis.getWorkspaces().then(workspaces => {
|
||||
return workspaces.map(workspace => {
|
||||
const blockSuiteWorkspace = createEmptyBlockSuiteWorkspace(
|
||||
workspace.id,
|
||||
(k: string) =>
|
||||
// fixme: token could be expired
|
||||
({ api: '/api/workspace', token: getLoginStorage()?.token }[k])
|
||||
);
|
||||
if (idx !== -1) {
|
||||
allWorkspaces.splice(idx, 1, workspace);
|
||||
} else {
|
||||
allWorkspaces.push(workspace);
|
||||
const dump = workspaces.map(workspace => {
|
||||
return {
|
||||
id: workspace.id,
|
||||
type: workspace.type,
|
||||
public: workspace.public,
|
||||
permission: workspace.permission,
|
||||
} satisfies z.infer<typeof schema>;
|
||||
});
|
||||
const old = storage.getItem(kAffineLocal);
|
||||
if (
|
||||
Array.isArray(old) &&
|
||||
old.every(item => schema.safeParse(item).success)
|
||||
) {
|
||||
const data = [...dump];
|
||||
old.forEach((item: z.infer<typeof schema>) => {
|
||||
const has = dump.find(dump => dump.id === item.id);
|
||||
if (!has) {
|
||||
data.push(item);
|
||||
}
|
||||
});
|
||||
storage.setItem(kAffineLocal, [...data]);
|
||||
}
|
||||
});
|
||||
|
||||
// only save data when login in
|
||||
const dump = allWorkspaces.map(workspace => {
|
||||
return {
|
||||
id: workspace.id,
|
||||
type: workspace.type,
|
||||
public: workspace.public,
|
||||
permission: workspace.permission,
|
||||
} satisfies z.infer<typeof schema>;
|
||||
const affineWorkspace: AffineWorkspace = {
|
||||
...workspace,
|
||||
flavour: WorkspaceFlavour.AFFINE,
|
||||
blockSuiteWorkspace,
|
||||
providers: [...createAffineProviders(blockSuiteWorkspace)],
|
||||
};
|
||||
return affineWorkspace;
|
||||
});
|
||||
storage.setItem(kAffineLocal, [...dump]);
|
||||
}
|
||||
});
|
||||
workspaces.forEach(workspace => {
|
||||
const idx = allWorkspaces.findIndex(({ id }) => id === workspace.id);
|
||||
if (idx !== -1) {
|
||||
allWorkspaces.splice(idx, 1, workspace);
|
||||
} else {
|
||||
allWorkspaces.push(workspace);
|
||||
}
|
||||
});
|
||||
|
||||
// only save data when login in
|
||||
const dump = allWorkspaces.map(workspace => {
|
||||
return {
|
||||
id: workspace.id,
|
||||
type: workspace.type,
|
||||
public: workspace.public,
|
||||
permission: workspace.permission,
|
||||
} satisfies z.infer<typeof schema>;
|
||||
});
|
||||
storage.setItem(kAffineLocal, [...dump]);
|
||||
} catch (e) {
|
||||
console.error('fetch affine workspaces failed', e);
|
||||
}
|
||||
|
||||
@@ -1,16 +1,14 @@
|
||||
import {
|
||||
createAuthClient,
|
||||
createBareClient,
|
||||
getApis,
|
||||
GoogleAuth,
|
||||
} from '@affine/datacenter';
|
||||
import { config } from '@affine/env';
|
||||
import {
|
||||
createUserApis,
|
||||
createWorkspaceApis,
|
||||
} from '@affine/workspace/affine/api';
|
||||
import { currentAffineUserAtom } from '@affine/workspace/affine/atom';
|
||||
import type { LoginResponse } from '@affine/workspace/affine/login';
|
||||
import { parseIdToken, setLoginStorage } from '@affine/workspace/affine/login';
|
||||
|
||||
import { isValidIPAddress } from '../utils/is-valid-ip-address';
|
||||
import { jotaiStore } from '../atoms';
|
||||
import { isValidIPAddress } from '../utils';
|
||||
|
||||
let prefixUrl = '/';
|
||||
if (typeof window === 'undefined') {
|
||||
@@ -31,32 +29,27 @@ if (typeof window === 'undefined') {
|
||||
params.get('prefixUrl') && (prefixUrl = params.get('prefixUrl') as string);
|
||||
}
|
||||
|
||||
const affineApis = {} as ReturnType<typeof createUserApis> &
|
||||
ReturnType<typeof createWorkspaceApis>;
|
||||
Object.assign(affineApis, createUserApis(prefixUrl));
|
||||
Object.assign(affineApis, createWorkspaceApis(prefixUrl));
|
||||
|
||||
if (!globalThis.AFFINE_APIS) {
|
||||
globalThis.AFFINE_APIS = affineApis;
|
||||
globalThis.setLogin = (response: LoginResponse) => {
|
||||
jotaiStore.set(currentAffineUserAtom, parseIdToken(response.token));
|
||||
setLoginStorage(response);
|
||||
};
|
||||
}
|
||||
|
||||
declare global {
|
||||
// eslint-disable-next-line no-var
|
||||
var affineApis:
|
||||
var setLogin: typeof setLoginStorage;
|
||||
// eslint-disable-next-line no-var
|
||||
var AFFINE_APIS:
|
||||
| undefined
|
||||
| (ReturnType<typeof createUserApis> &
|
||||
ReturnType<typeof createWorkspaceApis>);
|
||||
}
|
||||
|
||||
const affineApis = {} as ReturnType<typeof createUserApis> &
|
||||
ReturnType<typeof createWorkspaceApis>;
|
||||
Object.assign(affineApis, createUserApis(prefixUrl));
|
||||
Object.assign(affineApis, createWorkspaceApis(prefixUrl));
|
||||
if (!globalThis.affineApis) {
|
||||
globalThis.affineApis = affineApis;
|
||||
}
|
||||
|
||||
const bareAuth = createBareClient(prefixUrl);
|
||||
const googleAuth = new GoogleAuth(bareAuth);
|
||||
export const clientAuth = createAuthClient(bareAuth, googleAuth);
|
||||
export const apis = getApis(bareAuth, clientAuth, googleAuth);
|
||||
|
||||
if (!globalThis.AFFINE_APIS) {
|
||||
globalThis.AFFINE_APIS = apis;
|
||||
}
|
||||
|
||||
declare global {
|
||||
// eslint-disable-next-line no-var
|
||||
var AFFINE_APIS: ReturnType<typeof getApis>;
|
||||
}
|
||||
export { affineApis };
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { Workspace as RemoteWorkspace } from '@affine/datacenter';
|
||||
import type { Workspace as RemoteWorkspace } from '@affine/workspace/affine/api';
|
||||
import type { WorkspaceFlavour } from '@affine/workspace/type';
|
||||
import { Workspace as BlockSuiteWorkspace } from '@blocksuite/store';
|
||||
import type { NextPage } from 'next';
|
||||
|
||||
Reference in New Issue
Block a user