Skip to content

Commit

Permalink
fix: Files field fix (twentyhq#7376)
Browse files Browse the repository at this point in the history
## Description

Closes twentyhq#7324 
Closes twentyhq#7323

- This PR solves the issue - 
- twentyhq#7324 
- twentyhq#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 <[email protected]>
  • Loading branch information
harshit078 and lucasbordeau committed Oct 14, 2024
1 parent f1303a5 commit 5a367ef
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ 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;
Expand Down Expand Up @@ -62,7 +63,12 @@ const StyledLinkContainer = styled.div`
export const AttachmentRow = ({ attachment }: { attachment: Attachment }) => {
const theme = useTheme();
const [isEditing, setIsEditing] = useState(false);
const [attachmentName, setAttachmentName] = useState(attachment.name);

const { name: originalFileName, extension: attachmentFileExtension } =
getFileNameAndExtension(attachment.name);

const [attachmentFileName, setAttachmentFileName] =
useState(originalFileName);

const fieldContext = useMemo(
() => ({ recoilScopeId: attachment?.id ?? '' }),
Expand All @@ -85,16 +91,36 @@ export const AttachmentRow = ({ attachment }: { attachment: Attachment }) => {
setIsEditing(true);
};

const handleOnBlur = () => {
const saveAttachmentName = () => {
setIsEditing(false);

const newFileName = `${attachmentFileName}${attachmentFileExtension}`;

updateOneAttachment({
idToUpdate: attachment.id,
updateOneRecordInput: { name: attachmentName },
updateOneRecordInput: { name: newFileName },
});
};

const handleOnChange = (newName: string) => {
setAttachmentName(newName);
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 (
Expand All @@ -104,11 +130,11 @@ export const AttachmentRow = ({ attachment }: { attachment: Attachment }) => {
<AttachmentIcon attachmentType={attachment.type} />
{isEditing ? (
<TextInput
value={attachmentName}
value={attachmentFileName}
onChange={handleOnChange}
onBlur={handleOnBlur}
autoFocus
fullWidth
onKeyDown={handleOnKeyDown}
/>
) : (
<StyledLinkContainer>
Expand All @@ -129,9 +155,7 @@ export const AttachmentRow = ({ attachment }: { attachment: Attachment }) => {
<AttachmentDropdown
scopeKey={attachment.id}
onDelete={handleDelete}
onDownload={() => {
downloadFile(attachment.fullPath, attachment.name);
}}
onDownload={handleDownload}
onRename={handleRename}
/>
</StyledRightContent>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const getFileNameAndExtension = (filenameWithExtension: string) => {
const lastDotIndex = filenameWithExtension.lastIndexOf('.');

return {
name: filenameWithExtension.substring(0, lastDotIndex),
extension: filenameWithExtension.substring(lastDotIndex),
};
};

0 comments on commit 5a367ef

Please sign in to comment.