DocSpace-client/packages/common/store/InfoPanelStore.js

353 lines
9.5 KiB
JavaScript
Raw Normal View History

2022-02-08 12:11:31 +00:00
import { makeAutoObservable } from "mobx";
2023-12-06 12:15:28 +00:00
import { getUserById } from "../api/people";
import { combineUrl, getUserRole } from "../utils";
import { FolderType, ShareAccessRights } from "../constants";
import config from "PACKAGE_FILE";
import Filter from "../api/people/filter";
2022-11-03 16:35:55 +00:00
import { getRoomInfo } from "../api/rooms";
2022-10-24 17:44:19 +00:00
const observedKeys = [
"id",
"title",
"thumbnailStatus",
"thumbnailUrl",
"version",
"comment",
"roomType",
2023-11-07 00:17:29 +00:00
"rootFolderId",
2022-10-24 17:44:19 +00:00
];
const infoMembers = "info_members";
const infoHistory = "info_history";
// const infoDetails = "info_details";
2022-02-11 14:35:18 +00:00
class InfoPanelStore {
isVisible = false;
isMobileHidden = false;
selection = null;
2023-04-25 09:58:51 +00:00
selectionHistory = null;
selectionParentRoom = null;
selectionHistory = null;
roomsView = infoMembers;
fileView = infoHistory;
updateRoomMembers = null;
isScrollLocked = false;
historyWithFileList = false;
authStore = null;
settingsStore = null;
peopleStore = null;
filesStore = null;
selectedFolderStore = null;
treeFoldersStore = null;
membersList = null;
2022-02-08 12:11:31 +00:00
constructor() {
makeAutoObservable(this);
}
2022-02-08 12:11:31 +00:00
// Setters
setIsVisible = (bool) => {
this.setView(infoMembers);
this.isVisible = bool;
this.isScrollLocked = false;
};
setIsMobileHidden = (bool) => (this.isMobileHidden = bool);
2022-11-01 10:31:49 +00:00
setSelection = (selection) => {
if (this.getIsAccounts() && (!selection.email || !selection.displayName)) {
this.selection = selection.length
? selection
: { isSelectedFolder: true };
2022-11-01 10:31:49 +00:00
return;
}
2022-11-01 10:31:49 +00:00
this.selection = selection;
this.isScrollLocked = false;
2022-11-01 10:31:49 +00:00
};
setSelectionParentRoom = (obj) => (this.selectionParentRoom = obj);
2023-04-25 09:58:51 +00:00
setSelectionHistory = (obj) => (this.selectionHistory = obj);
setSelectionHistory = (obj) => {
this.selectionHistory = obj;
this.historyWithFileList = this.selection.isFolder || this.selection.isRoom;
};
resetView = () => {
this.roomsView = infoMembers;
this.fileView = infoHistory;
};
setView = (view) => {
this.roomsView = view;
this.fileView = view === infoMembers ? infoHistory : view;
this.isScrollLocked = false;
if (view !== infoMembers) this.setMembersList(null);
};
setUpdateRoomMembers = (updateRoomMembers) => {
2023-10-11 10:11:39 +00:00
this.setMembersList(null);
this.updateRoomMembers = updateRoomMembers;
};
setIsScrollLocked = (isScrollLocked) => {
this.isScrollLocked = isScrollLocked;
};
// Selection helpers //
getSelectedItems = () => {
2022-10-24 17:44:19 +00:00
const {
selection: filesStoreSelection,
bufferSelection: filesStoreBufferSelection,
} = this.filesStore;
2022-10-31 11:09:28 +00:00
const {
selection: peopleStoreSelection,
bufferSelection: peopleStoreBufferSelection,
} = this.peopleStore.selectionStore;
return this.getIsAccounts()
? peopleStoreSelection.length
? [...peopleStoreSelection]
: peopleStoreBufferSelection
? [peopleStoreBufferSelection]
: []
: filesStoreSelection?.length > 0
? [...filesStoreSelection]
2022-10-24 17:44:19 +00:00
: filesStoreBufferSelection
? [filesStoreBufferSelection]
: [];
};
getSelectedFolder = () => {
const selectedFolderStore = { ...this.selectedFolderStore };
return {
2022-10-24 17:44:19 +00:00
...selectedFolderStore,
isFolder: true,
isRoom: !!this.selectedFolderStore.roomType,
};
};
calculateSelection = (
props = { selectedItems: [], selectedFolder: null }
) => {
const selectedItems = props.selectedItems.length
? props.selectedItems
: this.getSelectedItems();
const selectedFolder = props.selectedFolder
? props.selectedFolder
: this.getSelectedFolder();
return selectedItems.length === 0
? this.normalizeSelection({
...selectedFolder,
isSelectedFolder: true,
isSelectedItem: false,
})
: selectedItems.length === 1
? this.normalizeSelection({
...selectedItems[0],
isSelectedFolder: false,
isSelectedItem: true,
})
: [...Array(selectedItems.length).keys()];
};
normalizeSelection = (selection) => {
const isContextMenuSelection = selection.isContextMenuSelection;
return {
...selection,
isRoom: selection.isRoom || !!selection.roomType,
icon: this.getInfoPanelItemIcon(selection, 32),
isContextMenuSelection: false,
wasContextMenuSelection: !!isContextMenuSelection,
canCopyPublicLink:
selection.access === ShareAccessRights.RoomManager ||
selection.access === ShareAccessRights.None,
};
2022-08-09 16:05:36 +00:00
};
reloadSelection = () => {
this.setSelection(this.calculateSelection());
};
2023-01-18 13:14:16 +00:00
updateRoomLogoCacheBreaker = () => {
const logo = this.selection.logo;
this.setSelection({
...this.selection,
logo: {
small: logo.small.split("?")[0] + "?" + new Date().getTime(),
medium: logo.medium.split("?")[0] + "?" + new Date().getTime(),
large: logo.large.split("?")[0] + "?" + new Date().getTime(),
original: logo.original.split("?")[0] + "?" + new Date().getTime(),
},
});
};
2022-11-03 16:35:55 +00:00
reloadSelectionParentRoom = async () => {
if (!this.getIsRooms) return;
const currentFolderRoomId =
this.selectedFolderStore.pathParts &&
this.selectedFolderStore.pathParts[1]?.id;
// const prevRoomId = this.selectionParentRoom?.id;
2022-11-03 16:35:55 +00:00
// if (!currentFolderRoomId || currentFolderRoomId === prevRoomId) return;
if (!currentFolderRoomId) return;
2022-11-03 16:35:55 +00:00
const newSelectionParentRoom = await getRoomInfo(currentFolderRoomId);
2022-12-11 20:02:25 +00:00
// if (prevRoomId === newSelectionParentRoom.id) return;
const roomIndex = this.selectedFolderStore.navigationPath.findIndex(
(f) => f.id === currentFolderRoomId
);
if (roomIndex > -1) {
this.selectedFolderStore.navigationPath[roomIndex].title =
newSelectionParentRoom.title;
}
2022-11-03 16:35:55 +00:00
this.setSelectionParentRoom(
this.normalizeSelection(newSelectionParentRoom)
);
};
2022-10-24 17:44:19 +00:00
isItemChanged = (oldItem, newItem) => {
for (let i = 0; i < observedKeys.length; i++) {
const value = observedKeys[i];
if (oldItem[value] !== newItem[value]) return true;
}
return false;
};
// Icon helpers //
getInfoPanelItemIcon = (item, size) => {
return item.isRoom || !!item.roomType
? item.rootFolderType === FolderType.Archive
? item.logo && item.logo.medium
: this.settingsStore.getIcon(
size,
null,
null,
null,
item.roomType,
true
)
? item.logo?.medium
: item.icon
? item.icon
: this.settingsStore.getIcon(size, null, null, null, item.roomType)
: item.isFolder
? this.settingsStore.getFolderIcon(item.providerKey, size)
: this.settingsStore.getIcon(size, item.fileExst || ".file");
};
// User link actions //
openUser = async (user, navigate) => {
2022-11-01 10:31:49 +00:00
if (user.id === this.authStore.userStore.user.id) {
this.openSelfProfile();
return;
}
2022-11-01 10:31:49 +00:00
const fetchedUser = await this.fetchUser(user.id);
this.openAccountsWithSelectedUser(fetchedUser, navigate);
};
openSelfProfile = () => {
this.peopleStore.profileActionsStore.onProfileClick();
};
openAccountsWithSelectedUser = async (user, navigate) => {
const path = [
window.DocSpaceConfig?.proxy?.url,
config.homepage,
"/accounts",
];
2022-10-11 06:51:21 +00:00
const newFilter = Filter.getDefault();
newFilter.page = 0;
newFilter.search = user.email;
newFilter.selectUserId = user.id;
2022-10-11 06:51:21 +00:00
path.push(`filter?${newFilter.toUrlParams()}`);
2022-11-01 10:31:49 +00:00
this.selectedFolderStore.setSelectedFolder(null);
2022-10-11 06:51:21 +00:00
this.treeFoldersStore.setSelectedNode(["accounts"]);
navigate(combineUrl(...path), { state: { user } });
};
fetchUser = async (userId) => {
const { getStatusType, getUserContextOptions } =
this.peopleStore.usersStore;
const fetchedUser = await getUserById(userId);
fetchedUser.role = getUserRole(fetchedUser);
fetchedUser.statusType = getStatusType(fetchedUser);
fetchedUser.options = getUserContextOptions(
false,
fetchedUser.isOwner,
fetchedUser.statusType,
fetchedUser.status
);
return fetchedUser;
};
// Routing helpers //
getCanDisplay = () => {
const pathname = window.location.pathname.toLowerCase();
const isFiles = this.getIsFiles(pathname);
const isRooms = this.getIsRooms(pathname);
const isAccounts = this.getIsAccounts(pathname);
const isGallery = this.getIsGallery(pathname);
return isRooms || isFiles || isGallery || isAccounts;
};
getIsFiles = (givenPathName) => {
const pathname = givenPathName || window.location.pathname.toLowerCase();
return (
pathname.indexOf("files") !== -1 || pathname.indexOf("personal") !== -1
);
};
getIsRooms = (givenPathName) => {
const pathname = givenPathName || window.location.pathname.toLowerCase();
return (
pathname.indexOf("rooms") !== -1 && !(pathname.indexOf("personal") !== -1)
);
};
getIsAccounts = (givenPathName) => {
const pathname = givenPathName || window.location.pathname.toLowerCase();
return (
pathname.indexOf("accounts") !== -1 && !(pathname.indexOf("view") !== -1)
);
};
getIsGallery = (givenPathName) => {
const pathname = givenPathName || window.location.pathname.toLowerCase();
return pathname.indexOf("form-gallery") !== -1;
};
getIsTrash = (givenPathName) => {
const pathname = givenPathName || window.location.pathname.toLowerCase();
return pathname.indexOf("files/trash") !== -1;
};
setMembersList = (membersList) => {
this.membersList = membersList;
};
2022-02-08 12:11:31 +00:00
}
2022-02-11 14:35:18 +00:00
export default InfoPanelStore;