feat(view): support save current view

This commit is contained in:
lawvs 2022-07-26 16:17:49 +08:00
parent aa320a174b
commit 98688592ab
3 changed files with 32 additions and 7 deletions

View File

@ -7,3 +7,4 @@ export const TABLE_VALUES_KEY = 'recastValues' as const;
*/ */
export const KANBAN_PROPERTIES_KEY = 'kanbanProps' as const; export const KANBAN_PROPERTIES_KEY = 'kanbanProps' as const;
export const META_VIEWS_KEY = 'recastViews' as const; export const META_VIEWS_KEY = 'recastViews' as const;
export const META_CURRENT_VIEW_ID_KEY = 'recastCurrentViewId' as const;

View File

@ -1,10 +1,11 @@
import { CSSProperties, ReactNode } from 'react'; import { CSSProperties } from 'react';
import { import {
KANBAN_PROPERTIES_KEY, KANBAN_PROPERTIES_KEY,
META_CURRENT_VIEW_ID_KEY,
META_PROPERTIES_KEY, META_PROPERTIES_KEY,
META_VIEWS_KEY, META_VIEWS_KEY,
} from './constant'; } from './constant';
import { RecastScene, RecastView } from './view'; import { RecastScene, RecastView, RecastViewId } from './view';
// --------------------------------------------------- // ---------------------------------------------------
// Property // Property
@ -117,5 +118,6 @@ export type RecastDataProperties = Partial<{
scene: RecastScene; scene: RecastScene;
[META_PROPERTIES_KEY]: RecastMetaProperty[]; [META_PROPERTIES_KEY]: RecastMetaProperty[];
[META_VIEWS_KEY]: RecastView[]; [META_VIEWS_KEY]: RecastView[];
[META_CURRENT_VIEW_ID_KEY]: RecastViewId;
[KANBAN_PROPERTIES_KEY]: RecastKanbanProperty; [KANBAN_PROPERTIES_KEY]: RecastKanbanProperty;
}>; }>;

View File

@ -1,8 +1,11 @@
import { nanoid } from 'nanoid'; import { nanoid } from 'nanoid';
import { useCallback, useState } from 'react'; import { useCallback } from 'react';
import { useRecastBlock } from './Context'; import { useRecastBlock } from './Context';
import { import {
KanbanView,
META_CURRENT_VIEW_ID_KEY,
META_VIEWS_KEY, META_VIEWS_KEY,
RecastPropertyId,
RecastScene, RecastScene,
RecastView, RecastView,
RecastViewId, RecastViewId,
@ -27,13 +30,32 @@ const DEFAULT_VIEWS: RecastView[] = [
}, },
]; ];
export const useRecastView = () => { /**
* Get the current view of the group
*/
export const useCurrentView = () => {
const recastBlock = useRecastBlock(); const recastBlock = useRecastBlock();
const recastViews = const recastViews =
recastBlock.getProperty(META_VIEWS_KEY) ?? DEFAULT_VIEWS; recastBlock.getProperty(META_VIEWS_KEY) ?? DEFAULT_VIEWS;
// TODO save cur view const currentViewId = recastBlock.getProperty(META_CURRENT_VIEW_ID_KEY);
const [currentView, changeView] = useState(recastViews[0]); const currentView =
recastViews.find(v => v.id === currentViewId) ?? recastViews[0];
const setCurrentView = useCallback(
async (newView: RecastView) => {
await recastBlock.setProperty(META_CURRENT_VIEW_ID_KEY, newView.id);
},
[recastBlock]
);
return [currentView, setCurrentView] as const;
};
export const useRecastView = () => {
const recastBlock = useRecastBlock();
const recastViews =
recastBlock.getProperty(META_VIEWS_KEY) ?? DEFAULT_VIEWS;
const [currentView, setCurrentView] = useCurrentView();
const getView = useCallback( const getView = useCallback(
(id: RecastViewId) => { (id: RecastViewId) => {
@ -118,7 +140,7 @@ export const useRecastView = () => {
return { return {
currentView, currentView,
recastViews, recastViews,
changeView, setCurrentView,
addView, addView,
updateView, updateView,
renameView, renameView,