Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3895] Memoize the explorer #3896

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ This will allow specifier to create images that fir perfectly in the project tem
- https://github.com/eclipse-sirius/sirius-web/issues/2163[#2163] [form] Make EMF default form support non changeable features
- https://github.com/eclipse-sirius/sirius-web/issues/4086[#4086] [form] Wrap widget returned by property section in a div with a specific classname
- https://github.com/eclipse-sirius/sirius-web/issues/4088[#4088] Change tree item context menu entries internal management
- https://github.com/eclipse-sirius/sirius-web/issues/3895[#3895] [trees] Memoize the explorer


== v2024.9.0
Expand Down
42 changes: 41 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*******************************************************************************/
import Typography from '@mui/material/Typography';
import { Theme } from '@mui/material/styles';
import { memo } from 'react';
import { makeStyles } from 'tss-react/mui';
import { GQLStyledString, GQLStyledStringFragmentStyle, StyledLabelInputProps } from './StyledLabel.type';

Expand Down Expand Up @@ -131,7 +132,7 @@ const getStyledString = (styledString: GQLStyledString) => {
});
};

export const StyledLabel = ({ styledString, selected, textToHighlight, marked }: StyledLabelInputProps) => {
export const StyledLabel = memo(({ styledString, selected, textToHighlight, marked }: StyledLabelInputProps) => {
const { classes } = useTreeItemStyle();
const textLabel = getTextFromStyledString(styledString);
let itemLabel: JSX.Element;
Expand Down Expand Up @@ -172,4 +173,4 @@ export const StyledLabel = ({ styledString, selected, textToHighlight, marked }:
</Typography>
</>
);
};
});
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface WorkbenchViewContribution {
side: WorkbenchViewSide;
title: string;
icon: React.ReactElement;
component: (props: WorkbenchViewComponentProps) => JSX.Element | null;
component: React.ComponentType<WorkbenchViewComponentProps>;
}

