refactor(view): migrate group by

This commit is contained in:
lawvs
2022-07-26 16:25:07 +08:00
parent 98688592ab
commit 2846e464d9
2 changed files with 35 additions and 13 deletions

View File

@@ -2,17 +2,18 @@ import { Protocol } from '@toeverything/datasource/db-service';
import { useCallback, useContext, useEffect, useState } from 'react'; import { useCallback, useContext, useEffect, useState } from 'react';
import { useEditor } from '../contexts'; import { useEditor } from '../contexts';
import { AsyncBlock } from '../editor'; import { AsyncBlock } from '../editor';
import { useRecastView } from '../recast-block';
import { useRecastBlock } from '../recast-block/Context'; import { useRecastBlock } from '../recast-block/Context';
import { import {
useRecastBlockMeta, useRecastBlockMeta,
useSelectProperty, useSelectProperty,
} from '../recast-block/property'; } from '../recast-block/property';
import type { RecastItem } from '../recast-block/types';
import { import {
KANBAN_PROPERTIES_KEY,
PropertyType, PropertyType,
RecastItem,
RecastMetaProperty, RecastMetaProperty,
RecastPropertyId, RecastPropertyId,
RecastScene,
} from '../recast-block/types'; } from '../recast-block/types';
import { supportChildren } from '../utils'; import { supportChildren } from '../utils';
import { import {
@@ -39,9 +40,9 @@ import { KanbanCard, KanbanGroup } from './types';
* @public * @public
*/ */
export const useRecastKanbanGroupBy = () => { export const useRecastKanbanGroupBy = () => {
const recastBlock = useRecastBlock();
const { getProperty, getProperties } = useRecastBlockMeta(); const { getProperty, getProperties } = useRecastBlockMeta();
const kanbanProperties = recastBlock.getProperty(KANBAN_PROPERTIES_KEY); const { currentView, withKanbanView } = useRecastView();
const isKanbanView = currentView.type === RecastScene.Kanban;
// TODO: remove filter // TODO: remove filter
// Add other type groupBy support // Add other type groupBy support
@@ -53,18 +54,19 @@ export const useRecastKanbanGroupBy = () => {
const setGroupBy = useCallback( const setGroupBy = useCallback(
async (id: RecastPropertyId) => { async (id: RecastPropertyId) => {
const ok = await recastBlock.setProperty(KANBAN_PROPERTIES_KEY, { if (!isKanbanView) {
...kanbanProperties, console.error('current view:', currentView);
groupBy: id, throw new Error(
}); 'Failed to set groupBy! Current view is not kanban view!'
if (!ok) { );
throw new Error('Failed to set groupBy');
} }
const { setGroupBy } = withKanbanView(currentView);
await setGroupBy(id);
}, },
[recastBlock, kanbanProperties] [currentView, isKanbanView, withKanbanView]
); );
const groupById = kanbanProperties?.groupBy; const groupById = isKanbanView ? currentView.groupBy : null;
// 1. groupBy is not set // 1. groupBy is not set
if (!groupById) { if (!groupById) {
return { return {

View File

@@ -20,7 +20,7 @@ const genViewId = () => nanoid(16) as RecastViewId; // This is a safe type cast
const DEFAULT_VIEWS: RecastView[] = [ const DEFAULT_VIEWS: RecastView[] = [
{ {
id: genViewId(), id: genViewId(),
name: 'List', name: 'Text View',
type: RecastScene.Page, type: RecastScene.Page,
}, },
{ {
@@ -137,6 +137,23 @@ export const useRecastView = () => {
[getView, recastViews, setViews] [getView, recastViews, setViews]
); );
/**
* Get kanban ability
*/
const withKanbanView = useCallback(
(view: KanbanView) => {
const groupBy = view.groupBy ?? null;
const setGroupBy = (groupBy: RecastPropertyId) => {
updateView({ ...view, groupBy });
};
return {
groupBy,
setGroupBy,
};
},
[updateView]
);
return { return {
currentView, currentView,
recastViews, recastViews,
@@ -145,5 +162,8 @@ export const useRecastView = () => {
updateView, updateView,
renameView, renameView,
removeView, removeView,
withKanbanView,
// TODO reorder API
}; };
}; };