import * as React from 'react';
import { injectIntl } from 'react-intl';
import type { IntlShape } from 'react-intl';
import { Button, IconButton, LoadingIndicator } from '@box/blueprint-web';
import { ArrowCurveForward, Checkmark } from '@box/blueprint-web-assets/icons/Line';
import { EllipsisBadge, XMark } from '@box/blueprint-web-assets/icons/Fill';
import Tooltip, { TooltipPosition } from '../../components/tooltip';
import {
ERROR_CODE_UPLOAD_FILE_SIZE_LIMIT_EXCEEDED,
STATUS_PENDING,
STATUS_IN_PROGRESS,
STATUS_STAGED,
STATUS_COMPLETE,
STATUS_ERROR,
} from '../../constants';
import messages from '../common/messages';
import type { UploadStatus } from '../../common/types/upload';
import './ItemAction.scss';
import { Size5, SurfaceStatusSurfaceSuccess } from '@box/blueprint-web-assets/tokens/tokens';
type ItemActionProps = {
error?: any,
intl: IntlShape,
isFolder?: boolean,
isResumableUploadsEnabled: boolean,
onClick: any,
onUpgradeCTAClick?: any,
status: UploadStatus,
};
const getIconWithTooltip = (
icon: React.ReactNode,
isDisabled: boolean,
isLoading: boolean,
onClick: any,
tooltip: boolean,
tooltipText: string,
) => {
if (isLoading) {
return ;
}
if (tooltip) {
return (
icon} />
);
}
return <>{icon}>;
};
const ItemAction = ({
error,
intl,
isFolder = false,
isResumableUploadsEnabled,
onClick,
onUpgradeCTAClick,
status,
}: ItemActionProps) => {
let icon: React.ReactNode = ;
let tooltip;
let isLoading = false;
const { code } = error || {};
const { formatMessage } = intl;
if (isFolder && status !== STATUS_PENDING) {
return null;
}
switch (status) {
case STATUS_COMPLETE:
icon = ;
if (!isResumableUploadsEnabled) {
tooltip = messages.remove;
}
break;
case STATUS_ERROR:
icon = ;
tooltip = isResumableUploadsEnabled ? messages.resume : messages.retry;
break;
case STATUS_IN_PROGRESS:
case STATUS_STAGED:
if (isResumableUploadsEnabled) {
isLoading = true;
} else {
icon = ;
tooltip = messages.uploadsCancelButtonTooltip;
}
break;
case STATUS_PENDING:
default:
if (isResumableUploadsEnabled) {
isLoading = true;
} else {
tooltip = messages.uploadsCancelButtonTooltip;
}
break;
}
if (status === STATUS_ERROR && code === ERROR_CODE_UPLOAD_FILE_SIZE_LIMIT_EXCEEDED && !!onUpgradeCTAClick) {
return (
);
}
const isDisabled = status === STATUS_STAGED;
const tooltipText = tooltip && formatMessage(tooltip);
return (
{getIconWithTooltip(icon, isDisabled, isLoading, onClick, tooltip, tooltipText)}
);
};
export default injectIntl(ItemAction);