Make SpaceTreeLevelLayoutStore into a singleton

Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com>
This commit is contained in:
Šimon Brandner 2021-05-11 11:16:14 +02:00
parent 4115fd8695
commit 1b877f2b7c
No known key found for this signature in database
GPG key ID: 9760693FDD98A790
2 changed files with 13 additions and 4 deletions

View file

@ -69,7 +69,7 @@ export class SpaceItem extends React.PureComponent<IItemProps, IItemState> {
constructor(props) {
super(props);
const collapsed = SpaceTreeLevelLayoutStore.getSpaceCollapsedState(
const collapsed = SpaceTreeLevelLayoutStore.instance.getSpaceCollapsedState(
props.space.roomId,
this.props.parents,
!props.isNested,
@ -88,7 +88,7 @@ export class SpaceItem extends React.PureComponent<IItemProps, IItemState> {
}
const newCollapsedState = !this.state.collapsed;
SpaceTreeLevelLayoutStore.setSpaceCollapsedState(
SpaceTreeLevelLayoutStore.instance.setSpaceCollapsedState(
this.props.space.roomId,
this.props.parents,
newCollapsedState,

View file

@ -26,12 +26,21 @@ const getSpaceCollapsedKey = (roomId: string, parents: Set<string>): string => {
};
export default class SpaceTreeLevelLayoutStore {
public static setSpaceCollapsedState(roomId: string, parents: Set<string>, collapsed: boolean) {
private static internalInstance: SpaceTreeLevelLayoutStore;
public static get instance(): SpaceTreeLevelLayoutStore {
if (!SpaceTreeLevelLayoutStore.internalInstance) {
SpaceTreeLevelLayoutStore.internalInstance = new SpaceTreeLevelLayoutStore();
}
return SpaceTreeLevelLayoutStore.internalInstance;
}
public setSpaceCollapsedState(roomId: string, parents: Set<string>, collapsed: boolean) {
// XXX: localStorage doesn't allow booleans
localStorage.setItem(getSpaceCollapsedKey(roomId, parents), collapsed.toString());
}
public static getSpaceCollapsedState(roomId: string, parents: Set<string>, fallback: boolean): boolean {
public getSpaceCollapsedState(roomId: string, parents: Set<string>, fallback: boolean): boolean {
const collapsedLocalStorage = localStorage.getItem(getSpaceCollapsedKey(roomId, parents));
// XXX: localStorage doesn't allow booleans
return collapsedLocalStorage ? collapsedLocalStorage === "true" : fallback;