mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-29 00:06:09 +08:00
chore: migrate page method to hooks
This commit is contained in:
@@ -28,15 +28,11 @@ import { useRouter } from 'next/router';
|
|||||||
import { useConfirm } from '@/providers/confirm-provider';
|
import { useConfirm } from '@/providers/confirm-provider';
|
||||||
import { SyncIcon } from './sync-icon';
|
import { SyncIcon } from './sync-icon';
|
||||||
import { toast } from '@/components/toast';
|
import { toast } from '@/components/toast';
|
||||||
|
import useCurrentPageMeta from '@/hooks/use-current-page-meta';
|
||||||
|
import { usePageHelper } from '@/hooks/use-page-helper';
|
||||||
const PopoverContent = () => {
|
const PopoverContent = () => {
|
||||||
const {
|
const { getPageMeta, editor, currentPage } = useAppState();
|
||||||
getPageMeta,
|
const { toggleFavoritePage, toggleDeletePage } = usePageHelper();
|
||||||
editor,
|
|
||||||
currentPage,
|
|
||||||
toggleFavoritePage,
|
|
||||||
toggleDeletePage,
|
|
||||||
} = useAppState();
|
|
||||||
const { mode, setMode } = useEditor();
|
const { mode, setMode } = useEditor();
|
||||||
const { confirm } = useConfirm();
|
const { confirm } = useConfirm();
|
||||||
|
|
||||||
@@ -192,7 +188,8 @@ const HeaderRight = () => {
|
|||||||
|
|
||||||
export const Header = ({ children }: PropsWithChildren<{}>) => {
|
export const Header = ({ children }: PropsWithChildren<{}>) => {
|
||||||
const [showWarning, setShowWarning] = useState(shouldShowWarning());
|
const [showWarning, setShowWarning] = useState(shouldShowWarning());
|
||||||
|
const currentPageMeta = useCurrentPageMeta();
|
||||||
|
console.log('currentPageMeta', currentPageMeta);
|
||||||
return (
|
return (
|
||||||
<StyledHeaderContainer hasWarning={showWarning}>
|
<StyledHeaderContainer hasWarning={showWarning}>
|
||||||
<BrowserWarning
|
<BrowserWarning
|
||||||
|
|||||||
@@ -22,13 +22,14 @@ import { Tooltip } from '@/ui/tooltip';
|
|||||||
import { useRouter } from 'next/router';
|
import { useRouter } from 'next/router';
|
||||||
import { useAppState } from '@/providers/app-state-provider/context';
|
import { useAppState } from '@/providers/app-state-provider/context';
|
||||||
import { toast } from '@/components/toast';
|
import { toast } from '@/components/toast';
|
||||||
|
import { usePageHelper } from '@/hooks/use-page-helper';
|
||||||
|
|
||||||
const FavoriteTag = ({
|
const FavoriteTag = ({
|
||||||
pageMeta: { favorite, id },
|
pageMeta: { favorite, id },
|
||||||
}: {
|
}: {
|
||||||
pageMeta: PageMeta;
|
pageMeta: PageMeta;
|
||||||
}) => {
|
}) => {
|
||||||
const { toggleFavoritePage } = useAppState();
|
const { toggleFavoritePage } = usePageHelper();
|
||||||
return (
|
return (
|
||||||
<Tooltip
|
<Tooltip
|
||||||
content={favorite ? 'Favourited' : 'Favourite'}
|
content={favorite ? 'Favourited' : 'Favourite'}
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import { useRouter } from 'next/router';
|
|
||||||
import { PageMeta, useEditor } from '@/providers/editor-provider';
|
import { PageMeta, useEditor } from '@/providers/editor-provider';
|
||||||
import { useConfirm } from '@/providers/confirm-provider';
|
import { useConfirm } from '@/providers/confirm-provider';
|
||||||
import { useAppState } from '@/providers/app-state-provider/context';
|
import { useAppState } from '@/providers/app-state-provider/context';
|
||||||
@@ -16,11 +15,12 @@ import {
|
|||||||
TrashIcon,
|
TrashIcon,
|
||||||
} from '@blocksuite/icons';
|
} from '@blocksuite/icons';
|
||||||
import { toast } from '@/components/toast';
|
import { toast } from '@/components/toast';
|
||||||
|
import { usePageHelper } from '@/hooks/use-page-helper';
|
||||||
|
|
||||||
export const OperationCell = ({ pageMeta }: { pageMeta: PageMeta }) => {
|
export const OperationCell = ({ pageMeta }: { pageMeta: PageMeta }) => {
|
||||||
const { id, favorite } = pageMeta;
|
const { id, favorite } = pageMeta;
|
||||||
const goToPage = useGoToPage();
|
const goToPage = useGoToPage();
|
||||||
const { toggleFavoritePage, toggleDeletePage } = useAppState();
|
const { toggleFavoritePage, toggleDeletePage } = usePageHelper();
|
||||||
const { confirm } = useConfirm();
|
const { confirm } = useConfirm();
|
||||||
|
|
||||||
const OperationMenu = (
|
const OperationMenu = (
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import { usePageList } from '@/providers/app-state-provider/usePageList';
|
|||||||
import { useLoadWorkspace } from '@/providers/app-state-provider/hooks';
|
import { useLoadWorkspace } from '@/providers/app-state-provider/hooks';
|
||||||
import NextImage from 'next/image';
|
import NextImage from 'next/image';
|
||||||
import noResultImage from './jungle-searching.png';
|
import noResultImage from './jungle-searching.png';
|
||||||
|
import { usePageHelper } from '@/hooks/use-page-helper';
|
||||||
export const Results = (props: {
|
export const Results = (props: {
|
||||||
query: string;
|
query: string;
|
||||||
loading: boolean;
|
loading: boolean;
|
||||||
@@ -26,7 +27,8 @@ export const Results = (props: {
|
|||||||
const pageList = usePageList(workspace);
|
const pageList = usePageList(workspace);
|
||||||
const goToPage = useGoToPage();
|
const goToPage = useGoToPage();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { search, currentWorkspaceId } = useAppState();
|
const { currentWorkspaceId } = useAppState();
|
||||||
|
const { search } = usePageHelper();
|
||||||
const List = config(currentWorkspaceId);
|
const List = config(currentWorkspaceId);
|
||||||
const [results, setResults] = useState(new Map<string, string | undefined>());
|
const [results, setResults] = useState(new Map<string, string | undefined>());
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import { useCallback } from 'react';
|
||||||
|
import { useAppState } from '@/providers/app-state-provider';
|
||||||
|
import { PageMeta } from '@blocksuite/store';
|
||||||
|
|
||||||
|
export type ChangePageMeta = (
|
||||||
|
pageId: string,
|
||||||
|
pageMeta: Partial<PageMeta>
|
||||||
|
) => void;
|
||||||
|
|
||||||
|
export const useChangePageMeta = () => {
|
||||||
|
const { currentWorkspace } = useAppState();
|
||||||
|
|
||||||
|
return useCallback<ChangePageMeta>(
|
||||||
|
(pageId, pageMeta) => {
|
||||||
|
currentWorkspace?.setPageMeta(pageId, {
|
||||||
|
...pageMeta,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
[currentWorkspace]
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default ChangePageMeta;
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
import { useCallback, useEffect, useState } from 'react';
|
||||||
|
import { useAppState } from '@/providers/app-state-provider';
|
||||||
|
import { PageMeta } from '@blocksuite/store';
|
||||||
|
|
||||||
|
export const useCurrentPageMeta = () => {
|
||||||
|
const { currentPage, currentWorkspace } = useAppState();
|
||||||
|
|
||||||
|
const pageMetaHandler = useCallback(() => {
|
||||||
|
if (!currentPage || !currentWorkspace) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
currentWorkspace?.meta.pageMetas.find(p => p.id === currentPage.pageId) ??
|
||||||
|
null
|
||||||
|
);
|
||||||
|
}, [currentPage, currentWorkspace]);
|
||||||
|
|
||||||
|
const [currentPageMeta, setCurrentPageMeta] = useState<PageMeta | null>(
|
||||||
|
pageMetaHandler
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setCurrentPageMeta(pageMetaHandler);
|
||||||
|
|
||||||
|
const dispose = currentWorkspace?.meta.pagesUpdated.on(res => {
|
||||||
|
setCurrentPageMeta(pageMetaHandler);
|
||||||
|
}).dispose;
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
dispose?.();
|
||||||
|
};
|
||||||
|
}, [currentPage, currentWorkspace, pageMetaHandler]);
|
||||||
|
|
||||||
|
return currentPageMeta;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default useCurrentPageMeta;
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
import { useState } from 'react';
|
||||||
|
import { EditorContainer } from '@blocksuite/editor';
|
||||||
|
import { useAppState } from '@/providers/app-state-provider';
|
||||||
|
|
||||||
|
export const usePageHandler = () => {
|
||||||
|
return null;
|
||||||
|
};
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
import { Page } from '@blocksuite/store';
|
||||||
|
import { QueryContent } from '@blocksuite/store/dist/workspace/search';
|
||||||
|
import { PageMeta } from '@/providers/editor-provider';
|
||||||
|
import { useAppState } from '@/providers/app-state-provider';
|
||||||
|
|
||||||
|
export type EditorHandlers = {
|
||||||
|
// createPage: (params?: { pageId?: string; title?: string }) => Promise<Page>;
|
||||||
|
// openPage: (
|
||||||
|
// pageId: string,
|
||||||
|
// query?: { [key: string]: string }
|
||||||
|
// ) => Promise<boolean>;
|
||||||
|
// getPageMeta: (pageId?: string) => PageMeta;
|
||||||
|
toggleDeletePage: (pageId: string) => void;
|
||||||
|
toggleFavoritePage: (pageId: string) => void;
|
||||||
|
// permanentlyDeletePage: (pageId: string) => void;
|
||||||
|
search: (query: QueryContent) => Map<string, string | undefined>;
|
||||||
|
// changeEditorMode: (pageId: string) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const usePageHelper = (): EditorHandlers => {
|
||||||
|
const { currentWorkspace } = useAppState();
|
||||||
|
|
||||||
|
return {
|
||||||
|
toggleFavoritePage: (pageId: string) => {
|
||||||
|
if (!currentWorkspace) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const pageMeta = currentWorkspace.meta.pageMetas.find(
|
||||||
|
p => p.id === pageId
|
||||||
|
);
|
||||||
|
if (pageMeta) {
|
||||||
|
currentWorkspace.setPageMeta(pageId, {
|
||||||
|
favorite: !pageMeta.favorite,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
toggleDeletePage: (pageId: string) => {
|
||||||
|
const pageMeta = currentWorkspace?.meta.pageMetas.find(
|
||||||
|
p => p.id === pageId
|
||||||
|
);
|
||||||
|
if (pageMeta) {
|
||||||
|
currentWorkspace!.setPageMeta(pageId, {
|
||||||
|
trash: !pageMeta.trash,
|
||||||
|
trashDate: +new Date(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
search: (query: QueryContent) => {
|
||||||
|
return currentWorkspace!.search(query);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
import { useEffect, useState } from 'react';
|
||||||
|
import { PageMeta } from '@/providers/app-state-provider/usePageList';
|
||||||
|
import { useAppState } from '@/providers/app-state-provider';
|
||||||
|
|
||||||
|
export const usePageMetaList = () => {
|
||||||
|
const { currentWorkspace } = useAppState();
|
||||||
|
const [pageList, setPageList] = useState<PageMeta[]>([]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!currentWorkspace) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setPageList(currentWorkspace.meta.pageMetas as PageMeta[]);
|
||||||
|
const dispose = currentWorkspace.meta.pagesUpdated.on(res => {
|
||||||
|
setPageList(currentWorkspace.meta.pageMetas as PageMeta[]);
|
||||||
|
}).dispose;
|
||||||
|
return () => {
|
||||||
|
dispose();
|
||||||
|
};
|
||||||
|
}, [currentWorkspace]);
|
||||||
|
|
||||||
|
return pageList;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default usePageMetaList;
|
||||||
@@ -43,9 +43,6 @@ export interface AppStateContext extends AppStateValue {
|
|||||||
((pageId?: string) => Promise<string | null>) | undefined
|
((pageId?: string) => Promise<string | null>) | undefined
|
||||||
>;
|
>;
|
||||||
getPageMeta: (pageId: string) => PageMeta | null;
|
getPageMeta: (pageId: string) => PageMeta | null;
|
||||||
toggleFavoritePage: (pageId: string) => void;
|
|
||||||
toggleDeletePage: (pageId: string) => void;
|
|
||||||
search: (query: QueryContent) => Map<string, string | undefined>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const AppState = createContext<AppStateContext>({
|
export const AppState = createContext<AppStateContext>({
|
||||||
@@ -66,9 +63,6 @@ export const AppState = createContext<AppStateContext>({
|
|||||||
loadPage: undefined,
|
loadPage: undefined,
|
||||||
createPage: undefined,
|
createPage: undefined,
|
||||||
getPageMeta: () => null,
|
getPageMeta: () => null,
|
||||||
toggleFavoritePage: () => {},
|
|
||||||
toggleDeletePage: () => {},
|
|
||||||
search: () => new Map<string, string>(),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export const useAppState = () => {
|
export const useAppState = () => {
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
export * from './context';
|
||||||
@@ -9,11 +9,9 @@ import {
|
|||||||
import { AppState } from './context';
|
import { AppState } from './context';
|
||||||
import type {
|
import type {
|
||||||
AppStateValue,
|
AppStateValue,
|
||||||
AppStateContext,
|
|
||||||
CreateEditorHandler,
|
CreateEditorHandler,
|
||||||
LoadWorkspaceHandler,
|
LoadWorkspaceHandler,
|
||||||
} from './context';
|
} from './context';
|
||||||
import { QueryContent } from '@blocksuite/store/dist/workspace/search';
|
|
||||||
import type { Page, Workspace } from '@blocksuite/store';
|
import type { Page, Workspace } from '@blocksuite/store';
|
||||||
import { EditorContainer } from '@blocksuite/editor';
|
import { EditorContainer } from '@blocksuite/editor';
|
||||||
|
|
||||||
@@ -25,12 +23,9 @@ export const AppStateProvider = ({ children }: { children?: ReactNode }) => {
|
|||||||
const [state, setState] = useState<AppStateValue>({
|
const [state, setState] = useState<AppStateValue>({
|
||||||
user: null,
|
user: null,
|
||||||
workspacesMeta: [],
|
workspacesMeta: [],
|
||||||
|
|
||||||
currentWorkspaceId: '',
|
currentWorkspaceId: '',
|
||||||
currentWorkspace: null,
|
currentWorkspace: null,
|
||||||
|
|
||||||
currentPage: null,
|
currentPage: null,
|
||||||
|
|
||||||
editor: null,
|
editor: null,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -45,6 +40,7 @@ export const AppStateProvider = ({ children }: { children?: ReactNode }) => {
|
|||||||
|
|
||||||
const [createEditorHandler, _setCreateEditorHandler] =
|
const [createEditorHandler, _setCreateEditorHandler] =
|
||||||
useState<CreateEditorHandler>();
|
useState<CreateEditorHandler>();
|
||||||
|
|
||||||
const setCreateEditorHandler = useCallback(
|
const setCreateEditorHandler = useCallback(
|
||||||
(handler: CreateEditorHandler) => {
|
(handler: CreateEditorHandler) => {
|
||||||
_setCreateEditorHandler(() => handler);
|
_setCreateEditorHandler(() => handler);
|
||||||
@@ -151,36 +147,6 @@ export const AppStateProvider = ({ children }: { children?: ReactNode }) => {
|
|||||||
null
|
null
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
toggleFavoritePage: (pageId: string) => {
|
|
||||||
const { currentWorkspace } = state;
|
|
||||||
if (!currentWorkspace) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const pageMeta = currentWorkspace.meta.pageMetas.find(
|
|
||||||
p => p.id === pageId
|
|
||||||
);
|
|
||||||
if (pageMeta) {
|
|
||||||
currentWorkspace.setPageMeta(pageId, {
|
|
||||||
favorite: !pageMeta.favorite,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
},
|
|
||||||
toggleDeletePage: (pageId: string) => {
|
|
||||||
const { currentWorkspace } = state;
|
|
||||||
const pageMeta = currentWorkspace?.meta.pageMetas.find(
|
|
||||||
p => p.id === pageId
|
|
||||||
);
|
|
||||||
if (pageMeta) {
|
|
||||||
currentWorkspace!.setPageMeta(pageId, {
|
|
||||||
trash: !pageMeta.trash,
|
|
||||||
trashDate: +new Date(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
},
|
|
||||||
search: (query: QueryContent) => {
|
|
||||||
const { currentWorkspace } = state;
|
|
||||||
return currentWorkspace!.search(query);
|
|
||||||
},
|
|
||||||
}),
|
}),
|
||||||
[state, setState, loadPage, loadWorkspace]
|
[state, setState, loadPage, loadWorkspace]
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user