Files
twenty/packages/twenty-front/src/modules/activities/files/components/AttachmentRow.tsx
T
Harshit Singh 54c328a7e6 fix: Files field fix (#7376)
## Description

Closes #7324 
Closes #7323

- This PR solves the issue - 
- #7324 
- #7323
- On Enter the rename of the file is saved
- Removed renaming of extension

## After Changes Behaviour


https://github.com/user-attachments/assets/f5c47cd4-883e-473e-9cfa-e277f8f630c2

---------

Co-authored-by: Lucas Bordeau <bordeau.lucas@gmail.com>
2024-10-10 10:41:23 +02:00

166 lines
4.8 KiB
TypeScript

import { ActivityRow } from '@/activities/components/ActivityRow';
import { AttachmentDropdown } from '@/activities/files/components/AttachmentDropdown';
import { AttachmentIcon } from '@/activities/files/components/AttachmentIcon';
import { Attachment } from '@/activities/files/types/Attachment';
import { downloadFile } from '@/activities/files/utils/downloadFile';
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { useDeleteOneRecord } from '@/object-record/hooks/useDeleteOneRecord';
import { useUpdateOneRecord } from '@/object-record/hooks/useUpdateOneRecord';
import {
FieldContext,
GenericFieldContextType,
} from '@/object-record/record-field/contexts/FieldContext';
import { TextInput } from '@/ui/input/components/TextInput';
import { useTheme } from '@emotion/react';
import styled from '@emotion/styled';
import { useMemo, useState } from 'react';
import { IconCalendar, OverflowingTextWithTooltip } from 'twenty-ui';
import { formatToHumanReadableDate } from '~/utils/date-utils';
import { getFileAbsoluteURI } from '~/utils/file/getFileAbsoluteURI';
import { getFileNameAndExtension } from '~/utils/file/getFileNameAndExtension';
const StyledLeftContent = styled.div`
align-items: center;
display: flex;
gap: ${({ theme }) => theme.spacing(3)};
width: 100%;
overflow: auto;
flex: 1;
`;
const StyledRightContent = styled.div`
align-items: center;
display: flex;
gap: ${({ theme }) => theme.spacing(0.5)};
`;
const StyledCalendarIconContainer = styled.div`
align-items: center;
color: ${({ theme }) => theme.font.color.light};
display: flex;
`;
const StyledLink = styled.a`
align-items: center;
color: ${({ theme }) => theme.font.color.primary};
display: flex;
text-decoration: none;
width: 100%;
:hover {
color: ${({ theme }) => theme.font.color.secondary};
}
`;
const StyledLinkContainer = styled.div`
overflow: auto;
width: 100%;
`;
export const AttachmentRow = ({ attachment }: { attachment: Attachment }) => {
const theme = useTheme();
const [isEditing, setIsEditing] = useState(false);
const { name: originalFileName, extension: attachmentFileExtension } =
getFileNameAndExtension(attachment.name);
const [attachmentFileName, setAttachmentFileName] =
useState(originalFileName);
const fieldContext = useMemo(
() => ({ recoilScopeId: attachment?.id ?? '' }),
[attachment?.id],
);
const { deleteOneRecord: deleteOneAttachment } = useDeleteOneRecord({
objectNameSingular: CoreObjectNameSingular.Attachment,
});
const handleDelete = () => {
deleteOneAttachment(attachment.id);
};
const { updateOneRecord: updateOneAttachment } = useUpdateOneRecord({
objectNameSingular: CoreObjectNameSingular.Attachment,
});
const handleRename = () => {
setIsEditing(true);
};
const saveAttachmentName = () => {
setIsEditing(false);
const newFileName = `${attachmentFileName}${attachmentFileExtension}`;
updateOneAttachment({
idToUpdate: attachment.id,
updateOneRecordInput: { name: newFileName },
});
};
const handleOnBlur = () => {
saveAttachmentName();
};
const handleOnChange = (newFileName: string) => {
setAttachmentFileName(newFileName);
};
const handleOnKeyDown = (e: React.KeyboardEvent) => {
if (e.key === 'Enter') {
saveAttachmentName();
}
};
const handleDownload = () => {
downloadFile(
attachment.fullPath,
`${attachmentFileName}${attachmentFileExtension}`,
);
};
return (
<FieldContext.Provider value={fieldContext as GenericFieldContextType}>
<ActivityRow disabled>
<StyledLeftContent>
<AttachmentIcon attachmentType={attachment.type} />
{isEditing ? (
<TextInput
value={attachmentFileName}
onChange={handleOnChange}
onBlur={handleOnBlur}
autoFocus
onKeyDown={handleOnKeyDown}
/>
) : (
<StyledLinkContainer>
<StyledLink
href={getFileAbsoluteURI(attachment.fullPath)}
target="__blank"
>
<OverflowingTextWithTooltip text={attachment.name} />
</StyledLink>
</StyledLinkContainer>
)}
</StyledLeftContent>
<StyledRightContent>
<StyledCalendarIconContainer>
<IconCalendar size={theme.icon.size.md} />
</StyledCalendarIconContainer>
{formatToHumanReadableDate(attachment.createdAt)}
<AttachmentDropdown
scopeKey={attachment.id}
onDelete={handleDelete}
onDownload={handleDownload}
onRename={handleRename}
/>
</StyledRightContent>
</ActivityRow>
</FieldContext.Provider>
);
};