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

View file

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