mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-27 02:42:25 +08: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 { useCallback } from 'react';
|
||||||
|
|
||||||
import {
|
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(): [
|
export function useCurrentWorkspace(): [
|
||||||
RemWorkspace | null,
|
RemWorkspace | null,
|
||||||
(id: string | null) => void
|
(id: string | null) => void
|
||||||
@@ -23,14 +29,16 @@ export function useCurrentWorkspace(): [
|
|||||||
const currentWorkspace = useAtomValue(currentWorkspaceAtom);
|
const currentWorkspace = useAtomValue(currentWorkspaceAtom);
|
||||||
const [, setId] = useAtom(currentWorkspaceIdAtom);
|
const [, setId] = useAtom(currentWorkspaceIdAtom);
|
||||||
const [, setPageId] = useAtom(currentPageIdAtom);
|
const [, setPageId] = useAtom(currentPageIdAtom);
|
||||||
|
const setLast = useSetAtom(lastWorkspaceIdAtom);
|
||||||
return [
|
return [
|
||||||
currentWorkspace,
|
currentWorkspace,
|
||||||
useCallback(
|
useCallback(
|
||||||
(id: string | null) => {
|
(id: string | null) => {
|
||||||
setPageId(null);
|
setPageId(null);
|
||||||
|
setLast(id);
|
||||||
setId(id);
|
setId(id);
|
||||||
},
|
},
|
||||||
[setId, setPageId]
|
[setId, setLast, setPageId]
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,7 +45,6 @@ const App = function App({
|
|||||||
}) {
|
}) {
|
||||||
const getLayout = Component.getLayout || EmptyLayout;
|
const getLayout = Component.getLayout || EmptyLayout;
|
||||||
const i18n = useMemo(() => createI18n(), []);
|
const i18n = useMemo(() => createI18n(), []);
|
||||||
|
|
||||||
if (process.env.NODE_ENV === 'development') {
|
if (process.env.NODE_ENV === 'development') {
|
||||||
// I know what I'm doing
|
// I know what I'm doing
|
||||||
// eslint-disable-next-line react-hooks/rules-of-hooks
|
// 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 React, { Suspense, useEffect } from 'react';
|
||||||
|
|
||||||
import { PageLoading } from '../components/pure/loading';
|
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 { useCreateFirstWorkspace } from '../hooks/use-create-first-workspace';
|
||||||
import { useWorkspaces } from '../hooks/use-workspaces';
|
import { useWorkspaces } from '../hooks/use-workspaces';
|
||||||
|
|
||||||
const IndexPageInner = () => {
|
const IndexPageInner = () => {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const workspaces = useWorkspaces();
|
const workspaces = useWorkspaces();
|
||||||
|
const lastWorkspaceId = useLastWorkspaceId();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!router.isReady) {
|
if (!router.isReady) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const firstWorkspace = workspaces.at(0);
|
const targetWorkspace =
|
||||||
if (firstWorkspace) {
|
(lastWorkspaceId &&
|
||||||
|
workspaces.find(({ id }) => id === lastWorkspaceId)) ||
|
||||||
|
workspaces.at(0);
|
||||||
|
|
||||||
|
if (targetWorkspace) {
|
||||||
const pageId =
|
const pageId =
|
||||||
firstWorkspace.blockSuiteWorkspace.meta.pageMetas.at(0)?.id;
|
targetWorkspace.blockSuiteWorkspace.meta.pageMetas.at(0)?.id;
|
||||||
if (pageId) {
|
if (pageId) {
|
||||||
router.replace({
|
router.replace({
|
||||||
pathname: '/workspace/[workspaceId]/[pageId]',
|
pathname: '/workspace/[workspaceId]/[pageId]',
|
||||||
query: {
|
query: {
|
||||||
workspaceId: firstWorkspace.id,
|
workspaceId: targetWorkspace.id,
|
||||||
pageId,
|
pageId,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@@ -32,29 +39,29 @@ const IndexPageInner = () => {
|
|||||||
router.replace({
|
router.replace({
|
||||||
pathname: '/workspace/[workspaceId]/all',
|
pathname: '/workspace/[workspaceId]/all',
|
||||||
query: {
|
query: {
|
||||||
workspaceId: firstWorkspace.id,
|
workspaceId: targetWorkspace.id,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}, 1000);
|
}, 1000);
|
||||||
const dispose = firstWorkspace.blockSuiteWorkspace.slots.pageAdded.once(
|
const dispose =
|
||||||
pageId => {
|
targetWorkspace.blockSuiteWorkspace.slots.pageAdded.once(pageId => {
|
||||||
clearTimeout(clearId);
|
clearTimeout(clearId);
|
||||||
router.replace({
|
router.replace({
|
||||||
pathname: '/workspace/[workspaceId]/[pageId]',
|
pathname: '/workspace/[workspaceId]/[pageId]',
|
||||||
query: {
|
query: {
|
||||||
workspaceId: firstWorkspace.id,
|
workspaceId: targetWorkspace.id,
|
||||||
pageId,
|
pageId,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
);
|
|
||||||
return () => {
|
return () => {
|
||||||
clearTimeout(clearId);
|
clearTimeout(clearId);
|
||||||
dispose.dispose();
|
dispose.dispose();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [router, workspaces]);
|
}, [lastWorkspaceId, router, workspaces]);
|
||||||
|
|
||||||
return <PageLoading />;
|
return <PageLoading />;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
30
tests/open-affine.spec.ts
Normal file
30
tests/open-affine.spec.ts
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import { expect } from '@playwright/test';
|
||||||
|
|
||||||
|
import { loadPage } from './libs/load-page';
|
||||||
|
import { test } from './libs/playwright';
|
||||||
|
import { createWorkspace } from './libs/workspace-logic';
|
||||||
|
|
||||||
|
loadPage();
|
||||||
|
|
||||||
|
test.describe('Open AFFiNE', () => {
|
||||||
|
test('Open last workspace when back to affine', async ({ page }) => {
|
||||||
|
await createWorkspace({ name: 'New Workspace 2' }, page);
|
||||||
|
// FIXME: can not get when the new workspace is surely created, hack a timeout to wait
|
||||||
|
// waiting for page loading end
|
||||||
|
await page.waitForTimeout(3000);
|
||||||
|
// show workspace list
|
||||||
|
await page.getByTestId('workspace-name').click();
|
||||||
|
|
||||||
|
//check workspace list length
|
||||||
|
const workspaceCards = await page.$$('data-testid=workspace-card');
|
||||||
|
expect(workspaceCards.length).toBe(2);
|
||||||
|
await workspaceCards[1].click();
|
||||||
|
await page.goto('http://localhost:8080');
|
||||||
|
|
||||||
|
const workspaceNameDom = await page.getByTestId('workspace-name');
|
||||||
|
const currentWorkspaceName = await workspaceNameDom.evaluate(
|
||||||
|
node => node.textContent
|
||||||
|
);
|
||||||
|
expect(currentWorkspaceName).toEqual('New Workspace 2');
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user