Skip to content

Commit

Permalink
refact: integrate attachments and ExpenseDrawer
Browse files Browse the repository at this point in the history
  • Loading branch information
kewitz committed Oct 1, 2024
1 parent 373d0cb commit fd508a8
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 15 deletions.
26 changes: 15 additions & 11 deletions components/FilesViewerModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ type FilesViewerModalProps = {
allowOutsideInteraction?: boolean;
canDownload?: boolean;
canOpenInNewWindow?: boolean;
hideCloseButton?: boolean;
};

export default function FilesViewerModal({
Expand All @@ -144,6 +145,7 @@ export default function FilesViewerModal({
openFileUrl,
canDownload = true,
canOpenInNewWindow = true,
hideCloseButton = false,
}: FilesViewerModalProps) {
const intl = useIntl();
const initialIndex = openFileUrl ? files?.findIndex(f => f.url === openFileUrl) : 0;
Expand Down Expand Up @@ -217,7 +219,7 @@ export default function FilesViewerModal({
// eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions
<div
className={clsx(
'absolute inset-0 z-[1500] bg-foreground/25 backdrop-blur-sm',
'absolute inset-0 z-[1500] backdrop-blur-sm',
open ? 'animate-in fade-in-0' : 'animate-out fade-out-0',
)}
onClick={onClose}
Expand Down Expand Up @@ -279,16 +281,18 @@ export default function FilesViewerModal({
</ButtonLink>
</StyledTooltip>
)}
<StyledTooltip
containerCursor="pointer"
noArrow
content={intl.formatMessage({ id: 'Close', defaultMessage: 'Close' })}
delayHide={0}
>
<Button onClick={onClose}>
<X size="24" aria-hidden="true" />
</Button>
</StyledTooltip>
{!hideCloseButton && (
<StyledTooltip
containerCursor="pointer"
noArrow
content={intl.formatMessage({ id: 'Close', defaultMessage: 'Close' })}
delayHide={0}
>
<Button onClick={onClose}>
<X size="24" aria-hidden="true" />
</Button>
</StyledTooltip>
)}
</Flex>
</Header>
{hasMultipleFiles && (
Expand Down
2 changes: 1 addition & 1 deletion components/budget/ExpenseBudgetItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ const ExpenseBudgetItem = ({
fontSize="11px"
cursor="pointer"
buttonSize="tiny"
onClick={() => setShowFilesViewerModal(true)}
onClick={useDrawer ? expandExpense : () => setShowFilesViewerModal(true)}
px={2}
ml={-2}
isBorderless
Expand Down
27 changes: 25 additions & 2 deletions components/expenses/Expense.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import { formatErrorMessage, getErrorFromGraphqlException } from '../../lib/erro
import { getFilesFromExpense, getPayoutProfiles } from '../../lib/expenses';
import { API_V2_CONTEXT } from '../../lib/graphql/helpers';
import { ExpenseStatus } from '../../lib/graphql/types/v2/graphql';
import useKeyboardKey, { E } from '../../lib/hooks/useKeyboardKey';
import useKeyboardKey, { E, ESCAPE_KEY } from '../../lib/hooks/useKeyboardKey';
import useLoggedInUser from '../../lib/hooks/useLoggedInUser';
import { usePrevious } from '../../lib/hooks/usePrevious';
import { useWindowResize, VIEWPORTS } from '../../lib/hooks/useWindowResize';
import { itemHasOCR } from './lib/ocr';

import ConfirmationModal from '../ConfirmationModal';
Expand Down Expand Up @@ -116,6 +117,7 @@ function Expense(props) {
legacyExpenseId,
isDrawer,
enableKeyboardShortcuts,
onClose,
} = props;
const { LoggedInUser, loadingLoggedInUser } = useLoggedInUser();
const intl = useIntl();
Expand Down Expand Up @@ -222,12 +224,24 @@ function Expense(props) {
},
});

useKeyboardKey({
keyMatch: ESCAPE_KEY,
callback: e => {
if (props.isDrawer && state.status !== PAGE_STATUS.EDIT) {
e.preventDefault();
onClose?.();
}
},
});

const [editExpense] = useMutation(editExpenseMutation, {
context: API_V2_CONTEXT,
});

const expenseTopRef = useRef(null);
const { status, editedExpense } = state;
const { viewport } = useWindowResize();
const isDesktop = viewport === VIEWPORTS.LARGE;

const expense = data?.expense;
const loggedInAccount = data?.loggedInAccount;
Expand Down Expand Up @@ -394,6 +408,11 @@ function Expense(props) {

const files = React.useMemo(() => getFilesFromExpense(expense, intl), [expense]);

useEffect(() => {
const showFilesViewerModal = isDrawer && isDesktop && files?.length > 0;
setState(state => ({ ...state, showFilesViewerModal }));
}, [files, isDesktop, isDrawer]);

const confirmSaveButtons = (
<Flex flex={1} flexWrap="wrap" gridGap={[2, 3]}>
<StyledButton
Expand Down Expand Up @@ -784,7 +803,10 @@ function Expense(props) {
{ expenseId: expense.legacyId },
)}
openFileUrl={openUrl}
onClose={() => setState(state => ({ ...state, showFilesViewerModal: false }))}
onClose={
isDrawer && isDesktop ? onClose : () => setState(state => ({ ...state, showFilesViewerModal: false }))
}
hideCloseButton={isDrawer && isDesktop}
/>
)}
</Box>
Expand All @@ -805,6 +827,7 @@ Expense.propTypes = {
fetchMore: PropTypes.func,
startPolling: PropTypes.func,
stopPolling: PropTypes.func,
onClose: PropTypes.func,
isRefetchingDataForUser: PropTypes.bool,
drawerActionsContainer: PropTypes.object,
isDrawer: PropTypes.bool,
Expand Down
1 change: 1 addition & 0 deletions components/expenses/ExpenseDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export default function ExpenseDrawer({ openExpenseLegacyId, handleClose, initia
startPolling={startPolling}
stopPolling={stopPolling}
isDrawer
onClose={handleClose}
enableKeyboardShortcuts={hasKeyboardShortcutsEnabled}
/>
</Drawer>
Expand Down
2 changes: 1 addition & 1 deletion components/expenses/ExpenseSummaryAdditionalInformation.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ const ExpenseSummaryAdditionalInformation = ({
? VIRTUAL_CARD
: expense.payoutMethod?.type
}
name={expense?.virtualCard?.name && `${expense.virtualCard.name} Card (${expense.virtualCard.last4})`}
name={expense.virtualCard?.name && `${expense.virtualCard.name} Card (${expense.virtualCard.last4})`}
/>
</Box>
<Container data-cy="expense-summary-payout-method-data" wordBreak="break-word">
Expand Down

0 comments on commit fd508a8

Please sign in to comment.