fix: hook useRecentlyViewed (#1430)
Co-authored-by: JimmFly <yangjinfei001@gmail.com>
This commit is contained in:
@@ -60,7 +60,7 @@ export const workspacesAtom = atom<Promise<RemWorkspace[]>>(async get => {
|
|||||||
return workspaces.filter(workspace => workspace !== null) as RemWorkspace[];
|
return workspaces.filter(workspace => workspace !== null) as RemWorkspace[];
|
||||||
});
|
});
|
||||||
|
|
||||||
type View = { title: string; id: string; mode: 'page' | 'edgeless' };
|
type View = { id: string; mode: 'page' | 'edgeless' };
|
||||||
|
|
||||||
export type WorkspaceRecentViews = Record<string, View[]>;
|
export type WorkspaceRecentViews = Record<string, View[]>;
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { UNTITLED_WORKSPACE_NAME } from '@affine/env';
|
||||||
import { useTranslation } from '@affine/i18n';
|
import { useTranslation } from '@affine/i18n';
|
||||||
import { EdgelessIcon, PaperIcon } from '@blocksuite/icons';
|
import { EdgelessIcon, PaperIcon } from '@blocksuite/icons';
|
||||||
import { assertExists } from '@blocksuite/store';
|
import { assertExists } from '@blocksuite/store';
|
||||||
@@ -94,18 +95,24 @@ export const Results: React.FC<ResultsProps> = ({
|
|||||||
{recentlyViewed.length > 0 && (
|
{recentlyViewed.length > 0 && (
|
||||||
<Command.Group heading={t('Recent')}>
|
<Command.Group heading={t('Recent')}>
|
||||||
{recentlyViewed
|
{recentlyViewed
|
||||||
.filter(
|
.filter(recent => {
|
||||||
recent =>
|
const page = pageList.find(page => recent.id === page.id);
|
||||||
pageList.find(page => recent.id === page.id)?.trash !== true
|
if (!page) {
|
||||||
)
|
return false;
|
||||||
|
} else {
|
||||||
|
return page.trash !== true;
|
||||||
|
}
|
||||||
|
})
|
||||||
.map(recent => {
|
.map(recent => {
|
||||||
|
const page = pageList.find(page => recent.id === page.id);
|
||||||
|
assertExists(page);
|
||||||
return (
|
return (
|
||||||
<Command.Item
|
<Command.Item
|
||||||
key={recent.id}
|
key={page.id}
|
||||||
value={recent.id}
|
value={page.id}
|
||||||
onSelect={() => {
|
onSelect={() => {
|
||||||
onClose();
|
onClose();
|
||||||
jumpToPage(blockSuiteWorkspace.id, recent.id);
|
jumpToPage(blockSuiteWorkspace.id, page.id);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<StyledListItem>
|
<StyledListItem>
|
||||||
@@ -114,7 +121,7 @@ export const Results: React.FC<ResultsProps> = ({
|
|||||||
) : (
|
) : (
|
||||||
<PaperIcon />
|
<PaperIcon />
|
||||||
)}
|
)}
|
||||||
<span>{recent.title}</span>
|
<span>{page.title || UNTITLED_WORKSPACE_NAME}</span>
|
||||||
</StyledListItem>
|
</StyledListItem>
|
||||||
</Command.Item>
|
</Command.Item>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -310,7 +310,6 @@ describe('useRecentlyViewed', () => {
|
|||||||
{
|
{
|
||||||
id: 'page0',
|
id: 'page0',
|
||||||
mode: 'page',
|
mode: 'page',
|
||||||
title: 'Untitled',
|
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ export function useSyncRecentViewsWithRouter(router: NextRouter) {
|
|||||||
const [workspace] = useCurrentWorkspace();
|
const [workspace] = useCurrentWorkspace();
|
||||||
const workspaceId = workspace?.id || null;
|
const workspaceId = workspace?.id || null;
|
||||||
const blockSuiteWorkspace = workspace?.blockSuiteWorkspace || null;
|
const blockSuiteWorkspace = workspace?.blockSuiteWorkspace || null;
|
||||||
const pageId = router.query.pageId as string;
|
const pageId = router.query.pageId;
|
||||||
const set = useSetAtom(workspaceRecentViresWriteAtom);
|
const set = useSetAtom(workspaceRecentViresWriteAtom);
|
||||||
const meta = usePageMeta(blockSuiteWorkspace).find(
|
const meta = usePageMeta(blockSuiteWorkspace).find(
|
||||||
meta => meta.id === pageId
|
meta => meta.id === pageId
|
||||||
@@ -31,7 +31,6 @@ export function useSyncRecentViewsWithRouter(router: NextRouter) {
|
|||||||
if (!workspaceId) return;
|
if (!workspaceId) return;
|
||||||
if (pageId && meta) {
|
if (pageId && meta) {
|
||||||
set(workspaceId, {
|
set(workspaceId, {
|
||||||
title: meta.title || 'Untitled',
|
|
||||||
id: pageId as string,
|
id: pageId as string,
|
||||||
mode: meta.mode || 'page',
|
mode: meta.mode || 'page',
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user