fix(core): delete page (#3419)

This commit is contained in:
Alex Yang
2023-07-27 11:12:11 -07:00
committed by GitHub
parent b47fbde479
commit a3d665503f
2 changed files with 33 additions and 7 deletions

View File

@@ -1,4 +1,9 @@
import type { WorkspaceSubPath } from '@affine/env/workspace';
import {
currentPageIdAtom,
currentWorkspaceIdAtom,
} from '@toeverything/plugin-infra/atom';
import { useSetAtom } from 'jotai';
import { useCallback } from 'react';
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
import { useLocation, useNavigate } from 'react-router-dom';
@@ -11,17 +16,22 @@ export enum RouteLogic {
export function useNavigateHelper() {
const location = useLocation();
const navigate = useNavigate();
const setWorkspaceId = useSetAtom(currentWorkspaceIdAtom);
const setCurrentPageId = useSetAtom(currentPageIdAtom);
const jumpToPage = useCallback(
(
workspaceId: string,
pageId: string,
logic: RouteLogic = RouteLogic.PUSH
) => {
setWorkspaceId(workspaceId);
setCurrentPageId(pageId);
return navigate(`/workspace/${workspaceId}/${pageId}`, {
replace: logic === RouteLogic.REPLACE,
});
},
[navigate]
[navigate, setCurrentPageId, setWorkspaceId]
);
const jumpToPublicWorkspacePage = useCallback(
(
@@ -29,11 +39,13 @@ export function useNavigateHelper() {
pageId: string,
logic: RouteLogic = RouteLogic.PUSH
) => {
setWorkspaceId(workspaceId);
setCurrentPageId(pageId);
return navigate(`/public-workspace/${workspaceId}/${pageId}`, {
replace: logic === RouteLogic.REPLACE,
});
},
[navigate]
[navigate, setCurrentPageId, setWorkspaceId]
);
const jumpToSubPath = useCallback(
(
@@ -41,14 +53,18 @@ export function useNavigateHelper() {
subPath: WorkspaceSubPath,
logic: RouteLogic = RouteLogic.PUSH
) => {
setWorkspaceId(workspaceId);
setCurrentPageId(null);
return navigate(`/workspace/${workspaceId}/${subPath}`, {
replace: logic === RouteLogic.REPLACE,
});
},
[navigate]
[navigate, setCurrentPageId, setWorkspaceId]
);
const openPage = useCallback(
(workspaceId: string, pageId: string) => {
setWorkspaceId(workspaceId);
setCurrentPageId(pageId);
const isPublicWorkspace =
location.pathname.indexOf('/public-workspace') === 0;
if (isPublicWorkspace) {
@@ -57,25 +73,35 @@ export function useNavigateHelper() {
return jumpToPage(workspaceId, pageId);
}
},
[jumpToPage, jumpToPublicWorkspacePage, location.pathname]
[
jumpToPage,
jumpToPublicWorkspacePage,
location.pathname,
setCurrentPageId,
setWorkspaceId,
]
);
const jumpToIndex = useCallback(
(logic: RouteLogic = RouteLogic.PUSH) => {
setWorkspaceId(null);
setCurrentPageId(null);
return navigate('/', {
replace: logic === RouteLogic.REPLACE,
});
},
[navigate]
[navigate, setCurrentPageId, setWorkspaceId]
);
const jumpTo404 = useCallback(
(logic: RouteLogic = RouteLogic.PUSH) => {
setWorkspaceId(null);
setCurrentPageId(null);
return navigate('/404', {
replace: logic === RouteLogic.REPLACE,
});
},
[navigate]
[navigate, setCurrentPageId, setWorkspaceId]
);
return {