export interface WorkbenchViewComponentProps {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import { TreeItemActionProps, TreeView } from '@eclipse-sirius/sirius-components-trees';
import UnfoldMoreIcon from '@mui/icons-material/UnfoldMore';
import IconButton from '@mui/material/IconButton';
import { useState } from 'react';
import { memo, useCallback, useState } from 'react';
import { makeStyles } from 'tss-react/mui';
import { ModelBrowserFilterBar } from './ModelBrowserFilterBar';
import { ModelBrowserTreeViewProps, ModelBrowserTreeViewState } from './ModelBrowserTreeView.types';
Expand Down Expand Up @@ -57,9 +57,9 @@ export const ModelBrowserTreeView = ({
}&descriptionId=${encodeURIComponent(widget.descriptionId)}&isContainment=${widget.reference.containment}`;
const { tree } = useModelBrowserSubscription(editingContextId, treeId, state.expanded, state.maxDepth);

const onExpandedElementChange = (expanded: string[], maxDepth: number) => {
const onExpandedElementChange = useCallback((expanded: string[], maxDepth: number) => {
setState((prevState) => ({ ...prevState, expanded, maxDepth }));
};
}, []);

return (
<>
Expand Down Expand Up @@ -92,7 +92,7 @@ export const ModelBrowserTreeView = ({
);
};

const WidgetReferenceTreeItemAction = ({ onExpandAll, item, isHovered }: TreeItemActionProps) => {
const WidgetReferenceTreeItemAction = memo(({ onExpandAll, item, isHovered }: TreeItemActionProps) => {
if (!onExpandAll || !item || !item.hasChildren || !isHovered) {
return null;
}
Expand All @@ -107,4 +107,4 @@ const WidgetReferenceTreeItemAction = ({ onExpandAll, item, isHovered }: TreeIte
<UnfoldMoreIcon style={{ fontSize: 12 }} />
</IconButton>
);
};
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import Dialog from '@mui/material/Dialog';
import DialogActions from '@mui/material/DialogActions';
import DialogContent from '@mui/material/DialogContent';
import DialogTitle from '@mui/material/DialogTitle';
import { useMemo, useState } from 'react';
import { makeStyles } from 'tss-react/mui';
import { useState } from 'react';
import { ModelBrowserTreeView } from '../components/ModelBrowserTreeView';
import { BrowseModalProps } from './BrowseModal.types';

Expand All @@ -32,6 +32,8 @@ export const BrowseModal = ({ editingContextId, widget, onClose }: BrowseModalPr
const { classes: styles } = useBrowserModalStyles();
const [browserSelection, setBrowserSelection] = useState<Selection>({ entries: widget.referenceValues });

const markedItemIds = useMemo(() => [], []);

return (
<SelectionContext.Provider
value={{
Expand All @@ -49,7 +51,7 @@ export const BrowseModal = ({ editingContextId, widget, onClose }: BrowseModalPr
<ModelBrowserTreeView
editingContextId={editingContextId}
widget={widget}
markedItemIds={[]}
markedItemIds={markedItemIds}
enableMultiSelection={widget.reference.manyValued}
title={'Choices'}
leafType={'reference'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import ListItemText from '@mui/material/ListItemText';
import MenuItem from '@mui/material/MenuItem';
import Select from '@mui/material/Select';
import { useMachine } from '@xstate/react';
import { useEffect } from 'react';
import { useEffect, useMemo } from 'react';
import { makeStyles } from 'tss-react/mui';
import { StateMachine } from 'xstate';
import { ModelBrowserTreeView } from '../components/ModelBrowserTreeView';
Expand Down Expand Up @@ -378,6 +378,8 @@ export const CreateModal = ({ editingContextId, widget, onClose, formId }: Creat
dispatch(changeContainerSelectionEvent);
};

const markedItemIds = useMemo(() => [], []);

return (
<SelectionContext.Provider
value={{
Expand All @@ -396,7 +398,7 @@ export const CreateModal = ({ editingContextId, widget, onClose, formId }: Creat
<ModelBrowserTreeView
editingContextId={editingContextId}
widget={widget}
markedItemIds={[]}
markedItemIds={markedItemIds}
enableMultiSelection={false}
title={'Select the container'}
leafType={'container'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { DiagramDialogVariable } from '@eclipse-sirius/sirius-components-diagram
import { TreeItemActionProps, TreeView } from '@eclipse-sirius/sirius-components-trees';
import UnfoldMoreIcon from '@mui/icons-material/UnfoldMore';
import IconButton from '@mui/material/IconButton';
import { useState } from 'react';
import { useCallback, useState } from 'react';
import { makeStyles } from 'tss-react/mui';
import { SelectionDialogTreeViewProps, SelectionDialogTreeViewState } from './SelectionDialogTreeView.types';
import { useSelectionDialogTreeSubscription } from './useSelectionDialogTreeSubscription';
Expand Down Expand Up @@ -47,9 +47,9 @@ export const SelectionDialogTreeView = ({
const treeId = `selection://?treeDescriptionId=${encodeURIComponent(treeDescriptionId)}${encodeVariables(variables)}`;
const { tree } = useSelectionDialogTreeSubscription(editingContextId, treeId, state.expanded, state.maxDepth);

const onExpandedElementChange = (expanded: string[], maxDepth: number) => {
const onExpandedElementChange = useCallback((expanded: string[], maxDepth: number) => {
setState((prevState) => ({ ...prevState, expanded, maxDepth }));
};
}, []);

return (
<div className={classes.borderStyle}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
useTreeFilters,
} from '@eclipse-sirius/sirius-components-trees';
import { Theme } from '@mui/material/styles';
import { useContext, useEffect, useRef, useState } from 'react';
import { memo, useCallback, useContext, useEffect, useRef, useState } from 'react';
import { makeStyles } from 'tss-react/mui';
import { ExplorerViewState } from './ExplorerView.types';
import { TreeDescriptionsMenu } from './TreeDescriptionsMenu';
Expand All @@ -40,11 +40,10 @@ const useStyles = makeStyles()((theme: Theme) => ({
overflow: 'auto',
},
}));

const isTreeRefreshedEventPayload = (payload: GQLTreeEventPayload): payload is GQLTreeRefreshedEventPayload =>
payload && payload.__typename === 'TreeRefreshedEventPayload';

export const ExplorerView = ({ editingContextId, readOnly }: WorkbenchViewComponentProps) => {
export const ExplorerView = memo(({ editingContextId, readOnly }: WorkbenchViewComponentProps) => {
const { classes: styles } = useStyles();

const initialState: ExplorerViewState = {
Expand All @@ -58,6 +57,7 @@ export const ExplorerView = ({ editingContextId, readOnly }: WorkbenchViewCompon
maxDepth: {},
tree: null,
};

const [state, setState] = useState<ExplorerViewState>(initialState);
const treeToolBarContributionComponents = useContext<TreeToolBarContextValue>(TreeToolBarContext).map(
(contribution) => contribution.props.component
Expand Down Expand Up @@ -146,7 +146,8 @@ export const ExplorerView = ({ editingContextId, readOnly }: WorkbenchViewCompon
/>
);
}
const onExpandedElementChange = (expanded: string[], maxDepth: number) => {

const onExpandedElementChange = useCallback((expanded: string[], maxDepth: number) => {
setState((prevState) => ({
...prevState,
expanded: {
Expand All @@ -158,7 +159,7 @@ export const ExplorerView = ({ editingContextId, readOnly }: WorkbenchViewCompon
[prevState.activeTreeDescriptionId]: maxDepth,
},
}));
};
}, []);

const treeDescriptionSelector: JSX.Element = explorerDescriptions.length > 1 && (
<TreeDescriptionsMenu
Expand Down Expand Up @@ -200,7 +201,7 @@ export const ExplorerView = ({ editingContextId, readOnly }: WorkbenchViewCompon
<TreeView
editingContextId={editingContextId}
readOnly={readOnly}
treeId={'explorer://'}
treeId="'explorer://"
tree={state.tree}
enableMultiSelection={true}
synchronizedWithSelection={state.synchronizedWithSelection}
Expand All @@ -214,4 +215,4 @@ export const ExplorerView = ({ editingContextId, readOnly }: WorkbenchViewCompon
</div>
</div>
);
};
});
17 changes: 10 additions & 7 deletions packages/trees/frontend/sirius-components-trees/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
"peerDependencies": {
"@apollo/client": "3.10.4",
"@eclipse-sirius/sirius-components-core": "*",
"@mui/material": "5.15.19",
"@mui/icons-material": "5.15.19",
"@mui/material": "5.15.19",
"@xstate/react": "3.0.0",
"graphql": "16.8.1",
"react": "18.3.1",
Expand All @@ -47,22 +47,25 @@
"@apollo/client": "3.10.4",
"@eclipse-sirius/sirius-components-core": "*",
"@eclipse-sirius/sirius-components-tsconfig": "*",
"@mui/material": "5.15.19",
"@mui/icons-material": "5.15.19",
"@mui/material": "5.15.19",
"@types/react": "18.3.3",
"@vitejs/plugin-react": "4.3.0",
"@xstate/react": "3.0.0",
"@vitest/coverage-v8": "1.6.0",
"jsdom": "16.7.0",
"@xstate/react": "3.0.0",
"graphql": "16.8.1",
"jsdom": "16.7.0",
"prettier": "2.7.1",
"react": "18.3.1",
"react-dom": "18.3.1",
"prettier": "2.7.1",
"rollup-plugin-peer-deps-external": "2.2.4",
"xstate": "4.32.1",
"tss-react": "4.9.7",
"typescript": "5.4.5",
"vite": "5.2.11",
"vitest": "1.6.0"
"vitest": "1.6.0",
"xstate": "4.32.1"
},
"dependencies": {
"zustand": "^5.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { create } from 'zustand';

export type StoreType = {
expanded: string[];
maxDepth: number;
updateMaxDepth: (maxDepth) => void;
updateExpended: (newExpended) => void;
onExpand: (id, depth) => void;
};

export const useTreeStore = create<StoreType>((set, get) => ({
expanded: [],
maxDepth: 0,
updateMaxDepth: (newMaxDepth) => set(() => ({ maxDepth: newMaxDepth })),
updateExpended: (newExpended) => set(() => ({ expanded: newExpended })),
onExpand: (id, depth) => {
console.log('onExpand ' + id + ' - ' + depth);
const currentExpanded = get().expanded;
const currentMaxDepth = get().maxDepth;
if (currentExpanded.includes(id)) {
const newExpanded = [...currentExpanded];
newExpanded.splice(newExpanded.indexOf(id), 1);
set((state) => ({ ...state, expanded: newExpanded, maxDepth: Math.max(currentMaxDepth, depth) }));
} else {
set((state) => ({ ...state, expanded: [...currentExpanded, id], maxDepth: Math.max(currentMaxDepth, depth) }));
}
},
}));
Loading
Loading