diff --git a/packages/frontend/core/src/components/explorer/context.ts b/packages/frontend/core/src/components/explorer/context.ts index 1555a64b3..c3ccede0d 100644 --- a/packages/frontend/core/src/components/explorer/context.ts +++ b/packages/frontend/core/src/components/explorer/context.ts @@ -1,31 +1,50 @@ -import { createContext, type Dispatch, type SetStateAction } from 'react'; +import { LiveData } from '@toeverything/infra'; +import { createContext } from 'react'; import type { DocListItemView } from './docs-view/doc-list-item'; import type { ExplorerPreference } from './types'; -export type DocExplorerContextType = ExplorerPreference & { - view: DocListItemView; - setView: Dispatch>; - groups: Array<{ key: string; items: string[] }>; - collapsed: string[]; - selectMode?: boolean; - selectedDocIds: string[]; - prevCheckAnchorId?: string | null; - onToggleCollapse: (groupId: string) => void; - onToggleSelect: (docId: string) => void; - onSelect: Dispatch>; - setPrevCheckAnchorId: Dispatch>; +export type DocExplorerContextType = { + view$: LiveData; + groups$: LiveData>; + collapsedGroups$: LiveData; + selectMode$?: LiveData; + selectedDocIds$: LiveData; + prevCheckAnchorId$?: LiveData; +} & { + [K in keyof ExplorerPreference as `${K}$`]: LiveData; }; -export const DocExplorerContext = createContext({ - view: 'list', - setView: () => {}, - groups: [], - collapsed: [], - selectedDocIds: [], - prevCheckAnchorId: null, - onToggleSelect: () => {}, - onToggleCollapse: () => {}, - onSelect: () => {}, - setPrevCheckAnchorId: () => {}, -}); +export const DocExplorerContext = createContext( + {} as any +); + +export const createDocExplorerContext = () => + ({ + view$: new LiveData('list'), + groups$: new LiveData>([]), + collapsedGroups$: new LiveData([]), + selectMode$: new LiveData(false), + selectedDocIds$: new LiveData([]), + prevCheckAnchorId$: new LiveData(null), + filters$: new LiveData([ + { + type: 'system', + key: 'trash', + value: 'false', + method: 'is', + }, + ]), + groupBy$: new LiveData(undefined), + orderBy$: new LiveData(undefined), + displayProperties$: new LiveData( + [] + ), + showDocIcon$: new LiveData(true), + showDocPreview$: new LiveData(true), + quickFavorite$: new LiveData(false), + quickSelect$: new LiveData(false), + quickSplit$: new LiveData(false), + quickTrash$: new LiveData(false), + quickTab$: new LiveData(false), + }) satisfies DocExplorerContextType; diff --git a/packages/frontend/core/src/components/explorer/display-menu/index.tsx b/packages/frontend/core/src/components/explorer/display-menu/index.tsx index ab97f36d0..fc2b0f1d4 100644 --- a/packages/frontend/core/src/components/explorer/display-menu/index.tsx +++ b/packages/frontend/core/src/components/explorer/display-menu/index.tsx @@ -11,85 +11,63 @@ import type { } from '@affine/core/modules/collection-rules/types'; import { useI18n } from '@affine/i18n'; import { ArrowDownSmallIcon } from '@blocksuite/icons/rc'; +import { useLiveData } from '@toeverything/infra'; import type React from 'react'; -import { useCallback } from 'react'; +import { useCallback, useContext } from 'react'; -import type { ExplorerPreference } from '../types'; +import { DocExplorerContext } from '../context'; import { GroupByList, GroupByName } from './group'; import { OrderByList, OrderByName } from './order'; import { DisplayProperties } from './properties'; import { QuickActionsConfig } from './quick-actions'; import * as styles from './styles.css'; -const ExplorerDisplayMenu = ({ - preference, - onChange, -}: { - preference: ExplorerPreference; - onChange?: (preference: ExplorerPreference) => void; -}) => { +const ExplorerDisplayMenu = () => { const t = useI18n(); + const explorerContextValue = useContext(DocExplorerContext); + const groupBy = useLiveData(explorerContextValue.groupBy$); + const orderBy = useLiveData(explorerContextValue.orderBy$); const handleGroupByChange = useCallback( (groupBy: GroupByParams) => { - onChange?.({ - ...preference, - groupBy, - }); + explorerContextValue.groupBy$?.next(groupBy); }, - [onChange, preference] + [explorerContextValue.groupBy$] ); const handleOrderByChange = useCallback( (orderBy: OrderByParams) => { - onChange?.({ - ...preference, - orderBy, - }); + explorerContextValue.orderBy$?.next(orderBy); }, - [onChange, preference] + [explorerContextValue.orderBy$] ); return (
- } + items={} >
{t['com.affine.explorer.display-menu.grouping']()} - {preference.groupBy ? ( - - ) : null} + {groupBy ? : null}
- } + items={} >
{t['com.affine.explorer.display-menu.ordering']()} - {preference.orderBy ? ( - - ) : null} + {orderBy ? : null}
- + - +
); }; @@ -97,24 +75,15 @@ const ExplorerDisplayMenu = ({ export const ExplorerDisplayMenuButton = ({ style, className, - preference, menuProps, - onChange, }: { style?: React.CSSProperties; className?: string; - preference: ExplorerPreference; - onChange?: (preference: ExplorerPreference) => void; menuProps?: Omit; }) => { const t = useI18n(); return ( - - } - {...menuProps} - > + } {...menuProps}>