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
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 28 additions & 4 deletions src/components/uploader/uploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
} = {}) {
Expand All @@ -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);
Expand All @@ -49,6 +52,7 @@ export function useUploader({
}
return subscribeToDrafts(() => {
const fileIds = getDraft(draftKey)?.fileIds ?? initialFileIds;
setInitialFileIds(fileIds);
setFileIds(fileIds);
});
}, [draftKey, initialFileIds]);
Expand Down Expand Up @@ -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 }),
Expand Down
Loading