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
Changes from 1 commit
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 @@ -60,7 +60,17 @@ const StyledLink = styled.a`
export const AttachmentRow = ({ attachment }: { attachment: Attachment }) => {
const theme = useTheme();
const [isEditing, setIsEditing] = useState(false);
const [attachmentName, setAttachmentName] = useState(attachment.name);
const getFileNameAndExtension = (filename: string) => {
const lastDotIndex = filename.lastIndexOf('.');
return {
name: filename.substring(0, lastDotIndex),
extension: filename.substring(lastDotIndex),
};
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider moving this function outside the component to improve performance


const { name: originalName, extension: fileExtension } =
getFileNameAndExtension(attachment.name);
const [attachmentName, setAttachmentName] = useState(originalName);

const fieldContext = useMemo(
() => ({ recoilScopeId: attachment?.id ?? '' }),
Expand All @@ -82,19 +92,29 @@ export const AttachmentRow = ({ attachment }: { attachment: Attachment }) => {
const handleRename = () => {
setIsEditing(true);
};

const handleOnBlur = () => {
const saveAttachmentName = () => {
setIsEditing(false);
const newFileName = `${attachmentName}${fileExtension}`;
updateOneAttachment({
idToUpdate: attachment.id,
updateOneRecordInput: { name: attachmentName },
updateOneRecordInput: { name: newFileName },
});
};

const handleOnBlur = () => {
saveAttachmentName();
};

const handleOnChange = (newName: string) => {
setAttachmentName(newName);
};

const handleOnKeyDown = (e: React.KeyboardEvent) => {
if (e.key === 'Enter') {
saveAttachmentName();
}
};

return (
<FieldContext.Provider value={fieldContext as GenericFieldContextType}>
<StyledRow>
Expand All @@ -106,7 +126,7 @@ export const AttachmentRow = ({ attachment }: { attachment: Attachment }) => {
onChange={handleOnChange}
onBlur={handleOnBlur}
autoFocus
fullWidth
onKeyDown={handleOnKeyDown}
/>
) : (
<StyledLink
Expand Down
Loading