Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Extract sort

Manav Rathi2fba8a2705e3
diff --git a/web/apps/photos/src/pages/shared-albums.tsx b/web/apps/photos/src/pages/shared-albums.tsx
index 0cc1434874..4d700952cf 100644
--- a/web/apps/photos/src/pages/shared-albums.tsx
+++ b/web/apps/photos/src/pages/shared-albums.tsx
@@ -98,8 +98,12 @@ export default function PublicCollectionGallery() {
const collectionKey = useRef<string>(null);
const url = useRef<string>(null);
const referralCode = useRef<string>("");
- const [publicFiles, setPublicFiles] = useState<EnteFile[]>(null);
- const [publicCollection, setPublicCollection] = useState<Collection>(null);
+ const [publicCollection, setPublicCollection] = useState<
+ Collection | undefined
+ >(undefined);
+ const [publicFiles, setPublicFiles] = useState<EnteFile[] | undefined>(
+ undefined,
+ );
const [errorMessage, setErrorMessage] = useState<string>(null);
const [loading, setLoading] = useState(true);
const [isPasswordProtected, setIsPasswordProtected] = useState(false);
@@ -229,13 +233,15 @@ export default function PublicCollectionGallery() {
referralCode.current =
await savedLastPublicCollectionReferralCode();
setPublicCollection(collection);
- const sortAsc =
- collection.pubMagicMetadata?.data.asc ?? false;
setIsPasswordProtected(
!!collection.publicURLs[0]?.passwordEnabled,
);
- const files = await savedPublicCollectionFiles(accessToken);
- setPublicFiles(sortFiles(files, sortAsc));
+ setPublicFiles(
+ sortFilesForCollection(
+ await savedPublicCollectionFiles(accessToken),
+ collection,
+ ),
+ );
accessTokenJWT =
await savedPublicCollectionAccessTokenJWT(accessToken);
}
@@ -330,7 +336,10 @@ export default function PublicCollectionGallery() {
credentials.current.accessToken,
credentials.current.accessTokenJWT,
collection,
- setPublicFiles,
+ (files) =>
+ setPublicFiles(
+ sortFilesForCollection(files, collection),
+ ),
);
} catch (e) {
const parsedError = parseSharingErrorCodes(e);
@@ -365,8 +374,8 @@ export default function PublicCollectionGallery() {
collectionUID,
collectionKey.current,
);
- setPublicCollection(null);
- setPublicFiles(null);
+ setPublicCollection(undefined);
+ setPublicFiles(undefined);
} else {
log.error("Public album remote pull failed", e);
}
@@ -425,6 +434,11 @@ export default function PublicCollectionGallery() {
});
};
+ const handleUploadFile = (file: EnteFile) =>
+ setPublicFiles(
+ sortFilesForCollection([...publicFiles, file], publicCollection),
+ );
+
const downloadFilesHelper = async () => {
try {
const selectedFiles = getSelectedFiles(selected, publicFiles);
@@ -543,9 +557,7 @@ export default function PublicCollectionGallery() {
uploadTypeSelectorIntent="collect"
uploadTypeSelectorView={uploadTypeSelectorView}
onRemotePull={publicAlbumsRemotePull}
- onUploadFile={(file) =>
- setPublicFiles(sortFiles([...publicFiles, file]))
- }
+ onUploadFile={handleUploadFile}
closeUploadTypeSelector={closeUploadTypeSelectorView}
onShowSessionExpiredDialog={showPublicLinkExpiredMessage}
{...{ dragAndDropFiles }}
@@ -559,6 +571,13 @@ export default function PublicCollectionGallery() {
);
}
+/**
+ * Sort the given {@link files} using {@link sortFiles}, using the ascending
+ * ordering preference if specified in the given {@link collection}'s metadata.
+ */
+const sortFilesForCollection = (files: EnteFile[], collection?: Collection) =>
+ sortFiles(files, collection?.pubMagicMetadata?.data.asc ?? false);
+
const EnteLogoLink = styled("a")(({ theme }) => ({
// Remove the excess space at the top.
svg: { verticalAlign: "middle" },
diff --git a/web/packages/new/albums/services/public-collection.ts b/web/packages/new/albums/services/public-collection.ts
index d7d089b80b..cddc3c89f5 100644
--- a/web/packages/new/albums/services/public-collection.ts
+++ b/web/packages/new/albums/services/public-collection.ts
@@ -5,7 +5,6 @@ import {
type PublicAlbumsCredentials,
} from "ente-base/http";
import { apiURL } from "ente-base/origins";
-import { sortFiles } from "ente-gallery/utils/file";
import {
decryptRemoteCollection,
RemoteCollection,
@@ -164,11 +163,13 @@ const getPublicCollectionInfo = async (accessToken: string) => {
* will also increase the updation time of the collection that contains them).
*
* @param onSetFiles A callback that is invoked each time a new batch of updates
- * to the collection's files is fetched and processed. THe callback is called
+ * to the collection's files is fetched and processed. The callback is called
* the consolidated list of files after applying the updates received so far.
*
+ * The provided files are in an arbitrary order, and must be sorted before use.
+ *
* This callback can get called multiple times during the pull. The callback can
- * also never get called if no changes were pulled.
+ * also never get called if no changes were pulled (or needed to be pulled).
*/
export const pullPublicCollectionFiles = async (
credentials: PublicAlbumsCredentials,
@@ -176,9 +177,13 @@ export const pullPublicCollectionFiles = async (
onSetFiles: (files: EnteFile[]) => void,
) => {
const { accessToken } = credentials;
- const sortAsc = collection.pubMagicMetadata?.data.asc ?? false;
let sinceTime = (await savedPublicCollectionLastSyncTime(accessToken)) ?? 0;
+
+ // Prior to reaching here, we would've already fetched the latest
+ // collection. If the updation time of the collection is the same as the
+ // last sync time, then we know there were no new updates (since updates to
+ // files also increase the updation time of their containing collection).
if (sinceTime == collection.updationTime) return;
const files = await savedPublicCollectionFiles(accessToken);
@@ -202,7 +207,7 @@ export const pullPublicCollectionFiles = async (
}
}
- const files = sortFiles([...filesByID.values()], sortAsc);
+ const files = [...filesByID.values()];
await savePublicCollectionFiles(accessToken, files);
await savePublicCollectionLastSyncTime(accessToken, sinceTime);
onSetFiles(files);