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

Add the post's attachments in the order they were added #1700

Merged
merged 1 commit into from
Nov 8, 2024
Merged
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
Returns the fileIds in the order in which the files were added
davidmz committed Oct 26, 2024
commit cb17350b8affb9e93293f4da31ddc16ba1f23a3b
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.136.0] - Not released
### Fixed
- The post attachments now appear in the same order as they were added by user.
Previously, attachments were sorted by the upload order.

## [1.135.3] - 2024-10-22
### Fixed
32 changes: 28 additions & 4 deletions src/components/uploader/uploader.js
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ let nextId = 1;
export function useUploader({
// Upload success (takes attachment object and upload ID)
onSuccess = null,
fileIds: initialFileIds = [],
fileIds: givenFileIds = [],
maxCount = Infinity,
draftKey,
} = {}) {
@@ -27,7 +27,10 @@ export function useUploader({
const [, forceUpdate] = useBool();

// Attachments management
const [fileIds, setFileIds] = useState(() => getDraft(draftKey)?.fileIds ?? initialFileIds);
const [initialFileIds, setInitialFileIds] = useState(
() => getDraft(draftKey)?.fileIds ?? givenFileIds,
);
const [fileIds, setFileIds] = useState(() => initialFileIds);
const updatedLocally = useRef(false);
const setFileIdsLocally = useCallback((arg) => {
setFileIds(arg);
@@ -49,6 +52,7 @@ export function useUploader({
}
return subscribeToDrafts(() => {
const fileIds = getDraft(draftKey)?.fileIds ?? initialFileIds;
setInitialFileIds(fileIds);
setFileIds(fileIds);
});
}, [draftKey, initialFileIds]);
@@ -114,15 +118,35 @@ export function useUploader({
});

useEffect(() => {
// All successful uploads in the right order
const allFileIds = [
...initialFileIds,
...[...uploadIds]
.filter((id) => statuses[id]?.success)
.map((id) => allUploads[id].attachment.id),
];

// Detect successful uploads
for (const id of uploadIds) {
if (statuses[id]?.success && unfinishedFiles.has(id)) {
unfinishedFiles.delete(id);
setFileIdsLocally((ids) => [...ids, allUploads[id].attachment.id]);
setFileIdsLocally((ids) => {
const newFileIds = new Set([...ids, allUploads[id].attachment.id]);
// Return updated ids in same order as allFileIds
return allFileIds.filter((id) => newFileIds.has(id));
});
onSuccess?.(allUploads[id].attachment, id);
}
}
}, [allUploads, onSuccess, setFileIdsLocally, statuses, unfinishedFiles, uploadIds]);
}, [
allUploads,
initialFileIds,
onSuccess,
setFileIdsLocally,
statuses,
unfinishedFiles,
uploadIds,
]);

const uploadProgressProps = useMemo(
() => ({ uploadIds, statuses, unfinishedFiles }),

Unchanged files with check annotations Beta

return { role, postLabel };
};
renderPostActions() {

Check warning on line 231 in src/components/post/post.jsx

GitHub Actions / build (18)

Method 'renderPostActions' has a complexity of 21. Maximum allowed is 20

Check warning on line 231 in src/components/post/post.jsx

GitHub Actions / build (20)

Method 'renderPostActions' has a complexity of 21. Maximum allowed is 20

Check warning on line 231 in src/components/post/post.jsx

GitHub Actions / build (21)

Method 'renderPostActions' has a complexity of 21. Maximum allowed is 20
const { props } = this;
const canonicalPostURI = canonicalURI(props);