mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-09-22 03:33:06 +08:00
fix: fallback to all page (#1749)
This commit is contained in:
@@ -24,7 +24,7 @@ import {
|
|||||||
} from '../../atoms';
|
} from '../../atoms';
|
||||||
import { LocalPlugin } from '../../plugins/local';
|
import { LocalPlugin } from '../../plugins/local';
|
||||||
import type { LocalWorkspace } from '../../shared';
|
import type { LocalWorkspace } from '../../shared';
|
||||||
import { BlockSuiteWorkspace } from '../../shared';
|
import { BlockSuiteWorkspace, WorkspaceSubPath } from '../../shared';
|
||||||
import { useIsFirstLoad, useOpenTips } from '../affine/use-is-first-load';
|
import { useIsFirstLoad, useOpenTips } from '../affine/use-is-first-load';
|
||||||
import {
|
import {
|
||||||
useRecentlyViewed,
|
useRecentlyViewed,
|
||||||
@@ -52,7 +52,13 @@ vi.mock(
|
|||||||
let blockSuiteWorkspace: BlockSuiteWorkspace;
|
let blockSuiteWorkspace: BlockSuiteWorkspace;
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
routerMock.useParser(
|
routerMock.useParser(
|
||||||
createDynamicRouteParser(['/workspace/[workspaceId]/[pageId]'])
|
createDynamicRouteParser([
|
||||||
|
`/workspace/[workspaceId/${WorkspaceSubPath.ALL}`,
|
||||||
|
`/workspace/[workspaceId/${WorkspaceSubPath.SETTING}`,
|
||||||
|
`/workspace/[workspaceId/${WorkspaceSubPath.TRASH}`,
|
||||||
|
`/workspace/[workspaceId/${WorkspaceSubPath.FAVORITE}`,
|
||||||
|
'/workspace/[workspaceId]/[pageId]',
|
||||||
|
])
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -214,6 +220,34 @@ describe('useSyncRouterWithCurrentWorkspaceAndPage', () => {
|
|||||||
expect(routerHook.result.current.asPath).toBe(`/workspace/${id}/page0`);
|
expect(routerHook.result.current.asPath).toBe(`/workspace/${id}/page0`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('from empty workspace', async () => {
|
||||||
|
const { ProviderWrapper, store } = await getJotaiContext();
|
||||||
|
const mutationHook = renderHook(() => useWorkspacesHelper(), {
|
||||||
|
wrapper: ProviderWrapper,
|
||||||
|
});
|
||||||
|
const id = await mutationHook.result.current.createLocalWorkspace('test0');
|
||||||
|
const workspaces = await store.get(workspacesAtom);
|
||||||
|
expect(workspaces.length).toEqual(1);
|
||||||
|
mutationHook.rerender();
|
||||||
|
const routerHook = renderHook(() => useRouter());
|
||||||
|
await routerHook.result.current.push(`/workspace/${id}/not_exist`);
|
||||||
|
routerHook.rerender();
|
||||||
|
expect(routerHook.result.current.asPath).toBe(`/workspace/${id}/not_exist`);
|
||||||
|
renderHook(
|
||||||
|
({ router }) => useSyncRouterWithCurrentWorkspaceAndPage(router),
|
||||||
|
{
|
||||||
|
wrapper: ProviderWrapper,
|
||||||
|
initialProps: {
|
||||||
|
router: routerHook.result.current,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
await new Promise(resolve => setTimeout(resolve, REDIRECT_TIMEOUT + 50));
|
||||||
|
|
||||||
|
expect(routerHook.result.current.asPath).toBe(`/workspace/${id}/all`);
|
||||||
|
});
|
||||||
|
|
||||||
test('from incorrect "/workspace/[workspaceId]/[pageId]"', async () => {
|
test('from incorrect "/workspace/[workspaceId]/[pageId]"', async () => {
|
||||||
const { ProviderWrapper, store } = await getJotaiContext();
|
const { ProviderWrapper, store } = await getJotaiContext();
|
||||||
const mutationHook = renderHook(() => useWorkspacesHelper(), {
|
const mutationHook = renderHook(() => useWorkspacesHelper(), {
|
||||||
@@ -238,7 +272,7 @@ describe('useSyncRouterWithCurrentWorkspaceAndPage', () => {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
await new Promise(resolve => setTimeout(resolve, REDIRECT_TIMEOUT));
|
await new Promise(resolve => setTimeout(resolve, REDIRECT_TIMEOUT + 50));
|
||||||
|
|
||||||
expect(routerHook.result.current.asPath).toBe(`/workspace/${id}/page0`);
|
expect(routerHook.result.current.asPath).toBe(`/workspace/${id}/page0`);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -4,8 +4,10 @@ import { useEffect } from 'react';
|
|||||||
|
|
||||||
import { currentPageIdAtom, jotaiStore } from '../atoms';
|
import { currentPageIdAtom, jotaiStore } from '../atoms';
|
||||||
import type { RemWorkspace } from '../shared';
|
import type { RemWorkspace } from '../shared';
|
||||||
|
import { WorkspaceSubPath } from '../shared';
|
||||||
import { useCurrentPageId } from './current/use-current-page-id';
|
import { useCurrentPageId } from './current/use-current-page-id';
|
||||||
import { useCurrentWorkspace } from './current/use-current-workspace';
|
import { useCurrentWorkspace } from './current/use-current-workspace';
|
||||||
|
import { RouteLogic, useRouterHelper } from './use-router-helper';
|
||||||
import { useWorkspaces } from './use-workspaces';
|
import { useWorkspaces } from './use-workspaces';
|
||||||
|
|
||||||
export function findSuitablePageId(
|
export function findSuitablePageId(
|
||||||
@@ -39,6 +41,7 @@ export function useSyncRouterWithCurrentWorkspaceAndPage(router: NextRouter) {
|
|||||||
const [currentWorkspace, setCurrentWorkspaceId] = useCurrentWorkspace();
|
const [currentWorkspace, setCurrentWorkspaceId] = useCurrentWorkspace();
|
||||||
const [currentPageId, setCurrentPageId] = useCurrentPageId();
|
const [currentPageId, setCurrentPageId] = useCurrentPageId();
|
||||||
const workspaces = useWorkspaces();
|
const workspaces = useWorkspaces();
|
||||||
|
const { jumpToSubPath } = useRouterHelper(router);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const listener: Parameters<typeof router.events.on>[1] = (url: string) => {
|
const listener: Parameters<typeof router.events.on>[1] = (url: string) => {
|
||||||
if (url.startsWith('/')) {
|
if (url.startsWith('/')) {
|
||||||
@@ -161,7 +164,8 @@ export function useSyncRouterWithCurrentWorkspaceAndPage(router: NextRouter) {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
const clearId = setTimeout(() => {
|
const clearId = setTimeout(() => {
|
||||||
if (jotaiStore.get(currentPageIdAtom) === null) {
|
const pageId = jotaiStore.get(currentPageIdAtom);
|
||||||
|
if (pageId === null) {
|
||||||
const id =
|
const id =
|
||||||
currentWorkspace.blockSuiteWorkspace.meta.pageMetas.at(0)?.id;
|
currentWorkspace.blockSuiteWorkspace.meta.pageMetas.at(0)?.id;
|
||||||
if (id) {
|
if (id) {
|
||||||
@@ -173,8 +177,15 @@ export function useSyncRouterWithCurrentWorkspaceAndPage(router: NextRouter) {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
setCurrentPageId(id);
|
setCurrentPageId(id);
|
||||||
|
dispose.dispose();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
jumpToSubPath(
|
||||||
|
currentWorkspace.blockSuiteWorkspace.id,
|
||||||
|
WorkspaceSubPath.ALL,
|
||||||
|
RouteLogic.REPLACE
|
||||||
|
);
|
||||||
dispose.dispose();
|
dispose.dispose();
|
||||||
}, REDIRECT_TIMEOUT);
|
}, REDIRECT_TIMEOUT);
|
||||||
return () => {
|
return () => {
|
||||||
@@ -194,5 +205,6 @@ export function useSyncRouterWithCurrentWorkspaceAndPage(router: NextRouter) {
|
|||||||
setCurrentWorkspaceId,
|
setCurrentWorkspaceId,
|
||||||
workspaces,
|
workspaces,
|
||||||
router,
|
router,
|
||||||
|
jumpToSubPath,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user