mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-11 20:08:37 +00:00
feat: single page API in public workspace (#1794)
This commit is contained in:
@@ -5,7 +5,42 @@ import { atom } from 'jotai';
|
||||
import { BlockSuiteWorkspace } from '../../shared';
|
||||
import { affineApis } from '../../shared/apis';
|
||||
|
||||
function createPublicWorkspace(workspaceId: string, binary: ArrayBuffer) {
|
||||
const blockSuiteWorkspace = createEmptyBlockSuiteWorkspace(
|
||||
workspaceId,
|
||||
(k: string) =>
|
||||
// fixme: token could be expired
|
||||
({ api: `api/workspace`, token: getLoginStorage()?.token }[k])
|
||||
);
|
||||
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 blockSuiteWorkspace;
|
||||
}
|
||||
|
||||
export const publicWorkspaceIdAtom = atom<string | null>(null);
|
||||
export const publicWorkspacePageIdAtom = atom<string | null>(null);
|
||||
export const publicPageBlockSuiteAtom = atom<Promise<BlockSuiteWorkspace>>(
|
||||
async get => {
|
||||
const workspaceId = get(publicWorkspaceIdAtom);
|
||||
const pageId = get(publicWorkspacePageIdAtom);
|
||||
if (!workspaceId || !pageId) {
|
||||
throw new Error('No workspace id or page id');
|
||||
}
|
||||
const binary = await affineApis.downloadPublicWorkspacePage(
|
||||
workspaceId,
|
||||
pageId
|
||||
);
|
||||
return createPublicWorkspace(workspaceId, binary);
|
||||
}
|
||||
);
|
||||
export const publicBlockSuiteAtom = atom<Promise<BlockSuiteWorkspace>>(
|
||||
async get => {
|
||||
const workspaceId = get(publicWorkspaceIdAtom);
|
||||
@@ -13,25 +48,6 @@ export const publicBlockSuiteAtom = atom<Promise<BlockSuiteWorkspace>>(
|
||||
throw new Error('No workspace id');
|
||||
}
|
||||
const binary = await affineApis.downloadWorkspace(workspaceId, true);
|
||||
const blockSuiteWorkspace = createEmptyBlockSuiteWorkspace(
|
||||
workspaceId,
|
||||
(k: string) =>
|
||||
// fixme: token could be expired
|
||||
({ api: `api/workspace`, token: getLoginStorage()?.token }[k])
|
||||
);
|
||||
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 blockSuiteWorkspace;
|
||||
return createPublicWorkspace(workspaceId, binary);
|
||||
}
|
||||
);
|
||||
|
||||
@@ -10,8 +10,9 @@ import type React from 'react';
|
||||
import { Suspense, useEffect } from 'react';
|
||||
|
||||
import {
|
||||
publicBlockSuiteAtom,
|
||||
publicPageBlockSuiteAtom,
|
||||
publicWorkspaceIdAtom,
|
||||
publicWorkspacePageIdAtom,
|
||||
} from '../../../atoms/public-workspace';
|
||||
import { QueryParamError } from '../../../components/affine/affine-error-eoundary';
|
||||
import { PageDetailEditor } from '../../../components/page-detail-editor';
|
||||
@@ -51,7 +52,7 @@ export const StyledBreadcrumbs = styled(Link)(({ theme }) => {
|
||||
const PublicWorkspaceDetailPageInner: React.FC<{
|
||||
pageId: string;
|
||||
}> = ({ pageId }) => {
|
||||
const blockSuiteWorkspace = useAtomValue(publicBlockSuiteAtom);
|
||||
const blockSuiteWorkspace = useAtomValue(publicPageBlockSuiteAtom);
|
||||
if (!blockSuiteWorkspace) {
|
||||
throw new Error('cannot find workspace');
|
||||
}
|
||||
@@ -101,6 +102,7 @@ export const PublicWorkspaceDetailPage: NextPageWithLayout = () => {
|
||||
const workspaceId = router.query.workspaceId;
|
||||
const pageId = router.query.pageId;
|
||||
const setWorkspaceId = useSetAtom(publicWorkspaceIdAtom);
|
||||
const setPageId = useSetAtom(publicWorkspacePageIdAtom);
|
||||
useEffect(() => {
|
||||
if (!router.isReady) {
|
||||
return;
|
||||
@@ -108,7 +110,10 @@ export const PublicWorkspaceDetailPage: NextPageWithLayout = () => {
|
||||
if (typeof workspaceId === 'string') {
|
||||
setWorkspaceId(workspaceId);
|
||||
}
|
||||
}, [router.isReady, setWorkspaceId, workspaceId]);
|
||||
if (typeof pageId === 'string') {
|
||||
setPageId(pageId);
|
||||
}
|
||||
}, [pageId, router.isReady, setPageId, setWorkspaceId, workspaceId]);
|
||||
const value = useAtomValue(publicWorkspaceIdAtom);
|
||||
if (!router.isReady || !value) {
|
||||
return <PageLoading />;
|
||||
|
||||
Reference in New Issue
Block a user