Skip to content

Commit

Permalink
feat(core): save user habits in right sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Mar 22, 2024
1 parent 85ee223 commit eba55f6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
import { LiveData } from '@toeverything/infra/livedata';
import type { GlobalState } from '@toeverything/infra/storage';

import type { RightSidebarView } from './right-sidebar-view';

const RIGHT_SIDEBAR_KEY = 'app:settings:rightsidebar';

export class RightSidebar {
readonly isOpen = new LiveData(false);
constructor(private readonly globalState: GlobalState) {}
readonly isOpen = LiveData.from(
this.globalState.watch<boolean>(RIGHT_SIDEBAR_KEY),
false
).map(Boolean);
readonly views = new LiveData<RightSidebarView[]>([]);
readonly front = this.views.map(
stack => stack[0] as RightSidebarView | undefined
);
readonly hasViews = this.views.map(stack => stack.length > 0);

open() {
this.isOpen.next(true);
this.globalState.set(RIGHT_SIDEBAR_KEY, true);
}

toggle() {
this.isOpen.next(!this.isOpen.value);
const value = !this.isOpen.value;
this.globalState.set(RIGHT_SIDEBAR_KEY, value);
}

close() {
this.isOpen.next(false);
this.globalState.set(RIGHT_SIDEBAR_KEY, false);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/core/src/modules/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function configureBusinessServices(services: ServiceCollection) {
.scope(WorkspaceScope)
.add(Workbench)
.add(Navigator, [Workbench])
.add(RightSidebar)
.add(RightSidebar, [GlobalState])
.add(WorkspacePropertiesAdapter, [Workspace])
.add(CollectionService, [Workspace])
.add(WorkspaceLegacyProperties, [Workspace])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import type { Doc as BlockSuiteDoc } from '@blocksuite/store';
import {
Doc,
globalBlockSuiteSchema,
GlobalState,
LiveData,
PageManager,
PageRecordList,
ServiceProviderContext,
Expand Down Expand Up @@ -65,7 +67,23 @@ import { PageNotFound } from '../../404';
import * as styles from './detail-page.css';
import { DetailPageHeader } from './detail-page-header';

const RIGHT_SIDEBAR_TABS_ACTIVE_KEY = 'app:settings:rightsidebar:tabs:active';

const DetailPageImpl = memo(function DetailPageImpl() {
const globalState = useService(GlobalState);
const activeTabName = useLiveData(
LiveData.from(
globalState.watch<SidebarTabName>(RIGHT_SIDEBAR_TABS_ACTIVE_KEY),
'journal'
)
);
const setActiveTabName = useCallback(
(name: string) => {
globalState.set(RIGHT_SIDEBAR_TABS_ACTIVE_KEY, name);
},
[globalState]
);

const page = useService(Doc);
const pageRecordList = useService(PageRecordList);
const currentPageId = page.id;
Expand All @@ -84,10 +102,6 @@ const DetailPageImpl = memo(function DetailPageImpl() {
}
}, [editor, isActiveView, setActiveBlockSuiteEditor]);

const [activeTabName, setActiveTabName] = useState<SidebarTabName | null>(
null
);

const pageMeta = useBlockSuiteDocMeta(docCollection).find(
meta => meta.id === page.id
);
Expand Down

0 comments on commit eba55f6

Please sign in to comment.