From 27b7c4e8fb5ecb1e996bd2aab098fa4914668e13 Mon Sep 17 00:00:00 2001 From: QiShaoXuan Date: Thu, 5 Jan 2023 18:33:17 +0800 Subject: [PATCH] chore: remove useless file & code --- .../providers/app-state-provider/context.ts | 4 +- .../app-state-provider/dynamic-blocksuite.tsx | 22 +------ .../src/providers/app-state-provider/hooks.ts | 51 -------------- .../providers/app-state-provider/provider.tsx | 66 ++++--------------- 4 files changed, 18 insertions(+), 125 deletions(-) delete mode 100644 packages/app/src/providers/app-state-provider/hooks.ts diff --git a/packages/app/src/providers/app-state-provider/context.ts b/packages/app/src/providers/app-state-provider/context.ts index 98e535396e..f907246edf 100644 --- a/packages/app/src/providers/app-state-provider/context.ts +++ b/packages/app/src/providers/app-state-provider/context.ts @@ -5,6 +5,7 @@ import type { Workspace as StoreWorkspace, } from '@blocksuite/store'; import type { EditorContainer } from '@blocksuite/editor'; + export type LoadWorkspaceHandler = ( workspaceId: string, user?: AccessTokenMessage | null @@ -20,7 +21,7 @@ export interface AppStateValue { currentPage: StorePage | null; - workspaces: Record; + // workspaces: Record; editor: EditorContainer | null; synced: boolean; @@ -53,7 +54,6 @@ export const AppState = createContext({ synced: false, // eslint-disable-next-line @typescript-eslint/no-empty-function refreshWorkspacesMeta: () => {}, - workspaces: {}, }); export const useAppState = () => { diff --git a/packages/app/src/providers/app-state-provider/dynamic-blocksuite.tsx b/packages/app/src/providers/app-state-provider/dynamic-blocksuite.tsx index 3a8fb7671c..4b7b876545 100644 --- a/packages/app/src/providers/app-state-provider/dynamic-blocksuite.tsx +++ b/packages/app/src/providers/app-state-provider/dynamic-blocksuite.tsx @@ -2,31 +2,13 @@ import { useEffect } from 'react'; import type { Page } from '@blocksuite/store'; import '@blocksuite/blocks'; import { EditorContainer } from '@blocksuite/editor'; -import type { LoadWorkspaceHandler, CreateEditorHandler } from './context'; -import { getDataCenter } from '@affine/datacenter'; +import type { CreateEditorHandler } from './context'; interface Props { - setLoadWorkspaceHandler: (handler: LoadWorkspaceHandler) => void; setCreateEditorHandler: (handler: CreateEditorHandler) => void; } -const DynamicBlocksuite = ({ - setLoadWorkspaceHandler, - setCreateEditorHandler, -}: Props) => { - useEffect(() => { - const openWorkspace: LoadWorkspaceHandler = async (workspaceId: string) => { - if (workspaceId) { - const dc = await getDataCenter(); - return dc.load(workspaceId, { providerId: 'affine' }); - } else { - return null; - } - }; - - setLoadWorkspaceHandler(openWorkspace); - }, [setLoadWorkspaceHandler]); - +const DynamicBlocksuite = ({ setCreateEditorHandler }: Props) => { useEffect(() => { const createEditorHandler: CreateEditorHandler = (page: Page) => { const editor = new EditorContainer(); diff --git a/packages/app/src/providers/app-state-provider/hooks.ts b/packages/app/src/providers/app-state-provider/hooks.ts deleted file mode 100644 index 6996bd03ef..0000000000 --- a/packages/app/src/providers/app-state-provider/hooks.ts +++ /dev/null @@ -1,51 +0,0 @@ -import { useEffect } from 'react'; -import { useRouter } from 'next/router'; -import { useAppState } from './context'; -import { usePageHelper } from '@/hooks/use-page-helper'; -export const useLoadWorkspace = () => { - const router = useRouter(); - const { loadWorkspace, currentWorkspace, currentWorkspaceId } = useAppState(); - - const workspaceId = router.query.workspaceId as string; - - useEffect(() => { - loadWorkspace?.(workspaceId); - }, [workspaceId, loadWorkspace]); - - return currentWorkspaceId === workspaceId ? currentWorkspace : null; -}; - -export const useLoadPage = () => { - const router = useRouter(); - const { loadPage, currentPage, currentWorkspaceId } = useAppState(); - const { createPage } = usePageHelper(); - const workspace = useLoadWorkspace(); - - const pageId = router.query.pageId as string; - - useEffect(() => { - if (!workspace) { - return; - } - const page = pageId ? workspace?.getPage(pageId) : null; - if (page) { - loadPage?.(pageId); - return; - } - - const savedPageId = workspace.meta.pageMetas[0]?.id; - if (savedPageId) { - router.push(`/workspace/${currentWorkspaceId}/${savedPageId}`); - return; - } - - createPage().then(async pageId => { - if (!pageId) { - return; - } - router.push(`/workspace/${currentWorkspaceId}/${pageId}`); - }); - }, [workspace, pageId, loadPage, createPage, router, currentWorkspaceId]); - - return currentPage?.id === pageId ? currentPage : null; -}; diff --git a/packages/app/src/providers/app-state-provider/provider.tsx b/packages/app/src/providers/app-state-provider/provider.tsx index daa60ff85e..ce071d8487 100644 --- a/packages/app/src/providers/app-state-provider/provider.tsx +++ b/packages/app/src/providers/app-state-provider/provider.tsx @@ -1,4 +1,4 @@ -import { useMemo, useState, useEffect, useCallback, useRef } from 'react'; +import { useMemo, useState, useCallback, useRef } from 'react'; import type { ReactNode } from 'react'; import dynamic from 'next/dynamic'; import { getDataCenter } from '@affine/datacenter'; @@ -8,12 +8,20 @@ import type { CreateEditorHandler, LoadWorkspaceHandler, } from './context'; -import { Page, Workspace as StoreWorkspace } from '@blocksuite/store'; +import { Page } from '@blocksuite/store'; import { EditorContainer } from '@blocksuite/editor'; const DynamicBlocksuite = dynamic(() => import('./dynamic-blocksuite'), { ssr: false, }); +const loadWorkspaceHandler: LoadWorkspaceHandler = async workspaceId => { + if (workspaceId) { + const dc = await getDataCenter(); + return dc.load(workspaceId, { providerId: 'affine' }); + } else { + return null; + } +}; export const AppStateProvider = ({ children }: { children?: ReactNode }) => { const refreshWorkspacesMeta = async () => { const dc = await getDataCenter(); @@ -30,43 +38,10 @@ export const AppStateProvider = ({ children }: { children?: ReactNode }) => { currentWorkspace: null, currentPage: null, editor: null, - // Synced is used to ensure that the provider has synced with the server, - // So after Synced set to true, the other state is sure to be set. - synced: false, refreshWorkspacesMeta, - workspaces: {}, + synced: true, }); - useEffect(() => { - (async () => { - const workspacesList = await Promise.all( - state.workspacesMeta.map(async ({ id }) => { - const workspace = - (await loadWorkspaceHandler?.(id, state.user)) || null; - return { id, workspace }; - }) - ); - const workspaces: Record = {}; - workspacesList.forEach(({ id, workspace }) => { - workspaces[id] = workspace; - }); - setState(state => ({ - ...state, - workspaces, - })); - })(); - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [state.workspacesMeta]); - - const [loadWorkspaceHandler, _setLoadWorkspaceHandler] = - useState(); - const setLoadWorkspaceHandler = useCallback( - (handler: LoadWorkspaceHandler) => { - _setLoadWorkspaceHandler(() => handler); - }, - [_setLoadWorkspaceHandler] - ); - const [createEditorHandler, _setCreateEditorHandler] = useState(); @@ -85,7 +60,7 @@ export const AppStateProvider = ({ children }: { children?: ReactNode }) => { return state.currentWorkspace; } const workspace = - (await loadWorkspaceHandler?.(workspaceId, state.user)) || null; + (await loadWorkspaceHandler(workspaceId, state.user)) || null; // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-expect-error @@ -103,6 +78,7 @@ export const AppStateProvider = ({ children }: { children?: ReactNode }) => { })); return workspace; }; + const loadPage = useRef(() => Promise.resolve(null) ); @@ -147,17 +123,6 @@ export const AppStateProvider = ({ children }: { children?: ReactNode }) => { setState(state => ({ ...state, editor })); }; - useEffect(() => { - if (!loadWorkspaceHandler) { - return; - } - setState(state => ({ - ...state, - workspacesMeta: [], - synced: true, - })); - }, [loadWorkspaceHandler]); - const context = useMemo( () => ({ ...state, @@ -172,10 +137,7 @@ export const AppStateProvider = ({ children }: { children?: ReactNode }) => { return ( - + {children} );