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

fix: Files field fix #7376

Merged
merged 4 commits into from
Oct 10, 2024
Merged
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
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),
};
};
Loading