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

[PB-2424] Feat: working with ancestors to display shared item details #1403

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
60 changes: 44 additions & 16 deletions src/app/drive/components/ItemDetailsDialog/ItemDetailsDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import errorService from 'app/core/services/error.service';
import { getItemPlainName } from 'app/crypto/services/utils';
import ItemDetailsSkeleton from './components/ItemDetailsSkeleton';
import { AdvancedSharedItem } from 'app/share/types';
import { useSelector } from 'react-redux';
import workspacesSelectors from '../../../store/slices/workspaces/workspaces.selectors';

const Header = ({ title, onClose }: { title: string; onClose: () => void }) => {
return (
Expand Down Expand Up @@ -83,6 +85,8 @@ const ItemDetailsDialog = ({
const itemName = `${item?.plainName ?? item?.name}` + `${item?.type && !item.isFolder ? '.' + item?.type : ''}`;
const user = localStorageService.getUser();
const isFolder = item?.isFolder;
const workspaceSelected = useSelector(workspacesSelectors.getSelectedWorkspace);
const isWorkspaceSelected = !!workspaceSelected;

useEffect(() => {
if (isOpen && item && user) {
Expand Down Expand Up @@ -119,29 +123,53 @@ const ItemDetailsDialog = ({
onClose();
}

function getSharedLocation(item: DriveItemDetails, ancestorPathNames: string[]): string {
let location = translate('sideNav.shared');
if (item.isFolder && ancestorPathNames.length > 0) {
location += '/' + ancestorPathNames.shift();
}
return location;
}

function getRegularLocation(item: DriveItemDetails, ancestorPathNames: string[]): string {
if (item.isFolder) {
ancestorPathNames.pop();
}
return item.view + (ancestorPathNames.length > 0 ? '/' + ancestorPathNames.join('/') : '');
}

function getLocation(item: DriveItemDetails, ancestors: DriveItemData[]): string {
const itemViewName = item.view;
const ancestorPathNames = ancestors.map((ancestor) => getItemPlainName(ancestor)).reverse();
ancestorPathNames.shift(); // Remove root parent

let location = '/';

if (itemViewName === 'Shared') {
location += getSharedLocation(item, ancestorPathNames);
return location;
}

location += getRegularLocation(item, ancestorPathNames);
return location;
}

Comment on lines +126 to +156
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remember to add tests for these functions, you will probably need to extract them from this file to add the unit tests

async function getDetailsData(
item: DriveItemDetails,
isShared: string,
uploaded: string,
modified: string,
email: string,
) {
const uuid = item.isFolder ? item.uuid : item.folderUuid;
const rootPathName = item.view;

const ancestors = await newStorageService.getFolderAncestors(uuid as string);

const getPathName = ancestors.map((ancestor) => getItemPlainName(ancestor as unknown as DriveItemData)).reverse();
const itemType = item.isFolder ? 'folder' : 'file';
const itemUuid = item.uuid;
const itemFolderUuid = item.folderUuid;

if (item.isFolder) {
getPathName.pop();
}

if (item.view === 'Drive') {
getPathName.shift();
}
const ancestors = isWorkspaceSelected
? await newStorageService.getFolderAncestorsInWorkspace(workspaceSelected.workspace.id, itemType, itemUuid)
: await newStorageService.getFolderAncestors(itemFolderUuid);

const path = '/' + rootPathName + (getPathName.length > 0 ? '/' + getPathName.join('/') : '');
const location = getLocation(item, ancestors as unknown as DriveItemData[]);

const details: ItemDetailsProps = {
name: item.name,
Expand All @@ -150,8 +178,8 @@ const ItemDetailsDialog = ({
size: item.isFolder ? undefined : bytesToString(item.size),
uploaded: uploaded,
modified: modified,
uploadedBy: item.userEmail ?? email,
location: path,
uploadedBy: item.workspaceItemUser?.creator?.email ?? item.userEmail ?? email,
location,
};

return details;
Expand Down
17 changes: 13 additions & 4 deletions src/app/drive/components/MoveItemsDialog/MoveItemsDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import { IRoot } from '../../../store/slices/storage/types';
import { DriveFileData, DriveFolderData, DriveItemData, FolderPathDialog } from '../../types';
import CreateFolderDialog from '../CreateFolderDialog/CreateFolderDialog';
import workspacesSelectors from '../../../store/slices/workspaces/workspaces.selectors';

interface MoveItemsDialogProps {
onItemsMoved?: () => void;
Expand All @@ -50,6 +51,8 @@ const MoveItemsDialog = (props: MoveItemsDialogProps): JSX.Element => {
const rootFolderID: string = useSelector((state: RootState) => storageSelectors.rootFolderId(state));
const itemParentId = itemsToMove[0]?.folderUuid ?? itemsToMove[0]?.folderUuid;
const isDriveAndCurrentFolder = !props.isTrash && itemParentId === destinationId;
const workspaceSelected = useSelector(workspacesSelectors.getSelectedWorkspace);
const isWorkspaceSelected = !!workspaceSelected;

const onCreateFolderButtonClicked = () => {
dispatch(uiActions.setIsCreateFolderDialogOpen(true));
Expand Down Expand Up @@ -129,8 +132,14 @@ const MoveItemsDialog = (props: MoveItemsDialogProps): JSX.Element => {
dispatch(setItemsToMove([]));
};

const setDriveBreadcrumb = async (itemsToMove) => {
const breadcrumbsList: FolderAncestor[] = await newStorageService.getFolderAncestors(itemsToMove[0].uuid);
const setDriveBreadcrumb = async (itemsToMove: DriveItemData[]) => {
const item = itemsToMove[0];
const itemUuid = item.uuid;
const itemFolderUuid = item.folderUuid;
const itemType = item.isFolder ? 'folder' : 'file';
const breadcrumbsList: FolderAncestor[] = isWorkspaceSelected
? await newStorageService.getFolderAncestorsInWorkspace(workspaceSelected.workspace.id, itemType, itemUuid)
: await newStorageService.getFolderAncestors(itemFolderUuid);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore:next-line
const fullPath = breadcrumbsList.toReversed();
Expand All @@ -143,14 +152,14 @@ const MoveItemsDialog = (props: MoveItemsDialogProps): JSX.Element => {
dispatch(storageActions.setNamePath(fullPathParsedNamesList));

const currentItemUuid = navigationService.getUuid();
const shouldUpdateBreadcrumb = itemsToMove[0].isFolder && currentItemUuid === itemsToMove[0].uuid;
const shouldUpdateBreadcrumb = item.isFolder && currentItemUuid === itemUuid;

if (itemsToMove.length > 1) {
return;
}

if (shouldUpdateBreadcrumb) {
await getAncestorsAndSetNamePath(itemsToMove[0].uuid as string, dispatch);
await getAncestorsAndSetNamePath(item.isFolder ? itemUuid : itemFolderUuid, dispatch);
}
};

Expand Down
8 changes: 6 additions & 2 deletions src/app/drive/services/folder.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,19 @@ export function createFolderByUuid(
return [finalPromise, requestCanceler];
}

export async function updateMetaData(folderUuid: string, metadata: DriveFolderMetadataPayload): Promise<void> {
export async function updateMetaData(
folderUuid: string,
metadata: DriveFolderMetadataPayload,
resourcesToken?: string,
): Promise<void> {
const storageClient = SdkFactory.getNewApiInstance().createNewStorageClient();

const payload = {
folderUuid,
name: metadata.itemName,
};

return storageClient.updateFolderNameWithUUID(payload);
return storageClient.updateFolderNameWithUUID(payload, resourcesToken);
}

export async function deleteFolder(folderData: DriveFolderData): Promise<void> {
Expand Down
11 changes: 11 additions & 0 deletions src/app/drive/services/new-storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from '@internxt/sdk/dist/drive/storage/types';
import { SdkFactory } from '../../core/factory/sdk';
import { RequestCanceler } from '@internxt/sdk/dist/shared/http/types';
import { ItemType } from '@internxt/sdk/dist/workspaces/types';

export async function searchItemsByName(name: string): Promise<DriveFileData[]> {
const storageClient = SdkFactory.getNewApiInstance().createNewStorageClient();
Expand All @@ -23,6 +24,15 @@ export async function getFolderAncestors(uuid: string): Promise<FolderAncestor[]
return storageClient.getFolderAncestors(uuid);
}

export async function getFolderAncestorsInWorkspace(
workspaceId: string,
itemType: ItemType,
uuid: string,
): Promise<FolderAncestor[]> {
const storageClient = SdkFactory.getNewApiInstance().createNewStorageClient();
return storageClient.getFolderAncestorsInWorkspace(workspaceId, itemType, uuid);
}

export async function getFolderMeta(uuid: string, workspaceId?: string, resourcesToken?: string): Promise<FolderMeta> {
const storageClient = SdkFactory.getNewApiInstance().createNewStorageClient();
return storageClient.getFolderMeta(uuid, workspaceId, resourcesToken);
Expand Down Expand Up @@ -75,6 +85,7 @@ export function getFolderContentByUuid({
const newStorageService = {
searchItemsByName,
getFolderAncestors,
getFolderAncestorsInWorkspace,
getFolderMeta,
getFolderTree,
checkDuplicatedFiles,
Expand Down
4 changes: 3 additions & 1 deletion src/app/drive/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RenewalPeriod } from '@internxt/sdk/dist/drive/payments/types';
import { ShareLink } from '@internxt/sdk/dist/drive/share/types';
import { ShareLink, WorkspaceItemUser } from '@internxt/sdk/dist/drive/share/types';
import { AppSumoDetails } from '@internxt/sdk/dist/shared/types/appsumo';
import { AdvancedSharedItem } from 'app/share/types';
import { SVGProps } from 'react';
Expand Down Expand Up @@ -28,6 +28,7 @@ export interface DriveFolderData {
sharings?: { type: string; id: string }[];
uuid: string;
type?: string;
workspaceItemUser?: WorkspaceItemUser;
}

export interface DriveFolderMetadataPayload {
Expand Down Expand Up @@ -60,6 +61,7 @@ export interface DriveFileData {
shares?: Array<ShareLink>;
sharings?: { type: string; id: string }[];
uuid: string;
workspaceItemUser?: WorkspaceItemUser;
}

interface Thumbnail {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,11 @@ const ShareItemNameField = ({ shareItem, onItemDoubleClicked, onNameClicked }) =
<div className={'flex h-full w-full flex-row items-center space-x-4 overflow-hidden'}>
<div className="relative flex h-10 w-10 shrink items-center justify-center">
<Icon className="flex h-full justify-center drop-shadow-soft" />
<div className="absolute -bottom-0.5 -right-1.5 flex h-5 w-5 items-center justify-center rounded-full bg-primary text-white ring-2 ring-surface">
<img src={usersIcon} width={13} alt="shared users" />
</div>
{shareItem.dateShared && (
CandelR marked this conversation as resolved.
Show resolved Hide resolved
<div className="absolute -bottom-0.5 -right-1.5 flex h-5 w-5 items-center justify-center rounded-full bg-primary text-white ring-2 ring-surface">
<img src={usersIcon} width={13} alt="shared users" />
</div>
)}
</div>
<button
className="w-full max-w-full truncate pr-16 text-left"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ const SharedItemListContainer = ({
const itemDetails: DriveItemDetails = {
...shareItem,
isShared: true,
userEmail: shareItem.user?.email ?? shareItem.credentials.networkUser,
userEmail:
shareItem.workspaceItemUser?.creator?.email ?? shareItem.user?.email ?? shareItem.credentials.networkUser,
view: isOwner ? 'Drive' : 'Shared',
};
dispatch(uiActions.setItemDetailsItem(itemDetails));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ const useSharedContextMenu = ({

const getFolderContextMenu = (folder: AdvancedSharedItem) => {
const userUUID = folder?.user?.uuid;
const isEditorUser = !isCurrentUserViewer;
const canHandleShareAccessSettings = isRootFolder && (isItemOwnedByCurrentUser(userUUID) || !isCurrentUserViewer);

const ownerCurrentUserOptions = isItemOwnedByCurrentUser(userUUID)
? {
openShareAccessSettings: openShareAccessSettings,
openShareAccessSettings: canHandleShareAccessSettings ? openShareAccessSettings : undefined,
renameItem: renameItem,
moveItem: moveItem,
moveToTrash: onOpenStopSharingDialog,
Expand All @@ -67,18 +69,20 @@ const useSharedContextMenu = ({
copyLink,
showDetails,
downloadItem,
renameItem: isEditorUser ? renameItem : undefined,
...ownerCurrentUserOptions,
});
};

const getItemContextMenu = (item: AdvancedSharedItem) => {
const userUUID = item?.user?.uuid;
const isSubFolderAndEditorUser = !isCurrentUserViewer && !isRootFolder;
const isRootFolderAndIsOwner = isRootFolder && isItemOwnedByCurrentUser(userUUID);
const isEditorUser = !isCurrentUserViewer;
const canHandleShareAccessSettings = isRootFolder && (isItemOwnedByCurrentUser(userUUID) || !isCurrentUserViewer);

const ownerCurrentUserOptions = isItemOwnedByCurrentUser(userUUID)
? {
openShareAccessSettings: openShareAccessSettings,
openShareAccessSettings: canHandleShareAccessSettings ? openShareAccessSettings : undefined,
renameItem: renameItem,
moveItem: moveItem,
moveToTrash: onOpenStopSharingDialog,
}
Expand All @@ -93,7 +97,7 @@ const useSharedContextMenu = ({
showDetails,
copyLink,
downloadItem: handleDownload,
renameItem: isSubFolderAndEditorUser || isRootFolderAndIsOwner ? renameItem : undefined,
renameItem: isEditorUser ? renameItem : undefined,
...ownerCurrentUserOptions,
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { FolderPath } from '../../../../drive/types';
import { uiActions } from '../../ui';
import { StorageState } from '../storage.model';
import storageSelectors from '../storage.selectors';
import { useSelector } from 'react-redux';
import workspacesSelectors from '../../workspaces/workspaces.selectors';

const parsePathNames = (breadcrumbsList: FolderAncestor[]) => {
// ADDED UNTIL WE UPDATE TYPESCRIPT VERSION
Expand All @@ -20,7 +22,11 @@ const parsePathNames = (breadcrumbsList: FolderAncestor[]) => {
};

export const getAncestorsAndSetNamePath = async (uuid: string, dispatch) => {
const breadcrumbsList: FolderAncestor[] = await newStorageService.getFolderAncestors(uuid);
const workspaceSelected = useSelector(workspacesSelectors.getSelectedWorkspace);
const isWorkspaceSelected = !!workspaceSelected;
const breadcrumbsList: FolderAncestor[] = isWorkspaceSelected
? await newStorageService.getFolderAncestorsInWorkspace(workspaceSelected.workspace.id, 'folder', uuid)
: await newStorageService.getFolderAncestors(uuid);
const fullPathParsedNames = parsePathNames(breadcrumbsList);
dispatch(storageActions.setNamePath(fullPathParsedNames));
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const updateItemMetadataThunk = createAsyncThunk<
namePathDestinationArray[0] = '';

item.isFolder
? await folderService.updateMetaData(item.uuid, metadata)
? await folderService.updateMetaData(item.uuid, metadata, resourceToken)
: await fileService.updateMetaData(item.uuid, metadata, resourceToken);

dispatch(
Expand Down
Loading