fix: blob fetch and state syncing between tabs (#1287)
This commit is contained in:
@@ -1,17 +1,10 @@
|
|||||||
import { atom } from 'jotai';
|
import { atom } from 'jotai';
|
||||||
import { createStore } from 'jotai/index';
|
import { createStore } from 'jotai';
|
||||||
import { atomWithStorage } from 'jotai/utils';
|
|
||||||
import { unstable_batchedUpdates } from 'react-dom';
|
import { unstable_batchedUpdates } from 'react-dom';
|
||||||
|
|
||||||
// workspace necessary atoms
|
// workspace necessary atoms
|
||||||
export const currentWorkspaceIdAtom = atomWithStorage<string | null>(
|
export const currentWorkspaceIdAtom = atom<string | null>(null);
|
||||||
'affine-current-workspace-id',
|
export const currentPageIdAtom = atom<string | null>(null);
|
||||||
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
|
// If the workspace is locked, it means that the user maybe updating the workspace
|
||||||
// from local to remote or vice versa
|
// from local to remote or vice versa
|
||||||
export const workspaceLockAtom = atom(false);
|
export const workspaceLockAtom = atom(false);
|
||||||
|
|||||||
@@ -16,7 +16,10 @@ export const publicBlockSuiteAtom = atom<Promise<BlockSuiteWorkspace>>(
|
|||||||
throw new Error('No workspace id');
|
throw new Error('No workspace id');
|
||||||
}
|
}
|
||||||
const binary = await apis.downloadWorkspace(workspaceId, true);
|
const binary = await apis.downloadWorkspace(workspaceId, true);
|
||||||
const blockSuiteWorkspace = createEmptyBlockSuiteWorkspace(workspaceId);
|
const blockSuiteWorkspace = createEmptyBlockSuiteWorkspace(
|
||||||
|
workspaceId,
|
||||||
|
(_: string) => undefined
|
||||||
|
);
|
||||||
BlockSuiteWorkspace.Y.applyUpdate(
|
BlockSuiteWorkspace.Y.applyUpdate(
|
||||||
blockSuiteWorkspace.doc,
|
blockSuiteWorkspace.doc,
|
||||||
new Uint8Array(binary)
|
new Uint8Array(binary)
|
||||||
|
|||||||
@@ -38,7 +38,10 @@ globalThis.dataCenter = dataCenter;
|
|||||||
|
|
||||||
function createRemLocalWorkspace(name: string) {
|
function createRemLocalWorkspace(name: string) {
|
||||||
const id = nanoid();
|
const id = nanoid();
|
||||||
const blockSuiteWorkspace = createEmptyBlockSuiteWorkspace(id);
|
const blockSuiteWorkspace = createEmptyBlockSuiteWorkspace(
|
||||||
|
id,
|
||||||
|
(_: string) => undefined
|
||||||
|
);
|
||||||
blockSuiteWorkspace.meta.setName(name);
|
blockSuiteWorkspace.meta.setName(name);
|
||||||
const workspace: LocalWorkspace = {
|
const workspace: LocalWorkspace = {
|
||||||
flavour: RemWorkspaceFlavour.LOCAL,
|
flavour: RemWorkspaceFlavour.LOCAL,
|
||||||
|
|||||||
@@ -19,7 +19,11 @@ declare global {
|
|||||||
|
|
||||||
const BroadcastPage: React.FC = () => {
|
const BroadcastPage: React.FC = () => {
|
||||||
const blockSuiteWorkspace = useMemo(
|
const blockSuiteWorkspace = useMemo(
|
||||||
() => createEmptyBlockSuiteWorkspace('broadcast-test'),
|
() =>
|
||||||
|
createEmptyBlockSuiteWorkspace(
|
||||||
|
'broadcast-test',
|
||||||
|
(_: string) => undefined
|
||||||
|
),
|
||||||
[]
|
[]
|
||||||
);
|
);
|
||||||
const [provider, setProvider] = useState<BroadCastChannelProvider | null>(
|
const [provider, setProvider] = useState<BroadCastChannelProvider | null>(
|
||||||
|
|||||||
@@ -29,7 +29,10 @@ const PreviewPage: NextPage<PreviewPageProps> = ({
|
|||||||
const [blockSuiteWorkspace, setBlockSuiteWorkspace] =
|
const [blockSuiteWorkspace, setBlockSuiteWorkspace] =
|
||||||
useState<BlockSuiteWorkspace | null>(null);
|
useState<BlockSuiteWorkspace | null>(null);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const blockSuiteWorkspace = createEmptyBlockSuiteWorkspace('preview');
|
const blockSuiteWorkspace = createEmptyBlockSuiteWorkspace(
|
||||||
|
'preview',
|
||||||
|
(_: string) => undefined
|
||||||
|
);
|
||||||
blockSuiteWorkspace.signals.pageAdded.once(() => {
|
blockSuiteWorkspace.signals.pageAdded.once(() => {
|
||||||
setBlockSuiteWorkspace(blockSuiteWorkspace);
|
setBlockSuiteWorkspace(blockSuiteWorkspace);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -92,7 +92,10 @@ export const LocalPlugin: WorkspacePlugin<RemWorkspaceFlavour.LOCAL> = {
|
|||||||
if (config.enableIndexedDBProvider) {
|
if (config.enableIndexedDBProvider) {
|
||||||
const workspaces = await Promise.all(
|
const workspaces = await Promise.all(
|
||||||
ids.map(id => {
|
ids.map(id => {
|
||||||
const blockSuiteWorkspace = createEmptyBlockSuiteWorkspace(id);
|
const blockSuiteWorkspace = createEmptyBlockSuiteWorkspace(
|
||||||
|
id,
|
||||||
|
(_: string) => undefined
|
||||||
|
);
|
||||||
const workspace: LocalWorkspace = {
|
const workspace: LocalWorkspace = {
|
||||||
id,
|
id,
|
||||||
flavour: RemWorkspaceFlavour.LOCAL,
|
flavour: RemWorkspaceFlavour.LOCAL,
|
||||||
@@ -139,7 +142,10 @@ export const LocalPlugin: WorkspacePlugin<RemWorkspaceFlavour.LOCAL> = {
|
|||||||
}
|
}
|
||||||
logger.info('no local workspace found, create a new one');
|
logger.info('no local workspace found, create a new one');
|
||||||
const workspaceId = nanoid();
|
const workspaceId = nanoid();
|
||||||
const blockSuiteWorkspace = createEmptyBlockSuiteWorkspace(workspaceId);
|
const blockSuiteWorkspace = createEmptyBlockSuiteWorkspace(
|
||||||
|
workspaceId,
|
||||||
|
(_: string) => undefined
|
||||||
|
);
|
||||||
blockSuiteWorkspace.meta.setName('Untitled Workspace');
|
blockSuiteWorkspace.meta.setName('Untitled Workspace');
|
||||||
localStorage.setItem(kStoreKey, JSON.stringify([workspaceId]));
|
localStorage.setItem(kStoreKey, JSON.stringify([workspaceId]));
|
||||||
blockSuiteWorkspace.createPage(nanoid());
|
blockSuiteWorkspace.createPage(nanoid());
|
||||||
|
|||||||
Reference in New Issue
Block a user