Rewrite SpaceTreeLevelLayoutStore to save paths

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

View file

@ -69,9 +69,15 @@ export class SpaceItem extends React.PureComponent<IItemProps, IItemState> {
constructor(props) {
super(props);
const collapsed = SpaceTreeLevelLayoutStore.getSpaceCollapsedState(
props.space.roomId,
this.props.parents,
!props.isNested,
);
this.state = {
// default to collapsed for root items
collapsed: SpaceTreeLevelLayoutStore.getSpaceCollapsedState(props.space, !props.isNested),
collapsed: collapsed,
contextMenuPosition: null,
};
}
@ -82,7 +88,11 @@ export class SpaceItem extends React.PureComponent<IItemProps, IItemState> {
}
const newCollapsedState = !this.state.collapsed;
SpaceTreeLevelLayoutStore.setSpaceCollapsedState(this.props.space, newCollapsedState);
SpaceTreeLevelLayoutStore.setSpaceCollapsedState(
this.props.space.roomId,
this.props.parents,
newCollapsedState,
);
this.setState({collapsed: newCollapsedState});
// don't bubble up so encapsulating button for space
// doesn't get triggered

View file

@ -14,18 +14,25 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import {Room} from "matrix-js-sdk/src/models/room";
const getSpaceCollapsedKey = (space: Room) => `mx_space_collapsed_${space.roomId}`;
const getSpaceCollapsedKey = (roomId: string, parents: Set<string>): string => {
const separator = "/";
let path = "";
if (parents) {
for (const entry of parents.entries()) {
path += entry + separator;
}
}
return `mx_space_collapsed_${path + roomId}`;
};
export default class SpaceTreeLevelLayoutStore {
public static setSpaceCollapsedState(space: Room, collapsed: boolean) {
public static setSpaceCollapsedState(roomId: string, parents: Set<string>, collapsed: boolean) {
// XXX: localStorage doesn't allow booleans
localStorage.setItem(getSpaceCollapsedKey(space), collapsed.toString());
localStorage.setItem(getSpaceCollapsedKey(roomId, parents), collapsed.toString());
}
public static getSpaceCollapsedState(space: Room, fallback: boolean): boolean {
const collapsedLocalStorage = localStorage.getItem(getSpaceCollapsedKey(space));
public static 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;
}