mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
chore: observer app change (#1922)
This commit is contained in:
parent
6aa9ba28d6
commit
8e22ef2230
@ -2,10 +2,11 @@ import { foldersActions, IFolder } from '../../../stores/reducers/folders/slice'
|
|||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { useAppDispatch, useAppSelector } from '../../../stores/store';
|
import { useAppDispatch, useAppSelector } from '../../../stores/store';
|
||||||
import { IPage, pagesActions } from '../../../stores/reducers/pages/slice';
|
import { IPage, pagesActions } from '../../../stores/reducers/pages/slice';
|
||||||
import { ViewLayoutTypePB } from '../../../../services/backend';
|
import { AppPB, ViewLayoutTypePB } from '../../../../services/backend';
|
||||||
import { AppBackendService } from '../../../stores/effects/folder/app/app_bd_svc';
|
import { AppBackendService } from '../../../stores/effects/folder/app/app_bd_svc';
|
||||||
import { WorkspaceBackendService } from '../../../stores/effects/folder/workspace/workspace_bd_svc';
|
import { WorkspaceBackendService } from '../../../stores/effects/folder/workspace/workspace_bd_svc';
|
||||||
import { useError } from '../../error/Error.hooks';
|
import { useError } from '../../error/Error.hooks';
|
||||||
|
import { AppObserver } from '../../../stores/effects/folder/app/app_observer';
|
||||||
|
|
||||||
const initialFolderHeight = 40;
|
const initialFolderHeight = 40;
|
||||||
const initialPageHeight = 40;
|
const initialPageHeight = 40;
|
||||||
@ -13,19 +14,48 @@ const animationDuration = 500;
|
|||||||
|
|
||||||
export const useFolderEvents = (folder: IFolder, pages: IPage[]) => {
|
export const useFolderEvents = (folder: IFolder, pages: IPage[]) => {
|
||||||
const appDispatch = useAppDispatch();
|
const appDispatch = useAppDispatch();
|
||||||
|
const workspace = useAppSelector((state) => state.workspace);
|
||||||
|
|
||||||
|
// Actions
|
||||||
const [showPages, setShowPages] = useState(false);
|
const [showPages, setShowPages] = useState(false);
|
||||||
const [showFolderOptions, setShowFolderOptions] = useState(false);
|
const [showFolderOptions, setShowFolderOptions] = useState(false);
|
||||||
const [showNewPageOptions, setShowNewPageOptions] = useState(false);
|
const [showNewPageOptions, setShowNewPageOptions] = useState(false);
|
||||||
const [showRenamePopup, setShowRenamePopup] = useState(false);
|
const [showRenamePopup, setShowRenamePopup] = useState(false);
|
||||||
|
|
||||||
|
// UI configurations
|
||||||
const [folderHeight, setFolderHeight] = useState(`${initialFolderHeight}px`);
|
const [folderHeight, setFolderHeight] = useState(`${initialFolderHeight}px`);
|
||||||
|
|
||||||
const workspace = useAppSelector((state) => state.workspace);
|
// Observers
|
||||||
|
const appObserver = new AppObserver(folder.id);
|
||||||
|
|
||||||
|
// Backend services
|
||||||
const appBackendService = new AppBackendService(folder.id);
|
const appBackendService = new AppBackendService(folder.id);
|
||||||
const workspaceBackendService = new WorkspaceBackendService(workspace.id || '');
|
const workspaceBackendService = new WorkspaceBackendService(workspace.id || '');
|
||||||
|
|
||||||
|
// Error
|
||||||
const error = useError();
|
const error = useError();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
void appObserver.subscribe({
|
||||||
|
onAppChanged: (change) => {
|
||||||
|
if (change.ok) {
|
||||||
|
const app: AppPB = change.val;
|
||||||
|
const updatedPages: IPage[] = app.belongings.items.map((view) => ({
|
||||||
|
id: view.id,
|
||||||
|
folderId: view.app_id,
|
||||||
|
pageType: view.layout,
|
||||||
|
title: view.name,
|
||||||
|
}));
|
||||||
|
appDispatch(pagesActions.didReceivePages(updatedPages));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return () => {
|
||||||
|
// Unsubscribe when the component is unmounted.
|
||||||
|
void appObserver.unsubscribe();
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (showPages) {
|
if (showPages) {
|
||||||
setFolderHeight(`${initialFolderHeight + pages.length * initialPageHeight}px`);
|
setFolderHeight(`${initialFolderHeight + pages.length * initialPageHeight}px`);
|
||||||
|
@ -56,13 +56,10 @@ export const useNavigationPanelHooks = function () {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
width,
|
width,
|
||||||
|
|
||||||
folders,
|
folders,
|
||||||
pages,
|
pages,
|
||||||
|
|
||||||
navigate,
|
navigate,
|
||||||
onPageClick,
|
onPageClick,
|
||||||
|
|
||||||
onCollapseNavigationClick,
|
onCollapseNavigationClick,
|
||||||
onFixNavigationClick,
|
onFixNavigationClick,
|
||||||
navigationPanelFixed,
|
navigationPanelFixed,
|
||||||
|
@ -3,11 +3,10 @@ import { AppPB, FlowyError, FolderNotification } from '../../../../../services/b
|
|||||||
import { ChangeNotifier } from '../../../../utils/change_notifier';
|
import { ChangeNotifier } from '../../../../utils/change_notifier';
|
||||||
import { FolderNotificationObserver } from '../notifications/observer';
|
import { FolderNotificationObserver } from '../notifications/observer';
|
||||||
|
|
||||||
export type AppUpdateNotifyValue = Result<AppPB, FlowyError>;
|
export type AppUpdateNotifyCallback = (value: Result<AppPB, FlowyError>) => void;
|
||||||
export type AppUpdateNotifyCallback = (value: AppUpdateNotifyValue) => void;
|
|
||||||
|
|
||||||
export class AppObserver {
|
export class AppObserver {
|
||||||
_appNotifier = new ChangeNotifier<AppUpdateNotifyValue>();
|
_appNotifier = new ChangeNotifier<Result<AppPB, FlowyError>>();
|
||||||
_listener?: FolderNotificationObserver;
|
_listener?: FolderNotificationObserver;
|
||||||
|
|
||||||
constructor(public readonly appId: string) {}
|
constructor(public readonly appId: string) {}
|
||||||
|
@ -14,6 +14,14 @@ export const pagesSlice = createSlice({
|
|||||||
name: 'pages',
|
name: 'pages',
|
||||||
initialState: initialState,
|
initialState: initialState,
|
||||||
reducers: {
|
reducers: {
|
||||||
|
didReceivePages(state, action: PayloadAction<IPage[]>) {
|
||||||
|
action.payload.forEach((updatedPage) => {
|
||||||
|
const index = state.findIndex((page) => page.id === updatedPage.id);
|
||||||
|
if (index !== -1) {
|
||||||
|
state.splice(index, 1, updatedPage);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
addPage(state, action: PayloadAction<IPage>) {
|
addPage(state, action: PayloadAction<IPage>) {
|
||||||
state.push(action.payload);
|
state.push(action.payload);
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user