mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 12:28:42 +00:00
feat: open last workspace when back or refresh affine (#1413)
Co-authored-by: himself65 <himself65@outlook.com>
This commit is contained in:
7
apps/web/src/hooks/affine/use-last-leave-workspace-id.ts
Normal file
7
apps/web/src/hooks/affine/use-last-leave-workspace-id.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { useAtomValue } from 'jotai';
|
||||
|
||||
import { lastWorkspaceIdAtom } from '../current/use-current-workspace';
|
||||
|
||||
export function useLastWorkspaceId() {
|
||||
return useAtomValue(lastWorkspaceIdAtom);
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { atom, useAtom, useAtomValue } from 'jotai';
|
||||
import { atom, useAtom, useAtomValue, useSetAtom } from 'jotai';
|
||||
import { atomWithStorage } from 'jotai/utils';
|
||||
import { useCallback } from 'react';
|
||||
|
||||
import {
|
||||
@@ -16,6 +17,11 @@ export const currentWorkspaceAtom = atom<Promise<RemWorkspace | null>>(
|
||||
}
|
||||
);
|
||||
|
||||
export const lastWorkspaceIdAtom = atomWithStorage<string | null>(
|
||||
'last_workspace_id',
|
||||
null
|
||||
);
|
||||
|
||||
export function useCurrentWorkspace(): [
|
||||
RemWorkspace | null,
|
||||
(id: string | null) => void
|
||||
@@ -23,14 +29,16 @@ export function useCurrentWorkspace(): [
|
||||
const currentWorkspace = useAtomValue(currentWorkspaceAtom);
|
||||
const [, setId] = useAtom(currentWorkspaceIdAtom);
|
||||
const [, setPageId] = useAtom(currentPageIdAtom);
|
||||
const setLast = useSetAtom(lastWorkspaceIdAtom);
|
||||
return [
|
||||
currentWorkspace,
|
||||
useCallback(
|
||||
(id: string | null) => {
|
||||
setPageId(null);
|
||||
setLast(id);
|
||||
setId(id);
|
||||
},
|
||||
[setId, setPageId]
|
||||
[setId, setLast, setPageId]
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -45,7 +45,6 @@ const App = function App({
|
||||
}) {
|
||||
const getLayout = Component.getLayout || EmptyLayout;
|
||||
const i18n = useMemo(() => createI18n(), []);
|
||||
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
// I know what I'm doing
|
||||
// eslint-disable-next-line react-hooks/rules-of-hooks
|
||||
|
||||
@@ -3,25 +3,32 @@ import { useRouter } from 'next/router';
|
||||
import React, { Suspense, useEffect } from 'react';
|
||||
|
||||
import { PageLoading } from '../components/pure/loading';
|
||||
import { useLastWorkspaceId } from '../hooks/affine/use-last-leave-workspace-id';
|
||||
import { useCreateFirstWorkspace } from '../hooks/use-create-first-workspace';
|
||||
import { useWorkspaces } from '../hooks/use-workspaces';
|
||||
|
||||
const IndexPageInner = () => {
|
||||
const router = useRouter();
|
||||
const workspaces = useWorkspaces();
|
||||
const lastWorkspaceId = useLastWorkspaceId();
|
||||
|
||||
useEffect(() => {
|
||||
if (!router.isReady) {
|
||||
return;
|
||||
}
|
||||
const firstWorkspace = workspaces.at(0);
|
||||
if (firstWorkspace) {
|
||||
const targetWorkspace =
|
||||
(lastWorkspaceId &&
|
||||
workspaces.find(({ id }) => id === lastWorkspaceId)) ||
|
||||
workspaces.at(0);
|
||||
|
||||
if (targetWorkspace) {
|
||||
const pageId =
|
||||
firstWorkspace.blockSuiteWorkspace.meta.pageMetas.at(0)?.id;
|
||||
targetWorkspace.blockSuiteWorkspace.meta.pageMetas.at(0)?.id;
|
||||
if (pageId) {
|
||||
router.replace({
|
||||
pathname: '/workspace/[workspaceId]/[pageId]',
|
||||
query: {
|
||||
workspaceId: firstWorkspace.id,
|
||||
workspaceId: targetWorkspace.id,
|
||||
pageId,
|
||||
},
|
||||
});
|
||||
@@ -32,29 +39,29 @@ const IndexPageInner = () => {
|
||||
router.replace({
|
||||
pathname: '/workspace/[workspaceId]/all',
|
||||
query: {
|
||||
workspaceId: firstWorkspace.id,
|
||||
workspaceId: targetWorkspace.id,
|
||||
},
|
||||
});
|
||||
}, 1000);
|
||||
const dispose = firstWorkspace.blockSuiteWorkspace.slots.pageAdded.once(
|
||||
pageId => {
|
||||
const dispose =
|
||||
targetWorkspace.blockSuiteWorkspace.slots.pageAdded.once(pageId => {
|
||||
clearTimeout(clearId);
|
||||
router.replace({
|
||||
pathname: '/workspace/[workspaceId]/[pageId]',
|
||||
query: {
|
||||
workspaceId: firstWorkspace.id,
|
||||
workspaceId: targetWorkspace.id,
|
||||
pageId,
|
||||
},
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
return () => {
|
||||
clearTimeout(clearId);
|
||||
dispose.dispose();
|
||||
};
|
||||
}
|
||||
}
|
||||
}, [router, workspaces]);
|
||||
}, [lastWorkspaceId, router, workspaces]);
|
||||
|
||||
return <PageLoading />;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user