mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-23 13:29:02 +08:00
refactor: remove null type in hooks (#1955)
This commit is contained in:
@@ -16,6 +16,10 @@ import { __unstableSchemas, AffineSchemas } from '@blocksuite/blocks/models';
|
||||
import type { Page } from '@blocksuite/store';
|
||||
import { assertExists } from '@blocksuite/store';
|
||||
import { render, renderHook } from '@testing-library/react';
|
||||
import {
|
||||
useBlockSuitePageMeta,
|
||||
usePageMetaHelper,
|
||||
} from '@toeverything/hooks/use-block-suite-page-meta';
|
||||
import { createStore, Provider } from 'jotai';
|
||||
import { useRouter } from 'next/router';
|
||||
import routerMock from 'next-router-mock';
|
||||
@@ -36,7 +40,6 @@ import {
|
||||
useLastVersion,
|
||||
useTipsDisplayStatus,
|
||||
} from '../use-is-first-load';
|
||||
import { usePageMeta, usePageMetaHelper } from '../use-page-meta';
|
||||
import {
|
||||
useRecentlyViewed,
|
||||
useSyncRecentViewsWithRouter,
|
||||
@@ -103,7 +106,7 @@ beforeEach(async () => {
|
||||
describe('usePageMetas', async () => {
|
||||
test('basic', async () => {
|
||||
const Component = () => {
|
||||
const pageMetas = usePageMeta(blockSuiteWorkspace);
|
||||
const pageMetas = useBlockSuitePageMeta(blockSuiteWorkspace);
|
||||
return (
|
||||
<div>
|
||||
{pageMetas.map(meta => (
|
||||
@@ -121,7 +124,7 @@ describe('usePageMetas', async () => {
|
||||
|
||||
test('mutation', () => {
|
||||
const { result, rerender } = renderHook(() =>
|
||||
usePageMeta(blockSuiteWorkspace)
|
||||
useBlockSuitePageMeta(blockSuiteWorkspace)
|
||||
);
|
||||
expect(result.current.length).toBe(3);
|
||||
expect(result.current[0].mode).not.exist;
|
||||
@@ -143,7 +146,7 @@ describe('usePageMetas', async () => {
|
||||
|
||||
test('update title', () => {
|
||||
const { result, rerender } = renderHook(() =>
|
||||
usePageMeta(blockSuiteWorkspace)
|
||||
useBlockSuitePageMeta(blockSuiteWorkspace)
|
||||
);
|
||||
expect(result.current.length).toBe(3);
|
||||
expect(result.current[0].mode).not.exist;
|
||||
@@ -260,7 +263,7 @@ describe('useRecentlyViewed', () => {
|
||||
});
|
||||
routerHook.rerender();
|
||||
const syncHook = renderHook(
|
||||
router => useSyncRecentViewsWithRouter(router),
|
||||
router => useSyncRecentViewsWithRouter(router, blockSuiteWorkspace),
|
||||
{
|
||||
wrapper: ProviderWrapper,
|
||||
initialProps: routerHook.result.current,
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
/**
|
||||
* @vitest-environment happy-dom
|
||||
*/
|
||||
import 'fake-indexeddb/auto';
|
||||
|
||||
import { __unstableSchemas, AffineSchemas } from '@blocksuite/blocks/models';
|
||||
import type { Page } from '@blocksuite/store';
|
||||
import { renderHook } from '@testing-library/react';
|
||||
import { beforeEach, describe, expect, test } from 'vitest';
|
||||
|
||||
import { BlockSuiteWorkspace } from '../../shared';
|
||||
import { useBlockSuiteWorkspaceHelper } from '../use-blocksuite-workspace-helper';
|
||||
import { usePageMeta } from '../use-page-meta';
|
||||
|
||||
let blockSuiteWorkspace: BlockSuiteWorkspace;
|
||||
|
||||
function handleNewPage(page: Page) {
|
||||
const pageBlockId = page.addBlock('affine:page', { title: '' });
|
||||
const frameId = page.addBlock('affine:frame', {}, pageBlockId);
|
||||
page.addBlock('affine:paragraph', {}, frameId);
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
blockSuiteWorkspace = new BlockSuiteWorkspace({
|
||||
id: 'test',
|
||||
})
|
||||
.register(AffineSchemas)
|
||||
.register(__unstableSchemas);
|
||||
handleNewPage(blockSuiteWorkspace.createPage('page0'));
|
||||
handleNewPage(blockSuiteWorkspace.createPage('page1'));
|
||||
handleNewPage(blockSuiteWorkspace.createPage('page2'));
|
||||
});
|
||||
|
||||
describe('useBlockSuiteWorkspaceHelper', () => {
|
||||
test('should create page', () => {
|
||||
expect(blockSuiteWorkspace.meta.pageMetas.length).toBe(3);
|
||||
const helperHook = renderHook(() =>
|
||||
useBlockSuiteWorkspaceHelper(blockSuiteWorkspace)
|
||||
);
|
||||
const pageMetaHook = renderHook(() => usePageMeta(blockSuiteWorkspace));
|
||||
expect(pageMetaHook.result.current.length).toBe(3);
|
||||
expect(blockSuiteWorkspace.meta.pageMetas.length).toBe(3);
|
||||
const page = helperHook.result.current.createPage('page4');
|
||||
expect(page.id).toBe('page4');
|
||||
expect(blockSuiteWorkspace.meta.pageMetas.length).toBe(4);
|
||||
pageMetaHook.rerender();
|
||||
expect(pageMetaHook.result.current.length).toBe(4);
|
||||
});
|
||||
|
||||
test('milestone', async () => {
|
||||
expect(blockSuiteWorkspace.meta.pageMetas.length).toBe(3);
|
||||
const helperHook = renderHook(() =>
|
||||
useBlockSuiteWorkspaceHelper(blockSuiteWorkspace)
|
||||
);
|
||||
await helperHook.result.current.markMilestone('test');
|
||||
expect(blockSuiteWorkspace.meta.pageMetas.length).toBe(3);
|
||||
handleNewPage(helperHook.result.current.createPage('page4'));
|
||||
expect(blockSuiteWorkspace.meta.pageMetas.length).toBe(4);
|
||||
expect(await helperHook.result.current.listMilestone()).toHaveProperty(
|
||||
'test'
|
||||
);
|
||||
await helperHook.result.current.revertMilestone('test');
|
||||
expect(blockSuiteWorkspace.meta.pageMetas.length).toBe(3);
|
||||
});
|
||||
});
|
||||
+8
-3
@@ -1,11 +1,16 @@
|
||||
import {
|
||||
useBlockSuitePageMeta,
|
||||
usePageMetaHelper,
|
||||
} from '@toeverything/hooks/use-block-suite-page-meta';
|
||||
import { useCallback } from 'react';
|
||||
|
||||
import type { BlockSuiteWorkspace } from '../../shared';
|
||||
import { usePageMeta, usePageMetaHelper } from '../use-page-meta';
|
||||
|
||||
export function useMetaHelper(blockSuiteWorkspace: BlockSuiteWorkspace | null) {
|
||||
export function useBlockSuiteMetaHelper(
|
||||
blockSuiteWorkspace: BlockSuiteWorkspace
|
||||
) {
|
||||
const { setPageMeta, getPageMeta } = usePageMetaHelper(blockSuiteWorkspace);
|
||||
const metas = usePageMeta(blockSuiteWorkspace);
|
||||
const metas = useBlockSuitePageMeta(blockSuiteWorkspace);
|
||||
|
||||
const removeToTrash = useCallback(
|
||||
(pageId: string, isRoot = true) => {
|
||||
@@ -17,7 +17,7 @@ export const lastWorkspaceIdAtom = atomWithSyncStorage<string | null>(
|
||||
);
|
||||
|
||||
export function useCurrentWorkspace(): [
|
||||
AllWorkspace | null,
|
||||
AllWorkspace,
|
||||
(id: string | null) => void
|
||||
] {
|
||||
const currentWorkspace = useAtomValue(rootCurrentWorkspaceAtom);
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
import type { Page } from '@blocksuite/store';
|
||||
import { assertExists } from '@blocksuite/store';
|
||||
import {
|
||||
getMilestones,
|
||||
markMilestone,
|
||||
revertUpdate,
|
||||
} from '@toeverything/y-indexeddb';
|
||||
import { useMemo } from 'react';
|
||||
|
||||
import type { BlockSuiteWorkspace } from '../shared';
|
||||
|
||||
export function useBlockSuiteWorkspaceHelper(
|
||||
blockSuiteWorkspace: BlockSuiteWorkspace | null
|
||||
) {
|
||||
return useMemo(
|
||||
() => ({
|
||||
createPage: (pageId: string, parentId?: string): Page => {
|
||||
assertExists(blockSuiteWorkspace);
|
||||
return blockSuiteWorkspace.createPage(pageId, parentId);
|
||||
},
|
||||
markMilestone: async (name: string) => {
|
||||
assertExists(blockSuiteWorkspace);
|
||||
const doc = blockSuiteWorkspace.doc;
|
||||
await markMilestone(blockSuiteWorkspace.id, doc, name);
|
||||
},
|
||||
revertMilestone: async (name: string) => {
|
||||
assertExists(blockSuiteWorkspace);
|
||||
const doc = blockSuiteWorkspace.doc;
|
||||
const list = await getMilestones(blockSuiteWorkspace.id);
|
||||
if (!list) {
|
||||
throw new Error('no milestone');
|
||||
}
|
||||
const milestone = list[name];
|
||||
if (milestone) {
|
||||
revertUpdate(doc, milestone, () => 'Map');
|
||||
}
|
||||
},
|
||||
listMilestone: async () => {
|
||||
assertExists(blockSuiteWorkspace);
|
||||
return await getMilestones(blockSuiteWorkspace.id);
|
||||
},
|
||||
}),
|
||||
[blockSuiteWorkspace]
|
||||
);
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
import type { PageBlockModel } from '@blocksuite/blocks';
|
||||
import type { PageMeta } from '@blocksuite/store';
|
||||
import { assertExists } from '@blocksuite/store';
|
||||
import { useEffect, useMemo, useState } from 'react';
|
||||
|
||||
import type { BlockSuiteWorkspace } from '../shared';
|
||||
|
||||
declare module '@blocksuite/store' {
|
||||
interface PageMeta {
|
||||
favorite?: boolean;
|
||||
subpageIds: string[];
|
||||
// If a page remove to trash, and it is a subpage, it will remove from its parent `subpageIds`, 'trashRelate' is use for save it parent
|
||||
trashRelate?: string;
|
||||
trash?: boolean;
|
||||
trashDate?: number;
|
||||
// whether to create the page with the default template
|
||||
init?: boolean;
|
||||
isRootPinboard?: boolean;
|
||||
}
|
||||
}
|
||||
|
||||
export function usePageMeta(
|
||||
blockSuiteWorkspace: BlockSuiteWorkspace | null
|
||||
): PageMeta[] {
|
||||
const [pageMeta, setPageMeta] = useState<PageMeta[]>(
|
||||
() => blockSuiteWorkspace?.meta.pageMetas ?? []
|
||||
);
|
||||
const [prev, setPrev] = useState(() => blockSuiteWorkspace);
|
||||
if (prev !== blockSuiteWorkspace) {
|
||||
setPrev(blockSuiteWorkspace);
|
||||
if (blockSuiteWorkspace) {
|
||||
setPageMeta(blockSuiteWorkspace.meta.pageMetas);
|
||||
}
|
||||
}
|
||||
useEffect(() => {
|
||||
if (blockSuiteWorkspace) {
|
||||
const dispose = blockSuiteWorkspace.meta.pageMetasUpdated.on(() => {
|
||||
setPageMeta(blockSuiteWorkspace.meta.pageMetas);
|
||||
});
|
||||
return () => {
|
||||
dispose.dispose();
|
||||
};
|
||||
}
|
||||
}, [blockSuiteWorkspace]);
|
||||
return pageMeta;
|
||||
}
|
||||
|
||||
export function usePageMetaHelper(
|
||||
blockSuiteWorkspace: BlockSuiteWorkspace | null
|
||||
) {
|
||||
return useMemo(
|
||||
() => ({
|
||||
setPageTitle: (pageId: string, newTitle: string) => {
|
||||
assertExists(blockSuiteWorkspace);
|
||||
const page = blockSuiteWorkspace.getPage(pageId);
|
||||
assertExists(page);
|
||||
const pageBlock = page
|
||||
.getBlockByFlavour('affine:page')
|
||||
.at(0) as PageBlockModel;
|
||||
assertExists(pageBlock);
|
||||
page.transact(() => {
|
||||
pageBlock.title.delete(0, pageBlock.title.length);
|
||||
pageBlock.title.insert(newTitle, 0);
|
||||
});
|
||||
assertExists(blockSuiteWorkspace);
|
||||
blockSuiteWorkspace.meta.setPageMeta(pageId, { title: newTitle });
|
||||
},
|
||||
setPageMeta: (pageId: string, pageMeta: Partial<PageMeta>) => {
|
||||
assertExists(blockSuiteWorkspace);
|
||||
blockSuiteWorkspace.meta.setPageMeta(pageId, pageMeta);
|
||||
},
|
||||
getPageMeta: (pageId: string) => {
|
||||
assertExists(blockSuiteWorkspace);
|
||||
return blockSuiteWorkspace.meta.getPageMeta(pageId);
|
||||
},
|
||||
shiftPageMeta: (pageId: string, index: number) => {
|
||||
assertExists(blockSuiteWorkspace);
|
||||
return blockSuiteWorkspace.meta.shiftPageMeta(pageId, index);
|
||||
},
|
||||
}),
|
||||
[blockSuiteWorkspace]
|
||||
);
|
||||
}
|
||||
@@ -2,12 +2,12 @@ import type { TreeViewProps } from '@affine/component';
|
||||
import { DebugLogger } from '@affine/debug';
|
||||
import type { PageMeta } from '@blocksuite/store';
|
||||
import { nanoid } from '@blocksuite/store';
|
||||
import { usePageMetaHelper } from '@toeverything/hooks/use-block-suite-page-meta';
|
||||
import { useBlockSuiteWorkspaceHelper } from '@toeverything/hooks/use-block-suite-workspace-helper';
|
||||
import { useCallback } from 'react';
|
||||
|
||||
import type { BlockSuiteWorkspace } from '../shared';
|
||||
import { useMetaHelper } from './affine/use-meta-helper';
|
||||
import { useBlockSuiteWorkspaceHelper } from './use-blocksuite-workspace-helper';
|
||||
import { usePageMetaHelper } from './use-page-meta';
|
||||
import { useBlockSuiteMetaHelper } from './affine/use-block-suite-meta-helper';
|
||||
import type { NodeRenderProps } from './use-pinboard-data';
|
||||
|
||||
const logger = new DebugLogger('pinboard');
|
||||
@@ -26,7 +26,7 @@ export function usePinboardHandler({
|
||||
onDelete,
|
||||
onDrop,
|
||||
}: {
|
||||
blockSuiteWorkspace: BlockSuiteWorkspace | null;
|
||||
blockSuiteWorkspace: BlockSuiteWorkspace;
|
||||
metas: PageMeta[];
|
||||
onAdd?: (addedId: string, parentId: string) => void;
|
||||
onDelete?: TreeViewProps<NodeRenderProps>['onDelete'];
|
||||
@@ -35,7 +35,7 @@ export function usePinboardHandler({
|
||||
const { createPage } = useBlockSuiteWorkspaceHelper(blockSuiteWorkspace);
|
||||
const { setPageMeta } = usePageMetaHelper(blockSuiteWorkspace);
|
||||
const { removeToTrash: removeToTrashHelper } =
|
||||
useMetaHelper(blockSuiteWorkspace);
|
||||
useBlockSuiteMetaHelper(blockSuiteWorkspace);
|
||||
// Just need handle add operation, delete check is handled in blockSuite's reference link
|
||||
const addReferenceLink = useCallback(
|
||||
(pageId: string, referenceId: string) => {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import type { Workspace } from '@blocksuite/store';
|
||||
import { useBlockSuitePageMeta } from '@toeverything/hooks/use-block-suite-page-meta';
|
||||
import { useAtomValue, useSetAtom } from 'jotai';
|
||||
import type { NextRouter } from 'next/router';
|
||||
import { useEffect } from 'react';
|
||||
@@ -8,7 +10,6 @@ import {
|
||||
workspaceRecentViresWriteAtom,
|
||||
} from '../atoms';
|
||||
import { useCurrentWorkspace } from './current/use-current-workspace';
|
||||
import { usePageMeta } from './use-page-meta';
|
||||
|
||||
export function useRecentlyViewed() {
|
||||
const [workspace] = useCurrentWorkspace();
|
||||
@@ -19,13 +20,14 @@ export function useRecentlyViewed() {
|
||||
return recentlyViewed[workspaceId] ?? [];
|
||||
}
|
||||
|
||||
export function useSyncRecentViewsWithRouter(router: NextRouter) {
|
||||
const [workspace] = useCurrentWorkspace();
|
||||
const workspaceId = workspace?.id || null;
|
||||
const blockSuiteWorkspace = workspace?.blockSuiteWorkspace || null;
|
||||
export function useSyncRecentViewsWithRouter(
|
||||
router: NextRouter,
|
||||
blockSuiteWorkspace: Workspace
|
||||
) {
|
||||
const workspaceId = blockSuiteWorkspace.id;
|
||||
const pageId = router.query.pageId;
|
||||
const set = useSetAtom(workspaceRecentViresWriteAtom);
|
||||
const meta = usePageMeta(blockSuiteWorkspace).find(
|
||||
const meta = useBlockSuitePageMeta(blockSuiteWorkspace).find(
|
||||
meta => meta.id === pageId
|
||||
);
|
||||
const currentMode = useAtomValue(workspacePreferredModeAtom)[
|
||||
|
||||
@@ -25,26 +25,6 @@ export function useRouterHelper(router: NextRouter) {
|
||||
},
|
||||
[router]
|
||||
);
|
||||
const jumpToWorkspace = useCallback(
|
||||
(workspaceId: string, logic: RouteLogic = RouteLogic.PUSH) => {
|
||||
if (router.pathname === '/workspace/[workspaceId]/[pageId]') {
|
||||
return router[logic]({
|
||||
pathname: `/workspace/[workspaceId]`,
|
||||
query: {
|
||||
workspaceId: workspaceId,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
return router[logic]({
|
||||
pathname: router.pathname,
|
||||
query: {
|
||||
workspaceId: workspaceId,
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
[router]
|
||||
);
|
||||
const jumpToPublicWorkspacePage = useCallback(
|
||||
(
|
||||
workspaceId: string,
|
||||
@@ -91,7 +71,6 @@ export function useRouterHelper(router: NextRouter) {
|
||||
|
||||
return {
|
||||
jumpToPage,
|
||||
jumpToWorkspace,
|
||||
jumpToPublicWorkspacePage,
|
||||
jumpToSubPath,
|
||||
openPage,
|
||||
|
||||
Reference in New Issue
Block a user